Product.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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\FactoryPerm;
  6. use app\common\library\ps\ProcessWriter;
  7. use app\common\model\ps\Product as ProductModel;
  8. /**
  9. * 产品接口
  10. */
  11. class Product extends Api
  12. {
  13. protected $noNeedRight = '*';
  14. /**
  15. * @var ProductModel
  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 ProductModel;
  25. $action = strtolower($this->request->action());
  26. if ($action === 'index' || $action === 'info') {
  27. if (!FactoryPerm::canAny(['product.view', 'workorder.view', 'workorder.edit', 'print.do'])) {
  28. $this->error('没有权限查看产品');
  29. }
  30. } else {
  31. $map = [
  32. 'add' => 'product.edit',
  33. 'edit' => 'product.edit',
  34. 'del' => 'product.delete',
  35. ];
  36. if (isset($map[$action]) && FactoryPerm::deny($map[$action])) {
  37. $this->error('没有权限:' . FactoryPerm::label($map[$action]));
  38. }
  39. }
  40. }
  41. /**
  42. * 列表
  43. */
  44. public function index()
  45. {
  46. $limit = $this->request->request('limit/d', 10);
  47. $keyword = $this->request->request('keyword', '', 'trim');
  48. $query = $this->model->order('id desc');
  49. if ($keyword !== '') {
  50. $query->where('name|code', 'like', '%' . $keyword . '%');
  51. }
  52. $list = $query->paginate($limit);
  53. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  54. }
  55. /**
  56. * 详情
  57. */
  58. public function info()
  59. {
  60. $id = $this->request->request('id/d');
  61. $row = $this->model->with('processes')->find($id);
  62. if (!$row) {
  63. $this->error(__('No Results were found'));
  64. }
  65. $this->success('', $row);
  66. }
  67. /**
  68. * 添加
  69. */
  70. public function add()
  71. {
  72. $params = $this->request->post();
  73. if (!$params) {
  74. $this->error(__('Parameter %s can not be empty', ''));
  75. }
  76. $params['creator_user_id'] = $this->auth->id;
  77. unset($params['id'], $params['factory_id'], $params['qty'], $params['sheet_qty']);
  78. $processes = isset($params['processes']) ? $params['processes'] : [];
  79. unset($params['processes']);
  80. $this->model->allowField(true)->save($params);
  81. ProcessWriter::save($this->model->id, $processes, 'product');
  82. $this->success('', ['id' => $this->model->id]);
  83. }
  84. /**
  85. * 编辑
  86. */
  87. public function edit()
  88. {
  89. $id = $this->request->post('id/d');
  90. $row = $this->model->find($id);
  91. if (!$row) {
  92. $this->error(__('No Results were found'));
  93. }
  94. $params = $this->request->post();
  95. unset($params['id'], $params['factory_id'], $params['qty'], $params['sheet_qty']);
  96. $processes = isset($params['processes']) ? $params['processes'] : null;
  97. unset($params['processes']);
  98. $row->allowField(true)->save($params);
  99. if (is_array($processes)) {
  100. ProcessWriter::save($row->id, $processes, 'product');
  101. }
  102. $this->success();
  103. }
  104. /**
  105. * 删除
  106. */
  107. public function del()
  108. {
  109. $ids = $this->request->post('id');
  110. if (!$ids) {
  111. $this->error(__('Parameter %s can not be empty', 'id'));
  112. }
  113. $list = $this->model->where('id', 'in', $ids)->select();
  114. foreach ($list as $row) {
  115. \think\Db::name('ps_product_process')->where('product_id', $row->id)->delete();
  116. $row->delete();
  117. }
  118. $this->success();
  119. }
  120. }