AccidentCasesScoreConfigController.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCasesScore;
  5. use common\models\AccidentCasesScoreConfig;
  6. use common\services\AccidentCasesScoreConfigService;
  7. use common\services\AccidentCasesScoreService;
  8. use frontend\modules\api\components\BaseAdminController;
  9. use Yii;
  10. use yii\db\Exception;
  11. use yii\web\Response;
  12. class AccidentCasesScoreConfigController extends BaseAdminController
  13. {
  14. /**
  15. * 评价打分显示配置
  16. * @return Response
  17. */
  18. public function actionInfo()
  19. {
  20. $id = 1;
  21. $AccidentCasesScoreConfig = AccidentCasesScoreConfig::findOne($id);
  22. //组合数据
  23. $data['data'] = json_decode($AccidentCasesScoreConfig->content, true);
  24. return $this->asJson($data);
  25. }
  26. /**
  27. * 评价打分显示配置修改
  28. * @return Response
  29. * @throws AjaxException
  30. * @throws Exception
  31. */
  32. public function actionUpdate()
  33. {
  34. //方法不超时
  35. set_time_limit(0);
  36. //数据
  37. $data = Yii::$app->request->post()["data"];
  38. //验证权重数据是否错误
  39. foreach (AccidentCasesScoreService::$quanzhongConfig as $configKey => $configValue) {
  40. if (!isset($data[$configKey]) || $data[$configKey] <= 0) {
  41. throw new AjaxException("打分权重未配置或者权重小于等于0!");
  42. }
  43. }
  44. //修改数据
  45. $id = 1;
  46. $AccidentCasesScoreConfig = AccidentCasesScoreConfig::findOne($id);
  47. $AccidentCasesScoreConfig->content = json_encode($data, true);
  48. $AccidentCasesScoreConfig->update_time = time();
  49. $AccidentCasesScoreConfig->save();
  50. //根据权重将总分数据重新计算
  51. AccidentCasesScoreConfigService::setAccidentCasesScoreConfigUpdate();
  52. $needUpdateAccidentCasesScore = AccidentCasesScoreService::calculateTotalScoreAll();
  53. if ($needUpdateAccidentCasesScore != []) {
  54. $sql = $this->batchUpdate(AccidentCasesScore::tableName(), "id", "score", $needUpdateAccidentCasesScore, true);
  55. Yii::$app->db->createCommand($sql)->execute();
  56. }
  57. AccidentCasesScoreConfigService::clearAccidentCasesScoreConfigUpdate();
  58. return $this->asJson();
  59. }
  60. }