AccidentCasesRelateToDictionaryService.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace common\services;
  3. use common\components\AjaxException;
  4. use common\models\AccidentCases;
  5. use common\models\AccidentCasesRelateToDictionary;
  6. use Yii;
  7. use yii\db\Exception;
  8. class AccidentCasesRelateToDictionaryService
  9. {
  10. public static function add(AccidentCases $AccidentCases): void
  11. {
  12. $param = [
  13. "dictionary_id", "accident_cases_id",
  14. ];
  15. $data = [];
  16. //事件性质(等级)
  17. $data[] = ["dictionary_id" => $AccidentCases->level];
  18. //事件类型—基本选项 数组
  19. $typeBaseArray = json_decode($AccidentCases->type_base);
  20. foreach ($typeBaseArray as $value) {
  21. $data[] = ["dictionary_id" => $value];
  22. }
  23. //事件类型-一级字段 数组
  24. $typeFirstArray = json_decode($AccidentCases->type_first);
  25. foreach ($typeFirstArray as $value) {
  26. $data[] = ["dictionary_id" => $value];
  27. }
  28. //事件类型-二级字段 数组
  29. $typeSecondArray = json_decode($AccidentCases->type_second);
  30. foreach ($typeSecondArray as $value) {
  31. $data[] = ["dictionary_id" => $value];
  32. }
  33. //事件类型-三级字段 数组
  34. $typeThirdArray = json_decode($AccidentCases->type_third);
  35. foreach ($typeThirdArray as $value) {
  36. $data[] = ["dictionary_id" => $value];
  37. }
  38. //事件类型—复合选项 数组
  39. $typeExtraArray = json_decode($AccidentCases->type_extra);
  40. foreach ($typeExtraArray as $value) {
  41. $data[] = ["dictionary_id" => $value];
  42. }
  43. //责任专业—事件归因
  44. $dutyCategoryArray = json_decode($AccidentCases->duty_category);
  45. foreach ($dutyCategoryArray as $value) {
  46. $data[] = ["dictionary_id" => $value];
  47. }
  48. //责任专业—原因
  49. $dutyReasonArray = json_decode($AccidentCases->duty_reason);
  50. foreach ($dutyReasonArray as $value) {
  51. $data[] = ["dictionary_id" => $value];
  52. }
  53. //日期特征
  54. $data[] = ["dictionary_id" => $AccidentCases->day_type];
  55. //时间特征
  56. $data[] = ["dictionary_id" => $AccidentCases->time_type];
  57. //所属线路
  58. $data[] = ["dictionary_id" => $AccidentCases->line];
  59. //自动化程度
  60. $data[] = ["dictionary_id" => $AccidentCases->automation_level];
  61. //信号供应商
  62. $data[] = ["dictionary_id" => $AccidentCases->signal_supplier];
  63. //车号
  64. $data[] = ["dictionary_id" => $AccidentCases->train_number];
  65. //列车编组
  66. $data[] = ["dictionary_id" => $AccidentCases->train_group];
  67. //列车型号
  68. $data[] = ["dictionary_id" => $AccidentCases->train_model];
  69. //发生位置-基本分类-二级字段 数组
  70. $positionBaseSecondArray = json_decode($AccidentCases->position_base_second);
  71. foreach ($positionBaseSecondArray as $value) {
  72. $data[] = ["dictionary_id" => $value];
  73. }
  74. //发生位置-派生-二级字段 数组
  75. $positionExtraSecondArray = json_decode($AccidentCases->position_extra_second);
  76. foreach ($positionExtraSecondArray as $value) {
  77. $data[] = ["dictionary_id" => $value];
  78. }
  79. //变电站类型
  80. $substation_typeArray = json_decode($AccidentCases->substation_type);
  81. foreach ($substation_typeArray as $value) {
  82. $data[] = ["dictionary_id" => $value];
  83. }
  84. //影响范围
  85. $data[] = ["dictionary_id" => $AccidentCases->incidence];
  86. //重组,添加上accident_cases_id
  87. foreach ($data as $key => $value) {
  88. if ($value["dictionary_id"] == 0 || $value["dictionary_id"] == "" || $value["dictionary_id"] == "[]" || $value["dictionary_id"] == [] || $value["dictionary_id"] == null) {
  89. unset($data[$key]);
  90. } else {
  91. $data[$key]["accident_cases_id"] = $AccidentCases->id;
  92. }
  93. }
  94. //需要改造成batchInsert来节约资源
  95. Yii::$app->db->createCommand()->batchInsert(AccidentCasesRelateToDictionary::tableName(), $param, $data)->execute();
  96. }
  97. /**
  98. * @param AccidentCases $AccidentCases
  99. * @return void
  100. * @throws Exception
  101. */
  102. public static function update(AccidentCases $AccidentCases): void
  103. {
  104. $relationData = [];
  105. $needUpdateZero = [];
  106. $needUpdate = [];
  107. $needAdd = [];
  108. $zeroList = [];
  109. $AccidentCasesRelationshipList = AccidentCasesRelateToDictionary::find()
  110. ->where(["accident_cases_id" => $AccidentCases->id])
  111. ->asArray()->all();
  112. foreach ($AccidentCasesRelationshipList as $AccidentCasesRelationship) {
  113. if ($AccidentCasesRelationship["dictionary_id"] == 0) {
  114. $zeroList[] = $AccidentCasesRelationship["id"];
  115. } else {
  116. $relationData[$AccidentCasesRelationship["dictionary_id"]] = [$AccidentCasesRelationship["id"], $AccidentCasesRelationship["dictionary_id"]];
  117. }
  118. }
  119. //事件性质(等级)
  120. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "level");
  121. //事件类型—基本选项 数组
  122. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "type_base");
  123. //事件类型-一级字段 数组
  124. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "type_first");
  125. //事件类型-二级字段 数组
  126. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "type_second");
  127. //事件类型-三级字段 数组
  128. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "type_third");
  129. //事件类型—复合选项 数组
  130. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "type_extra");
  131. //责任专业—事件归因
  132. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "duty_category");
  133. //责任专业—原因
  134. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "duty_reason");
  135. //日期特征
  136. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "day_type");
  137. //时间特征
  138. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "time_type");
  139. //所属线路
  140. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "line");
  141. //自动化程度
  142. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "automation_level");
  143. //信号供应商
  144. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "signal_supplier");
  145. //车号
  146. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "train_number");
  147. //列车编组
  148. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "train_group");
  149. //列车型号
  150. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "train_model");
  151. //发生位置-基本分类-二级字段 数组
  152. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "position_base_second");
  153. //发生位置-派生-二级字段 数组
  154. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "position_extra_second");
  155. //变电站类型
  156. self::dealWithNeedArrayData($needAdd, $relationData, $AccidentCases, "substation_type");
  157. //影响范围
  158. self::dealWithNeedUpdateSingleData($needAdd, $relationData, $AccidentCases, "incidence");
  159. //根据needAdd的数量将needUpdateZero和zeroList变为needUpdate
  160. foreach ($needAdd as $key => $value) {
  161. if ($relationData != []) {
  162. unset($needAdd[$key]);
  163. $AccidentCasesRelationshipData = array_shift($relationData);
  164. $needUpdate[] = [$AccidentCasesRelationshipData[0], $value["dictionary_id"]];
  165. } elseif ($zeroList != []) {
  166. unset($needAdd[$key]);
  167. $needUpdate[] = [array_shift($zeroList), $value["dictionary_id"]];
  168. } else {
  169. break;
  170. }
  171. }
  172. //多余的数据$relationData转成needUpdateZero
  173. foreach ($relationData as $value) {
  174. $needUpdateZero[] = $value[0];
  175. }
  176. //都更新完了还不够用,那就存储$needAdd
  177. if ($needAdd != []) {
  178. $param = [
  179. "accident_cases_id", "dictionary_id"
  180. ];
  181. Yii::$app->db->createCommand()->batchInsert(AccidentCasesRelateToDictionary::tableName(), $param, $needAdd)->execute();
  182. }
  183. //执行needUpdateZero
  184. if ($needUpdateZero != []) {
  185. AccidentCasesRelateToDictionary::updateAll(["dictionary_id" => 0], ["id" => $needUpdateZero]);
  186. }
  187. //执行needUpdate
  188. foreach ($needUpdate as $value) {
  189. Yii::$app->db->createCommand()->update(AccidentCasesRelateToDictionary::tableName(), ['dictionary_id' => $value[1]], ["id" => $value[0]])->execute();
  190. }
  191. }
  192. public static function delete(AccidentCases $AccidentCases): void
  193. {
  194. //关系真删除
  195. AccidentCasesRelateToDictionary::deleteAll(["accident_cases_id" => $AccidentCases->id]);
  196. }
  197. protected static function dealWithNeedUpdateSingleData(&$needAdd, &$relationData, AccidentCases $AccidentCases, $param_name): void
  198. {
  199. if ($AccidentCases->$param_name != "" && $AccidentCases->$param_name != null) {
  200. if (isset($relationData[$AccidentCases->$param_name])) {
  201. //老数据存在的,就不做处理
  202. unset($relationData[$AccidentCases->$param_name]);
  203. } else {
  204. //老数据不存在,塞到添加列表里面
  205. $needAdd[] = ["accident_cases_id" => $AccidentCases->id, "dictionary_id" => $AccidentCases->$param_name];
  206. }
  207. }
  208. }
  209. /**
  210. * @param $needAdd
  211. * @param $relationData
  212. * @param AccidentCases $AccidentCases
  213. * @param $param_name
  214. * @return void
  215. * @throws AjaxException
  216. */
  217. protected static function dealWithNeedArrayData(&$needAdd, $relationData, AccidentCases $AccidentCases, $param_name): void
  218. {
  219. $array = json_decode($AccidentCases->$param_name);
  220. if (!is_array($array)) {
  221. throw new AjaxException("{$param_name}数据传入错误,不是数组!");
  222. }
  223. foreach ($array as $value) {
  224. if ($value !== null && $value !== "") {
  225. if (isset($relationData[$AccidentCases->$param_name])) {
  226. //老数据存在的,就不做处理
  227. unset($relationData[$AccidentCases->$param_name]);
  228. } else {
  229. //老数据不存在,塞到添加列表里面
  230. $needAdd[] = ["accident_cases_id" => $AccidentCases->id, "dictionary_id" => $value];
  231. }
  232. }
  233. }
  234. }
  235. /**
  236. * @throws AjaxException
  237. */
  238. protected static function dealWithNeedArrayDataOld(&$needUpdateZero, &$needAdd, $relationData, AccidentCases $AccidentCases, $param_name): void
  239. {
  240. $array = json_decode($AccidentCases->$param_name);
  241. if (!is_array($array)) {
  242. throw new AjaxException("{$param_name}不是数组!");
  243. }
  244. if (isset($relationData[$param_name])) {
  245. foreach ($relationData[$param_name] as $key => $value) {
  246. $arrayKey = array_search($relationData[$param_name][$key][1], $array);
  247. if ($arrayKey !== false) {
  248. unset($array[$arrayKey]);
  249. } else {
  250. $needUpdateZero[] = $value[0];
  251. }
  252. }
  253. }
  254. foreach ($array as $value) {
  255. if ($value !== null && $value !== "") {
  256. $needAdd[] = ["accident_cases_id" => $AccidentCases->id, "dictionary_id" => $value];
  257. }
  258. }
  259. }
  260. protected static function dealWithNeedDeleteData(&$needUpdateZero, $relationData, $param_name): void
  261. {
  262. if (isset($relationData[$param_name])) {
  263. foreach ($relationData[$param_name] as $key => $value) {
  264. $needUpdateZero[] = $value[0];
  265. }
  266. }
  267. }
  268. protected static function dealWithNeedUpdateSingleDataOld(&$needUpdate, &$needUpdateZero, &$needAdd, $relationData, AccidentCases $AccidentCases, $param_name): void
  269. {
  270. if (isset($relationData[$param_name])) {
  271. foreach ($relationData[$param_name] as $key => $value) {
  272. if ($key < 1) {
  273. if ($relationData[$param_name][$key][1] != $AccidentCases->$param_name) {
  274. if ($AccidentCases->$param_name !== null && $AccidentCases->$param_name !== "") {
  275. $needUpdate[] = [$value[0], $AccidentCases->$param_name];
  276. } else {
  277. $needUpdateZero[] = $value[0];
  278. }
  279. }
  280. } else {
  281. $needUpdateZero[] = $value[0];
  282. }
  283. }
  284. } else {
  285. if ($AccidentCases->$param_name !== null && $AccidentCases->$param_name !== "") {
  286. $needAdd[] = ["accident_cases_id" => $AccidentCases->id, "dictionary_id" => $AccidentCases->$param_name];
  287. }
  288. }
  289. }
  290. }