Supplierscorerule.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if ($qw === '' || !is_numeric($qw) || (float)$qw < 0) {
  135. $this->error('质量分%须为非负数字');
  136. }
  137. if ($pw === '' || !is_numeric($pw) || (float)$pw < 0) {
  138. $this->error('价格分%须为非负数字');
  139. }
  140. if ((float)$qw + (float)$pw <= 0) {
  141. $this->error('两项权重之和须大于 0');
  142. }
  143. $status = trim((string)($params['status'] ?? 'normal'));
  144. if (!in_array($status, ['normal', 'hidden'], true)) {
  145. $status = 'normal';
  146. }
  147. $qwNum = round((float)$qw, 2);
  148. $pwNum = round((float)$pw, 2);
  149. $qwLabel = rtrim(rtrim(sprintf('%.2F', $qwNum), '0'), '.');
  150. $pwLabel = rtrim(rtrim(sprintf('%.2F', $pwNum), '0'), '.');
  151. return [
  152. 'name' => '质量分' . $qwLabel . '%+价格分' . $pwLabel . '%',
  153. 'quality_weight' => $qwNum,
  154. 'price_weight' => $pwNum,
  155. 'is_default' => !empty($params['is_default']) ? 1 : 0,
  156. 'status' => $status,
  157. ];
  158. }
  159. protected function clearOtherDefaults(int $keepId): void
  160. {
  161. if ($keepId <= 0) {
  162. return;
  163. }
  164. $this->model->where('id', 'neq', $keepId)->update([
  165. 'is_default' => 0,
  166. 'updatetime' => date('Y-m-d H:i:s'),
  167. ]);
  168. }
  169. }