model = new \app\admin\model\Supplierscorerule; } public function index() { $this->request->filter(['strip_tags', 'trim']); // 打开列表即补 name 字段并写入三条订单类型方案 ProcuremenSupplierScore::ensureSchema(); if ($this->request->isAjax()) { list($where, $sort, $order, $offset, $limit) = $this->buildparams(); $list = $this->model->where($where)->order($sort, $order)->paginate($limit); $rows = []; foreach ($list as $row) { $arr = $row->toArray(); $arr['name'] = trim((string)($arr['name'] ?? '')); $arr['is_default_text'] = !empty($arr['is_default']) ? '启用' : '未启用'; $rows[] = $arr; } return json(['total' => $list->total(), 'rows' => $rows]); } return $this->view->fetch(); } public function add() { if (!$this->request->isPost()) { return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $data = $this->normalizeRuleParams($params); $now = date('Y-m-d H:i:s'); $data['createtime'] = $now; $data['updatetime'] = $now; Db::startTrans(); try { $result = $this->model->allowField(true)->save($data); if ($result === false) { throw new Exception($this->model->getError()); } if (!empty($data['is_default'])) { $this->clearOtherDefaults((int)$this->model->id); } Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success(); } public function edit($ids = null) { $row = $this->model->get($ids); if (!$row) { $this->error(__('No Results were found')); } if (!$this->request->isPost()) { $this->view->assign('row', $row); return $this->view->fetch(); } $params = $this->request->post('row/a'); if (empty($params)) { $this->error(__('Parameter %s can not be empty', '')); } $data = $this->normalizeRuleParams($params); $data['updatetime'] = date('Y-m-d H:i:s'); Db::startTrans(); try { $result = $row->allowField(true)->save($data); if ($result === false) { throw new Exception($row->getError()); } if (!empty($data['is_default'])) { $this->clearOtherDefaults((int)$row->id); } Db::commit(); } catch (ValidateException|PDOException|Exception $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success(); } /** * 设为默认计算规则 */ public function setdefault($ids = null) { $id = (int)($ids ?: $this->request->param('ids', 0)); if ($id <= 0) { $this->error('参数错误'); } $row = $this->model->get($id); if (!$row) { $this->error(__('No Results were found')); } Db::startTrans(); try { $this->model->where('id', '>', 0)->update(['is_default' => 0, 'updatetime' => date('Y-m-d H:i:s')]); $row->save(['is_default' => 1, 'updatetime' => date('Y-m-d H:i:s')]); Db::commit(); } catch (\Throwable $e) { Db::rollback(); $this->error($e->getMessage()); } $this->success('已设为默认规则'); } /** * @param array $params * @return array */ protected function normalizeRuleParams(array $params): array { $name = trim((string)($params['name'] ?? '')); if ($name === '') { $this->error('请填写规则名称'); } if (mb_strlen($name) > 100) { $this->error('规则名称过长'); } $qw = trim((string)($params['quality_weight'] ?? '')); $pw = trim((string)($params['price_weight'] ?? '')); $lw = trim((string)($params['lead_weight'] ?? '0')); if ($qw === '' || !preg_match('/^\d+$/', $qw)) { $this->error('质量分%须为非负整数'); } if ($pw === '' || !preg_match('/^\d+$/', $pw)) { $this->error('价格分%须为非负整数'); } if ($lw === '') { $lw = '0'; } if (!preg_match('/^\d+$/', $lw)) { $this->error('交期分%须为非负整数'); } $qwNum = (int)$qw; $pwNum = (int)$pw; $lwNum = (int)$lw; if ($qwNum + $pwNum + $lwNum <= 0) { $this->error('至少一项权重大于 0'); } return [ 'name' => $name, 'quality_weight' => $qwNum, 'price_weight' => $pwNum, 'lead_weight' => $lwNum, 'is_default' => !empty($params['is_default']) ? 1 : 0, ]; } protected function clearOtherDefaults(int $keepId): void { if ($keepId <= 0) { return; } $this->model->where('id', 'neq', $keepId)->update([ 'is_default' => 0, 'updatetime' => date('Y-m-d H:i:s'), ]); } }