SpecializeCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace App\Console\Commands\Pair;
  3. use App\Models\Fpdx\GroupMemberModel;
  4. use App\Models\Fpdx\GroupModel;
  5. use App\Models\Fpdx\PairModel;
  6. use Illuminate\Console\Command;
  7. class SpecializeCommand extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'pair:city';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '城市专场';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. //
  38. $stageId = $this->ask('选择期数?');
  39. $city = $this->ask('选择城市?');
  40. $groups = explode(',', $this->ask('群ID'));
  41. $activityType = '72h';
  42. $pairs = \DB::table('kdgx_partner_charge_pair')
  43. ->select('kdgx_partner_charge_pair.*')
  44. ->join('kdgx_partner_charge_user', 'kdgx_partner_charge_pair.uid', '=', 'kdgx_partner_charge_user.uid')
  45. ->where('kdgx_partner_charge_user.address', 'like', "%$city%")
  46. ->where([
  47. 'kdgx_partner_charge_pair.activity_type' => $activityType,
  48. 'kdgx_partner_charge_pair.stage_id' => $stageId,
  49. 'kdgx_partner_charge_pair.partner' => 2
  50. ])->whereNotNull('kdgx_partner_charge_pair.assoc_id')
  51. ->where('group_id', 0)
  52. ->get();
  53. foreach ($pairs as $pair) {
  54. $left = PairModel::find($pair->assoc_id);
  55. $right = $pair;
  56. $group = GroupModel::whereIn('id', $groups)->where('activity_type', $activityType)
  57. ->where('type', 'pair')->whereColumn('number', '>', 'total_number')->first();
  58. if (!$group) {
  59. throw new \Exception("配对群组不够用");
  60. }
  61. PairModel::where(['id' => $left->id])->update(['group_id' => $group->id]);
  62. PairModel::where(['id' => $right->id])->update(['group_id' => $group->id]);
  63. GroupMemberModel::create([
  64. 'uid' => $left->uid,
  65. 'stage_id' => $stageId,
  66. 'group_id' => $group->id,
  67. 'type' => 'pair',
  68. 'activity_type' => $activityType
  69. ]);
  70. GroupMemberModel::create([
  71. 'uid' => $right->uid,
  72. 'stage_id' => $stageId,
  73. 'group_id' => $group->id,
  74. 'type' => 'pair',
  75. 'activity_type' => $activityType
  76. ]);
  77. $group->increment('total_number', 2);
  78. }
  79. }
  80. }