IntoGroupCommand.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. namespace App\Console\Commands\Pair;
  3. use App\Http\Controllers\Core\Ding;
  4. use App\Models\Fpdx\PairModel;
  5. use Illuminate\Console\Command;
  6. use App\Models\Fpdx\ActivityModel;
  7. use App\Models\Fpdx\GroupMemberModel;
  8. use App\Models\Fpdx\GroupModel;
  9. class IntoGroupCommand extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'pair:into:group';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '分配对象::自动进入群组';
  23. private $stage_id;
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. private $privates = [
  34. "四川",
  35. "重庆",
  36. "河南",
  37. "河北",
  38. "北京",
  39. "天津",
  40. "山东",
  41. "江苏",
  42. "浙江",
  43. "上海",
  44. "江西",
  45. "福建",
  46. "澳门",
  47. "香港",
  48. "台湾",
  49. "广东",
  50. "海南",
  51. "广西",
  52. "安徽",
  53. "湖南",
  54. "湖北",
  55. "云南",
  56. "贵州",
  57. "西藏",
  58. "新疆",
  59. "青海",
  60. "甘肃",
  61. "宁夏",
  62. "内蒙古",
  63. "陕西",
  64. "山西",
  65. "辽宁",
  66. "吉林",
  67. "黑龙江"
  68. ];
  69. /**
  70. * Execute the console command.
  71. *
  72. * @return mixed
  73. * @throws \Exception
  74. */
  75. public function handle()
  76. {
  77. $this->stage_id = ActivityModel::where('signend_time', '<=', time())->where('close_time', '>=', time())
  78. ->value('stage_id');
  79. if (app()->environment() == 'production') {
  80. //开始钉钉通知
  81. \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/pairgroup/begin/{$this->stage_id}")->get();
  82. }
  83. $this->pair("72h");
  84. $this->pair("qbj");
  85. if (app()->environment() == 'production') {
  86. //结束钉钉通知
  87. \Curl::to("http://api.deep.fenpeiduixiang.com/api/ding/pairgroup/end/{$this->stage_id}")->get();
  88. }
  89. }
  90. public function pair($activity_type)
  91. {
  92. // 按省排序
  93. foreach ($this->privates as $private) {
  94. $pairs = \DB::table('kdgx_partner_charge_pair')
  95. ->select('kdgx_partner_charge_pair.*')
  96. ->join('kdgx_partner_charge_user', 'kdgx_partner_charge_pair.uid', '=', 'kdgx_partner_charge_user.uid')
  97. ->where('kdgx_partner_charge_user.address', 'like', "{$private}-%")
  98. ->where([
  99. 'kdgx_partner_charge_pair.activity_type' => $activity_type,
  100. 'kdgx_partner_charge_pair.stage_id' => $this->stage_id,
  101. 'kdgx_partner_charge_pair.partner' => 2
  102. ])->whereNotNull('kdgx_partner_charge_pair.assoc_id')
  103. ->where('group_id', 0)
  104. ->get();
  105. foreach ($pairs as $pair) {
  106. $left = PairModel::find($pair->assoc_id);
  107. $right = $pair;
  108. $group = GroupModel::where('stage_id', $this->stage_id)->where('activity_type', $activity_type)
  109. ->where('type', 'pair')->whereColumn('number', '>', 'total_number')->first();
  110. if (!$group) {
  111. Ding::robot([
  112. 'title' => '分群通知',
  113. 'text' => "配对群组不够用!!!请查看群容量后,再执行"
  114. ]);
  115. throw new \Exception("配对群组不够用");
  116. }
  117. PairModel::where(['id' => $left->id])->update(['group_id' => $group->id]);
  118. PairModel::where(['id' => $right->id])->update(['group_id' => $group->id]);
  119. GroupMemberModel::create([
  120. 'uid' => $left->uid,
  121. 'stage_id' => $this->stage_id,
  122. 'group_id' => $group->id,
  123. 'type' => 'pair',
  124. 'activity_type' => $activity_type
  125. ]);
  126. GroupMemberModel::create([
  127. 'uid' => $right->uid,
  128. 'stage_id' => $this->stage_id,
  129. 'group_id' => $group->id,
  130. 'type' => 'pair',
  131. 'activity_type' => $activity_type
  132. ]);
  133. $group->increment('total_number', 2);
  134. }
  135. }
  136. // 兼容城市填错的人送入房间
  137. $pairs = PairModel::where([
  138. 'stage_id' => $this->stage_id,
  139. 'group_id' => 0,
  140. 'activity_type' => $activity_type
  141. ])->whereNotNull('assoc_id')
  142. ->where('partner', 2)
  143. ->get();
  144. foreach ($pairs as $pair) {
  145. $left = PairModel::find($pair->assoc_id);
  146. $right = $pair;
  147. $group = GroupModel::where('stage_id', $this->stage_id)->where('activity_type', $activity_type)
  148. ->where('type', 'pair')->whereColumn('number', '>', 'total_number')->first();
  149. if (!$group) {
  150. Ding::robot([
  151. 'title' => '分群通知',
  152. 'text' => "配对群组不够用!!!请查看群容量后,再执行"
  153. ]);
  154. throw new \Exception("配对群组不够用");
  155. }
  156. PairModel::where(['id' => $left->id])->update(['group_id' => $group->id]);
  157. PairModel::where(['id' => $right->id])->update(['group_id' => $group->id]);
  158. GroupMemberModel::create([
  159. 'uid' => $left->uid,
  160. 'stage_id' => $this->stage_id,
  161. 'group_id' => $group->id,
  162. 'type' => 'pair',
  163. 'activity_type' => $activity_type
  164. ]);
  165. GroupMemberModel::create([
  166. 'uid' => $right->uid,
  167. 'stage_id' => $this->stage_id,
  168. 'group_id' => $group->id,
  169. 'type' => 'pair',
  170. 'activity_type' => $activity_type
  171. ]);
  172. $group->increment('total_number', 2);
  173. }
  174. }
  175. }