BasicController.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. <?php
  2. namespace frontend\modules\api\controllers;
  3. use common\components\AjaxException;
  4. use common\models\Rules;
  5. use common\models\RulesPoints;
  6. use common\services\EmergencyResponseRelateToRulesPointsService;
  7. use common\services\EmergencyResponseService;
  8. use common\services\OperationalAdjustmentsRelateToRulesPointsService;
  9. use common\services\OperationalAdjustmentsService;
  10. use common\services\RulesPointsService;
  11. use common\services\RulesService;
  12. use common\util\RulesPointObj;
  13. use frontend\modules\api\components\BaseAdminController;
  14. use Throwable;
  15. use Yii;
  16. use yii\base\InvalidConfigException;
  17. use yii\db\Exception;
  18. use yii\web\Response;
  19. class BasicController extends BaseAdminController
  20. {
  21. /**
  22. * 规则需要展示的列表
  23. * @return Response
  24. */
  25. public function actionRulesShowList($rule_name, $start_time, $end_time)
  26. {
  27. $rulesPointsList = RulesPointsService::getRulesPointsQuery()->where(["or", ["like", "name", $rule_name], ["like", "content", $rule_name]])->asArray()->all();
  28. $ids = array_column($rulesPointsList, "rule_id");
  29. $query = RulesService::getQuery()
  30. ->andWhere(["rule_status" => 1])
  31. ->andWhere([
  32. "or",
  33. ["like", "rule_name", $rule_name],
  34. ["id" => $ids],
  35. ]);
  36. if ($end_time == "") {
  37. $query->andWhere([
  38. "or",
  39. ["and", [">=", "end_time", $start_time], ["<=", "create_time", $start_time]],
  40. ["and", ["end_time" => null], ["<=", "create_time", $start_time]]
  41. ]);
  42. } else {
  43. $query->andWhere([
  44. "or",
  45. ["<=", "create_time", $end_time],
  46. [">=", "end_time", $start_time],
  47. ]);
  48. }
  49. $query->orderBy("create_time desc,end_time desc,id desc");
  50. //组合数据
  51. $ruleList = RulesService::getAllList($query);
  52. $data['data'] = $this->getRealRuleList($ruleList);
  53. return $this->asJson($data);
  54. }
  55. protected function getRealRuleList($ruleList)
  56. {
  57. $ruleIdList = [];
  58. foreach ($ruleList as $rule) {
  59. $ruleIdList[] = $rule->getId();
  60. }
  61. $rulesPointsIdList = array_column(RulesPointsService::getRulesPointsQuery()->where(["rule_id" => $ruleIdList])->select("distinct(rule_id)")->asArray()->all(), "rule_id");
  62. $realRuleList = [];
  63. foreach ($ruleList as $rule) {
  64. if (in_array($rule->getId(), $rulesPointsIdList)) {
  65. $realRuleList[] = $rule;
  66. }
  67. }
  68. return $realRuleList;
  69. }
  70. /**
  71. * 废止规则需要展示的列表 新增协议
  72. * @return Response
  73. */
  74. public function actionRulesShowListWhenRulesAdd($rule_id, $rule_name, $start_time)
  75. {
  76. $rulesPointsList = RulesPointsService::getRulesPointsQuery()->where(["or", ["like", "name", $rule_name], ["like", "content", $rule_name]])->asArray()->all();
  77. $ids = array_column($rulesPointsList, "rule_id");
  78. $query = RulesService::getQuery()
  79. ->andWhere(["end_time" => null])
  80. ->andWhere(["<>", "id", $rule_id])
  81. ->andWhere(["<", "create_time", $start_time])
  82. ->andWhere([
  83. "or",
  84. ["like", "rule_name", $rule_name],
  85. ["id" => $ids],
  86. ]);
  87. $query->orderBy("create_time desc,end_time desc,id desc");
  88. //组合数据
  89. $ruleList = RulesService::getAllList($query);
  90. $data['data'] = $this->getRealRuleList($ruleList);
  91. return $this->asJson($data);
  92. }
  93. /**
  94. * 规则需要展示的列表
  95. * @param $id
  96. * @return Response
  97. * @throws AjaxException
  98. */
  99. public function actionRulesGetById($id)
  100. {
  101. $Rules = RulesService::getRulesById($id);
  102. //获取知识点
  103. $query = RulesPointsService::getRulesPointsQuery()->andWhere(["rule_id" => $id]);
  104. $point_content = RulesPointsService::getAllList($query);
  105. //规章添加知识点
  106. $rulesData = RulesService::getRuleWithContent($Rules, $point_content);
  107. //组合数据
  108. $data['data'] = $rulesData;
  109. return $this->asJson($data);
  110. }
  111. /**
  112. * 根据案例ID展示规章
  113. * @return Response
  114. */
  115. public function actionRulesGetByCaseId($id)
  116. {
  117. $EmergencyResponseId = array_column(EmergencyResponseService::getEmergencyResponseQuery()->andWhere(["accident_id" => $id])->all(), "id");
  118. $OperationalAdjustmentsId = array_column(OperationalAdjustmentsService::getQuery()->andWhere(["accident_id" => $id])->all(), "id");
  119. $pointIds = [];
  120. $pointIds = array_merge($pointIds, array_column(EmergencyResponseRelateToRulesPointsService::getListByEmergencyResponseId($EmergencyResponseId), "rules_point_id"));
  121. $pointIds = array_merge($pointIds, array_column(OperationalAdjustmentsRelateToRulesPointsService::getListByOperationalAdjustmentsId($OperationalAdjustmentsId), "rules_point_id"));
  122. $pointIds = array_values(array_unique($pointIds));
  123. $ids = array_column(RulesPointsService::getRulesPointsQuery()->where(["id" => $pointIds])->select("distinct(rule_id)")->all(), "rule_id");
  124. $query = RulesService::getQuery();
  125. $query->where(["id" => $ids]);
  126. //组合数据
  127. $data['data'] = RulesService::getAllList($query);
  128. return $this->asJson($data);
  129. }
  130. /**
  131. * 通过应急处置ID或者运营调整ID查找规章
  132. * @param $id
  133. * @param $type
  134. * @return Response
  135. */
  136. public function actionRulesAndPointsByEventId($id, $type)
  137. {
  138. if ($type == 1) {
  139. $pointIds = array_column(EmergencyResponseRelateToRulesPointsService::getListByEmergencyResponseId($id), "rules_point_id");
  140. } else {
  141. $pointIds = array_column(OperationalAdjustmentsRelateToRulesPointsService::getListByOperationalAdjustmentsId($id), "rules_point_id");
  142. }
  143. /** @var RulesPoints[] $pointList */
  144. $pointList = RulesPointsService::getRulesPointsQuery()->where(["id" => $pointIds])->all();
  145. $newPointList = [];
  146. foreach ($pointList as $point) {
  147. $newPointList[$point->rule_id][] = new RulesPointObj($point);
  148. }
  149. $ruleId = array_column($pointList, "rule_id");
  150. /** @var Rules[] $ruleList */
  151. $ruleList = RulesService::getQuery()->where(["id" => $ruleId])->all();
  152. $newRuleList = [];
  153. foreach ($ruleList as $rule) {
  154. $newRuleList[] = RulesService::getRuleWithContent($rule, $newPointList[$rule->id]);
  155. }
  156. //组合数据
  157. $data['data'] = $newRuleList;
  158. return $this->asJson($data);
  159. }
  160. /**
  161. * 规则列表
  162. * @return Response
  163. */
  164. public function actionRulesList(int $current = 1, int $page = 10)
  165. {
  166. //重新整合数据
  167. $post = Yii::$app->request->post();
  168. $rule_content = $post["content"] ?? ""; //规章概要
  169. $point_content = $post["rule_content"] ?? "";//规章知识点
  170. $post["rule_content"] = $rule_content;
  171. Yii::$app->request->setBodyParams($post);
  172. $query = RulesService::getQuery()
  173. ->limit($page)
  174. ->offset(($current - 1) * $page);
  175. //排序
  176. $this->initSequence($query, "create_time desc,end_time desc,id desc");
  177. //主要筛选条件
  178. //规章知识点筛选条件
  179. $ruleIdList = [];
  180. if ($point_content !== "") {
  181. $ruleIdList = array_column(RulesPointsService::getRulesPointsQuery()->andWhere(["like", "content", $point_content])->select("distinct(rule_id)")->asArray()->all(), "rule_id");
  182. }
  183. if ($ruleIdList != []) {
  184. $query->andWhere(["id" => $ruleIdList]);
  185. }
  186. //规章筛选条件
  187. $this->addConditionToQuery("rule_name", $query, true);
  188. $this->addConditionToQuery("rule_content", $query, true);
  189. $this->addConditionToQuery("rule_category", $query);
  190. $this->addConditionToQuery("rule_status", $query);
  191. $this->addStartEndConditionToQuery("create_time", $query);
  192. $this->addStartEndConditionToQuery("end_time", $query);
  193. $this->addConditionToQuery("version", $query, true);
  194. //组合数据
  195. $data['data'] = RulesService::getAllList($query);
  196. $data['total'] = $query->count();
  197. return $this->asJson($data);
  198. }
  199. /**
  200. * 规章添加
  201. * @return Response
  202. * @throws Exception
  203. */
  204. public function actionRulesAdd()
  205. {
  206. $post = Yii::$app->request->post();
  207. if (!isset($post["rule_name"]) || strlen(trim($post["rule_name"])) == 0 || !isset($post["version"]) || strlen(trim($post["version"])) == 0) {
  208. return $this->asJson([], 1, "名称不可以为空!");
  209. }
  210. $rules = RulesService::getQuery()->andWhere(["rule_name" => $post["rule_name"], "version" => $post["version"]])->one();
  211. if ($rules) {
  212. return $this->asJson([], 1, "名称+版本号 {$post["rule_name"]}+{$post["version"]} 已存在!");
  213. }
  214. $model = new Rules($post);
  215. $model->update_time = date("Y-m-d H:i:s");
  216. if ($post['end_time'] && $post['end_time'] != "") {
  217. $model->end_time = $post['end_time'];
  218. }
  219. if (!$model->save()) {
  220. throw new Exception($model->getErrorSummary(true)[0]);
  221. }
  222. // $repealedIds = [];
  223. // if ($repealedIds != []) {
  224. // $endTime = date("Y-m-d H:i:s", strtotime($model->create_time) - 1);
  225. // Rules::updateAll(["end_time" => $endTime], ["id" => $repealedIds]);
  226. // }
  227. $data = [];
  228. return $this->asJson($data);
  229. }
  230. protected function getRuleNameVersionString($data)
  231. {
  232. return trim($data["title"]) . '+' . trim($data["version"]);
  233. }
  234. /**
  235. * 从excel批量导入规章
  236. * @return Response
  237. * @throws Exception
  238. */
  239. public function actionRulesAddBatch()
  240. {
  241. // 获取传值
  242. $post = Yii::$app->request->post();
  243. $json = $post;
  244. $time = time();
  245. $ruleNameVersionList = [];
  246. $ruleNameVersionIdList = [];
  247. $rulesParamData = [];
  248. $rulesParam = [
  249. 'rule_name', //规章名字
  250. 'version', //规章版本
  251. 'rule_content', //规章概要
  252. 'rule_status',
  253. 'create_time',
  254. 'end_time',
  255. 'update_time',
  256. 'rule_category',
  257. ];
  258. foreach ($json as $key => $value) {
  259. //统计好规章个数
  260. $ruleNameVersionString = $this->getRuleNameVersionString($value);
  261. if (!in_array($ruleNameVersionString, $ruleNameVersionList)) {
  262. $ruleNameVersionList[] = $ruleNameVersionString;
  263. $end_time = trim($value["end_time"]) === "" ? null : date("Y-m-d H:i:s", strtotime(trim($value["end_time"])));
  264. $create_time = date("Y-m-d H:i:s", strtotime(trim($value["create_time"])));
  265. $rulesParamData[] = [
  266. 'rule_name' => trim($value["title"]), //规章名字
  267. 'version' => trim($value["version"]), //规章版本
  268. 'rule_content' => trim($value["content"]), //规章概要
  269. 'rule_status' => 1, //规章版本
  270. 'create_time' => $create_time,//实施日期
  271. 'end_time' => $end_time, //废止日期
  272. 'update_time' => date("Y-m-d H:i:s", $time),
  273. 'rule_category' => "",
  274. ];
  275. }
  276. }
  277. /** @var Rules[] $rulesList */
  278. $rulesList = RulesService::getQuery()->andWhere(["CONCAT_WS('+',`rule_name`,`version`)" => $ruleNameVersionList])->select("`id`,CONCAT_WS('+',`rule_name`,`version`) AS `rule_name_version`")->all();
  279. foreach ($rulesList as $rule) {
  280. $exitsKey = array_search($rule->rule_name_version, $ruleNameVersionList);
  281. if ($exitsKey !== false) {
  282. //已经有的规章就不保存了
  283. unset($rulesParamData[$exitsKey]);
  284. }
  285. }
  286. //储存规章
  287. Yii::$app->db->createCommand()->batchInsert(Rules::tableName(), $rulesParam, $rulesParamData)->execute();
  288. //查询存入的规章信息,得到规章的ID
  289. /** @var Rules[] $rulesList */
  290. $rulesList = RulesService::getQuery()->andWhere(["CONCAT_WS('+',`rule_name`,`version`)" => $ruleNameVersionList])->select("`id`,CONCAT_WS('+',`rule_name`,`version`) AS `rule_name_version`")->all();
  291. foreach ($rulesList as $rule) {
  292. $ruleNameVersionIdList[$rule->rule_name_version] = $rule->id;
  293. }
  294. //上传的规章点列表
  295. $needUpdateName = [];
  296. $needUpdateContent = [];
  297. $needUpdateDic = [];
  298. $rulesPointParamData = [];
  299. $ruleIdPointList = [];
  300. foreach ($json as $key => $value) {
  301. $ruleNameVersionString = $this->getRuleNameVersionString($value);
  302. $rule_id = $ruleNameVersionIdList[$ruleNameVersionString];
  303. $point = trim($value["point"]);
  304. //插入
  305. $rulesPointParamData[] = [
  306. 'rule_id' => $rule_id,
  307. 'name' => isset($value["name"]) ? trim($value["name"]) : '',
  308. 'point' => $point,
  309. 'content' => isset($value["rule_content"]) ? trim($value["rule_content"]) : "",
  310. 'dic' => isset($value["dic"]) ? trim($value["dic"]) : '',
  311. ];
  312. $ruleIdPointList[] = $rule_id . "+" . $point;
  313. }
  314. //数据库规章对应的已有规章点列表
  315. /** @var RulesPoints[] $rulesPoints */
  316. $rulesPoints = RulesPointsService::getRulesPointsQuery()->andWhere(["CONCAT_WS('+',`rule_id`,`point`)" => $ruleIdPointList])->select("`id`,CONCAT_WS('+',`rule_id`,`point`) AS `rule_id_point`")->all();
  317. foreach ($rulesPoints as $key => $rulesPoint) {
  318. $exitsKey = array_search($rulesPoint->rule_id_point, $ruleIdPointList);
  319. if ($exitsKey !== false) {
  320. //已经有的规章点需要修改
  321. if ($rulesPoint->content !== $rulesPointParamData[$exitsKey]["content"]) {
  322. $needUpdateContent[] = [
  323. "id" => $rulesPoint->id,
  324. "content" => $rulesPointParamData[$exitsKey]["content"],
  325. ];
  326. }
  327. if ($rulesPoint->name !== $rulesPointParamData[$exitsKey]["name"]) {
  328. $needUpdateName[] = [
  329. "id" => $rulesPoint->id,
  330. "name" => $rulesPointParamData[$exitsKey]["name"],
  331. ];
  332. }
  333. if ($rulesPoint->dic !== $rulesPointParamData[$exitsKey]["dic"]) {
  334. $needUpdateDic[] = [
  335. "id" => $rulesPoint->id,
  336. "dic" => $rulesPointParamData[$exitsKey]["dic"],
  337. ];
  338. }
  339. unset($rulesPointParamData[$exitsKey]);
  340. }
  341. }
  342. $rulesPointParam = [
  343. 'rule_id',
  344. 'name',
  345. 'point',
  346. 'content',
  347. 'dic',
  348. ];
  349. //添加规章知识点
  350. Yii::$app->db->createCommand()->batchInsert(RulesPoints::tableName(), $rulesPointParam, $rulesPointParamData)->execute();
  351. //修改规章知识点
  352. if ($needUpdateContent != []) {
  353. $sql = $this->batchUpdate(RulesPoints::tableName(), "id", "content", $needUpdateContent, true);
  354. Yii::$app->db->createCommand($sql)->execute();
  355. }
  356. if ($needUpdateName != []) {
  357. $sql = $this->batchUpdate(RulesPoints::tableName(), "id", "name", $needUpdateName, true);
  358. Yii::$app->db->createCommand($sql)->execute();
  359. }
  360. if ($needUpdateDic != []) {
  361. $sql = $this->batchUpdate(RulesPoints::tableName(), "id", "dic", $needUpdateDic, true);
  362. Yii::$app->db->createCommand($sql)->execute();
  363. }
  364. return $this->asJson();
  365. }
  366. /**
  367. * 规章修改
  368. * @param $id
  369. * @return Response
  370. * @throws AjaxException
  371. * @throws InvalidConfigException
  372. */
  373. public function actionRulesUpdate($id)
  374. {
  375. //校验
  376. $model = RulesService::getRulesById($id);
  377. $this->setAttributeFromGetAndPost($model);
  378. //校验是否重名
  379. $otherInfo = RulesService::getQuery()
  380. ->andWhere(['<>', 'id', $id])
  381. ->andWhere(["rule_name" => $model->rule_name, "version" => $model->version])
  382. ->one();
  383. if ($otherInfo) {
  384. return $this->asJson([], 1, "名称+版本号 {$model->rule_name}+{$model->version} 已存在!"); //通知前端
  385. }
  386. if ($model->save()) {
  387. return $this->asJson();
  388. } else {
  389. return $this->returnError($model->getErrorSummary(true)[0]);
  390. }
  391. }
  392. /**
  393. * 规章删除
  394. * @param $id
  395. * @return Response
  396. * @throws Exception
  397. * @throws Throwable
  398. */
  399. public function actionRulesDelete($id)
  400. {
  401. //校验
  402. $model = RulesService::getRulesById($id);
  403. //开启事务
  404. Yii::$app->db->transaction(function () use ($model, $id) {
  405. $model->delete_time = time();
  406. if (!$model->save()) {
  407. throw new Exception($model->getErrorSummary(true)[0]);
  408. }
  409. //删除对应的知识点
  410. RulesPointsService::deleteByRulesId($id);
  411. });
  412. return $this->asJson();
  413. }
  414. /**
  415. * 规章列表
  416. * @param $category
  417. * @return Response
  418. */
  419. public function actionRulesListCategory($category)
  420. {
  421. $query = RulesService::getQuery()
  422. ->andWhere(['rule_category' => $category, 'rule_status' => 1])
  423. ->orderBy("id desc");
  424. $data['data'] = RulesService::getAllList($query);
  425. return $this->asJson($data);
  426. }
  427. /**
  428. * 规章知识点列表
  429. * @param null $rule_id
  430. * @return Response
  431. */
  432. public function actionRulesPointsListById($rule_id = null)
  433. {
  434. $query = RulesPointsService::getRulesPointsQuery()
  435. ->andWhere(['rule_id' => $rule_id])
  436. ->orderBy("point desc");
  437. $data['data'] = RulesPointsService::getAllList($query);
  438. return $this->asJson($data);
  439. }
  440. /**
  441. * 规章知识点添加
  442. * @return Response
  443. * @throws Exception
  444. */
  445. public function actionRulesPointsAdd()
  446. {
  447. $post = Yii::$app->request->post();
  448. if (!isset($post["rule_id"])) {
  449. return $this->asJson([], 1, "数据非法!");
  450. }
  451. $rules = RulesPointsService::getRulesPointsQuery()->andWhere(["rule_id" => $post["rule_id"], "point" => $post["point"]])->one();
  452. if ($rules) {
  453. return $this->asJson([], 1, "知识点编号 {$post["point"]} 已存在!");
  454. }
  455. $model = new RulesPoints($post);
  456. if (!$model->save()) {
  457. throw new Exception($model->getErrorSummary(true)[0]);
  458. }
  459. $data = [];
  460. return $this->asJson($data);
  461. }
  462. /**
  463. * 规章知识点更新
  464. * @param $id
  465. * @return Response
  466. * @throws InvalidConfigException
  467. * @throws AjaxException
  468. */
  469. public function actionRulesPointsUpdate($id)
  470. {
  471. $model = RulesPointsService::getRulesPointsById($id);
  472. $this->setAttributeFromGetAndPost($model);
  473. //判断某个编号是否重复
  474. $RulesPoints = RulesPointsService::getRulesPointsQuery()->andWhere(["and", ["rule_id" => $model->rule_id, "point" => $model->point], ["<>", "id", $id]])->one();
  475. if ($RulesPoints) {
  476. throw new AjaxException("同一个规章,编号不能重复!");
  477. }
  478. if ($model->save()) {
  479. return $this->asJson();
  480. } else {
  481. return $this->returnError($model->getErrorSummary(true)[0]);
  482. }
  483. }
  484. /**
  485. * 规章知识点删除
  486. * @param $id
  487. * @return Response
  488. * @throws Exception
  489. */
  490. public function actionRulesPointsDeleteById($id)
  491. {
  492. $model = RulesPointsService::getRulesPointsById($id);
  493. $model->delete_time = time();
  494. if (!$model->save()) {
  495. throw new Exception($model->getErrorSummary(true)[0]);
  496. }
  497. return $this->asJson();
  498. }
  499. }