Worker.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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'], "1")) {
  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. $data['pack_specs'] = [
  40. [
  41. 'pack_name' => '三斤',
  42. 'pack_count' => '120',
  43. 'pack_money' => '100'
  44. ],
  45. [
  46. 'pack_name' => '五斤',
  47. 'pack_count' => '120',
  48. 'pack_money' => '100'
  49. ],
  50. [
  51. 'pack_name' => '五斤保温',
  52. 'pack_count' => '120',
  53. 'pack_money' => '100'
  54. ],
  55. ];
  56. $this->success('ok', $data);
  57. }
  58. //添加出入库
  59. public function scan(ScanLog $scanLog)
  60. {
  61. $data = $this->request->post();
  62. $result = false;
  63. Db::startTrans();
  64. $insert_data = [];
  65. try {
  66. validate(WorkerValidate::class)->scene('scan')->check($data);
  67. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  68. $time = time();
  69. $insert_data = [
  70. 'user_id' => $this->userinfo['id'],
  71. 'code' => $data['code'],
  72. 'order_status' => 0,
  73. 'remark' => '未找到单号',
  74. 'order_data' => [],
  75. 'createtime' => date('Y-m-d H:i:s', $time)
  76. ];
  77. $sql_data = $this->shopDeliveryModel->where('waybill_no', $data['code'])->find();
  78. if (!empty($sql_data)) {
  79. if (!empty($sql_data['user_id']) && $sql_data['user_id'] != 0) {
  80. $insert_data['order_status'] = 0;
  81. $insert_data['remark'] = '请勿重复录入';
  82. } else {
  83. $result = $sql_data->save([
  84. 'user_id' => $this->userinfo['id'],
  85. 'entry_time' => $time,
  86. 'updatetime' => $time
  87. ]);
  88. if ($result) {
  89. $insert_data['order_status'] = 1;
  90. $insert_data['remark'] = '录入成功';
  91. } else {
  92. $insert_data['remark'] = '录入失败';
  93. }
  94. }
  95. $entry_time = $sql_data['entry_time'];
  96. if (!empty($sql_data['entry_time'])) {
  97. $entry_time = date('Y-m-d H:i:s', $sql_data['entry_time']);
  98. }
  99. switch ($sql_data['incubator']) {
  100. case 1:
  101. $sql_data['incubator_title'] = '单层保温';
  102. break;
  103. case 2:
  104. $sql_data['incubator_title'] = '双层保温';
  105. break;
  106. default:
  107. $sql_data['incubator_title'] = '不是保温箱';
  108. break;
  109. }
  110. $sql_data['entry_time'] = $entry_time;
  111. $insert_data['order_data'] = $sql_data;
  112. }
  113. $result = $scanLog->save($insert_data);
  114. Db::commit();
  115. } catch (ValidateException $e) {
  116. return $this->error($e->getError());
  117. } catch (\Exception $e) {
  118. Db::rollback();
  119. $this->error($e->getMessage());
  120. }
  121. if ($result === false) {
  122. $this->error(__('没有新增任何数据'));
  123. }
  124. $this->success('', $insert_data);
  125. }
  126. //扫描记录
  127. public function scanlog(ScanLog $scanLog)
  128. {
  129. $where = [];
  130. $limit = $this->request->post('limit/d', 10); //条数
  131. $time = $this->request->post('create_time/s'); //日期
  132. // if (!empty($type)) $where[] = ['a.type', '=', $type];
  133. // if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  134. // if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  135. if (!empty($time)) {
  136. $arr = explode(',', $time);
  137. $where[] = ['sl.createtime', '>=', strtotime($arr[0])];
  138. $where[] = ['sl.createtime', '<=', strtotime($arr[1])];
  139. }
  140. $list = $scanLog
  141. ->where('user_id', $this->userinfo['id'])
  142. ->where($where)
  143. ->order('createtime desc')
  144. ->paginate($limit)
  145. ->each(function ($item, $key) {
  146. $order_data=$this->shopDeliveryModel->where('waybill_no', $item['code'])->find();
  147. $item['order_data'] = [];
  148. if (!empty($order_data['waybill_no'])) {
  149. $incubator_title = '不是保温箱';
  150. switch ($order_data['incubator']) {
  151. case 1:
  152. $incubator_title = '单层保温';
  153. break;
  154. case 2:
  155. $incubator_title = '双层保温';
  156. break;
  157. }
  158. $item['order_data']=$order_data;
  159. $item['order_data']['incubator_title']=$incubator_title;
  160. }
  161. return $item;
  162. });
  163. $this->success('ok', $list);
  164. }
  165. }