Worker.php 11 KB

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