Worker.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. <?php
  2. namespace app\api\controller;
  3. use app\api\service\auth\MysqlAdapter;
  4. use app\common\model\ScanLog;
  5. use app\common\model\StockConfig;
  6. use app\api\service\StockService;
  7. use app\common\model\StockLog;
  8. use think\exception\HttpResponseException;
  9. use think\exception\ValidateException;
  10. use app\api\validate\Worker as WorkerValidate;
  11. use think\facade\Cache;
  12. use think\facade\Db;
  13. use think\Response;
  14. use app\common\model\GroupUser as GroupUserModel;
  15. use app\api\controller\GroupUser as GroupUserController;
  16. use app\common\model\ShopDelivery as ShopDeliveryModel;
  17. use app\common\model\WorkerOut as WorkerOutModel;
  18. use app\common\model\PackSpecs as PackSpecsModel;
  19. /**
  20. * 打包工人相关接口
  21. */
  22. class Worker extends Base
  23. {
  24. protected $shopDeliveryModel = null;
  25. protected $groupUserController = null;
  26. protected $quantity_sum = 0;
  27. protected $labor_cost_money_sum = 0;
  28. protected $quantity_avg = 0;
  29. protected $labor_cost_money_avg = 0;
  30. protected $worker_num = 0;
  31. protected $worker_list = [];
  32. protected $time =null;
  33. /**
  34. * 判断权限
  35. * @access protected
  36. */
  37. protected function _initialize()
  38. {
  39. parent::_initialize();
  40. $this->shopDeliveryModel = new ShopDeliveryModel();
  41. $this->groupUserController = new GroupUserController();
  42. if (!str_contains($this->userinfo['role'], "1")) {
  43. $this->error(__('没有权限' . $this->userinfo['role']));
  44. }
  45. }
  46. //首页
  47. public function index(StockConfig $stockConfig,ScanLog $scanLog)
  48. {
  49. $data = [];
  50. $data['today'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'today')->count();
  51. $data['yesterday'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'yesterday')->count();
  52. $todayTime = date("Y-m-d");
  53. $startTime = strtotime($todayTime . ' 00:00:00');
  54. $startTime = date('Y-m-d H:i:s', $startTime);
  55. $endTime = strtotime($todayTime . ' 23:59:59');
  56. $endTime = date('Y-m-d H:i:s', $endTime);
  57. $time=$startTime.','.$endTime;
  58. $arr = explode(',', $time);
  59. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  60. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  61. $pid = $this->userinfo['id'];
  62. $where[] = ['sl.user_id', '=', $pid];
  63. $list = $scanLog->alias('sl')
  64. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  65. ->where('sl.order_status', 1)
  66. ->where('sl.spec_id','<>', null)
  67. ->where($where)
  68. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as pack_count,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as pack_money")
  69. ->group('sl.spec_id')
  70. ->select()->each(function ($item, $key) {
  71. $spec_name = $this->get_order_info($item['code']);
  72. $item['pack_name']=$spec_name;
  73. unset($item['code']);
  74. });
  75. $data['pack_specs']=$list;
  76. // $data['pack_specs'] = [
  77. // [
  78. // 'pack_name' => '三斤',
  79. // 'pack_count' => '120',
  80. // 'pack_money' => '100'
  81. // ],
  82. // [
  83. // 'pack_name' => '五斤',
  84. // 'pack_count' => '120',
  85. // 'pack_money' => '100'
  86. // ],
  87. // [
  88. // 'pack_name' => '五斤保温',
  89. // 'pack_count' => '120',
  90. // 'pack_money' => '100'
  91. // ],
  92. // ];
  93. $this->success('ok', $data);
  94. }
  95. //添加出入库
  96. public function scan(ScanLog $scanLog)
  97. {
  98. $data = $this->request->post();
  99. $result = false;
  100. Db::startTrans();
  101. $insert_data = [];
  102. try {
  103. validate(WorkerValidate::class)->scene('scan')->check($data);
  104. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  105. $time = time();
  106. $insert_data = [
  107. 'user_id' => $this->userinfo['id'],
  108. 'code' => $data['code'],
  109. 'order_status' => 0,
  110. 'remark' => '未找到单号',
  111. 'order_data' => [],
  112. 'createtime' => date('Y-m-d H:i:s', $time)
  113. ];
  114. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  115. if (!empty($sql_data)) {
  116. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  117. $insert_data['order_status'] = 2;
  118. $insert_data['remark'] = '请勿重复录入';
  119. } else {
  120. $result = $sql_data->save([
  121. 'user_id' => $this->userinfo['id'],
  122. 'entry_time' => $time,
  123. 'updatetime' => $time
  124. ]);
  125. if ($result) {
  126. $insert_data['order_status'] = 1;
  127. $insert_data['remark'] = '录入成功';
  128. $insert_data['spec_id'] = $sql_data['spec_id'];
  129. } else {
  130. $insert_data['remark'] = '录入失败';
  131. }
  132. }
  133. $entry_time = $sql_data['entry_time'];
  134. if (!empty($sql_data['entry_time'])) {
  135. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  136. }
  137. switch ($sql_data['incubator']) {
  138. case 1:
  139. $sql_data['incubator_title'] = '单层保温';
  140. break;
  141. case 2:
  142. $sql_data['incubator_title'] = '双层保温';
  143. break;
  144. default:
  145. $sql_data['incubator_title'] = '不是保温箱';
  146. break;
  147. }
  148. $sql_data['entry_time'] = $entry_time;
  149. $insert_data['order_data'] = $sql_data;
  150. }
  151. $result = $scanLog->save($insert_data);
  152. Db::commit();
  153. } catch (ValidateException $e) {
  154. return $this->error($e->getError());
  155. } catch (\Exception $e) {
  156. Db::rollback();
  157. $this->error($e->getMessage());
  158. }
  159. if ($result === false) {
  160. $this->error(__('没有新增任何数据'));
  161. }
  162. $this->success('', $insert_data);
  163. }
  164. //扫描记录
  165. public function scanlog(ScanLog $scanLog)
  166. {
  167. $where = [];
  168. $limit = $this->request->post('limit/d', 10); //条数
  169. $time = $this->request->post('create_time/s'); //日期
  170. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  171. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  172. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  173. if (!empty($time)) {
  174. $arr = explode(',', $time);
  175. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  176. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  177. }
  178. $list = $scanLog->alias('sl')
  179. ->where('user_id', $this->userinfo['id'])
  180. ->whereIn('order_status', [0,1,2])
  181. ->where($where)
  182. ->order('createtime desc')
  183. ->paginate($limit)
  184. ->each(function ($item, $key) {
  185. $order_data = $this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  186. $item['order_data'] = [];
  187. if (!empty($order_data['waybill_no'])) {
  188. $incubator_title = '不是保温箱';
  189. switch ($order_data['incubator']) {
  190. case 1:
  191. $incubator_title = '单层保温';
  192. break;
  193. case 2:
  194. $incubator_title = '双层保温';
  195. break;
  196. }
  197. $item['order_data'] = $order_data;
  198. $item['order_data']['incubator_title'] = $incubator_title;
  199. }
  200. return $item;
  201. });
  202. $this->success('ok', $list);
  203. }
  204. //数据统计,今日昨日,本周
  205. public function pack_data_statistics(ScanLog $scanLog, GroupUserModel $groupUserModel)
  206. {
  207. $where = [];
  208. $limit = $this->request->post('limit/d', 10); //条数
  209. $todayTime = date("Y-m-d");
  210. $startTime = strtotime($todayTime . ' 00:00:00');
  211. $startTime = date('Y-m-d H:i:s', $startTime);
  212. $endTime = strtotime($todayTime . ' 23:59:59');
  213. $endTime = date('Y-m-d H:i:s', $endTime);
  214. $time=$startTime.','.$endTime;
  215. $time = $this->request->post('create_time/s',$time); //日期
  216. $this->time=$time;
  217. if (!empty($time)) {
  218. $arr = explode(',', $time);
  219. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  220. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  221. }
  222. $pid = $this->userinfo['id'];
  223. $where[] = ['sl.user_id', '=', $pid];
  224. $list = $scanLog->alias('sl')
  225. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  226. ->where('sl.order_status', 1)
  227. ->where('sl.spec_id','<>', null)
  228. ->where($where)
  229. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as spec_quantity_sum,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as spec_labor_cost_money_sum")
  230. ->group('sl.spec_id')
  231. ->paginate($limit)->each(function ($item, $key) {
  232. $spec_name = $this->get_order_info($item['code']);
  233. $item['spec_name']=$spec_name;
  234. unset($item['code']);
  235. });
  236. $list_sum = $scanLog->alias('sl')
  237. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  238. ->where('sl.order_status', 1)
  239. ->where('sl.spec_id','<>', null)
  240. ->where($where)
  241. ->field("sl.code,sl.spec_id,SUM(COALESCE(sd.num,0) * 1) as spec_quantity_sum,SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as spec_labor_cost_money_sum")
  242. ->group('sl.spec_id')
  243. ->select();
  244. foreach ($list_sum as $item) {
  245. $spec_quantity_sum=$item['spec_quantity_sum'];
  246. $spec_labor_cost_money_sum=$item['spec_labor_cost_money_sum'];
  247. $this->quantity_sum=bcadd($this->quantity_sum, $spec_quantity_sum,0);
  248. $this->labor_cost_money_sum=bcadd($this->labor_cost_money_sum, $spec_labor_cost_money_sum,2);
  249. }
  250. $data = [
  251. 'quantity_sum' => $this->quantity_sum,
  252. 'labor_cost_money_sum' => $this->labor_cost_money_sum,
  253. 'list' => empty($list) ? [] :$list
  254. ];
  255. $this->success('ok', $data);
  256. }
  257. //获取今天出工人员状态,0=未出个,1=出工
  258. public function get_worker_out_state($id, $pid,$time)
  259. {
  260. $groupUserModel = new GroupUserModel();
  261. $count = $groupUserModel->where('id', $id)->where('pid', $pid)->count();
  262. if ($count == 0) return 1;
  263. $workerOutModel = new WorkerOutModel();
  264. $todayTime = strtotime('today');
  265. $where = [];
  266. $where[] = ['user_id', '=', $id];
  267. $where[] = ['pid', '=', $pid];
  268. $where[] = ['state', '=', 1];
  269. if (!empty($time)) {
  270. $arr = explode(',', $time);
  271. $where[] = ['createtime', '>=', strtotime($arr[0])];
  272. $where[] = ['createtime', '<=', strtotime($arr[1])];
  273. }
  274. return $workerOutModel->where($where)->count();
  275. }
  276. public function get_order_info($waybill_no){
  277. return $this->shopDeliveryModel->where('waybill_no',$waybill_no)->value('spec_name');
  278. }
  279. }