CaseService.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCases;
  5. use common\models\AccidentCasesStudy;
  6. use common\models\BaseUser;
  7. use common\models\EventOverview;
  8. use common\util\AccidentCasesFocusObj;
  9. use common\util\AccidentCasesObj;
  10. use common\util\AccidentCasesScoreObj;
  11. use common\util\EmergencyResponseObj;
  12. use common\util\Holiday;
  13. use yii\db\ActiveQuery;
  14. class CaseService
  15. {
  16. //因为案例数据使用了假删除,查询语句统一管理
  17. /**
  18. * @param $id
  19. * @return AccidentCases
  20. * @throws AjaxException
  21. */
  22. public static function getCaseById($id): AccidentCases
  23. {
  24. /** @var AccidentCases $AccidentCases */
  25. $AccidentCases = self::getCaseQuery()->andWhere(["id" => $id])->one();
  26. if (!$AccidentCases) {
  27. throw new AjaxException("案例不存在!");
  28. }
  29. return $AccidentCases;
  30. }
  31. /**
  32. * @return ActiveQuery
  33. */
  34. public static function getCaseQuery(): ActiveQuery
  35. {
  36. return AccidentCases::find()->where(["delete_time" => 0]);
  37. }
  38. public static function getCaseShowQuery(): ActiveQuery
  39. {
  40. return AccidentCases::find()->where(["delete_time" => 0, "status" => 1]);
  41. }
  42. /**
  43. * @throws AjaxException
  44. */
  45. public static function setCheckNameList($model, $checkList): void
  46. {
  47. // var_dump(isEmpty($checkList));exit;
  48. /** @var $model AccidentCases */
  49. if ($checkList == []) {
  50. $model->check_name_list = "[]";
  51. } else {
  52. $checkNameList = [];
  53. $userList = array_column(UserService::getCanCheckList(), null, 'username');
  54. foreach ($checkList as $value) {
  55. if (isset($userList[$value])) {
  56. $checkNameList[] = $userList[$value]->name;
  57. } else {
  58. throw new AjaxException("审核人不符合要求!");
  59. }
  60. }
  61. $model->check_name_list = json_encode($checkNameList);
  62. }
  63. }
  64. /**
  65. * @throws AjaxException
  66. */
  67. public static function checkIsPublish($id, $userInfo): AccidentCases
  68. {
  69. //status 0 3 4可以修改
  70. $AccidentCases = self::getCaseById($id);
  71. if (!in_array($AccidentCases->status, [0, 4])) {
  72. throw new AjaxException("只能修改待审核和退回的案例!");
  73. }
  74. if ($AccidentCases->status === 1) {
  75. throw new AjaxException("发布中的案例无法操作!");
  76. }
  77. if ($AccidentCases->create_by != $userInfo->username) {
  78. if (!$userInfo->isSuperAdmin) {
  79. throw new AjaxException("只能操作自己创建的案例!");
  80. }
  81. }
  82. $AccidentCases->status = 0; //只要修改过了,自动变成待审核
  83. $AccidentCases->save();
  84. return $AccidentCases;
  85. }
  86. public static function deriveByAccidentCasesModel(AccidentCases $model): void
  87. {
  88. //万年历(日期、时间);派生:[日期特征][时间特征]
  89. list($model->day_type, $model->time_type) = self::deriveByStartTime($model->start_time);
  90. //
  91. $DictionaryDeriveInfo = DictionaryRelationshipService::getDictionaryDeriveInfo();
  92. if ($model->train_number != null) {
  93. //车号;派生:[列车编组][车型][线路]
  94. list($model->train_group, $model->train_model) = self::deriveByTrainNumber($model->train_number, $DictionaryDeriveInfo);
  95. }
  96. if ($model->line != null) {
  97. //所属线路分类;派生:[自动化程度];[信号系统供应商]
  98. list($model->automation_level, $model->signal_supplier) = self::deriveByLine($model->line, $DictionaryDeriveInfo);
  99. }
  100. //故障持续时长=故障/外部影响消除时间-事发时间
  101. $model->emergency_duration = ceil(($model->elimination_time - $model->start_time) / 60);
  102. //运营事件持续时长=运营基本恢复时间-事发时间
  103. $model->operation_duration = ceil(($model->recovery_time - $model->start_time) / 60);
  104. }
  105. /**
  106. * @param $start_time
  107. * @return int[] [$dayType, $timeType]
  108. */
  109. protected static function deriveByStartTime($start_time): array
  110. {
  111. return Holiday::getDayTypeAndTimeType($start_time);
  112. }
  113. protected static function deriveByTrainNumber($train_number, $DictionaryDeriveInfo = null): array
  114. {
  115. if (!$DictionaryDeriveInfo) {
  116. $DictionaryDeriveInfo = DictionaryRelationshipService::getDictionaryDeriveInfo();
  117. }
  118. $train_group = 0;
  119. $train_model = 0;
  120. $line = 0;
  121. $TrainNumberDeriveList = $DictionaryDeriveInfo[$train_number];
  122. foreach ($TrainNumberDeriveList as $TrainNumberDerive) {
  123. if ($TrainNumberDerive["param_name"] == "train_group") {
  124. $train_group = $TrainNumberDerive["relation_id"];
  125. } elseif ($TrainNumberDerive["param_name"] == "train_model") {
  126. $train_model = $TrainNumberDerive["relation_id"];
  127. } elseif ($TrainNumberDerive["param_name"] == "line") {
  128. $line = $TrainNumberDerive["relation_id"];
  129. }
  130. }
  131. return [$train_group, $train_model];
  132. }
  133. protected static function deriveByLine($line, $DictionaryDeriveInfo = null): array
  134. {
  135. if (!$DictionaryDeriveInfo) {
  136. $DictionaryDeriveInfo = DictionaryRelationshipService::getDictionaryDeriveInfo();
  137. }
  138. $automation_level = 0;
  139. $signal_supplier = 0;
  140. $lineDeriveList = $DictionaryDeriveInfo[$line];
  141. foreach ($lineDeriveList as $lineDerive) {
  142. if ($lineDerive["param_name"] == "automation_level") {
  143. $automation_level = $lineDerive["relation_id"];
  144. } elseif ($lineDerive["param_name"] == "signal_supplier") {
  145. $signal_supplier = $lineDerive["relation_id"];
  146. }
  147. }
  148. return [$automation_level, $signal_supplier];
  149. }
  150. /**
  151. * 获取案例详情
  152. * @param $id
  153. * @param string $type
  154. * @return array
  155. * @throws AjaxException
  156. */
  157. public static function getCaseDetail($id, string $type = "compare"): array
  158. {
  159. $AccidentCases = self::getCaseById($id);
  160. // $AccidentCases = self::checkIsPublish($id);
  161. self::dealWithCase($AccidentCases);
  162. list($totalScore, $emergencyResponseTotalScore, $operationalAdjustmentsTotalScore, $emergencyResponseScoreList, $operationalAdjustmentsScoreList) = AccidentCasesScoreService::getAccidentCasesDetailTotalSore($AccidentCases);
  163. $AccidentCases = $AccidentCases->attributes;
  164. $AccidentCases["totalScore"] = $totalScore;
  165. $AccidentCases["emergencyResponseTotalScore"] = $emergencyResponseTotalScore;
  166. $AccidentCases["operationalAdjustmentsTotalScore"] = $operationalAdjustmentsTotalScore;
  167. $AccidentCases["emergencyResponseScoreList"] = $emergencyResponseScoreList;
  168. $AccidentCases["operationalAdjustmentsScoreList"] = $operationalAdjustmentsScoreList;
  169. $AccidentCases["eventOverviewList"] = [];
  170. //查询原始资料
  171. $sourceDataList = array_column(EventSourceDataService::getSourceDataListByAccidentId($id), null, "id");
  172. /** @var EventOverview[] $eventOverviewList */
  173. $eventOverviewList = EventOverviewService::getEventOverviewQuery()->andWhere(["accident_id" => $AccidentCases["id"]])->orderBy('start_time asc,id asc')->all();
  174. foreach ($eventOverviewList as $eventOverview) {
  175. OperationalAdjustmentsService::dealWithSourceData($eventOverview, $sourceDataList);
  176. $AccidentCases["eventOverviewList"][] = $eventOverview;
  177. }
  178. $AccidentCases["emergencyResponseList"] = [];
  179. $emergencyResponseList = EmergencyResponseService::getEmergencyResponseQuery()->andWhere(["accident_id" => $AccidentCases["id"]])->orderBy('start_time asc,id asc')->all();
  180. EmergencyResponseService::listAddDimension($emergencyResponseList);
  181. foreach ($emergencyResponseList as $emergencyResponse) {
  182. OperationalAdjustmentsService::dealWithRules($emergencyResponse);
  183. OperationalAdjustmentsService::dealWithSourceData($emergencyResponse, $sourceDataList);
  184. $emergencyResponseObj = new EmergencyResponseObj($emergencyResponse);
  185. if ($type == "compare") {
  186. $AccidentCases["emergencyResponseList"][$emergencyResponse["type_first"]][] = $emergencyResponseObj;
  187. $AccidentCases["emergencyResponseList"][$emergencyResponse["type_second"]][] = $emergencyResponseObj;
  188. } else {
  189. $AccidentCases["emergencyResponseList"][] = $emergencyResponseObj;
  190. }
  191. }
  192. $AccidentCases["operationalAdjustmentsList"] = [];
  193. $operationalAdjustmentsList = OperationalAdjustmentsService::getQuery()->andWhere(["accident_id" => $AccidentCases["id"]])->orderBy('start_time asc,id asc')->all();
  194. OperationalAdjustmentsService::listAddDimension($operationalAdjustmentsList);
  195. foreach ($operationalAdjustmentsList as $operationalAdjustments) {
  196. OperationalAdjustmentsService::dealWithRules($operationalAdjustments);
  197. OperationalAdjustmentsService::dealWithSourceData($operationalAdjustments, $sourceDataList);
  198. $operationalAdjustmentsObj = new EmergencyResponseObj($operationalAdjustments);
  199. if ($type == "compare") {
  200. $AccidentCases["operationalAdjustmentsList"][$operationalAdjustments["type_first"]][] = $operationalAdjustmentsObj;
  201. $AccidentCases["operationalAdjustmentsList"][$operationalAdjustments["type_second"]][] = $operationalAdjustmentsObj;
  202. } else {
  203. $AccidentCases["operationalAdjustmentsList"][] = $operationalAdjustmentsObj;
  204. }
  205. }
  206. return $AccidentCases;
  207. }
  208. protected static function dealWithCase(AccidentCases $AccidentCases): void
  209. {
  210. //将数组从json转化出来
  211. $AccidentCases->type_base = json_decode($AccidentCases->type_base);
  212. $AccidentCases->type_first = json_decode($AccidentCases->type_first);
  213. $AccidentCases->type_second = json_decode($AccidentCases->type_second);
  214. $AccidentCases->type_third = json_decode($AccidentCases->type_third);
  215. $AccidentCases->type_extra = json_decode($AccidentCases->type_extra);
  216. $AccidentCases->position_base_second = json_decode($AccidentCases->position_base_second);
  217. $AccidentCases->position_extra_second = json_decode($AccidentCases->position_extra_second);
  218. $AccidentCases->substation_type = json_decode($AccidentCases->substation_type);
  219. $AccidentCases->duty_category = json_decode($AccidentCases->duty_category);
  220. $AccidentCases->duty_reason = json_decode($AccidentCases->duty_reason);
  221. $AccidentCases->check_list = json_decode($AccidentCases->check_list);
  222. $AccidentCases->check_name_list = json_decode($AccidentCases->check_name_list);
  223. }
  224. protected static function setTotalScore(AccidentCases $AccidentCases, $totalScoreList, $accidentId): void
  225. {
  226. //案例评分
  227. if (isset($totalScoreList[$accidentId])) {
  228. $AccidentCases->totalScore = new AccidentCasesScoreObj($totalScoreList[$accidentId]);
  229. } else {
  230. $AccidentCases->totalScore = null;
  231. }
  232. }
  233. public static function dealWithList($data): array
  234. {
  235. $newData = [];
  236. $accidentIds = array_column($data, "id");
  237. $totalScoreList = array_column(AccidentCasesScoreService::getTotalScoreList($accidentIds), null, "accident_id");
  238. foreach ($data as $key => $value) {
  239. /** @var AccidentCases $value */
  240. self::dealWithCase($value);
  241. self::setTotalScore($value, $totalScoreList, $value->id);
  242. $newData[] = new AccidentCasesObj($value);
  243. }
  244. return $newData;
  245. }
  246. public static function getTopCase($data)
  247. {
  248. }
  249. public static function dealWithFocusList($data): array
  250. {
  251. $newData = [];
  252. foreach ($data as $key => $value) {
  253. /** @var AccidentCases $value */
  254. $newData[] = new AccidentCasesFocusObj($value);
  255. }
  256. return $newData;
  257. }
  258. public static function study($id, BaseUser $userInfo)
  259. {
  260. $date = date("Y-m-d");
  261. $datetime = date("Y-m-d H:i:s");
  262. $studyInfo = AccidentCasesStudy::findOne(["accident_id" => $id, "uid" => $userInfo->id, "create_date" => $date]);
  263. if (!$studyInfo) {
  264. $studyInfo = new AccidentCasesStudy();
  265. $studyInfo->uid = $userInfo->id;
  266. $studyInfo->username = $userInfo->username;
  267. $studyInfo->name = $userInfo->name;
  268. $studyInfo->create_date = $date;
  269. $studyInfo->accident_id = $id;
  270. $studyInfo->learning_duration = 10;
  271. $studyInfo->last_learn_time = $datetime;
  272. $studyInfo->start_time = $datetime;
  273. $studyInfo->save();
  274. } else {
  275. if ($studyInfo->learning_duration < 1800) {
  276. //超过15秒以上,学习时间加10秒,同一个案例ID每天学习时长上限是30分钟。超过部分不予计算
  277. $studyInfo->learning_duration += 10;
  278. $studyInfo->last_learn_time = $datetime;
  279. $studyInfo->save();
  280. }
  281. }
  282. }
  283. public static function getCaseStudySqlSelectInfo(): string
  284. {
  285. return "a.title,s.uid,s.username,s.name,s.accident_id,s.learning_duration,s.create_date,s.start_time";
  286. }
  287. public static function setStudyTime($userInfoList): array
  288. {
  289. $listNew = [];
  290. foreach ($userInfoList as $userInfo){
  291. $startTime = date("Y-m-d H:i:s",strtotime("-30 days"));
  292. $totalCount = AccidentCasesStudy::find()->where(["uid" => $userInfo["id"]])->sum("learning_duration");
  293. $monthCount = AccidentCasesStudy::find()->where(["uid" => $userInfo["id"]])->andWhere([">=","start_time",$startTime])->sum("learning_duration");
  294. $userInfo["learning_duration"] = (int)$totalCount;
  295. $userInfo["learning_duration_month"] = (int)$monthCount;
  296. $listNew[] = $userInfo;
  297. }
  298. return $listNew;
  299. }
  300. }