Supplierscorerule.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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';
  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. if ($this->request->isAjax()) {
  29. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  30. $list = $this->model->where($where)->order($sort, $order)->paginate($limit);
  31. $rows = [];
  32. foreach ($list as $row) {
  33. $arr = $row->toArray();
  34. $arr['is_default_text'] = !empty($arr['is_default']) ? '使用' : '未使用';
  35. $rows[] = $arr;
  36. }
  37. return json(['total' => $list->total(), 'rows' => $rows]);
  38. }
  39. return $this->view->fetch();
  40. }
  41. public function add()
  42. {
  43. if (!$this->request->isPost()) {
  44. return $this->view->fetch();
  45. }
  46. $params = $this->request->post('row/a');
  47. if (empty($params)) {
  48. $this->error(__('Parameter %s can not be empty', ''));
  49. }
  50. $data = $this->normalizeRuleParams($params);
  51. $now = date('Y-m-d H:i:s');
  52. $data['createtime'] = $now;
  53. $data['updatetime'] = $now;
  54. Db::startTrans();
  55. try {
  56. $result = $this->model->allowField(true)->save($data);
  57. if ($result === false) {
  58. throw new Exception($this->model->getError());
  59. }
  60. if (!empty($data['is_default'])) {
  61. $this->clearOtherDefaults((int)$this->model->id);
  62. }
  63. Db::commit();
  64. } catch (ValidateException|PDOException|Exception $e) {
  65. Db::rollback();
  66. $this->error($e->getMessage());
  67. }
  68. $this->success();
  69. }
  70. public function edit($ids = null)
  71. {
  72. $row = $this->model->get($ids);
  73. if (!$row) {
  74. $this->error(__('No Results were found'));
  75. }
  76. if (!$this->request->isPost()) {
  77. $this->view->assign('row', $row);
  78. return $this->view->fetch();
  79. }
  80. $params = $this->request->post('row/a');
  81. if (empty($params)) {
  82. $this->error(__('Parameter %s can not be empty', ''));
  83. }
  84. $data = $this->normalizeRuleParams($params);
  85. $data['updatetime'] = date('Y-m-d H:i:s');
  86. Db::startTrans();
  87. try {
  88. $result = $row->allowField(true)->save($data);
  89. if ($result === false) {
  90. throw new Exception($row->getError());
  91. }
  92. if (!empty($data['is_default'])) {
  93. $this->clearOtherDefaults((int)$row->id);
  94. }
  95. Db::commit();
  96. } catch (ValidateException|PDOException|Exception $e) {
  97. Db::rollback();
  98. $this->error($e->getMessage());
  99. }
  100. $this->success();
  101. }
  102. /**
  103. * 设为默认计算规则
  104. */
  105. public function setdefault($ids = null)
  106. {
  107. $id = (int)($ids ?: $this->request->param('ids', 0));
  108. if ($id <= 0) {
  109. $this->error('参数错误');
  110. }
  111. $row = $this->model->get($id);
  112. if (!$row) {
  113. $this->error(__('No Results were found'));
  114. }
  115. Db::startTrans();
  116. try {
  117. $this->model->where('id', '>', 0)->update(['is_default' => 0, 'updatetime' => date('Y-m-d H:i:s')]);
  118. $row->save(['is_default' => 1, 'updatetime' => date('Y-m-d H:i:s')]);
  119. Db::commit();
  120. } catch (\Throwable $e) {
  121. Db::rollback();
  122. $this->error($e->getMessage());
  123. }
  124. $this->success('已设为默认规则');
  125. }
  126. /**
  127. * @param array<string, mixed> $params
  128. * @return array<string, mixed>
  129. */
  130. protected function normalizeRuleParams(array $params): array
  131. {
  132. $qw = trim((string)($params['quality_weight'] ?? ''));
  133. $pw = trim((string)($params['price_weight'] ?? ''));
  134. $lw = trim((string)($params['lead_weight'] ?? '0'));
  135. if ($qw === '' || !preg_match('/^\d+$/', $qw)) {
  136. $this->error('商务/技术分%须为非负整数');
  137. }
  138. if ($pw === '' || !preg_match('/^\d+$/', $pw)) {
  139. $this->error('价格分%须为非负整数');
  140. }
  141. if ($lw === '') {
  142. $lw = '0';
  143. }
  144. if (!preg_match('/^\d+$/', $lw)) {
  145. $this->error('交货分%须为非负整数');
  146. }
  147. $qwNum = (int)$qw;
  148. $pwNum = (int)$pw;
  149. $lwNum = (int)$lw;
  150. if ($qwNum + $pwNum + $lwNum <= 0) {
  151. $this->error('至少一项权重大于 0');
  152. }
  153. $status = trim((string)($params['status'] ?? 'normal'));
  154. if (!in_array($status, ['normal', 'hidden'], true)) {
  155. $status = 'normal';
  156. }
  157. $parts = [];
  158. if ($qwNum > 0) {
  159. $parts[] = '商务/技术分' . $qwNum . '%';
  160. }
  161. if ($pwNum > 0) {
  162. $parts[] = '价格分' . $pwNum . '%';
  163. }
  164. if ($lwNum > 0) {
  165. $parts[] = '交货分' . $lwNum . '%';
  166. }
  167. return [
  168. 'name' => $parts !== [] ? implode('+', $parts) : '规则',
  169. 'quality_weight' => $qwNum,
  170. 'price_weight' => $pwNum,
  171. 'lead_weight' => $lwNum,
  172. // 兼容旧字段:权重大于 0 即视为纳入
  173. 'lead_enabled' => $lwNum > 0 ? 1 : 0,
  174. 'is_default' => !empty($params['is_default']) ? 1 : 0,
  175. 'status' => $status,
  176. ];
  177. }
  178. protected function clearOtherDefaults(int $keepId): void
  179. {
  180. if ($keepId <= 0) {
  181. return;
  182. }
  183. $this->model->where('id', 'neq', $keepId)->update([
  184. 'is_default' => 0,
  185. 'updatetime' => date('Y-m-d H:i:s'),
  186. ]);
  187. }
  188. }