EmergencyResponseDimensionService.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\EmergencyResponse;
  5. use common\models\EmergencyResponseDimension;
  6. use common\models\OperationalAdjustmentsDimension;
  7. use Yii;
  8. use yii\db\ActiveQuery;
  9. use yii\db\Exception;
  10. class EmergencyResponseDimensionService
  11. {
  12. public static function getQuery(): ActiveQuery
  13. {
  14. return EmergencyResponseDimension::find()->where(["delete_time" => 0]);
  15. }
  16. /**
  17. * @param $EmergencyResponseId
  18. * @return EmergencyResponseDimension[]
  19. */
  20. public static function getListByEmergencyResponseId($EmergencyResponseId): array
  21. {
  22. return self::getQuery()->andWhere(["emergency_response_id" => $EmergencyResponseId])->all();
  23. }
  24. public static function add(EmergencyResponse $EmergencyResponse)
  25. {
  26. $param = [
  27. "accident_id",
  28. "emergency_response_id",
  29. "content",
  30. "type",
  31. "update_time"
  32. ];
  33. $time = time();
  34. $paramData = [];
  35. foreach (AccidentCasesDimensionService::$typeList as $type) {
  36. $paramData[] = [
  37. "accident_id" => $EmergencyResponse->accident_id,
  38. "emergency_response_id" => $EmergencyResponse->id,
  39. "content" => "",
  40. "type" => $type,
  41. "update_time" => $time,
  42. ];
  43. }
  44. Yii::$app->db->createCommand()->batchInsert(EmergencyResponseDimension::tableName(), $param, $paramData)->execute();
  45. }
  46. /**
  47. * @param $allParams
  48. * @return void
  49. * @throws AjaxException
  50. * @throws Exception
  51. */
  52. public static function update($allParams, $userInfo)
  53. {
  54. /** @var EmergencyResponseDimension $model */
  55. $model = self::getQuery()->andWhere(["emergency_response_id" => $allParams["emergency_response_id"], "type" => $allParams["type"]])->one();
  56. if (!$model) {
  57. throw new AjaxException("该评价维度不存在!");
  58. }
  59. $origin = AdminLogService::getOrigin($model, $allParams);
  60. $case = CaseService::checkIsPublish($model->accident_id, $userInfo);
  61. $model->content = $allParams["content"];
  62. $model->status = $allParams["status"];
  63. $model->update_time = time();
  64. if (!$model->save()) {
  65. throw new Exception($model->getErrorSummary(true)[0]);
  66. }
  67. AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $allParams);
  68. }
  69. public static function deleteByEmergencyResponseId($EmergencyResponseId): void
  70. {
  71. EmergencyResponseDimension::updateAll(["delete_time" => 0], ["emergency_response_id" => $EmergencyResponseId]);
  72. }
  73. public static function deleteByAccidentId($AccidentId): void
  74. {
  75. OperationalAdjustmentsDimension::updateAll(["delete_time" => time()], ["accident_id" => $AccidentId]);
  76. }
  77. }