Worker.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. /**
  15. * 打包工人相关接口
  16. */
  17. class Worker extends Base
  18. {
  19. /**
  20. * 判断权限
  21. * @access protected
  22. */
  23. protected function _initialize()
  24. {
  25. parent::_initialize();
  26. if(!str_contains($this->userinfo['role'], "4")){
  27. $this->error(__('没有权限' . $this->userinfo['role']));
  28. }
  29. }
  30. //首页
  31. public function index(StockConfig $stockConfig)
  32. {
  33. $data = [];
  34. $data['today'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'today')->count();
  35. $data['yesterday'] = ScanLog::where('user_id', $this->userinfo['id'])->whereTime('createtime', 'yesterday')->count();
  36. $this->success('ok', $data);
  37. }
  38. //添加出入库
  39. public function scan(ScanLog $scanLog)
  40. {
  41. $data = $this->request->post();
  42. $result = false;
  43. Db::startTrans();
  44. $insert_data = [];
  45. try {
  46. validate(WorkerValidate::class)->scene('scan')->check($data);
  47. //$resData = $stockService::setGoOutStock($this->userinfo['id'], $data);
  48. $insert_data = [
  49. 'user_id' => $this->userinfo['id'],
  50. 'code' => $data['code'],
  51. 'order_status' => 0,
  52. 'remark' => '未找到单号',
  53. ];
  54. $result = $scanLog->save($insert_data);
  55. Db::commit();
  56. }catch (ValidateException $e) {
  57. return $this->error($e->getError());
  58. } catch (\Exception $e) {
  59. Db::rollback();
  60. $this->error($e->getMessage());
  61. }
  62. if ($result === false) {
  63. $this->error(__('没有新增任何数据'));
  64. }
  65. $this->success('', $insert_data);
  66. }
  67. //扫描记录
  68. public function scanlog(ScanLog $scanLog)
  69. {
  70. $where = [];
  71. $limit = $this->request->post('limit/d', 10); //条数
  72. $time = $this->request->post('create_time/s'); //日期
  73. if(!empty($type)) $where[] = ['a.type', '=', $type];
  74. if(!empty($type_id)) $where[] = ['a.type_id', '=',$type_id];
  75. if(!empty($spec_id)) $where[] = ['a.variety_id', '=',$spec_id];
  76. if(!empty($time)){
  77. $arr = explode(',', $time);
  78. $where[] = ['a.createtime', '>=', strtotime($arr[0])];
  79. $where[] = ['a.createtime', '<=', strtotime($arr[1])];
  80. }
  81. $list = ScanLog::where('user_id', $this->userinfo['id'])
  82. ->whereTime('createtime', 'today')
  83. ->order('id desc')
  84. ->paginate($limit);
  85. $this->success('ok', $list);
  86. }
  87. }