| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184 |
- <?php
- namespace app\admin\controller;
- use app\common\controller\Backend;
- use app\common\library\ProcuremenSupplierScore;
- use think\Db;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- use Exception;
- /**
- * 供应商服务评分权重规则
- *
- * @icon fa fa-sliders
- */
- class Supplierscorerule extends Backend
- {
- /** @var \app\admin\model\Supplierscorerule */
- protected $model = null;
- protected $searchFields = 'id';
- public function _initialize()
- {
- parent::_initialize();
- ProcuremenSupplierScore::ensureSchema();
- $this->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<string, mixed> $params
- * @return array<string, mixed>
- */
- 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'),
- ]);
- }
- }
|