| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130 |
- <?php
- namespace app\api\controller\ps;
- use app\common\controller\Api;
- use app\common\library\ps\FactoryContext;
- use app\common\library\ps\FactoryPerm;
- use app\common\library\ps\ProcessWriter;
- use app\common\model\ps\Product as ProductModel;
- /**
- * 产品接口
- */
- class Product extends Api
- {
- protected $noNeedRight = '*';
- /**
- * @var ProductModel
- */
- protected $model = null;
- public function _initialize()
- {
- parent::_initialize();
- if (!FactoryContext::id()) {
- $this->error('账号未绑定工厂');
- }
- $this->model = new ProductModel;
- $action = strtolower($this->request->action());
- if ($action === 'index' || $action === 'info') {
- if (!FactoryPerm::canAny(['product.view', 'workorder.view', 'workorder.edit', 'print.do'])) {
- $this->error('没有权限查看产品');
- }
- } else {
- $map = [
- 'add' => 'product.edit',
- 'edit' => 'product.edit',
- 'del' => 'product.delete',
- ];
- if (isset($map[$action]) && FactoryPerm::deny($map[$action])) {
- $this->error('没有权限:' . FactoryPerm::label($map[$action]));
- }
- }
- }
- /**
- * 列表
- */
- public function index()
- {
- $limit = $this->request->request('limit/d', 10);
- $keyword = $this->request->request('keyword', '', 'trim');
- $query = $this->model->order('id desc');
- if ($keyword !== '') {
- $query->where('name|code', 'like', '%' . $keyword . '%');
- }
- $list = $query->paginate($limit);
- $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
- }
- /**
- * 详情
- */
- public function info()
- {
- $id = $this->request->request('id/d');
- $row = $this->model->with('processes')->find($id);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $this->success('', $row);
- }
- /**
- * 添加
- */
- public function add()
- {
- $params = $this->request->post();
- if (!$params) {
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $params['creator_user_id'] = $this->auth->id;
- unset($params['id'], $params['factory_id'], $params['qty'], $params['sheet_qty']);
- $processes = isset($params['processes']) ? $params['processes'] : [];
- unset($params['processes']);
- $this->model->allowField(true)->save($params);
- ProcessWriter::save($this->model->id, $processes, 'product');
- $this->success('', ['id' => $this->model->id]);
- }
- /**
- * 编辑
- */
- public function edit()
- {
- $id = $this->request->post('id/d');
- $row = $this->model->find($id);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $params = $this->request->post();
- unset($params['id'], $params['factory_id'], $params['qty'], $params['sheet_qty']);
- $processes = isset($params['processes']) ? $params['processes'] : null;
- unset($params['processes']);
- $row->allowField(true)->save($params);
- if (is_array($processes)) {
- ProcessWriter::save($row->id, $processes, 'product');
- }
- $this->success();
- }
- /**
- * 删除
- */
- public function del()
- {
- $ids = $this->request->post('id');
- if (!$ids) {
- $this->error(__('Parameter %s can not be empty', 'id'));
- }
- $list = $this->model->where('id', 'in', $ids)->select();
- foreach ($list as $row) {
- \think\Db::name('ps_product_process')->where('product_id', $row->id)->delete();
- $row->delete();
- }
- $this->success();
- }
- }
|