AutoSendTrusteeShipCustomerMsg.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Modules\OfficialAccount\Services\BatchCustomMsgService;
  5. use App\Modules\Book\Services\BookConfigService;
  6. use App\Modules\OfficialAccount\Services\CustomMsgService;
  7. use App\Modules\OfficialAccount\Services\OfficialAccountService;
  8. use App\Modules\OfficialAccount\Models\CustomMsgSendTimeConfig;
  9. use DB;
  10. class AutoSendTrusteeShipCustomerMsg extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'AutoSendTrusteeShipCustomerMsg';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '自动推送托管客服消息';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. private $hour_begin;
  39. private $hour_end;
  40. public function handle()
  41. {
  42. $this->hour_begin = date('H:00:00');
  43. $this->hour_end = date('H:59:59');
  44. $batch_no = '默认批次';
  45. //所有开启推送的渠道
  46. $all_channel_ids = DB::table('custom_msg_switchs_msgs')
  47. ->where('status',1)
  48. ->where('custom_category','auto_custom_trusteeship')
  49. ->select(DB::raw("distinct distribution_channel_id as unique_distribution_channel_id"))
  50. ->pluck('unique_distribution_channel_id')
  51. ->toArray();
  52. //自定义时间的渠道
  53. $customize_time_channel_ids = CustomMsgSendTimeConfig::where('is_enable',1)
  54. ->where('custom_category','auto_custom_trusteeship')
  55. ->whereIn('distribution_channel_id',$all_channel_ids)
  56. ->select(DB::raw("distinct distribution_channel_id as unique_distribution_channel_id"))
  57. ->pluck('unique_distribution_channel_id')
  58. ->toArray();
  59. //获取没有自定义发送时间的站点19点统一发送
  60. if($this->hour_begin == '19:00:00'){
  61. //判断今天是否已经发送过
  62. $today_send_status = DB::table('batch_custom_send_msgs')
  63. ->where('send_time','>',date('Y-m-d'))
  64. ->where('name','like','%'.$batch_no.'%')
  65. ->first();
  66. if(!$today_send_status){
  67. //需要跑的站点= 所有开启推送的站点 - 自定义时间的站点
  68. $channel_ids = array_diff($all_channel_ids,$customize_time_channel_ids);
  69. dump($batch_no);
  70. $this->autoSendTrusteeShipCustomerMsg($channel_ids,$batch_no);
  71. }
  72. }
  73. //获取有自定义发送时间且在一小时内需要创建的站点
  74. $channel_ids = CustomMsgSendTimeConfig::where('is_enable',1)
  75. ->where('custom_category','auto_custom_trusteeship')
  76. ->whereBetween('send_time',[$this->hour_begin,$this->hour_end])
  77. ->whereIn('distribution_channel_id',$all_channel_ids)
  78. ->where(function ($query) {
  79. $query->where('last_send_date','<',date('Y-m-d'))
  80. ->orWhereNull('last_send_date');
  81. })
  82. ->select(DB::raw("distinct distribution_channel_id as unique_distribution_channel_id"))
  83. ->pluck('unique_distribution_channel_id')
  84. ->toArray();
  85. $batch_no ='自定义批次';
  86. dump($batch_no);
  87. $this->autoSendTrusteeShipCustomerMsg($channel_ids,$batch_no);
  88. }
  89. /**
  90. * 创建客服消息
  91. */
  92. public function autoSendTrusteeShipCustomerMsg($channel_ids,$batch_no = '')
  93. {
  94. \Log::info('autoSendTrusteeShipCustomerMsg:');
  95. $sexs = ['a', 'b'];
  96. $customMsgPrams['send_time'] = date('Y-m-d H:00:00');
  97. $customMsgPrams['redirect_url'] = '';
  98. $customMsgPrams['subscribe_time'] = 'g';// 关注1天前,有过交互的
  99. $customMsgPrams['trusteeship'] = '1'; // 托管
  100. $customMsgPrams['balance'] = 'z';
  101. $customMsgPrams['order_type'] = 'z';
  102. $customMsgPrams['category_id'] = 'z';
  103. $customMsgPrams['status'] = '1';
  104. $customMsgPrams['is_show_list'] = '0';// 分销界面不展示
  105. $batch_name = $batch_no;
  106. //设置书籍标题的站点
  107. $book_title_channel_ids = DB::table('custom_msg_switchs_msgs')
  108. ->where('status',1)
  109. ->where('custom_category','auto_custom_trusteeship')
  110. ->where('title_type','BOOK')
  111. ->select(DB::raw("distinct distribution_channel_id as unique_distribution_channel_id"))
  112. ->pluck('unique_distribution_channel_id')
  113. ->toArray();
  114. //
  115. dump('需要跑的站点');
  116. dump($channel_ids);
  117. dump('设置书籍标题的站点');
  118. dump($book_title_channel_ids);
  119. //带书名标题的站点
  120. dump('需要跑的设置书籍标题的站点');
  121. $book_title_channels = array_intersect($channel_ids,$book_title_channel_ids);
  122. dump($book_title_channels);
  123. dump('需要跑的带系统标题的站点');
  124. //带系统标题的站点
  125. $default_title_channels = array_diff($channel_ids,$book_title_channels);
  126. dump($default_title_channels);
  127. //带默认标题
  128. if ($default_title_channels) {
  129. dump('带系统标题');
  130. foreach ($sexs as $sex) {
  131. $customMsgPrams['content'] = $this->getMsgContent($sex, 'SYS');
  132. $customMsgPrams['sex'] = $sex;
  133. $customMsgPrams['name'] = '自动发送托管客服消息_系统标题'.date('YmdH').$batch_name;
  134. \Log::info($customMsgPrams);
  135. $batch_res = BatchCustomMsgService::addBatchCustomSendMsgs($customMsgPrams, $batch_no);
  136. if ($batch_res == 1) {
  137. //创建客服消息
  138. $this->BatchCreateCustomSendMsgs($customMsgPrams,$default_title_channels,$batch_no);
  139. }
  140. }
  141. }
  142. //带书名标题
  143. if ($book_title_channels) {
  144. dump('带书名标题');
  145. foreach ($sexs as $sex) {
  146. $customMsgPrams['content'] = $this->getMsgContent($sex, 'BOOK');
  147. $customMsgPrams['sex'] = $sex;
  148. $customMsgPrams['name'] = '自动发送托管客服消息_书名标题'.date('YmdH').$batch_name;
  149. \Log::info($customMsgPrams);
  150. $batch_res = BatchCustomMsgService::addBatchCustomSendMsgs($customMsgPrams,$batch_no);
  151. \Log::info('batch_res:' . $batch_res . ' batch_no:' . $batch_no);
  152. if ($batch_res == 1) {
  153. //创建客服消息
  154. $this->BatchCreateCustomSendMsgs($customMsgPrams,$book_title_channels,$batch_no);
  155. }
  156. }
  157. }
  158. }
  159. public function getMsgContent($sex,$title_type='')
  160. {
  161. // 男生
  162. if($sex == 'a'){
  163. $books = BookConfigService::getTrusteeShipChannelBook('','男频', 4);
  164. }
  165. // 女生
  166. else{
  167. $books = BookConfigService::getTrusteeShipChannelBook('','女频', 4);
  168. }
  169. $books = CustomMsgService::get_book_full_infos($books,$sex,$title_type);
  170. return json_encode($books);
  171. }
  172. public function BatchCreateCustomSendMsgs($customMsgPrams,$in_channel_ids=[],$batch_no=''){
  173. $customMsgPrams['batch_no'] = $batch_no;
  174. //按站点取其下公众号创建摸板消息
  175. foreach($in_channel_ids as $channel_id){
  176. $time_config = CustomMsgSendTimeConfig::getTimeConfig($channel_id,[
  177. 'begin_time'=>$this->hour_begin,
  178. 'end_time'=>$this->hour_end,
  179. 'custom_category'=>'auto_custom_trusteeship',
  180. ]);
  181. if($time_config){
  182. $customMsgPrams['send_time']=date('Y-m-d').' '.$time_config->send_time;
  183. $time_config->last_send_date =date('Y-m-d');
  184. $time_config->save();
  185. }
  186. $officialAccountParams['in_channle_ids'] = [$channel_id];
  187. $official_accounts = OfficialAccountService::getTrusteeshipOfficialAccountsWithParams($officialAccountParams);
  188. \Log::info($official_accounts);
  189. $customMsgPrams['distribution_channel_id'] = $channel_id;
  190. foreach($official_accounts as $official_account){
  191. $customMsgPrams['appid'] = $official_account->appid;
  192. \Log::info('getTrusteeshipOfficialAccounts:appid:'.$official_account->appid.' distribution_channel_id:'.$channel_id);
  193. //先判断在发送时间段1小时以内,有没有发起过相同的模板消息,如果有发送过,就提示用户已经创建过相同模板消息,不创建新的模板消息
  194. $isSendCustomer = CustomMsgService::isSendCustomerAtSameTimeAndSex($customMsgPrams);
  195. if (!empty($isSendCustomer)) {
  196. \Log::info('isSendCustomerAtSameTime already_send:appid:'.$customMsgPrams['appid'].' distribution_channel_id:'.$customMsgPrams['distribution_channel_id']);
  197. continue;
  198. }
  199. $customMsgService = CustomMsgService::addCustomSendMsgs($customMsgPrams);
  200. }
  201. }
  202. }
  203. }