DreamDJS.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. $message = "你的梦想《".$item->name."》已结束,点击了解接下来的步骤";
  50. $arr = [
  51. 'user_id'=>0,
  52. 'message'=>$message,
  53. 'to_user_id'=>$item->user_id,
  54. 'dream_id'=>$item->id,
  55. 'is_end'=>1,
  56. 'type_id'=>1,
  57. 'attr_id'=>6,
  58. ];
  59. SystemInfoModel::firstOrCreate($arr);
  60. // (2) 给支持者发送消息
  61. $support_dream = SupportDreamModel::where('dream_id',$item->id)->get();
  62. $top = [] ;
  63. foreach ($support_dream as $item2) {
  64. if (!array_key_exists($item2->user_id,$top)) {
  65. $top[$item2->user_id] = $item2->score;
  66. }else{
  67. $top[$item2->user_id] += $item2->score;
  68. }
  69. }
  70. arsort($top);
  71. $new_arr = array_values($top);
  72. if (!empty($top)) {
  73. foreach ($top as $k => $v) {
  74. $key = array_search($v,$new_arr); //排名
  75. $message = $item->name."已经结束啦!谢谢你的支持,你可是他的第".($key+1)."支持者哦!";
  76. if (empty($key)) { //最大支持者
  77. $message = "恭喜你成为《".$item->name."》的《梦主》! ";
  78. $info = $dream_user->nickname."会以你提供的微信/电话联系你约好时间地点亲自感谢你给予的支持和鼓励。如果你不希望见面或
  79. 不想要梦想者拥有你的联系方式,请按《不需要见面》的按钮或联系客服。";
  80. $arr2 = [
  81. 'user_id'=>$item->user_id,
  82. 'info'=>$info,
  83. 'message'=>$message,
  84. 'to_user_id'=>$k,
  85. 'dream_id'=>$item->id,
  86. 'is_end'=>1,
  87. 'is_url'=>1,
  88. 'type_id'=>2,
  89. 'is_max'=>1,
  90. ];
  91. }else{
  92. $arr2 = [
  93. 'user_id'=>0,
  94. 'message'=>$message,
  95. 'to_user_id'=>$k,
  96. 'dream_id'=>$item->id,
  97. 'is_end'=>1,
  98. 'is_url'=>1,
  99. 'type_id'=>2,
  100. ];
  101. }
  102. SystemInfoModel::firstOrCreate($arr2);
  103. }
  104. }
  105. }
  106. }
  107. // 2 我关注的梦想倒计时 通知消息
  108. $dreams =DreamInfoModel::with('user')->where('end_time','>',date('Y-m-d H:i:s'))->get();
  109. if (count($dreams) > 0) {
  110. foreach ($dreams as $item) {
  111. if (date('Y-m-d',strtotime($item->end_time)) == date('Y-m-d',time()+1*24*3600)) {
  112. $message = $item->name.'过1天就要结束啦!';
  113. }elseif(date('Y-m-d H:i',strtotime($item->end_time)) == date('Y-m-d H:i',time()+3600)){
  114. $message = $item->name.'过1小时就要结束啦!';
  115. }else{
  116. $message = '';
  117. }
  118. if (!empty($message)) {
  119. $user_ids = UserCareDream::where('dream_id',$item->id)->select('id','user_id')->get()->toArray();
  120. $arr_ids = array_column($user_ids,'user_id');
  121. if (!empty($arr_ids)) {
  122. foreach ($arr_ids as $u_id) {
  123. $arr3 = [
  124. 'user_id'=>0,
  125. 'message'=>$message,
  126. 'to_user_id'=>$u_id,
  127. 'dream_id'=>$item->id,
  128. 'is_url'=>1,
  129. 'type_id'=>2,
  130. 'attr_id'=>8,
  131. ];
  132. SystemInfoModel::firstOrCreate($arr3);
  133. }
  134. }
  135. }
  136. }
  137. }
  138. }
  139. }