Worker.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. $result = $sql_data->save([
  63. 'user_id' => $this->userinfo['id'],
  64. 'entry_time' => $time,
  65. 'updatetime' => $time
  66. ]);
  67. if ($result) {
  68. $insert_data['order_status'] = 1;
  69. $insert_data['remark'] = '录入成功';
  70. } else {
  71. $insert_data['remark'] = '录入失败';
  72. }
  73. $entry_time=$sql_data['entry_time'];
  74. if(!empty($sql_data['entry_time'])){
  75. $entry_time=date('Y-m-d H:i:s',$sql_data['entry_time']);
  76. }
  77. switch ($sql_data['incubator']) {
  78. case 1:
  79. $sql_data['incubator_title']='单层保温';
  80. break;
  81. case 2:
  82. $sql_data['incubator_title']='双层保温';
  83. break;
  84. default:
  85. $sql_data['incubator_title']='不是保温箱';
  86. break;
  87. }
  88. $sql_data['entry_time']=$entry_time;
  89. $insert_data['order_data']=$sql_data;
  90. }
  91. $result = $scanLog->save($insert_data);
  92. Db::commit();
  93. } catch (ValidateException $e) {
  94. return $this->error($e->getError());
  95. } catch (\Exception $e) {
  96. Db::rollback();
  97. $this->error($e->getMessage());
  98. }
  99. if ($result === false) {
  100. $this->error(__('没有新增任何数据'));
  101. }
  102. $this->success('', $insert_data);
  103. }
  104. //扫描记录
  105. public function scanlog(ScanLog $scanLog)
  106. {
  107. $where = [];
  108. $limit = $this->request->post('limit/d', 10); //条数
  109. $time = $this->request->post('create_time/s'); //日期
  110. if (!empty($type)) $where[] = ['a.type', '=', $type];
  111. if (!empty($type_id)) $where[] = ['a.type_id', '=', $type_id];
  112. if (!empty($spec_id)) $where[] = ['a.variety_id', '=', $spec_id];
  113. if (!empty($time)) {
  114. $arr = explode(',', $time);
  115. $where[] = ['a.createtime', '>=', strtotime($arr[0])];
  116. $where[] = ['a.createtime', '<=', strtotime($arr[1])];
  117. }
  118. $list = ScanLog::where('user_id', $this->userinfo['id'])
  119. ->whereTime('createtime', 'today')
  120. ->order('id desc')
  121. ->paginate($limit);
  122. $this->success('ok', $list);
  123. }
  124. }