packExpiredCheck.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\OrderPack;
  4. use Illuminate\Console\Command;
  5. class packExpiredCheck extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'packExpiredCheck';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '服务包过期检测,定时任务1个小时跑一次';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return mixed
  32. */
  33. public function handle()
  34. {
  35. $hour = date('H');
  36. if ($hour == 12) {
  37. //还差3天过期
  38. $this->checkPackExpire(1);
  39. //还差1天过期
  40. $this->checkPackExpire(2);
  41. }
  42. //已经过期了
  43. $this->checkPackExpire(3);
  44. return true;
  45. }
  46. private function checkPackExpire($expire_type)
  47. {
  48. $end_time = strtotime('+3 days');
  49. $send_type = 8;
  50. $expire_type_arr = [1];
  51. if ($expire_type == 2) {
  52. $end_time = strtotime('+1 days');
  53. $expire_type_arr = [1,2];
  54. }
  55. if ($expire_type == 3) {
  56. $end_time = time();
  57. $send_type = 9;
  58. $expire_type_arr = [1,2,3];
  59. }
  60. $orderPack = OrderPack::with(['user'])->whereIn('expire_type', $expire_type_arr)->where('end_time', '<', $end_time)->get()->toArray();
  61. if (!empty($orderPack)) {
  62. foreach ($orderPack as $k => $v) {
  63. //更新订单服务包过期类型
  64. OrderPack::where('id', $v['id'])->increment('expire_type');
  65. //发送微信消息
  66. $official_arr = [$v['user']['openid'], $v['pack_name'], date('Y-m-d H:i:s', $v['start_time']), date('Y-m-d H:i:s', $v['end_time'])];
  67. $service_time = date('Y/m/d', $v['start_time']). ' - '. date('Y/m/d', $v['end_time']);
  68. $subscribe_arr = [$v['user']['openid'], $v['pack_name'], $service_time, $v['user']['nickname']];
  69. send_wechat_message($send_type, $official_arr, $subscribe_arr);
  70. }
  71. }
  72. return true;
  73. }
  74. }