Worker.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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\ShopDelivery as ShopDeliveryModel;
  15. /**
  16. * 打包工人相关接口
  17. */
  18. class Worker extends Base
  19. {
  20. protected $shopDeliveryModel = null;
  21. /**
  22. * 判断权限
  23. * @access protected
  24. */
  25. protected function _initialize()
  26. {
  27. parent::_initialize();
  28. $this->shopDeliveryModel = new ShopDeliveryModel();
  29. if (!str_contains($this->userinfo['role'], "4")) {
  30. $this->error(__('没有权限' . $this->userinfo['role']));
  31. }
  32. }
  33. //首页
  34. public function index(StockConfig $stockConfig)
  35. {
  36. $data = [];
  37. $data['today'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'today')->count();
  38. $data['yesterday'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'yesterday')->count();
  39. $this->success('ok', $data);
  40. }
  41. //添加出入库
  42. public function scan(ScanLog $scanLog)
  43. {
  44. $data = $this->request->post();
  45. $result = false;
  46. Db::startTrans();
  47. $insert_data = [];
  48. try {
  49. validate(WorkerValidate::class)->scene('scan')->check($data);
  50. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  51. $time = time();
  52. $insert_data = [
  53. 'user_id' => $this->userinfo['id'],
  54. 'code' => $data['code'],
  55. 'order_status' => 0,
  56. 'remark' => '未找到单号',
  57. 'order_data' => [],
  58. 'createtime' => date('Y-m-d H:i:s', $time)
  59. ];
  60. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  61. if (!empty($sql_data)) {
  62. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  63. $insert_data['order_status'] = 0;
  64. $insert_data['remark'] = '请勿重复录入';
  65. } else {
  66. $result = $sql_data->save([
  67. 'user_id' => $this->userinfo['id'],
  68. 'entry_time' => $time,
  69. 'updatetime' => $time
  70. ]);
  71. if ($result) {
  72. $insert_data['order_status'] = 1;
  73. $insert_data['remark'] = '录入成功';
  74. } else {
  75. $insert_data['remark'] = '录入失败';
  76. }
  77. }
  78. $entry_time = $sql_data['entry_time'];
  79. if (!empty($sql_data['entry_time'])) {
  80. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  81. }
  82. switch ($sql_data['incubator']) {
  83. case 1:
  84. $sql_data['incubator_title'] = '单层保温';
  85. break;
  86. case 2:
  87. $sql_data['incubator_title'] = '双层保温';
  88. break;
  89. default:
  90. $sql_data['incubator_title'] = '不是保温箱';
  91. break;
  92. }
  93. $sql_data['entry_time'] = $entry_time;
  94. $insert_data['order_data'] = $sql_data;
  95. }
  96. $result = $scanLog->save($insert_data);
  97. Db::commit();
  98. } catch (ValidateException $e) {
  99. return $this->error($e->getError());
  100. } catch (\Exception $e) {
  101. Db::rollback();
  102. $this->error($e->getMessage());
  103. }
  104. if ($result === false) {
  105. $this->error(__('没有新增任何数据'));
  106. }
  107. $this->success('', $insert_data);
  108. }
  109. //扫描记录
  110. public function scanlog(ScanLog $scanLog)
  111. {
  112. $where = [];
  113. $limit = $this->request->post('limit/d', 10); //条数
  114. $time = $this->request->post('create_time/s'); //日期
  115. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  116. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  117. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  118. if (!empty($time)) {
  119. $arr = explode(',', $time);
  120. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  121. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  122. }
  123. $list = $scanLog
  124. ->where('user_id', $this->userinfo['id'])
  125. ->where($where)
  126. ->order('createtime desc')
  127. ->paginate($limit)
  128. ->each(function ($item, $key) {
  129. $order_data=$this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  130. $item['order_data'] = [];
  131. if (!empty($order_data['waybill_no'])) {
  132. $incubator_title = '不是保温箱';
  133. switch ($order_data['incubator']) {
  134. case 1:
  135. $incubator_title = '单层保温';
  136. break;
  137. case 2:
  138. $incubator_title = '双层保温';
  139. break;
  140. }
  141. $item['order_data']=$order_data;
  142. $item['order_data']['incubator_title']=$incubator_title;
  143. }
  144. return $item;
  145. });
  146. $this->success('ok', $list);
  147. }
  148. }