Worker.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. $time = $this->request->post('create_time/s',$time); //日期
  59. $this->time=$time;
  60. if (!empty($time)) {
  61. $arr = explode(',', $time);
  62. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  63. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  64. }
  65. $pid = $this->userinfo['id'];
  66. $where[] = ['sl.user_id', '=', $pid];
  67. $list = $scanLog->alias('sl')
  68. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  69. ->where('sl.order_status', 1)
  70. ->where('sl.spec_id','<>', null)
  71. ->where($where)
  72. ->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")
  73. ->group('sl.spec_id')
  74. ->select()->each(function ($item, $key) {
  75. $spec_name = $this->get_order_info($item['code']);
  76. $item['pack_name']=$spec_name;
  77. unset($item['code']);
  78. });
  79. $data['pack_specs']=$list;
  80. // $data['pack_specs'] = [
  81. // [
  82. // 'pack_name' => '三斤',
  83. // 'pack_count' => '120',
  84. // 'pack_money' => '100'
  85. // ],
  86. // [
  87. // 'pack_name' => '五斤',
  88. // 'pack_count' => '120',
  89. // 'pack_money' => '100'
  90. // ],
  91. // [
  92. // 'pack_name' => '五斤保温',
  93. // 'pack_count' => '120',
  94. // 'pack_money' => '100'
  95. // ],
  96. // ];
  97. $this->success('ok', $data);
  98. }
  99. //添加出入库
  100. public function scan(ScanLog $scanLog)
  101. {
  102. $data = $this->request->post();
  103. $result = false;
  104. Db::startTrans();
  105. $insert_data = [];
  106. try {
  107. validate(WorkerValidate::class)->scene('scan')->check($data);
  108. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  109. $time = time();
  110. $insert_data = [
  111. 'user_id' => $this->userinfo['id'],
  112. 'code' => $data['code'],
  113. 'order_status' => 0,
  114. 'remark' => '未找到单号',
  115. 'order_data' => [],
  116. 'createtime' => date('Y-m-d H:i:s', $time)
  117. ];
  118. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  119. if (!empty($sql_data)) {
  120. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  121. $insert_data['order_status'] = 2;
  122. $insert_data['remark'] = '请勿重复录入';
  123. } else {
  124. $result = $sql_data->save([
  125. 'user_id' => $this->userinfo['id'],
  126. 'entry_time' => $time,
  127. 'updatetime' => $time
  128. ]);
  129. if ($result) {
  130. $insert_data['order_status'] = 1;
  131. $insert_data['remark'] = '录入成功';
  132. $insert_data['spec_id'] = $sql_data['spec_id'];
  133. } else {
  134. $insert_data['remark'] = '录入失败';
  135. }
  136. }
  137. $entry_time = $sql_data['entry_time'];
  138. if (!empty($sql_data['entry_time'])) {
  139. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  140. }
  141. switch ($sql_data['incubator']) {
  142. case 1:
  143. $sql_data['incubator_title'] = '单层保温';
  144. break;
  145. case 2:
  146. $sql_data['incubator_title'] = '双层保温';
  147. break;
  148. default:
  149. $sql_data['incubator_title'] = '不是保温箱';
  150. break;
  151. }
  152. $sql_data['entry_time'] = $entry_time;
  153. $insert_data['order_data'] = $sql_data;
  154. }
  155. $result = $scanLog->save($insert_data);
  156. Db::commit();
  157. } catch (ValidateException $e) {
  158. return $this->error($e->getError());
  159. } catch (\Exception $e) {
  160. Db::rollback();
  161. $this->error($e->getMessage());
  162. }
  163. if ($result === false) {
  164. $this->error(__('没有新增任何数据'));
  165. }
  166. $this->success('', $insert_data);
  167. }
  168. //扫描记录
  169. public function scanlog(ScanLog $scanLog)
  170. {
  171. $where = [];
  172. $limit = $this->request->post('limit/d', 10); //条数
  173. $time = $this->request->post('create_time/s'); //日期
  174. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  175. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  176. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  177. if (!empty($time)) {
  178. $arr = explode(',', $time);
  179. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  180. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  181. }
  182. $list = $scanLog->alias('sl')
  183. ->where('user_id', $this->userinfo['id'])
  184. ->whereIn('order_status', [0,1,2])
  185. ->where($where)
  186. ->order('createtime desc')
  187. ->paginate($limit)
  188. ->each(function ($item, $key) {
  189. $order_data = $this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  190. $item['order_data'] = [];
  191. if (!empty($order_data['waybill_no'])) {
  192. $incubator_title = '不是保温箱';
  193. switch ($order_data['incubator']) {
  194. case 1:
  195. $incubator_title = '单层保温';
  196. break;
  197. case 2:
  198. $incubator_title = '双层保温';
  199. break;
  200. }
  201. $item['order_data'] = $order_data;
  202. $item['order_data']['incubator_title'] = $incubator_title;
  203. }
  204. return $item;
  205. });
  206. $this->success('ok', $list);
  207. }
  208. //数据统计,今日昨日,本周
  209. public function pack_data_statistics(ScanLog $scanLog, GroupUserModel $groupUserModel)
  210. {
  211. $where = [];
  212. $limit = $this->request->post('limit/d', 10); //条数
  213. $todayTime = date("Y-m-d");
  214. $startTime = strtotime($todayTime . ' 00:00:00');
  215. $startTime = date('Y-m-d H:i:s', $startTime);
  216. $endTime = strtotime($todayTime . ' 23:59:59');
  217. $endTime = date('Y-m-d H:i:s', $endTime);
  218. $time=$startTime.','.$endTime;
  219. $time = $this->request->post('create_time/s',$time); //日期
  220. $this->time=$time;
  221. if (!empty($time)) {
  222. $arr = explode(',', $time);
  223. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  224. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  225. }
  226. $pid = $this->userinfo['id'];
  227. $where[] = ['sl.user_id', '=', $pid];
  228. $list = $scanLog->alias('sl')
  229. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  230. ->where('sl.order_status', 1)
  231. ->where('sl.spec_id','<>', null)
  232. ->where($where)
  233. ->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")
  234. ->group('sl.spec_id')
  235. ->paginate($limit)->each(function ($item, $key) {
  236. $spec_name = $this->get_order_info($item['code']);
  237. $item['spec_name']=$spec_name;
  238. unset($item['code']);
  239. });
  240. $list_sum = $scanLog->alias('sl')
  241. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  242. ->where('sl.order_status', 1)
  243. ->where('sl.spec_id','<>', null)
  244. ->where($where)
  245. ->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")
  246. ->group('sl.spec_id')
  247. ->select();
  248. foreach ($list_sum as $item) {
  249. $spec_quantity_sum=$item['spec_quantity_sum'];
  250. $spec_labor_cost_money_sum=$item['spec_labor_cost_money_sum'];
  251. $this->quantity_sum=bcadd($this->quantity_sum, $spec_quantity_sum,0);
  252. $this->labor_cost_money_sum=bcadd($this->labor_cost_money_sum, $spec_labor_cost_money_sum,2);
  253. }
  254. $data = [
  255. 'quantity_sum' => $this->quantity_sum,
  256. 'labor_cost_money_sum' => $this->labor_cost_money_sum,
  257. 'list' => empty($list) ? [] :$list
  258. ];
  259. $this->success('ok', $data);
  260. }
  261. //获取今天出工人员状态,0=未出个,1=出工
  262. public function get_worker_out_state($id, $pid,$time)
  263. {
  264. $groupUserModel = new GroupUserModel();
  265. $count = $groupUserModel->where('id', $id)->where('pid', $pid)->count();
  266. if ($count == 0) return 1;
  267. $workerOutModel = new WorkerOutModel();
  268. $todayTime = strtotime('today');
  269. $where = [];
  270. $where[] = ['user_id', '=', $id];
  271. $where[] = ['pid', '=', $pid];
  272. $where[] = ['state', '=', 1];
  273. if (!empty($time)) {
  274. $arr = explode(',', $time);
  275. $where[] = ['createtime', '>=', strtotime($arr[0])];
  276. $where[] = ['createtime', '<=', strtotime($arr[1])];
  277. }
  278. return $workerOutModel->where($where)->count();
  279. }
  280. public function get_order_info($waybill_no){
  281. return $this->shopDeliveryModel->where('waybill_no',$waybill_no)->value('spec_name');
  282. }
  283. }