Worker.php 10 KB

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