DreamDJS.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\DreamInfoModel;
  4. use App\Models\SupportDreamModel;
  5. use App\Models\SystemInfoModel;
  6. use App\Models\UserCareDream;
  7. use Illuminate\Console\Command;
  8. use Illuminate\Support\Facades\Auth;
  9. use App\Helper\JpushHelper;
  10. use Illuminate\Support\Facades\Log;
  11. class DreamDJS extends Command
  12. {
  13. use JpushHelper;
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'DreamDJS';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'Command description';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return mixed
  39. */
  40. public function handle()
  41. {
  42. //执行逻辑
  43. Log::info('hello xiaoqiang-----------------------');
  44. $end_dream_info = DreamInfoModel::with('user')->where('end_time','<',date('Y-m-d H:i:s'))->get();
  45. if (count($end_dream_info)>0) {
  46. foreach ($end_dream_info as $item){
  47. // (1)给梦想者发消息
  48. $dream_user = $item->user;
  49. if (!empty($dream_user)) {
  50. $message = "你的梦想《".$item->name."》已结束,点击了解接下来的步骤";
  51. $arr = [
  52. 'user_id'=>0,
  53. 'message'=>$message,
  54. 'to_user_id'=>$item->user_id,
  55. 'dream_id'=>$item->id,
  56. 'is_end'=>1,
  57. 'type_id'=>1,
  58. 'attr_id'=>6,
  59. ];
  60. SystemInfoModel::firstOrCreate($arr);
  61. // (2) 给支持者发送消息
  62. $support_dream = SupportDreamModel::where('dream_id',$item->id)->get();
  63. $top = [] ;
  64. foreach ($support_dream as $item2) {
  65. if (!array_key_exists($item2->user_id,$top)) {
  66. $top[$item2->user_id] = $item2->score;
  67. }else{
  68. $top[$item2->user_id] += $item2->score;
  69. }
  70. }
  71. arsort($top);
  72. $new_arr = array_values($top);
  73. if (!empty($top)) {
  74. foreach ($top as $k => $v) {
  75. $key = array_search($v,$new_arr); //排名
  76. $message = $item->name."已经结束啦!谢谢你的支持,你可是他的第".($key+1)."支持者哦!";
  77. if (empty($key)) { //最大支持者
  78. $message = "恭喜你成为《".$item->name."》的《梦主》! ";
  79. $info = $dream_user->nickname."会以你提供的微信/电话联系你约好时间地点亲自感谢你给予的支持和鼓励。如果你不希望见面或
  80. 不想要梦想者拥有你的联系方式,请按《不需要见面》的按钮或联系客服。";
  81. $arr2 = [
  82. 'user_id'=>$item->user_id,
  83. 'info'=>$info,
  84. 'message'=>$message,
  85. 'to_user_id'=>$k,
  86. 'dream_id'=>$item->id,
  87. 'is_end'=>1,
  88. 'is_url'=>1,
  89. 'type_id'=>2,
  90. 'is_max'=>1,
  91. ];
  92. }else{
  93. $arr2 = [
  94. 'user_id'=>0,
  95. 'message'=>$message,
  96. 'to_user_id'=>$k,
  97. 'dream_id'=>$item->id,
  98. 'is_end'=>1,
  99. 'is_url'=>1,
  100. 'type_id'=>2,
  101. ];
  102. }
  103. SystemInfoModel::firstOrCreate($arr2);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. // 2 我关注的梦想倒计时 通知消息
  110. $dreams =DreamInfoModel::with('user')->where('end_time','>',date('Y-m-d H:i:s'))->get();
  111. if (count($dreams) > 0) {
  112. foreach ($dreams as $item) {
  113. if (date('Y-m-d',strtotime($item->end_time)) == date('Y-m-d',time()+1*24*3600)) {
  114. $message = $item->name.'过1天就要结束啦!';
  115. }elseif(date('Y-m-d H:i',strtotime($item->end_time)) == date('Y-m-d H:i',time()+3600)){
  116. $message = $item->name.'过1小时就要结束啦!';
  117. }else{
  118. $message = '';
  119. }
  120. if (!empty($message)) {
  121. $user_ids = UserCareDream::where('dream_id',$item->id)->select('id','user_id')->get()->toArray();
  122. $arr_ids = array_column($user_ids,'user_id');
  123. if (!empty($arr_ids)) {
  124. foreach ($arr_ids as $u_id) {
  125. $arr3 = [
  126. 'user_id'=>0,
  127. 'message'=>$message,
  128. 'to_user_id'=>$u_id,
  129. 'dream_id'=>$item->id,
  130. 'is_url'=>1,
  131. 'type_id'=>2,
  132. 'attr_id'=>8,
  133. ];
  134. SystemInfoModel::firstOrCreate($arr3);
  135. }
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }