AccidentCasesScoreService.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCases;
  5. use common\models\AccidentCasesScore;
  6. use common\models\AccidentCasesScoreConfig;
  7. use common\util\AccidentCasesDimensionObj;
  8. use common\util\AccidentCasesScoreObj;
  9. use common\util\CalculateTotalScoreObj;
  10. use Yii;
  11. use yii\db\ActiveRecord;
  12. use yii\db\Exception;
  13. class AccidentCasesScoreService
  14. {
  15. public static int $totalScore = 1;
  16. public static int $EmergencyResponseTotalCategory = 2;
  17. public static int $EmergencyResponseCategory = 3;
  18. public static int $OperationalAdjustmentsTotalCategory = 4;
  19. public static int $OperationalAdjustmentsCategory = 5;
  20. public static function getCategoryList()
  21. {
  22. return [
  23. self::$totalScore => [0],
  24. self::$EmergencyResponseTotalCategory => [247],
  25. self::$EmergencyResponseCategory => [248, 249, 250, 251, 252, 253],
  26. self::$OperationalAdjustmentsTotalCategory => [346],
  27. self::$OperationalAdjustmentsCategory => [347, 348],
  28. ];
  29. }
  30. public static function getContentDictionaryIdList($categoryList = [])
  31. {
  32. if ($categoryList == []) {
  33. $categoryList = self::getCategoryList();
  34. }
  35. return array_merge($categoryList[self::$EmergencyResponseTotalCategory], $categoryList[self::$EmergencyResponseCategory], $categoryList[self::$OperationalAdjustmentsTotalCategory], $categoryList[self::$OperationalAdjustmentsCategory]);
  36. }
  37. public static function initScore($accidentId): void
  38. {
  39. $param = [
  40. "accident_id",
  41. "dictionary_id",
  42. "category",
  43. "content",
  44. "content_two",
  45. "score",
  46. "update_time"
  47. ];
  48. $time = time();
  49. $paramData = [];
  50. $categoryList = self::getCategoryList();
  51. foreach ($categoryList as $category => $dictionaryIdList) {
  52. foreach ($dictionaryIdList as $dictionaryId) {
  53. $paramData[] = [
  54. "accident_id" => $accidentId,
  55. "dictionary_id" => $dictionaryId,
  56. "category" => $category,
  57. "content" => "",
  58. "content_two" => "",
  59. "score" => 0,
  60. "update_time" => $time,
  61. ];
  62. }
  63. }
  64. Yii::$app->db->createCommand()->batchInsert(AccidentCasesScore::tableName(), $param, $paramData)->execute();
  65. //初始化评价维度
  66. AccidentCasesDimensionService::initContent($accidentId, self::getContentDictionaryIdList(self::getCategoryList()));
  67. }
  68. public static function getQuery()
  69. {
  70. return AccidentCasesScore::find()->where(["delete_time" => 0]);
  71. }
  72. public static function checkNeedCalculate(AccidentCasesScore $AccidentCasesScore): bool
  73. {
  74. if (in_array($AccidentCasesScore->category, self::getAutoCategoryList())) {
  75. $need = false;
  76. } else {
  77. $need = true;
  78. }
  79. return $need;
  80. }
  81. /**
  82. * @return array [
  83. * self::$totalScore,
  84. * self::$EmergencyResponseTotalCategory,
  85. * self::$OperationalAdjustmentsTotalCategory
  86. * ];
  87. */
  88. protected static function getAutoCategoryList(): array
  89. {
  90. return [
  91. self::$totalScore,
  92. self::$EmergencyResponseTotalCategory,
  93. self::$OperationalAdjustmentsTotalCategory
  94. ];
  95. }
  96. public static $quanzhongConfig = [
  97. 247 => 100, //category 2
  98. 248 => 100, //category 3
  99. 249 => 100, //category 3
  100. 250 => 100, //category 3
  101. 251 => 100, //category 3
  102. 252 => 100, //category 3
  103. 253 => 100, //category 3
  104. 346 => 100, //category 4
  105. 347 => 100, //category 4
  106. 348 => 100, //category 4
  107. ];
  108. public static function calculateTotalScore($accident_id): void
  109. {
  110. $AccidentCasesScoreConfig = AccidentCasesScoreConfig::findOne(1);
  111. if (!$AccidentCasesScoreConfig) {
  112. $AccidentCasesScoreConfig = new AccidentCasesScoreConfig();
  113. $AccidentCasesScoreConfig->content = json_encode(self::$quanzhongConfig);
  114. $AccidentCasesScoreConfig->save();
  115. }
  116. $quanzhong = json_decode($AccidentCasesScoreConfig->content, true);
  117. $num = 0;
  118. $calculateTotalScoreObj = new CalculateTotalScoreObj();
  119. $needUpdate = [];
  120. /** @var AccidentCasesScore[] $AccidentScoreList */
  121. $AccidentScoreList = self::getQuery()->andWhere(["accident_id" => $accident_id])->orderBy("accident_id asc")->all();
  122. foreach ($AccidentScoreList as $AccidentScore) {
  123. $obj = self::getAccidentCasesScoreObj($AccidentScore);
  124. if (in_array($AccidentScore->category, self::getAutoCategoryList())) {
  125. $needUpdate[] = $obj;
  126. }
  127. $attr = "_" . $AccidentScore->dictionary_id;
  128. $calculateTotalScoreObj->$attr = $obj;
  129. $num++;
  130. if ($num == 11) {
  131. $num = 0;
  132. $calculateTotalScoreObj->calculate($quanzhong);
  133. }
  134. }
  135. foreach ($needUpdate as $value) {
  136. Yii::$app->db->createCommand()->update(AccidentCasesScore::tableName(), ['score' => $value->score], ["id" => $value->id])->execute();
  137. }
  138. }
  139. public static function calculateTotalScoreAll()
  140. {
  141. $AccidentCasesScoreConfig = AccidentCasesScoreConfig::findOne(1);
  142. $quanzhong = json_decode($AccidentCasesScoreConfig->content, true);
  143. $needUpdate = [];
  144. $AccidentScoreArrayList = [];
  145. /** @var AccidentCasesScore[] $AccidentScoreList */
  146. $AccidentScoreList = self::getQuery()->select("id,accident_id,category,dictionary_id,score")->all();
  147. foreach ($AccidentScoreList as $AccidentScore) {
  148. $AccidentScoreArrayList[$AccidentScore->accident_id][] = $AccidentScore;
  149. }
  150. foreach ($AccidentScoreArrayList as $AccidentScoreArray) {
  151. $calculateTotalScoreObj = new CalculateTotalScoreObj();
  152. if (count($AccidentScoreArray) === 11) {
  153. foreach ($AccidentScoreArray as $AccidentScore) {
  154. $obj = self::getAccidentCasesScoreObj($AccidentScore);
  155. if (in_array($AccidentScore->category, self::getAutoCategoryList())) {
  156. $needUpdate[] = $obj;
  157. }
  158. $attr = "_" . $AccidentScore->dictionary_id;
  159. $calculateTotalScoreObj->$attr = $obj;
  160. }
  161. $calculateTotalScoreObj->calculate($quanzhong);
  162. }
  163. }
  164. $needUpdateAccidentCasesScore = [];
  165. foreach ($needUpdate as $value) {
  166. $needUpdateAccidentCasesScore[] = [
  167. "id" => $value->id,
  168. "score" => $value->score,
  169. ];
  170. }
  171. return $needUpdateAccidentCasesScore;
  172. }
  173. /**
  174. * @param $id
  175. * @return AccidentCasesScore
  176. * @throws AjaxException
  177. */
  178. public static function getAccidentCasesScoreById($id): AccidentCasesScore
  179. {
  180. /** @var AccidentCasesScore $AccidentCasesScore */
  181. $AccidentCasesScore = self::getQuery()->andWhere(["id" => $id])->one();
  182. if (!$AccidentCasesScore) {
  183. throw new AjaxException("该评价不存在!");
  184. }
  185. return $AccidentCasesScore;
  186. }
  187. /**
  188. * 获取详情页评价数据
  189. * @param AccidentCases $AccidentCases
  190. * @return array
  191. */
  192. public static function getAccidentCasesDetailTotalSore(AccidentCases $AccidentCases): array
  193. {
  194. $totalScore = [];
  195. $emergencyResponseTotalScore = [];
  196. $operationalAdjustmentsTotalScore = [];
  197. $emergencyResponseScoreList = [];
  198. $operationalAdjustmentsScoreList = [];
  199. $accidentCasesScoreList = self::getList($AccidentCases->id);
  200. self::listAddDimension($AccidentCases->id, $accidentCasesScoreList);
  201. foreach ($accidentCasesScoreList as $accidentCasesScore) {
  202. $obj = self::getAccidentCasesScoreObj($accidentCasesScore);
  203. if ($obj->category == self::$totalScore) {
  204. $totalScore = $obj;
  205. } elseif ($obj->category == self::$EmergencyResponseTotalCategory) {
  206. $obj->category = 0;
  207. $emergencyResponseTotalScore = $obj;
  208. } elseif ($obj->category == self::$OperationalAdjustmentsTotalCategory) {
  209. $obj->category = 0;
  210. $operationalAdjustmentsTotalScore = $obj;
  211. } elseif ($obj->category == self::$EmergencyResponseCategory) {
  212. $obj->category = 0;
  213. $emergencyResponseScoreList[] = $obj;
  214. } elseif ($obj->category == self::$OperationalAdjustmentsCategory) {
  215. $obj->category = 0;
  216. $operationalAdjustmentsScoreList[] = $obj;
  217. }
  218. }
  219. return [$totalScore, $emergencyResponseTotalScore, $operationalAdjustmentsTotalScore, $emergencyResponseScoreList, $operationalAdjustmentsScoreList];
  220. }
  221. /**
  222. * 案件总评分列表
  223. * @param $accidentIds
  224. * @return array|ActiveRecord[]
  225. */
  226. public static function getTotalScoreList($accidentIds)
  227. {
  228. return self::getList($accidentIds, [self::$totalScore]);
  229. }
  230. public static function getEmergencyResponseScore($accidentId): array
  231. {
  232. return self::getScore($accidentId, [self::$EmergencyResponseTotalCategory, self::$EmergencyResponseCategory]);
  233. }
  234. public static function getOperationalAdjustmentsScore($accidentId): array
  235. {
  236. return self::getScore($accidentId, [self::$OperationalAdjustmentsTotalCategory, self::$OperationalAdjustmentsCategory]);
  237. }
  238. public static function updateByTotalScore($accidentId, $totalScore)
  239. {
  240. $param = [
  241. "content" => "",
  242. "content_two" => "",
  243. ];
  244. if ($totalScore) {
  245. if (isset($totalScore["content"])) {
  246. $param["content"] = $totalScore["content"];
  247. }
  248. if (isset($totalScore["content_two"])) {
  249. $param["content_two"] = $totalScore["content_two"];
  250. }
  251. }
  252. self::updateByAccidentId($accidentId, $param);
  253. }
  254. /**
  255. * @throws \Throwable
  256. * @throws AjaxException
  257. */
  258. public static function updateById($id, $allParams, $userInfo)
  259. {
  260. $model = self::getAccidentCasesScoreById($id);
  261. // $origin = AdminLogService::getOrigin($model, $allParams);
  262. self::update($model, $allParams, $userInfo);
  263. // AdminLogService::saveLogWithUpdateHistory($origin, $model, $allParams);
  264. }
  265. /**
  266. * @param $accidentId
  267. * @param $allParams
  268. * @return void
  269. * @throws AjaxException
  270. * @throws \Throwable
  271. */
  272. public
  273. static function updateByAccidentId($accidentId, $allParams)
  274. {
  275. /** @var AccidentCasesScore $model */
  276. $model = self::getQuery()->andWhere(["accident_id" => $accidentId, "category" => self::$totalScore])->one();
  277. if (!$model) {
  278. throw new AjaxException("总分不存在!");
  279. }
  280. self::update($model, $allParams);
  281. }
  282. public
  283. static function deleteByAccidentCasesId($accident_id): void
  284. {
  285. AccidentCasesScore::updateAll(["delete_time" => time()], ["accident_id" => $accident_id]);
  286. }
  287. /**
  288. * @param AccidentCasesScore $model
  289. * @param $allParams
  290. * @param $userInfo
  291. * @return void
  292. * @throws AjaxException
  293. * @throws \Throwable
  294. */
  295. protected static function update(AccidentCasesScore $model, $allParams, $userInfo)
  296. {
  297. $origin = AdminLogService::getOrigin($model, $allParams);
  298. $case = CaseService::checkIsPublish($model->accident_id, $userInfo);
  299. if (!isset($allParams["score"]) && !isset($allParams["content"]) && !isset($allParams["content_two"])) {
  300. return;
  301. }
  302. $needCalculate = self::checkNeedCalculate($model);
  303. if ($needCalculate) {
  304. $model->score = $allParams["score"];
  305. }
  306. $model->content = $allParams["content"] ?? "";
  307. $model->content_two = $allParams["content_two"] ?? "";
  308. $model->update_time = time();
  309. //开启事务
  310. Yii::$app->db->transaction(function () use ($model, $needCalculate) {
  311. if (!$model->save()) {
  312. throw new Exception($model->getErrorSummary(true)[0]);
  313. }
  314. //计算总分
  315. if ($needCalculate) {
  316. self::calculateTotalScore($model->accident_id);
  317. }
  318. });
  319. AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $allParams);
  320. }
  321. protected
  322. static function getScore($accidentId, $categoryList): array
  323. {
  324. $AccidentCasesScoreList = self::getList($accidentId, $categoryList);
  325. self::listAddDimension($accidentId, $AccidentCasesScoreList);
  326. $scoreList = [];
  327. $totalScore = [];
  328. foreach ($AccidentCasesScoreList as $AccidentCasesScore) {
  329. $scoreObj = self::getAccidentCasesScoreObj($AccidentCasesScore);
  330. if ($scoreObj->category === $categoryList[0]) {
  331. $scoreObj->category = 0;
  332. $totalScore = $scoreObj;
  333. } else {
  334. $scoreObj->category = 0;
  335. $scoreList[] = $scoreObj;
  336. }
  337. }
  338. return [$scoreList, $totalScore];
  339. }
  340. protected
  341. static function listAddDimension($accidentId, $data): void
  342. {
  343. $newAccidentCasesDimensionList = [];
  344. $AccidentCasesDimensionList = AccidentCasesDimensionService::getList($accidentId);
  345. foreach ($AccidentCasesDimensionList as $AccidentCasesDimension) {
  346. $newAccidentCasesDimensionList[$AccidentCasesDimension->dictionary_id][] = new AccidentCasesDimensionObj($AccidentCasesDimension);
  347. }
  348. foreach ($data as $value) {
  349. $value->dimension = $newAccidentCasesDimensionList[$value->dictionary_id] ?? [];
  350. }
  351. }
  352. /**
  353. * @param AccidentCasesScore $AccidentCasesScore
  354. * @return AccidentCasesScoreObj
  355. */
  356. protected
  357. static function getAccidentCasesScoreObj(AccidentCasesScore $AccidentCasesScore): AccidentCasesScoreObj
  358. {
  359. return new AccidentCasesScoreObj($AccidentCasesScore);
  360. }
  361. /**
  362. *
  363. * @param $accidentId
  364. * @param array $categoryList
  365. * @return AccidentCasesScore[]
  366. */
  367. public
  368. static function getList($accidentId, array $categoryList = []): array
  369. {
  370. /** @var AccidentCasesScore[] $AccidentCasesScoreList */
  371. $query = self::getQuery()->andWhere(["accident_id" => $accidentId]);
  372. if ($categoryList != []) {
  373. $query->andWhere(["category" => $categoryList]);
  374. }
  375. $AccidentCasesScoreList = $query->select("id,accident_id,dictionary_id,category,content,content_two,score")
  376. ->All();
  377. // if (!$AccidentCasesScoreList) {
  378. // self::initScore($accidentId);
  379. // return self::getList($accidentId, $categoryList);
  380. // }
  381. return $AccidentCasesScoreList;
  382. }
  383. }