Supplierscorerule.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\ProcuremenSupplierScore;
  5. use think\Db;
  6. use think\exception\PDOException;
  7. use think\exception\ValidateException;
  8. use Exception;
  9. /**
  10. * 供应商服务评分权重规则
  11. *
  12. * @icon fa fa-sliders
  13. */
  14. class Supplierscorerule extends Backend
  15. {
  16. /** @var \app\admin\model\Supplierscorerule */
  17. protected $model = null;
  18. protected $searchFields = 'id,name';
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. ProcuremenSupplierScore::ensureSchema();
  23. $this->model = new \app\admin\model\Supplierscorerule;
  24. }
  25. public function index()
  26. {
  27. $this->request->filter(['strip_tags', 'trim']);
  28. // 打开列表即补 name 字段并写入三条订单类型方案
  29. ProcuremenSupplierScore::ensureSchema();
  30. if ($this->request->isAjax()) {
  31. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  32. $list = $this->model->where($where)->order($sort, $order)->paginate($limit);
  33. $rows = [];
  34. foreach ($list as $row) {
  35. $arr = $row->toArray();
  36. $arr['name'] = trim((string)($arr['name'] ?? ''));
  37. $arr['is_default_text'] = !empty($arr['is_default']) ? '启用' : '未启用';
  38. $rows[] = $arr;
  39. }
  40. return json(['total' => $list->total(), 'rows' => $rows]);
  41. }
  42. return $this->view->fetch();
  43. }
  44. public function add()
  45. {
  46. if (!$this->request->isPost()) {
  47. return $this->view->fetch();
  48. }
  49. $params = $this->request->post('row/a');
  50. if (empty($params)) {
  51. $this->error(__('Parameter %s can not be empty', ''));
  52. }
  53. $data = $this->normalizeRuleParams($params);
  54. $now = date('Y-m-d H:i:s');
  55. $data['createtime'] = $now;
  56. $data['updatetime'] = $now;
  57. Db::startTrans();
  58. try {
  59. $result = $this->model->allowField(true)->save($data);
  60. if ($result === false) {
  61. throw new Exception($this->model->getError());
  62. }
  63. if (!empty($data['is_default'])) {
  64. $this->clearOtherDefaults((int)$this->model->id);
  65. }
  66. Db::commit();
  67. } catch (ValidateException|PDOException|Exception $e) {
  68. Db::rollback();
  69. $this->error($e->getMessage());
  70. }
  71. $this->success();
  72. }
  73. public function edit($ids = null)
  74. {
  75. $row = $this->model->get($ids);
  76. if (!$row) {
  77. $this->error(__('No Results were found'));
  78. }
  79. if (!$this->request->isPost()) {
  80. $this->view->assign('row', $row);
  81. return $this->view->fetch();
  82. }
  83. $params = $this->request->post('row/a');
  84. if (empty($params)) {
  85. $this->error(__('Parameter %s can not be empty', ''));
  86. }
  87. $data = $this->normalizeRuleParams($params);
  88. $data['updatetime'] = date('Y-m-d H:i:s');
  89. Db::startTrans();
  90. try {
  91. $result = $row->allowField(true)->save($data);
  92. if ($result === false) {
  93. throw new Exception($row->getError());
  94. }
  95. if (!empty($data['is_default'])) {
  96. $this->clearOtherDefaults((int)$row->id);
  97. }
  98. Db::commit();
  99. } catch (ValidateException|PDOException|Exception $e) {
  100. Db::rollback();
  101. $this->error($e->getMessage());
  102. }
  103. $this->success();
  104. }
  105. /**
  106. * 设为默认计算规则
  107. */
  108. public function setdefault($ids = null)
  109. {
  110. $id = (int)($ids ?: $this->request->param('ids', 0));
  111. if ($id <= 0) {
  112. $this->error('参数错误');
  113. }
  114. $row = $this->model->get($id);
  115. if (!$row) {
  116. $this->error(__('No Results were found'));
  117. }
  118. Db::startTrans();
  119. try {
  120. $this->model->where('id', '>', 0)->update(['is_default' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
  121. $row->save(['is_default' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
  122. Db::commit();
  123. } catch (\Throwable $e) {
  124. Db::rollback();
  125. $this->error($e->getMessage());
  126. }
  127. $this->success('已设为默认规则');
  128. }
  129. /**
  130. * @param array<string, mixed> $params
  131. * @return array<string, mixed>
  132. */
  133. protected function normalizeRuleParams(array $params): array
  134. {
  135. $name = trim((string)($params['name'] ?? ''));
  136. if ($name === '') {
  137. $this->error('请填写规则名称');
  138. }
  139. if (mb_strlen($name) > 100) {
  140. $this->error('规则名称过长');
  141. }
  142. $qw = trim((string)($params['quality_weight'] ?? ''));
  143. $pw = trim((string)($params['price_weight'] ?? ''));
  144. $lw = trim((string)($params['lead_weight'] ?? '0'));
  145. if ($qw === '' || !preg_match('/^\d+$/', $qw)) {
  146. $this->error('质量分%须为非负整数');
  147. }
  148. if ($pw === '' || !preg_match('/^\d+$/', $pw)) {
  149. $this->error('价格分%须为非负整数');
  150. }
  151. if ($lw === '') {
  152. $lw = '0';
  153. }
  154. if (!preg_match('/^\d+$/', $lw)) {
  155. $this->error('交期分%须为非负整数');
  156. }
  157. $qwNum = (int)$qw;
  158. $pwNum = (int)$pw;
  159. $lwNum = (int)$lw;
  160. if ($qwNum + $pwNum + $lwNum <= 0) {
  161. $this->error('至少一项权重大于 0');
  162. }
  163. return [
  164. 'name' => $name,
  165. 'quality_weight' => $qwNum,
  166. 'price_weight' => $pwNum,
  167. 'lead_weight' => $lwNum,
  168. 'is_default' => !empty($params['is_default']) ? 1 : 0,
  169. ];
  170. }
  171. protected function clearOtherDefaults(int $keepId): void
  172. {
  173. if ($keepId <= 0) {
  174. return;
  175. }
  176. $this->model->where('id', 'neq', $keepId)->update([
  177. 'is_default' => 0,
  178. 'updatetime' => date('Y-m-d H:i:s'),
  179. ]);
  180. }
  181. }