RulesPointsRelationshipService.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. <?php
  2. namespace common\services;
  3. use common\models\EmergencyResponse;
  4. use common\models\EmergencyResponseRelateToRulesPoints;
  5. use common\models\OperationalAdjustments;
  6. use common\models\OperationalAdjustmentsRelateToRulesPoints;
  7. use Yii;
  8. class RulesPointsRelationshipService
  9. {
  10. public static function addEmergencyResponseRelationship(EmergencyResponse $model): void
  11. {
  12. self::add($model, "EmergencyResponse");
  13. }
  14. public static function addOperationalAdjustmentsRelationship(OperationalAdjustments $model): void
  15. {
  16. self::add($model, "OperationalAdjustments");
  17. }
  18. public static function updateEmergencyResponseRelationship(EmergencyResponse $model): void
  19. {
  20. self::update($model, "EmergencyResponse");
  21. }
  22. public static function updateOperationalAdjustmentsRelationship(OperationalAdjustments $model): void
  23. {
  24. self::update($model, "OperationalAdjustments");
  25. }
  26. protected static function add($model, $type): void
  27. {
  28. $data = [];
  29. //规章 数组
  30. $rulesPointIds = [];
  31. /**
  32. * [
  33. * {"rule_id":1,"point":["1"]},
  34. * {"rule_id":2,"point":["1","2"]}
  35. * ]
  36. */
  37. /** @var OperationalAdjustments $model */
  38. /** @var EmergencyResponse $model */
  39. $rulesArray = json_decode($model->rules, true);
  40. foreach ($rulesArray as $rules) {
  41. $ids = array_column(RulesPointsService::getRulesPointsQuery()->select("id")->andWhere(["rule_id" => $rules["rule_id"], "point" => $rules["point"]])->all(), "id");
  42. $rulesPointIds = array_merge($rulesPointIds, $ids);
  43. }
  44. foreach ($rulesPointIds as $rules_point_id) {
  45. if ($type === "OperationalAdjustments") {
  46. $data[] = [
  47. "operational_adjustments_id" => $model->id,
  48. "rules_point_id" => $rules_point_id,
  49. ];
  50. } else {
  51. $data[] = [
  52. "emergency_response_id" => $model->id,
  53. "rules_point_id" => $rules_point_id,
  54. ];
  55. }
  56. }
  57. if ($type === "OperationalAdjustments") {
  58. $param = [
  59. "operational_adjustments_id", "rules_point_id",
  60. ];
  61. $tableName = OperationalAdjustmentsRelateToRulesPoints::tableName();
  62. } else {
  63. $param = [
  64. "emergency_response_id", "rules_point_id",
  65. ];
  66. $tableName = EmergencyResponseRelateToRulesPoints::tableName();
  67. }
  68. //需要改造成batchInsert来节约资源
  69. Yii::$app->db->createCommand()->batchInsert($tableName, $param, $data)->execute();
  70. }
  71. protected static function update($model, $type): void
  72. {
  73. $relationData = [];
  74. $needUpdate = [];
  75. $needUpdateZero = [];
  76. $needAdd = [];
  77. $zeroList = [];
  78. //查询关系表
  79. /** @var OperationalAdjustments $model */
  80. /** @var EmergencyResponse $model */
  81. if ($type === "OperationalAdjustments") {
  82. $RelationshipList = OperationalAdjustmentsRelateToRulesPoints::find()
  83. ->where(["operational_adjustments_id" => $model->id])
  84. ->select("id,rules_point_id")->asArray()->all();
  85. $tableName = OperationalAdjustmentsRelateToRulesPoints::tableName();
  86. } else {
  87. $RelationshipList = EmergencyResponseRelateToRulesPoints::find()
  88. ->where(["emergency_response_id" => $model->id])
  89. ->select("id,rules_point_id")->asArray()->all();
  90. $tableName = EmergencyResponseRelateToRulesPoints::tableName();
  91. }
  92. foreach ($RelationshipList as $Relationship) {
  93. if ($Relationship["rules_point_id"] == 0) {
  94. $zeroList[] = $Relationship["id"];
  95. } else {
  96. $relationData[] = [$Relationship["id"], $Relationship["rules_point_id"]];
  97. }
  98. }
  99. //规章 数组
  100. self::dealWithNeedArrayData($needUpdateZero, $needAdd, $relationData, $model, $type);
  101. //处理数据
  102. //根据needAdd的数量将needUpdateZero和zeroList变为needUpdate
  103. foreach ($needAdd as $key => $value) {
  104. if ($needUpdateZero != []) {
  105. unset($needAdd[$key]);
  106. $needUpdate[] = [array_shift($needUpdateZero), $value["rules_point_id"]];
  107. } elseif ($zeroList != []) {
  108. unset($needAdd[$key]);
  109. $needUpdate[] = [array_shift($zeroList), $value["rules_point_id"]];
  110. } else {
  111. break;
  112. }
  113. }
  114. //都更新完了还不够用,那就存储$needAdd
  115. if ($needAdd != []) {
  116. if ($type === "OperationalAdjustments") {
  117. $param = [
  118. "operational_adjustments_id", "rules_point_id"
  119. ];
  120. } else {
  121. $param = [
  122. "emergency_response_id", "rules_point_id"
  123. ];
  124. }
  125. Yii::$app->db->createCommand()->batchInsert($tableName, $param, $needAdd)->execute();
  126. }
  127. //执行needUpdateZero
  128. if ($needUpdateZero != []) {
  129. if ($type === "OperationalAdjustments") {
  130. OperationalAdjustmentsRelateToRulesPoints::updateAll(["rules_point_id" => 0], ["id" => $needUpdateZero]);
  131. } else {
  132. EmergencyResponseRelateToRulesPoints::updateAll(["rules_point_id" => 0], ["id" => $needUpdateZero]);
  133. }
  134. }
  135. //执行needUpdate
  136. foreach ($needUpdate as $value) {
  137. Yii::$app->db->createCommand()->update($tableName, ['rules_point_id' => $value[1]], ["id" => $value[0]])->execute();
  138. }
  139. }
  140. protected static function dealWithNeedArrayData(&$needUpdateZero, &$needAdd, $relationData, $model, $type): void
  141. {
  142. $rulesPointIds = [];
  143. /** @var OperationalAdjustments $model */
  144. /** @var EmergencyResponse $model */
  145. $rulesArray = json_decode($model->rules, true);
  146. foreach ($rulesArray as $rules) {
  147. $ids = array_column(RulesPointsService::getRulesPointsQuery()->select("id")->andWhere(["rule_id" => $rules["rule_id"], "point" => $rules["point"]])->all(), "id");
  148. $rulesPointIds = array_merge($rulesPointIds, $ids);
  149. }
  150. foreach ($relationData as $key => $value) {
  151. $arrayKey = array_search($relationData[$key][1], $rulesPointIds);
  152. if ($arrayKey !== false) {
  153. unset($rulesPointIds[$arrayKey]);
  154. } else {
  155. $needUpdateZero[] = $value[0];
  156. }
  157. }
  158. foreach ($rulesPointIds as $value) {
  159. if ($type === "OperationalAdjustments") {
  160. $needAdd[] = ["operational_adjustments_id" => $model->id, "rules_point_id" => $value];
  161. } else {
  162. $needAdd[] = ["emergency_response_id" => $model->id, "rules_point_id" => $value];
  163. }
  164. }
  165. }
  166. }