Worker.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. /**
  19. * 打包工人相关接口
  20. */
  21. class Worker extends Base
  22. {
  23. protected $shopDeliveryModel = null;
  24. protected $groupUserController = null;
  25. protected $quantity_sum = 0;
  26. protected $labor_cost_money_sum = 0;
  27. protected $quantity_avg = 0;
  28. protected $labor_cost_money_avg = 0;
  29. protected $worker_num = 0;
  30. protected $worker_list = [];
  31. protected $time =null;
  32. /**
  33. * 判断权限
  34. * @access protected
  35. */
  36. protected function _initialize()
  37. {
  38. parent::_initialize();
  39. $this->shopDeliveryModel = new ShopDeliveryModel();
  40. $this->groupUserController = new GroupUserController();
  41. if (!str_contains($this->userinfo['role'], "1")) {
  42. $this->error(__('没有权限' . $this->userinfo['role']));
  43. }
  44. }
  45. //首页
  46. public function index(StockConfig $stockConfig)
  47. {
  48. $data = [];
  49. $data['today'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'today')->count();
  50. $data['yesterday'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'yesterday')->count();
  51. $this->success('ok', $data);
  52. }
  53. //添加出入库
  54. public function scan(ScanLog $scanLog)
  55. {
  56. $data = $this->request->post();
  57. $result = false;
  58. Db::startTrans();
  59. $insert_data = [];
  60. try {
  61. validate(WorkerValidate::class)->scene('scan')->check($data);
  62. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  63. $time = time();
  64. $insert_data = [
  65. 'user_id' => $this->userinfo['id'],
  66. 'code' => $data['code'],
  67. 'order_status' => 0,
  68. 'remark' => '未找到单号',
  69. 'order_data' => [],
  70. 'createtime' => date('Y-m-d H:i:s', $time)
  71. ];
  72. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  73. if (!empty($sql_data)) {
  74. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  75. $insert_data['order_status'] = 0;
  76. $insert_data['remark'] = '请勿重复录入';
  77. } else {
  78. $result = $sql_data->save([
  79. 'user_id' => $this->userinfo['id'],
  80. 'entry_time' => $time,
  81. 'updatetime' => $time
  82. ]);
  83. if ($result) {
  84. $insert_data['order_status'] = 1;
  85. $insert_data['remark'] = '录入成功';
  86. } else {
  87. $insert_data['remark'] = '录入失败';
  88. }
  89. }
  90. $entry_time = $sql_data['entry_time'];
  91. if (!empty($sql_data['entry_time'])) {
  92. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  93. }
  94. switch ($sql_data['incubator']) {
  95. case 1:
  96. $sql_data['incubator_title'] = '单层保温';
  97. break;
  98. case 2:
  99. $sql_data['incubator_title'] = '双层保温';
  100. break;
  101. default:
  102. $sql_data['incubator_title'] = '不是保温箱';
  103. break;
  104. }
  105. $sql_data['entry_time'] = $entry_time;
  106. $insert_data['order_data'] = $sql_data;
  107. }
  108. $result = $scanLog->save($insert_data);
  109. Db::commit();
  110. } catch (ValidateException $e) {
  111. return $this->error($e->getError());
  112. } catch (\Exception $e) {
  113. Db::rollback();
  114. $this->error($e->getMessage());
  115. }
  116. if ($result === false) {
  117. $this->error(__('没有新增任何数据'));
  118. }
  119. $this->success('', $insert_data);
  120. }
  121. //扫描记录
  122. public function scanlog(ScanLog $scanLog)
  123. {
  124. $where = [];
  125. $limit = $this->request->post('limit/d', 10); //条数
  126. $time = $this->request->post('create_time/s'); //日期
  127. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  128. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  129. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  130. if (!empty($time)) {
  131. $arr = explode(',', $time);
  132. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  133. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  134. }
  135. $list = $scanLog->alias('sl')
  136. ->where('user_id', $this->userinfo['id'])
  137. ->where('order_status', 1)
  138. ->where($where)
  139. ->order('createtime desc')
  140. ->paginate($limit)
  141. ->each(function ($item, $key) {
  142. $order_data = $this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  143. $item['order_data'] = [];
  144. if (!empty($order_data['waybill_no'])) {
  145. $incubator_title = '不是保温箱';
  146. switch ($order_data['incubator']) {
  147. case 1:
  148. $incubator_title = '单层保温';
  149. break;
  150. case 2:
  151. $incubator_title = '双层保温';
  152. break;
  153. }
  154. $item['order_data'] = $order_data;
  155. $item['order_data']['incubator_title'] = $incubator_title;
  156. }
  157. return $item;
  158. });
  159. $this->success('ok', $list);
  160. }
  161. //数据统计,今日昨日,本周
  162. public function pack_data_statistics(ScanLog $scanLog, GroupUserModel $groupUserModel)
  163. {
  164. $where = [];
  165. $group_where = [];
  166. $todayTime = date("Y-m-d");
  167. $startTime = strtotime($todayTime . ' 00:00:00');
  168. $startTime = date('Y-m-d H:i:s', $startTime);
  169. $endTime = strtotime($todayTime . ' 23:59:59');
  170. $endTime = date('Y-m-d H:i:s', $endTime);
  171. $time=$startTime.','.$endTime;
  172. $time = $this->request->post('create_time/s',$time); //日期
  173. $this->time=$time;
  174. if (!empty($time)) {
  175. $arr = explode(',', $time);
  176. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  177. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  178. }
  179. $pid = $this->userinfo['id'];
  180. $where[] = ['sl.user_id', '=', $pid];
  181. $sql = $scanLog->alias('sl')
  182. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  183. ->where('order_status', 1)
  184. ->where($where);
  185. // 总件数
  186. $this->quantity_sum = $sql->sum('num');
  187. // 总工价
  188. $this->labor_cost_money_sum = $scanLog->alias('sl')
  189. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  190. ->where('sl.order_status', 1)
  191. ->where($where)
  192. ->value("SUM(COALESCE(sd.num,0) * COALESCE(sd.labor_cost_money,0)) as total_labor_cost");
  193. $list = $groupUserModel
  194. ->where('pid', '=', $pid)
  195. ->field('id,pid,nickname')
  196. ->select()->each(function ($item, $key) {
  197. $state = $this->get_worker_out_state($item['id'], $this->userinfo['id'],$this->time);
  198. $state = $state == 0 ? false : true;
  199. // $item['selected']=$state;
  200. if ($state) {
  201. $this->worker_num++;
  202. $this->worker_list[]=$item;
  203. }
  204. });
  205. // 工价平均值
  206. $quantity_sum = strval($this->labor_cost_money_sum);
  207. $num = strval($this->worker_num);
  208. $labor_cost_money_avg = bcdiv($quantity_sum, $num, 2);
  209. // 件数平均值
  210. $quantity_avg = strval($this->quantity_sum);
  211. $this->worker_num = strval($this->worker_num);
  212. $quantity_avg = bcdiv($quantity_avg, $this->worker_num, 2);
  213. $data = [
  214. 'quantity_sum' => $this->quantity_sum,
  215. 'labor_cost_money_sum' => $this->labor_cost_money_sum,
  216. 'quantity_avg' => $quantity_avg,
  217. 'labor_cost_money_avg' => $labor_cost_money_avg,
  218. 'worker_list' => empty($this->worker_list) ? [] : $this->worker_list
  219. ];
  220. $this->success('ok', $data);
  221. }
  222. //获取今天出工人员状态,0=未出个,1=出工
  223. public function get_worker_out_state($id, $pid,$time)
  224. {
  225. $groupUserModel = new GroupUserModel();
  226. $count = $groupUserModel->where('id', $id)->where('pid', $pid)->count();
  227. if ($count == 0) return 1;
  228. $workerOutModel = new WorkerOutModel();
  229. $todayTime = strtotime('today');
  230. $where = [];
  231. $where[] = ['user_id', '=', $id];
  232. $where[] = ['pid', '=', $pid];
  233. $where[] = ['state', '=', 1];
  234. if (!empty($time)) {
  235. $arr = explode(',', $time);
  236. $where[] = ['createtime', '>=', strtotime($arr[0])];
  237. $where[] = ['createtime', '<=', strtotime($arr[1])];
  238. }
  239. return $workerOutModel->where($where)->count();
  240. }
  241. }