Worker.php 2.8 KB

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