Worker.php 10 KB

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