Workorder.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\FactoryContext;
  5. use app\common\library\ps\ProcessWriter;
  6. use app\common\model\ps\Product as ProductModel;
  7. use app\common\model\ps\Workorder as WorkorderModel;
  8. /**
  9. * 工单接口
  10. */
  11. class Workorder extends Api
  12. {
  13. protected $noNeedRight = '*';
  14. /**
  15. * @var WorkorderModel
  16. */
  17. protected $model = null;
  18. public function _initialize()
  19. {
  20. parent::_initialize();
  21. if (!FactoryContext::id()) {
  22. $this->error('账号未绑定工厂');
  23. }
  24. $this->model = new WorkorderModel;
  25. }
  26. /**
  27. * 列表
  28. */
  29. public function index()
  30. {
  31. $limit = $this->request->request('limit/d', 10);
  32. $keyword = $this->request->request('keyword', '', 'trim');
  33. $query = $this->model->order('id desc');
  34. if ($keyword !== '') {
  35. $query->where('no|batch_no|customer', 'like', '%' . $keyword . '%');
  36. }
  37. $list = $query->paginate($limit);
  38. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  39. }
  40. /**
  41. * 详情
  42. */
  43. public function info()
  44. {
  45. $id = $this->request->request('id/d');
  46. $row = $this->model->with(['processes', 'product.processes'])->find($id);
  47. if (!$row) {
  48. $this->error(__('No Results were found'));
  49. }
  50. $this->success('', $row);
  51. }
  52. /**
  53. * 添加
  54. */
  55. public function add()
  56. {
  57. $params = $this->request->post();
  58. if (!$params) {
  59. $this->error(__('Parameter %s can not be empty', ''));
  60. }
  61. $params['creator_user_id'] = $this->auth->id;
  62. unset($params['id'], $params['factory_id']);
  63. $processes = isset($params['processes']) ? $params['processes'] : [];
  64. unset($params['processes']);
  65. $this->model->allowField(true)->save($params);
  66. if (!$processes && !empty($params['product_id'])) {
  67. $product = ProductModel::with('processes')->find($params['product_id']);
  68. $processes = $product ? $product['processes'] : [];
  69. }
  70. ProcessWriter::save($this->model->id, $processes, 'workorder');
  71. $this->success('', ['id' => $this->model->id]);
  72. }
  73. /**
  74. * 编辑
  75. */
  76. public function edit()
  77. {
  78. $id = $this->request->post('id/d');
  79. $row = $this->model->find($id);
  80. if (!$row) {
  81. $this->error(__('No Results were found'));
  82. }
  83. $params = $this->request->post();
  84. unset($params['id'], $params['factory_id']);
  85. $processes = isset($params['processes']) ? $params['processes'] : null;
  86. unset($params['processes']);
  87. $row->allowField(true)->save($params);
  88. if (is_array($processes)) {
  89. ProcessWriter::save($row->id, $processes, 'workorder');
  90. }
  91. $this->success();
  92. }
  93. /**
  94. * 删除
  95. */
  96. public function del()
  97. {
  98. $ids = $this->request->post('id');
  99. if (!$ids) {
  100. $this->error(__('Parameter %s can not be empty', 'id'));
  101. }
  102. $list = $this->model->where('id', 'in', $ids)->select();
  103. foreach ($list as $row) {
  104. \think\Db::name('ps_workorder_process')->where('workorder_id', $row->id)->delete();
  105. $row->delete();
  106. }
  107. $this->success();
  108. }
  109. }