EmergencyResponseController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\EmergencyResponse;
  5. use common\services\AccidentCasesScoreService;
  6. use common\services\AdminLogService;
  7. use common\services\CaseService;
  8. use common\services\EmergencyResponseDimensionService;
  9. use common\services\EmergencyResponseRelateToRulesPointsService;
  10. use common\services\EmergencyResponseService;
  11. use common\util\DealWithPostData;
  12. use frontend\modules\api\components\BaseAdminController;
  13. use Throwable;
  14. use Yii;
  15. use yii\base\InvalidConfigException;
  16. use yii\db\Exception;
  17. use yii\web\Response;
  18. class EmergencyResponseController extends BaseAdminController
  19. {
  20. /**
  21. * 应急处置列表
  22. * @param int $accident_id
  23. * @return Response
  24. * @throws AjaxException
  25. */
  26. public function actionList(int $accident_id)
  27. {
  28. if (!$accident_id) {
  29. throw new AjaxException("accident_id不存在!");
  30. }
  31. $query = EmergencyResponseService::getEmergencyResponseQuery()->andWhere(["accident_id" => $accident_id]);
  32. //排序
  33. $this->initSequence($query, "start_time desc");
  34. //主要筛选条件
  35. $query = $this->addConditionToQuery("type_first", $query);
  36. //组合数据
  37. $list = EmergencyResponseService::dealWithList($query->all(), $accident_id);
  38. EmergencyResponseService::listAddDimension($list);
  39. $list = EmergencyResponseService::getEmergencyResponseObjList($list);
  40. $data['data'] = $list;
  41. list($data['scoreList'], $data['totalScore']) = AccidentCasesScoreService::getEmergencyResponseScore($accident_id);
  42. return $this->asJson($data);
  43. }
  44. /**
  45. * 应急处置添加
  46. * @return Response
  47. * @throws Exception
  48. * @throws Throwable
  49. */
  50. public function actionAdd()
  51. {
  52. $post = Yii::$app->request->post();
  53. $post = DealWithPostData::changePostDataArrayToJson($post);
  54. $case = CaseService::checkIsPublish($post["accident_id"], $this->userInfo);
  55. $param = [
  56. "accident_id" => $post["accident_id"],
  57. "assess" => $post["assess"],
  58. "assess_two" => $post["assess_two"],
  59. "content" => $post["content"],
  60. "important_content" => $post["important_content"],
  61. "is_important" => $post["is_important"] ? 1 : 0,
  62. "is_time_period" => isset($post["is_time_period"]) && $post["is_time_period"] ? 1 : 0,
  63. "end_time" => isset($post["end_time"]) && $post["end_time"] !== "" ? $post["end_time"] : 0,
  64. "rules" => $post["rules"],
  65. "source_data" => $post["source_data"],
  66. "start_time" => $post["start_time"],
  67. "type_first" => $post["type_first"],
  68. "type_fourth" => $post["type_fourth"],
  69. "type_second" => $post["type_second"],
  70. "type_third" => $post["type_third"],
  71. ];
  72. $model = new EmergencyResponse($param);
  73. $origin = AdminLogService::getOrigin($model, $this->getAllParams(), true);
  74. Yii::$app->db->transaction(function () use ($model) {
  75. if (!$model->save()) {
  76. throw new Exception($model->getErrorSummary(true)[0]);
  77. }
  78. //将应急处置与规章表的关系添加
  79. EmergencyResponseRelateToRulesPointsService::add($model);
  80. //评价维度表添加
  81. EmergencyResponseDimensionService::add($model);
  82. });
  83. AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
  84. return $this->asJson();
  85. }
  86. /**
  87. * 应急处置修改
  88. * @param $id
  89. * @return Response
  90. * @throws InvalidConfigException
  91. * @throws Exception
  92. * @throws Throwable`
  93. */
  94. public function actionUpdate($id)
  95. {
  96. //通过setBodyParams来修改Yii::$app->request->post()返回值
  97. Yii::$app->request->setBodyParams(DealWithPostData::changePostDataArrayToJson(Yii::$app->request->post()));
  98. $model = EmergencyResponseService::getEmergencyResponseById($id);
  99. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  100. $case = CaseService::checkIsPublish($model->accident_id, $this->userInfo);
  101. $this->setAttributeFromGetAndPost($model);
  102. Yii::$app->db->transaction(function () use ($model) {
  103. if (!$model->save()) {
  104. throw new Exception($model->getErrorSummary(true)[0]);
  105. }
  106. //将应急处置与规章表的关系更新
  107. EmergencyResponseRelateToRulesPointsService::update($model);
  108. });
  109. AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
  110. return $this->asJson();
  111. }
  112. /**
  113. * 应急处置删除
  114. * @param $id
  115. * @return Response
  116. * @throws Exception
  117. * @throws Throwable
  118. */
  119. public function actionDelete($id)
  120. {
  121. $model = EmergencyResponseService::getEmergencyResponseById($id);
  122. $case = CaseService::checkIsPublish($model->accident_id, $this->userInfo);
  123. $model->delete_time = time();
  124. Yii::$app->db->transaction(function () use ($model) {
  125. if (!$model->save()) {
  126. throw new Exception($model->getErrorSummary(true)[0]);
  127. }
  128. //将应急处置与规章表的关系删除
  129. EmergencyResponseRelateToRulesPointsService::deleteByEmergencyResponseId($model->id);
  130. //删除评价维度
  131. EmergencyResponseDimensionService::deleteByEmergencyResponseId($model->id);
  132. });
  133. //返回数据
  134. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  135. AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
  136. return $this->asJson();
  137. }
  138. }