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|spec|remark', '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(); } }