AccidentCasesController.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCases;
  5. use common\models\AccidentCasesScore;
  6. use common\services\AccidentCasesRelateToDictionaryService;
  7. use common\services\AccidentCasesDimensionService;
  8. use common\services\AccidentCasesScoreConfigService;
  9. use common\services\AccidentCasesScoreService;
  10. use common\services\AdminLogService;
  11. use common\services\CaseService;
  12. use common\services\EmergencyResponseDimensionService;
  13. use common\services\EmergencyResponseRelateToRulesPointsService;
  14. use common\services\EmergencyResponseService;
  15. use common\services\EventOverviewService;
  16. use common\services\EventSourceDataService;
  17. use common\services\MailService;
  18. use common\services\OperationalAdjustmentsDimensionService;
  19. use common\services\OperationalAdjustmentsRelateToRulesPointsService;
  20. use common\services\OperationalAdjustmentsService;
  21. use common\services\UserService;
  22. use common\util\DealWithPostData;
  23. use frontend\modules\api\components\BaseAdminController;
  24. use Throwable;
  25. use Yii;
  26. use yii\base\InvalidConfigException;
  27. use yii\db\Exception;
  28. use yii\web\Response;
  29. class AccidentCasesController extends BaseAdminController
  30. {
  31. /**
  32. * 案例列表
  33. * @param int $current
  34. * @param int $page
  35. * @return Response
  36. * @throws AjaxException
  37. */
  38. public function actionList(int $current = 1, int $page = 10)
  39. {
  40. $query = CaseService::getCaseQuery()
  41. ->limit($page)
  42. ->offset(($current - 1) * $page);
  43. //排序
  44. $this->initSequence($query);
  45. //主要筛选条件
  46. $query = $this->addConditionToQuery("title", $query, true);
  47. $query = $this->addConditionToQuery("status", $query);
  48. // * @property int $status 状态 0-待提交 1-已发布 2已提交 3已审核 4驳回
  49. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  50. //--基础是查看已审核和已发布的 ["status" => [1, 3]]
  51. //判断用户是否有创建的权限 有的话可以查看自己创建的 ["create_by" => $this->userInfo->username ]
  52. //判断用户是否有审核权限 有的话可以查看 没有指定审核人的["check_list"=>"[]"] 指定与自己相关的 从指定审核人表里面查找 ["check_list"=>]
  53. if ($userRoleAuth->isSuperAdmin) {
  54. $query = $query->andWhere(["status" => [0, 1, 2, 3, 4]]);
  55. } else if ($userRoleAuth->caseAdd && $userRoleAuth->caseCheck) {
  56. $query = $query->andWhere(["or", ["status" => [1, 3]], ["create_by" => $this->userInfo->username], ["status" => [2], "check_list" => "[]"], ["and", ["status" => [2]], ["like", "check_list", $this->userInfo->username]]]);
  57. } else if ($userRoleAuth->caseAdd) {
  58. $query = $query->andWhere(["or", ["status" => [1, 3]], ["create_by" => $this->userInfo->username]]);
  59. } else if ($userRoleAuth->caseCheck) {
  60. // $idList = $userRoleAuth->caseCheck;
  61. // $query = $query->andWhere(["or", ["status" => [1, 3]], ["check_list" => "[]"], ["id" => $idList]]);
  62. $query = $query->andWhere(["or", ["status" => [1, 3]], ["status" => [2], "check_list" => "[]"], ["and", ["status" => [2]], ["like", "check_list", $this->userInfo->username]]]);
  63. } else {
  64. $query = $query->andWhere(["status" => [1, 3]]);
  65. }
  66. //组合数据
  67. $dataInfo = CaseService::dealWithList($query->all());
  68. //待审核案例置顶
  69. if ($current == 1) {
  70. $data['total'] = $query->count();
  71. $newData = [];
  72. $top = CaseService::dealWithList($query->andWhere(["status" => 2])->all());
  73. if ($top == []) {
  74. $newData = $dataInfo;
  75. } else {
  76. $topIds = [];
  77. foreach ($top as $value) {
  78. $topIds[] = $value->id;
  79. $newData[] = $value;
  80. }
  81. foreach ($dataInfo as $value) {
  82. if (!in_array($value->id, $topIds)) {
  83. $newData[] = $value;
  84. }
  85. }
  86. }
  87. $data['data'] = $newData;
  88. } else {
  89. $data['data'] = $dataInfo;
  90. $data['total'] = $query->count();
  91. }
  92. return $this->asJson($data);
  93. }
  94. /**
  95. * 案例添加
  96. * @return Response
  97. * @throws InvalidConfigException
  98. * @throws Throwable
  99. */
  100. public function actionAdd(): Response
  101. {
  102. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  103. if (!$userRoleAuth->caseAdd) {
  104. /**
  105. * in_array("/accident-cases/add", $rules)
  106. * && in_array("/accident-cases/commit", $rules)
  107. * && in_array("/accident-cases/update", $rules)
  108. * && in_array("/accident-cases/delete", $rules)
  109. */
  110. return $this->returnError("权限不足:" . '/accident-cases/add' . "/accident-cases/commit" . "/accident-cases/cancel" . "/accident-cases/update" . "/accident-cases/delete");
  111. }
  112. $allParam = $this->getAllParams();
  113. unset($allParam['adminLogModel']);
  114. unset($allParam['adminLogId']);
  115. $post = DealWithPostData::changePostDataArrayToJson($allParam);
  116. $model = new AccidentCases($post);
  117. $origin = AdminLogService::getOrigin($model, $this->getAllParams(), true);
  118. $model->update_time = date("Y-m-d H:i:s");
  119. $model->status = 0;
  120. //开启事务
  121. Yii::$app->db->transaction(function () use ($model) {
  122. //添加派生关系
  123. CaseService::deriveByAccidentCasesModel($model);
  124. $model->create_by = $this->userInfo->username;
  125. $model->create_name = $this->userInfo->name;
  126. if (!$model->save()) {
  127. throw new Exception($model->getErrorSummary(true)[0]);
  128. }
  129. //将案例与字典表的关系保存
  130. AccidentCasesRelateToDictionaryService::add($model);
  131. //生成评分数据
  132. AccidentCasesScoreService::initScore($model->id);
  133. });
  134. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  135. //返回数据
  136. return $this->asJson();
  137. }
  138. /**
  139. * 上架案例
  140. * @param $id
  141. * @return Response
  142. * @throws AjaxException
  143. * @throws InvalidConfigException
  144. */
  145. public function actionPublish($id)
  146. {
  147. $model = CaseService::getCaseById($id);
  148. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  149. if ($userRoleAuth->caseCheck) {
  150. if ($model->check_list == "[]" || str_contains($model->check_list, $this->userInfo->username)) {
  151. if ($model->status != 3) {
  152. return $this->returnError("只能上架已审核的案例");
  153. }
  154. } else {
  155. if (!$userRoleAuth->isSuperAdmin) {
  156. return $this->returnError("只能由" . $model->check_name_list . "上架");
  157. }
  158. }
  159. }
  160. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  161. $model->status = 1;
  162. $model->check_remark = $this->getParams("check_remark") ?? "";
  163. $model->save();
  164. //返回数据
  165. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  166. return $this->asJson();
  167. }
  168. /**
  169. * 下架案例
  170. * @param $id
  171. * @return Response
  172. * @throws AjaxException
  173. * @throws InvalidConfigException
  174. */
  175. public function actionRevoke($id)
  176. {
  177. $model = CaseService::getCaseById($id);
  178. if ($model->status != 1) {
  179. return $this->returnError("只能下架已上架的案例");
  180. }
  181. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  182. $model->status = 3;
  183. $model->check_remark = $this->getParams("check_remark") ?? "";
  184. $model->save();
  185. //返回数据
  186. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  187. return $this->asJson();
  188. }
  189. /**
  190. * 拒绝案例
  191. * @param $id
  192. * @return Response
  193. * @throws AjaxException
  194. * @throws InvalidConfigException
  195. */
  196. public function actionRefuse($id)
  197. {
  198. $model = CaseService::getCaseById($id);
  199. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  200. if ($userRoleAuth->caseCheck) {
  201. if ($model->check_list == "[]" || str_contains($model->check_list, $this->userInfo->username)) {
  202. if ($model->status != 2 && $model->status != 3) {
  203. return $this->returnError("只能退回已提交和已审核的案例");
  204. }
  205. } else {
  206. if (!$userRoleAuth->isSuperAdmin) {
  207. return $this->returnError("只能由" . $model->check_name_list . "退回");
  208. }
  209. }
  210. }
  211. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  212. $model->status = 4;
  213. $model->check_remark = $this->getParams("check_remark") ?? "";
  214. $model->check_by = $this->userInfo->username;
  215. $model->check_name = $this->userInfo->name ?? "系统管理员";
  216. $model->save();
  217. //发送信息
  218. MailService::sendRefuseMail($model, $model->check_remark);
  219. //历史
  220. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  221. return $this->asJson();
  222. }
  223. /**
  224. * 提交案例
  225. * @param $id
  226. * @return Response
  227. * @throws AjaxException
  228. * @throws InvalidConfigException
  229. */
  230. public function actionCommit($id): Response
  231. {
  232. $model = CaseService::getCaseById($id);
  233. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  234. if (!$userRoleAuth->isSuperAdmin) {
  235. if ($model->create_by != $this->userInfo->username) {
  236. return $this->returnError("只能提交自己创建的案例");
  237. }
  238. }
  239. if ($model->status != 0 && $model->status != 4) {
  240. return $this->returnError("只能提交待审核或已退回的案例");
  241. }
  242. $params = $this->getAllParams();
  243. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  244. $model->status = 2;
  245. $model->check_list = json_encode($params["check_people"]);
  246. CaseService::setCheckNameList($model, $params["check_people"]);
  247. $model->create_remark = $params["check_remark"];
  248. $model->save();
  249. //发送信息
  250. MailService::sendCommitMail($model, $model->create_remark);
  251. //返回数据
  252. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  253. return $this->asJson();
  254. }
  255. /**
  256. * 取消提交案例
  257. * @param $id
  258. * @return Response
  259. * @throws AjaxException
  260. * @throws InvalidConfigException
  261. */
  262. public function actionCancel($id)
  263. {
  264. $model = CaseService::getCaseById($id);
  265. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  266. if (!$userRoleAuth->isSuperAdmin) {
  267. if ($model->create_by != $this->userInfo->username) {
  268. return $this->returnError("只能取消提交自己创建的案例");
  269. }
  270. }
  271. if ($model->status != 2) {
  272. return $this->returnError("只能取消审核中的案例");
  273. }
  274. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  275. $model->status = 0;
  276. $model->create_remark = $this->getParams("check_remark") ?? "";
  277. $model->save();
  278. //返回数据
  279. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  280. return $this->asJson();
  281. }
  282. /**
  283. * 通过案例
  284. * @param $id
  285. * @return Response
  286. * @throws AjaxException
  287. * @throws InvalidConfigException
  288. */
  289. public function actionPass($id)
  290. {
  291. //通过以后自动上架
  292. $model = CaseService::getCaseById($id);
  293. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  294. if ($userRoleAuth->caseCheck) {
  295. if ($model->check_list == "[]" || str_contains($model->check_list, $this->userInfo->username)) {
  296. if ($model->status != 2) {
  297. return $this->returnError("只能通过审核中的案例");
  298. }
  299. } else {
  300. if (!$userRoleAuth->isSuperAdmin) {
  301. return $this->returnError("只能由" . $model->check_name_list . "通过");
  302. }
  303. }
  304. }
  305. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  306. $model->check_remark = $this->getParams("check_remark") ?? "";
  307. $model->status = 3;
  308. $model->check_by = $this->userInfo->username;
  309. $model->check_name = $this->userInfo->name ?? "系统管理员";
  310. $model->save();
  311. //返回数据
  312. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  313. return $this->asJson();
  314. }
  315. /**
  316. * 案例修改
  317. * @param $id
  318. * @return Response
  319. * @throws AjaxException
  320. * @throws InvalidConfigException
  321. * @throws Throwable
  322. */
  323. public function actionUpdate($id)
  324. {
  325. //status 0 2 3 4可以修改
  326. $model = CaseService::checkIsPublish($id, $this->userInfo);
  327. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  328. $totalScore = isset(Yii::$app->request->post()["totalScore"]) ? Yii::$app->request->post()["totalScore"] : null;
  329. //通过setBodyParams来修改Yii::$app->request->post()返回值
  330. Yii::$app->request->setBodyParams(DealWithPostData::changePostDataArrayToJson(Yii::$app->request->post()));
  331. $this->setAttributeFromGetAndPost($model);
  332. //开启事务
  333. Yii::$app->db->transaction(function () use ($model, $totalScore) {
  334. //添加派生关系
  335. CaseService::deriveByAccidentCasesModel($model);
  336. $model->update_time = date("Y-m-d H:i:s");
  337. $model->status = 0;
  338. if (!$model->save()) {
  339. throw new Exception($model->getErrorSummary(true)[0]);
  340. }
  341. //将案例与字典表的关系更新
  342. AccidentCasesRelateToDictionaryService::update($model);
  343. //修改评价
  344. // AccidentCasesScoreService::updateByTotalScore($model->id, $totalScore);
  345. });
  346. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  347. //返回数据
  348. return $this->asJson();
  349. }
  350. /**
  351. * 评价总评价评语 评价结果
  352. * @param $id
  353. * @return Response
  354. * @throws AjaxException
  355. * @throws InvalidConfigException
  356. */
  357. public function actionUpdateTotalScore($id)
  358. {
  359. if (AccidentCasesScoreConfigService::getAccidentCasesScoreConfigUpdate()) {
  360. throw new AjaxException("打分配置正在更新中,请稍后打分!");
  361. }
  362. $case = $accidentModel = CaseService::checkIsPublish($id, $this->userInfo);
  363. $totalScore = isset(Yii::$app->request->post()["totalScore"]) ? Yii::$app->request->post()["totalScore"] : null;
  364. //修改评价
  365. $model = AccidentCasesScore::findOne(["accident_id" => $accidentModel->id, "category" => 1]);
  366. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  367. if ($totalScore) {
  368. if (isset($totalScore["content"])) {
  369. $model->content = $totalScore["content"];
  370. }
  371. if (isset($totalScore["content_two"])) {
  372. $model->content_two = $totalScore["content_two"];
  373. }
  374. }
  375. $model->save();
  376. AdminLogService::saveLogWithUpdateHistory($origin, $model, $case->id, $case->title, $this->getAllParams());
  377. return $this->asJson();
  378. }
  379. /**
  380. * 案例删除
  381. * @param $id
  382. * @return Response
  383. * @throws AjaxException
  384. * @throws Throwable
  385. */
  386. public function actionDelete($id)
  387. {
  388. $model = CaseService::checkIsPublish($id, $this->userInfo);
  389. $userRoleAuth = UserService::getUserRoleAuth($this->userInfo);
  390. if (!$userRoleAuth->isSuperAdmin) {
  391. if ($model->create_by != $this->userInfo->username) {
  392. return $this->returnError("只能删除自己创建的案例");
  393. }
  394. }
  395. if ($model->status != 0 && $model->status != 4) {
  396. return $this->returnError("只能删除待审核或已退回的案例");
  397. }
  398. $model->delete_time = time();
  399. //开启事务
  400. Yii::$app->db->transaction(function () use ($model, $id) {
  401. if (!$model->save()) {
  402. throw new Exception($model->getErrorSummary(true)[0]);
  403. }
  404. //将案例与字典表的关系删除
  405. AccidentCasesRelateToDictionaryService::delete($model);
  406. //删除原始资料和缓存数据
  407. EventSourceDataService::deleteByAccidentCasesId($id);
  408. //删除评价打分
  409. AccidentCasesScoreService::deleteByAccidentCasesId($id);
  410. //删除总评价维度
  411. AccidentCasesDimensionService::deleteByAccidentCasesId($id);
  412. //将与案例相关的 事件概览、应急处置和运营调整 删除
  413. //删除应急处置 删除应急处置与规章表的关系 删除应急处置评价维度
  414. $EmergencyResponseIdList = array_column(EmergencyResponseService::getEmergencyResponseQuery()->andWhere(["accident_id" => $id])->select("accident_id")->all(), "accident_id");
  415. EmergencyResponseService::deleteByAccidentCasesId($id);
  416. EmergencyResponseDimensionService::deleteByAccidentId($id);
  417. EmergencyResponseRelateToRulesPointsService::deleteByEmergencyResponseId($EmergencyResponseIdList);
  418. //删除事件概览
  419. EventOverviewService::deleteByAccidentCasesId($id);
  420. //删除运营调整 删除运营调整与规章表的关系 删除评价维度
  421. $OperationalAdjustmentsIdList = array_column(OperationalAdjustmentsService::getQuery()->andWhere(["accident_id" => $id])->select("accident_id")->all(), "accident_id");
  422. OperationalAdjustmentsService::deleteByAccidentCasesId($id);
  423. OperationalAdjustmentsDimensionService::deleteByAccidentId($id);
  424. OperationalAdjustmentsRelateToRulesPointsService::deleteByOperationalAdjustmentsId($OperationalAdjustmentsIdList);
  425. });
  426. //返回数据
  427. $origin = AdminLogService::getOrigin($model, $this->getAllParams());
  428. AdminLogService::saveLogWithUpdateHistory($origin, $model, $model->id, $model->title, $this->getAllParams());
  429. return $this->asJson();
  430. }
  431. /**
  432. * 案例学习
  433. * @return Response
  434. * @throws InvalidConfigException
  435. */
  436. public function actionStudy($id)
  437. {
  438. $this->getParams("id");
  439. CaseService::study($id,$this->userInfo);
  440. return $this->asJson();
  441. }
  442. }