CustomMsgStrategyAutoSend.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace App\Console\Commands\CustomMsg;
  3. use App\Modules\OfficialAccount\Services\CustomMsgService;
  4. use App\Modules\OfficialAccount\Models\CustomMsgStrategy;
  5. use Illuminate\Console\Command;
  6. use DB;
  7. class CustomMsgStrategyAutoSend extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'auth_send_custom_msg_by_strategy';
  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. $hour_begin = date('H:00:00');
  39. $hour_end = date('H:59:59');
  40. $strategies = CustomMsgStrategy::whereNull('deleted_at')
  41. ->where('status',1)
  42. ->where(function ($query) {
  43. $query->where('last_send_date','<',date('Y-m-d 00:00:00'))
  44. ->orWhereNull('last_send_date');
  45. })
  46. ->whereBetween('send_time',[$hour_begin,$hour_end])
  47. ->select([
  48. 'id',
  49. 'appid',
  50. 'name',
  51. 'send_time',
  52. 'content',
  53. 'description',
  54. 'book_name',
  55. 'chapter_name',
  56. 'redirect_url',
  57. 'subscribe_time',
  58. 'sex',
  59. 'balance',
  60. 'order_type',
  61. 'category_id',
  62. 'is_full_send',
  63. 'distribution_channel_id',
  64. 'status',
  65. 'batch_order_sn',
  66. 'custom_type',
  67. ])
  68. ->get()
  69. ;
  70. $strategies = json_decode(json_encode($strategies), true);
  71. foreach ($strategies as $strategy){
  72. $strategy['status']=1;
  73. $strategy_id = $strategy['id'];
  74. unset($strategy['id']);
  75. $strategy['send_time']=date('Y-m-d ').$strategy['send_time'];
  76. //先判断在发送时间段1小时以内,有没有发起过相同的模板消息,如果有发送过,就提示用户已经创建过相同模板消息,不创建新的模板消息
  77. $isSendCustomer = CustomMsgService::isSendCustomerAtSameTime($strategy);
  78. if (!empty($isSendCustomer)) {
  79. \Log::info('isSendCustomerAtSameTime:' . $strategy['distribution_channel_id']);
  80. } else {
  81. CustomMsgStrategy::where('id',$strategy_id)->update(['last_send_date' => date('Y-m-d H:i:s')]);
  82. $customMsgService = CustomMsgService::addCustomSendMsgs($strategy);
  83. }
  84. }
  85. }
  86. }