model = new \app\admin\model\Supplierscorerule; } public function index() { $this->request->filter(['strip_tags', 'trim']); 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['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 { $qw = trim((string)($params['quality_weight'] ?? '')); $pw = trim((string)($params['price_weight'] ?? '')); if ($qw === '' || !is_numeric($qw) || (float)$qw < 0) { $this->error('质量分%须为非负数字'); } if ($pw === '' || !is_numeric($pw) || (float)$pw < 0) { $this->error('价格分%须为非负数字'); } if ((float)$qw + (float)$pw <= 0) { $this->error('两项权重之和须大于 0'); } $status = trim((string)($params['status'] ?? 'normal')); if (!in_array($status, ['normal', 'hidden'], true)) { $status = 'normal'; } $qwNum = round((float)$qw, 2); $pwNum = round((float)$pw, 2); $qwLabel = rtrim(rtrim(sprintf('%.2F', $qwNum), '0'), '.'); $pwLabel = rtrim(rtrim(sprintf('%.2F', $pwNum), '0'), '.'); return [ 'name' => '质量分' . $qwLabel . '%+价格分' . $pwLabel . '%', 'quality_weight' => $qwNum, 'price_weight' => $pwNum, 'is_default' => !empty($params['is_default']) ? 1 : 0, 'status' => $status, ]; } 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'), ]); } }