MatchService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace App\Services\Pair;
  3. use App\Http\Controllers\Core\Ding;
  4. use App\Models\Fpdx\ActivityModel;
  5. use App\Models\Fpdx\PairModel;
  6. use App\Services\Service;
  7. class MatchService extends Service
  8. {
  9. // 加载重新匹配队列
  10. public function rematch()
  11. {
  12. $ings = ActivityModel::getActivitys();
  13. $stage_id = $ings['ing'];
  14. $pairModel = new PairModel();
  15. if (app()->environment() == 'production') {
  16. $number = $pairModel->where('stage_id', $stage_id)->where('rematch', 1)->count();
  17. Ding::robot([
  18. 'title' => '[分配对象]重新匹配|开始',
  19. 'text' => "选择重匹人数{$number}",
  20. ]);
  21. }
  22. // 匹配数据初始化
  23. $this->initReMatchQueue($stage_id);
  24. $cronpair = new \App\Http\Controllers\Crontab\Pair();
  25. // $cronpair->pipei(array($pairModel, 'getRematchPair'), [$stage_id, '72h', '浙江-杭州']);
  26. // $cronpair->pipei(array($pairModel, 'getRematchPair'), [$stage_id, 'qbj']);
  27. $cronpair->pipei(array($pairModel, 'getRematchPair'), [$stage_id, '72h']);
  28. if (app()->environment() == 'production') {
  29. $number = $pairModel->where('stage_id', $stage_id)->where('rematch', 1)->whereNotNull('assoc_id')->count();
  30. Ding::robot([
  31. 'title' => "[分配对象]重新匹配|完成",
  32. 'text' => "重配成功人数{$number}",
  33. ]);
  34. }
  35. $check = new CheckService();
  36. $check->baocun($stage_id);
  37. $check->check($stage_id);
  38. }
  39. // 重新匹配
  40. public function initReMatchQueue(int $stage_id)
  41. {
  42. if (app()->environment() == 'production') {
  43. Ding::robot([
  44. 'title' => '[分配对象]重新匹配|数据初始化|开始',
  45. 'text' => "数据初始化|开始",
  46. ]);
  47. }
  48. $pairs = PairModel::where([
  49. ['stage_id', $stage_id],
  50. ['partner', 1],
  51. ['room_id', 0],
  52. ])->whereBetween('state', [400, 499])->whereNotNull('assoc_id')->get();
  53. foreach ($pairs as $pair) {
  54. try {
  55. $other = PairModel::findOrFail($pair->assoc_id);
  56. /**
  57. * pair同意 + other同意
  58. */
  59. if (0 < $pair->confirm && 0 < $other->confirm) {
  60. continue;
  61. }
  62. /**
  63. * pair不操作 + other不操作
  64. */
  65. if (0 < $pair->confirm && 0 < $other->confirm) {
  66. continue;
  67. }
  68. /**
  69. * pair不同意 + other不同意
  70. * exitGroup
  71. * pair.last_assoc = pair.assoc_id;
  72. * other.last_assoc = other.assoc_id
  73. * pair.assoc_id = other.assoc_id = null;
  74. * pair.rematch = other.rematch = 1;
  75. * pair.partner = other.partner = 0;
  76. */
  77. if (0 > $pair->confirm && 0 > $other->confirm) {
  78. $gs = new GroupService();
  79. $gs->exitGroup($pair);
  80. $pair->last_assoc = $pair->assoc_id;
  81. $other->last_assoc = $other->assoc_id;
  82. $pair->assoc_id = $other->assoc_id = null;
  83. $pair->rematch = $other->rematch = 1;
  84. $pair->partner = $other->partner = 0;
  85. $pair->save() && $other->save();
  86. continue;
  87. }
  88. /**
  89. * pair同意 + other不同意
  90. * exitGroup
  91. * pair.last_assoc = pair.assoc_id;
  92. * other.last_assoc = other.assoc_id
  93. * pair.assoc_id = other.assoc_id = null;
  94. * pair.rematch = other.rematch = 1;
  95. * pair.partner = other.partner = 0;
  96. */
  97. if (0 < $pair->confirm && 0 > $other->confirm) {
  98. $gs = new GroupService();
  99. $gs->exitGroup($pair);
  100. $pair->last_assoc = $pair->assoc_id;
  101. $other->last_assoc = $other->assoc_id;
  102. $pair->assoc_id = $other->assoc_id = null;
  103. $pair->rematch = $other->rematch = 1;
  104. $pair->partner = $other->partner = 0;
  105. $pair->save() && $other->save();
  106. continue;
  107. }
  108. /**
  109. * pair不同意 + other同意
  110. * exitGroup
  111. * pair.last_assoc = pair.assoc_id;
  112. * other.last_assoc = other.assoc_id
  113. * pair.assoc_id = other.assoc_id = null;
  114. * pair.rematch = other.rematch = 1;
  115. * pair.partner = other.partner = 0;
  116. */
  117. if (0 > $pair->confirm && 0 < $other->confirm) {
  118. $gs = new GroupService();
  119. $gs->exitGroup($pair);
  120. $pair->last_assoc = $pair->assoc_id;
  121. $other->last_assoc = $other->assoc_id;
  122. $pair->assoc_id = $other->assoc_id = null;
  123. $pair->rematch = $other->rematch = 1;
  124. $pair->partner = $other->partner = 0;
  125. $pair->save() && $other->save();
  126. continue;
  127. }
  128. /**
  129. * pair同意 + other不操作 :
  130. * exitGroup
  131. * pair.last_assoc = pair.assoc_id;
  132. * other.last_assoc = other.assoc_id
  133. * pair.assoc_id = other.assoc_id = null;
  134. * pair.rematch = 1;
  135. * pair.partner = other.partner = 0;
  136. * other.state = other.state + 400;
  137. *
  138. */
  139. if (0 < $pair->confirm && 0 == $other->confirm) {
  140. $gs = new GroupService();
  141. $gs->exitGroup($pair);
  142. $pair->last_assoc = $pair->assoc_id;
  143. $other->last_assoc = $other->assoc_id;
  144. $pair->assoc_id = $other->assoc_id = null;
  145. $pair->rematch = 1;
  146. $pair->partner = $other->partner = 0;
  147. $other->state = $other->state + 400;
  148. $pair->save() && $other->save();
  149. continue;
  150. }
  151. /**
  152. * pair不操作 + other同意 :
  153. * exitGroup
  154. * pair.last_assoc = pair.assoc_id;
  155. * other.last_assoc = other.assoc_id
  156. * pair.assoc_id = other.assoc_id = null;
  157. * other.rematch = 1;
  158. * pair.partner = other.partner = 0;
  159. * pair.state = pair.state + 400;
  160. *
  161. */
  162. if (0 == $pair->confirm && 0 < $other->confirm) {
  163. $gs = new GroupService();
  164. $gs->exitGroup($pair);
  165. $pair->last_assoc = $pair->assoc_id;
  166. $other->last_assoc = $other->assoc_id;
  167. $pair->assoc_id = $other->assoc_id = null;
  168. $other->rematch = 1;
  169. $pair->partner = $other->partner = 0;
  170. $pair->state = $pair->state + 400;
  171. $pair->save() && $other->save();
  172. continue;
  173. }
  174. /**
  175. * pair不同意 + other不操作 :
  176. * exitGroup
  177. * pair.last_assoc = pair.assoc_id;
  178. * other.last_assoc = other.assoc_id
  179. * pair.assoc_id = other.assoc_id = null;
  180. * pair.rematch = 1;
  181. * pair.partner = other.partner = 0;
  182. * other.state = other.state + 400;
  183. */
  184. if (0 > $pair->confirm && 0 == $other->confirm) {
  185. $gs = new GroupService();
  186. $gs->exitGroup($pair);
  187. $pair->last_assoc = $pair->assoc_id;
  188. $other->last_assoc = $other->assoc_id;
  189. $pair->assoc_id = $other->assoc_id = null;
  190. $pair->rematch = 1;
  191. $pair->partner = $other->partner = 0;
  192. $other->state = $other->state + 400;
  193. $pair->save() && $other->save();
  194. continue;
  195. }
  196. /**
  197. * pair不操作 + other不同意 :
  198. * exitGroup
  199. * pair.last_assoc = pair.assoc_id;
  200. * other.last_assoc = other.assoc_id
  201. * pair.assoc_id = other.assoc_id = null;
  202. * other.rematch = 1;
  203. * pair.partner = other.partner = 0;
  204. * pair.state = pair.state + 400;
  205. *
  206. */
  207. if ($pair->confirm == 0 && 0 > $other->confirm) {
  208. $gs = new GroupService();
  209. $gs->exitGroup($pair);
  210. $pair->last_assoc = $pair->assoc_id;
  211. $other->last_assoc = $other->assoc_id;
  212. $pair->assoc_id = $other->assoc_id = null;
  213. $other->rematch = 1;
  214. $pair->partner = $other->partner = 0;
  215. $pair->state = $pair->state + 400;
  216. $pair->save() && $other->save();
  217. continue;
  218. }
  219. } catch (\Exception $e) {
  220. continue;
  221. }
  222. }
  223. if (app()->environment() == 'production') {
  224. Ding::robot([
  225. 'title' => '[分配对象]重新匹配|数据初始化|完成',
  226. 'text' => "数据初始化|完成",
  227. ]);
  228. }
  229. }
  230. }