AccidentCasesDimensionService.php 3.0 KB

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