Worker.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. use app\common\model\ImportList as ImportListModel;
  20. use app\admin\command\FengSu;
  21. /**
  22. * 打包工人相关接口
  23. */
  24. class Worker extends Base
  25. {
  26. protected $shopDeliveryModel = null;
  27. protected $groupUserController = null;
  28. protected $quantity_sum = 0;
  29. protected $labor_cost_money_sum = 0;
  30. protected $quantity_avg = 0;
  31. protected $labor_cost_money_avg = 0;
  32. protected $worker_num = 0;
  33. protected $worker_list = [];
  34. protected $time = null;
  35. /**
  36. * 判断权限
  37. * @access protected
  38. */
  39. protected function _initialize()
  40. {
  41. parent::_initialize();
  42. $this->shopDeliveryModel = new ShopDeliveryModel();
  43. $this->groupUserController = new GroupUserController();
  44. if (!str_contains($this->userinfo['role'], "1")) {
  45. $this->error(__('没有权限' . $this->userinfo['role']));
  46. }
  47. }
  48. //首页
  49. public function index(StockConfig $stockConfig, ScanLog $scanLog)
  50. {
  51. $data = [];
  52. $data['today'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'today')->count();
  53. $data['yesterday'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'yesterday')->count();
  54. $todayTime = date("Y-m-d");
  55. $startTime = strtotime($todayTime . ' 00:00:00');
  56. $startTime = date('Y-m-d H:i:s', $startTime);
  57. $endTime = strtotime($todayTime . ' 23:59:59');
  58. $endTime = date('Y-m-d H:i:s', $endTime);
  59. $time = $startTime . ',' . $endTime;
  60. $arr = explode(',', $time);
  61. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  62. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  63. $pid = $this->userinfo['id'];
  64. $where[] = ['sl.user_id', '=', $pid];
  65. $list = $scanLog->alias('sl')
  66. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  67. ->where('sl.order_status', 1)
  68. ->where('sl.spec_id', '<>', null)
  69. ->where($where)
  70. ->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")
  71. ->group('sl.spec_id')
  72. ->select()->each(function ($item, $key) {
  73. $spec_name = $this->get_order_info($item['code']);
  74. $item['pack_name'] = $spec_name;
  75. unset($item['code']);
  76. });
  77. $data['pack_specs'] = $list;
  78. // $data['pack_specs'] = [
  79. // [
  80. // 'pack_name' => '三斤',
  81. // 'pack_count' => '120',
  82. // 'pack_money' => '100'
  83. // ],
  84. // [
  85. // 'pack_name' => '五斤',
  86. // 'pack_count' => '120',
  87. // 'pack_money' => '100'
  88. // ],
  89. // [
  90. // 'pack_name' => '五斤保温',
  91. // 'pack_count' => '120',
  92. // 'pack_money' => '100'
  93. // ],
  94. // ];
  95. $this->success('ok', $data);
  96. }
  97. //添加出入库
  98. public function scan(ScanLog $scanLog)
  99. {
  100. $data = $this->request->post();
  101. $result = false;
  102. Db::startTrans();
  103. $insert_data = [];
  104. try {
  105. validate(WorkerValidate::class)->scene('scan')->check($data);
  106. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  107. $time = time();
  108. $insert_data = [
  109. 'user_id' => $this->userinfo['id'],
  110. 'code' => $data['code'],
  111. 'order_status' => 0,
  112. 'remark' => '未找到单号',
  113. 'order_data' => [],
  114. 'createtime' => date('Y-m-d H:i:s', $time)
  115. ];
  116. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  117. if (!empty($sql_data)) {
  118. $sql_data = $this->get_order_state($sql_data);
  119. if (false) {
  120. switch ($sql_data['order_status']) {
  121. case 'TRADE_CLOSED':
  122. $insert_data['order_status'] = 3;
  123. $insert_data['remark'] = '已退款';
  124. break;
  125. default:
  126. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  127. $insert_data['order_status'] = 2;
  128. $insert_data['remark'] = '请勿重复录入';
  129. } else {
  130. $result = $sql_data->save([
  131. 'user_id' => $this->userinfo['id'],
  132. 'entry_time' => $time,
  133. 'updatetime' => $time
  134. ]);
  135. if ($result) {
  136. $insert_data['order_status'] = 1;
  137. $insert_data['remark'] = '录入成功';
  138. $insert_data['spec_id'] = $sql_data['spec_id'];
  139. } else {
  140. $insert_data['remark'] = '录入失败';
  141. }
  142. }
  143. $entry_time = $sql_data['entry_time'];
  144. if (!empty($sql_data['entry_time'])) {
  145. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  146. }
  147. switch ($sql_data['incubator']) {
  148. case 1:
  149. $sql_data['incubator_title'] = '单层保温';
  150. break;
  151. case 2:
  152. $sql_data['incubator_title'] = '双层保温';
  153. break;
  154. default:
  155. $sql_data['incubator_title'] = '不是保温箱';
  156. break;
  157. }
  158. $sql_data['entry_time'] = $entry_time;
  159. break;
  160. }
  161. }
  162. $insert_data['order_data'] = $sql_data;
  163. }
  164. $result = $scanLog->save($insert_data);
  165. Db::commit();
  166. } catch (ValidateException $e) {
  167. return $this->error($e->getError());
  168. } catch (\Exception $e) {
  169. Db::rollback();
  170. $this->error($e->getMessage());
  171. }
  172. if ($result === false) {
  173. $this->error(__('没有新增任何数据'));
  174. }
  175. $this->success('', $insert_data);
  176. }
  177. //接口判断订单状态
  178. public function get_order_state($item)
  179. {
  180. if(!empty($item['waybill_no'])){
  181. $importListModel=new ImportListModel();
  182. $item=$importListModel->where("waybill_no",$item['waybill_no'])->find();
  183. switch ($item['type_id']) {
  184. // 1:风速
  185. case 1:
  186. $fengSu=new FengSu();
  187. return $fengSu->get_fengsu_order($item['trade_from'],$item['waybill_no']);
  188. // return $item;
  189. break;
  190. //2:聚水潭
  191. case 2:
  192. break;
  193. }
  194. return $item;
  195. }
  196. }
  197. //扫描记录
  198. public function scanlog(ScanLog $scanLog)
  199. {
  200. $where = [];
  201. $limit = $this->request->post('limit/d', 10); //条数
  202. $time = $this->request->post('create_time/s'); //日期
  203. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  204. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  205. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  206. if (!empty($time)) {
  207. $arr = explode(',', $time);
  208. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  209. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  210. }
  211. $list = $scanLog->alias('sl')
  212. ->where('user_id', $this->userinfo['id'])
  213. // ->whereIn('order_status', [0, 1, 2])
  214. ->where($where)
  215. ->order('createtime desc')
  216. ->paginate($limit)
  217. ->each(function ($item, $key) {
  218. $order_data = $this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  219. $item['order_data'] = [];
  220. if (!empty($order_data['waybill_no'])) {
  221. $incubator_title = '不是保温箱';
  222. switch ($order_data['incubator']) {
  223. case 1:
  224. $incubator_title = '单层保温';
  225. break;
  226. case 2:
  227. $incubator_title = '双层保温';
  228. break;
  229. }
  230. $item['order_data'] = $order_data;
  231. $item['order_data']['incubator_title'] = $incubator_title;
  232. }
  233. return $item;
  234. });
  235. $this->success('ok', $list);
  236. }
  237. //数据统计,今日昨日,本周
  238. public function pack_data_statistics(ScanLog $scanLog, GroupUserModel $groupUserModel)
  239. {
  240. $where = [];
  241. $limit = $this->request->post('limit/d', 10); //条数
  242. $todayTime = date("Y-m-d");
  243. $startTime = strtotime($todayTime . ' 00:00:00');
  244. $startTime = date('Y-m-d H:i:s', $startTime);
  245. $endTime = strtotime($todayTime . ' 23:59:59');
  246. $endTime = date('Y-m-d H:i:s', $endTime);
  247. $time = $startTime . ',' . $endTime;
  248. $time = $this->request->post('create_time/s', $time); //日期
  249. $this->time = $time;
  250. if (!empty($time)) {
  251. $arr = explode(',', $time);
  252. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  253. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  254. }
  255. $pid = $this->userinfo['id'];
  256. $where[] = ['sl.user_id', '=', $pid];
  257. $list = $scanLog->alias('sl')
  258. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  259. ->where('sl.order_status', 1)
  260. ->where('sl.spec_id', '<>', null)
  261. ->where($where)
  262. ->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")
  263. ->group('sl.spec_id')
  264. ->paginate($limit)->each(function ($item, $key) {
  265. $spec_name = $this->get_order_info($item['code']);
  266. $item['spec_name'] = $spec_name;
  267. unset($item['code']);
  268. });
  269. $list_sum = $scanLog->alias('sl')
  270. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  271. ->where('sl.order_status', 1)
  272. ->where('sl.spec_id', '<>', null)
  273. ->where($where)
  274. ->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")
  275. ->group('sl.spec_id')
  276. ->select();
  277. $quantity_sum = 0;
  278. $labor_cost_money_sum = 0;
  279. foreach ($list_sum as $item) {
  280. $spec_quantity_sum = $item['spec_quantity_sum'];
  281. $spec_labor_cost_money_sum = $item['spec_labor_cost_money_sum'];
  282. $quantity_sum = bcadd(strval($quantity_sum), strval($spec_quantity_sum), 0);
  283. $labor_cost_money_sum = bcadd(strval($labor_cost_money_sum), strval($spec_labor_cost_money_sum), 4);
  284. $labor_cost_money_sum = round((float)$labor_cost_money_sum, 2);
  285. }
  286. $data = [
  287. 'quantity_sum' => $quantity_sum,
  288. 'labor_cost_money_sum' => $labor_cost_money_sum,
  289. 'list' => empty($list) ? [] : $list
  290. ];
  291. $this->success('ok', $data);
  292. }
  293. //获取数量、总价
  294. public function quantity_labor_cost_money(ScanLog $scanLog)
  295. {
  296. $where = [];
  297. $todayTime = date("Y-m-d");
  298. $startTime = strtotime($todayTime . ' 00:00:00');
  299. $startTime = date('Y-m-d H:i:s', $startTime);
  300. $endTime = strtotime($todayTime . ' 23:59:59');
  301. $endTime = date('Y-m-d H:i:s', $endTime);
  302. $time = $startTime . ',' . $endTime;
  303. $time = $this->request->post('create_time/s', $time); //日期
  304. $this->time = $time;
  305. if (!empty($time)) {
  306. $arr = explode(',', $time);
  307. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  308. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  309. }
  310. $pid = $this->userinfo['id'];
  311. $where[] = ['sl.user_id', '=', $pid];
  312. $list_sum = $scanLog->alias('sl')
  313. ->join("shop_delivery sd", "sl.code = sd.waybill_no", "INNER")
  314. ->where('sl.order_status', 1)
  315. ->where('sl.spec_id', '<>', null)
  316. ->where($where)
  317. ->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")
  318. ->group('sl.spec_id')
  319. ->select();
  320. $quantity_sum = 0;
  321. $labor_cost_money_sum = 0;
  322. foreach ($list_sum as $item) {
  323. $spec_quantity_sum = $item['spec_quantity_sum'];
  324. $spec_labor_cost_money_sum = $item['spec_labor_cost_money_sum'];
  325. $quantity_sum = bcadd(strval($quantity_sum), strval($spec_quantity_sum), 0);
  326. $labor_cost_money_sum = bcadd(strval($labor_cost_money_sum), strval($spec_labor_cost_money_sum), 4);
  327. $labor_cost_money_sum = round((float)$labor_cost_money_sum, 2);
  328. }
  329. $data = [
  330. 'quantity_sum' => $quantity_sum,
  331. 'labor_cost_money_sum' => $labor_cost_money_sum
  332. ];
  333. return $data;
  334. }
  335. //获取今天出工人员状态,0=未出个,1=出工
  336. public function get_worker_out_state($id, $pid, $time)
  337. {
  338. $groupUserModel = new GroupUserModel();
  339. $count = $groupUserModel->where('id', $id)->where('pid', $pid)->count();
  340. if ($count == 0) return 1;
  341. $workerOutModel = new WorkerOutModel();
  342. $todayTime = strtotime('today');
  343. $where = [];
  344. $where[] = ['user_id', '=', $id];
  345. $where[] = ['pid', '=', $pid];
  346. $where[] = ['state', '=', 1];
  347. if (!empty($time)) {
  348. $arr = explode(',', $time);
  349. $where[] = ['createtime', '>=', strtotime($arr[0])];
  350. $where[] = ['createtime', '<=', strtotime($arr[1])];
  351. }
  352. return $workerOutModel->where($where)->count();
  353. }
  354. public function get_order_info($waybill_no)
  355. {
  356. return $this->shopDeliveryModel->where('waybill_no', $waybill_no)->value('spec_name');
  357. }
  358. }