BatchCustomMsgService.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Modules\OfficialAccount\Services;
  3. use App\Modules\Book\Services\BookConfigService;
  4. use App\Modules\OfficialAccount\Models\BatchCustomSendMsgs;
  5. class BatchCustomMsgService
  6. {
  7. /**
  8. * 通过id获取关键字
  9. */
  10. static function batchCustomMsgById($prams)
  11. {
  12. return BatchCustomSendMsgs::batchCustomMsgById($prams['id']);
  13. }
  14. /**
  15. * 更新状态
  16. */
  17. static function updateBatchCustomSendMsgStatusById($prams)
  18. {
  19. return BatchCustomSendMsgs::updateBatchCustomSendMsgStatusById($prams['id'],$prams['status'],$prams['del_flag']);
  20. }
  21. /**
  22. * 获取批次客服消息列表
  23. */
  24. static function batchCustomMsgList()
  25. {
  26. return BatchCustomSendMsgs::batchCustomMsgList();
  27. }
  28. //判断1小时内是否有相同的客服消息插入过
  29. static function isSendCustomerAtSameTime($customerPrams){
  30. return BatchCustomSendMsgs::isSendCustomerAtSameTime($customerPrams['distribution_channel_id'],$customerPrams['appid'],$customerPrams['send_time']);
  31. }
  32. //判断1小时内是否有相同的客服消息插入过
  33. static function isSendCustomerAtSameTimeSex($customerPrams){
  34. return BatchCustomSendMsgs::isSendCustomerAtSameTimeSex($customerPrams['send_time'],$customerPrams['sex']);
  35. }
  36. /**
  37. * 发送客服消息
  38. */
  39. static function addBatchCustomSendMsgs($customSendMsgsPrams,&$batch_no)
  40. {
  41. \Log::info('addBatchCustomSendMsgs_start');
  42. try {
  43. //先判断在发送时间段1小时以内,有没有发起过相同的模板消息,如果有发送过,就提示用户已经创建过相同模板消息,不创建新的模板消息
  44. $isSendCustomer = BatchCustomSendMsgs::isSendCustomerAtSameTimeSex($customSendMsgsPrams['send_time'],$customSendMsgsPrams['sex']);
  45. if (!empty($isSendCustomer)) {
  46. \Log::info('addBatchCustomSendMsgs already_send'.json_encode($customSendMsgsPrams));
  47. return 4;
  48. }
  49. $batchCustomSendMsgsPrams = array();
  50. $batchCustomSendMsgsPrams['name'] = $customSendMsgsPrams['name'];
  51. $batchCustomSendMsgsPrams['send_time'] = $customSendMsgsPrams['send_time'];
  52. $batchCustomSendMsgsPrams['content'] = $customSendMsgsPrams['content'];
  53. $batchCustomSendMsgsPrams['redirect_url'] = $customSendMsgsPrams['redirect_url'];
  54. $batchCustomSendMsgsPrams['sex'] = $customSendMsgsPrams['sex'];
  55. $batchCustomSendMsgsPrams['subscribe_time'] = $customSendMsgsPrams['subscribe_time'];
  56. $batchCustomSendMsgsPrams['balance'] = $customSendMsgsPrams['balance'];
  57. $batchCustomSendMsgsPrams['order_type'] = $customSendMsgsPrams['order_type'];
  58. $batchCustomSendMsgsPrams['category_id'] = $customSendMsgsPrams['category_id'];
  59. $batchCustomSendMsgsPrams['status'] = $customSendMsgsPrams['status'];
  60. $batchCustomSendMsgsPrams['del_flag'] = 0;
  61. $batch_create_res = BatchCustomSendMsgs::create($customSendMsgsPrams);
  62. $batch_no = isset($batch_create_res->id)?$batch_create_res->id:'';
  63. \Log::info('$batch_create_res:'.$batch_no);
  64. return 1;
  65. } catch (\Exception $e) {
  66. \Log::info('addBatchCustomSendMsgs_ept:'.$e->getMessage());
  67. return 0;
  68. }
  69. }
  70. /**
  71. * 获取批次客服消息列表
  72. */
  73. static function autoSendTrusteeShipCustomerMsg($sex)
  74. {
  75. \Log::info('autoSendTrusteeShipCustomerMsg:'.$sex);
  76. // 每天晚上19点
  77. $customMsgPrams['send_time'] = date('Y-m-d 19:00:00');
  78. $customMsgPrams['name'] = '自动发送托管客服消息:'.$customMsgPrams['send_time'];
  79. $books = [];
  80. // 男生
  81. if($sex == 'a'){
  82. $books = BookConfigService::getTrusteeShipChannelBook('','男频', 4);
  83. }
  84. // 女生
  85. else{
  86. $books = BookConfigService::getTrusteeShipChannelBook('','女频', 4);
  87. }
  88. $books = CustomMsgService::get_book_full_infos($books,'');
  89. // 随机推送内容
  90. $customMsgPrams['content'] = json_encode($books);
  91. $customMsgPrams['redirect_url'] = '';
  92. $customMsgPrams['sex'] = $sex;
  93. $customMsgPrams['subscribe_time'] = 'g';// 关注1天前,有过交互的
  94. $customMsgPrams['trusteeship'] = '1'; // 托管
  95. $customMsgPrams['balance'] = 'z';
  96. $customMsgPrams['order_type'] = 'z';
  97. $customMsgPrams['category_id'] = 'z';
  98. $customMsgPrams['status'] = '1';
  99. $customMsgPrams['is_show_list'] = '0';// 分销界面不展示
  100. \Log::info($customMsgPrams);
  101. $batch_no = '';
  102. $batch_res = BatchCustomMsgService::addBatchCustomSendMsgs($customMsgPrams,$batch_no);
  103. \Log::info('batch_res:'.$batch_res.' batch_no:'.$batch_no);
  104. if($batch_res == 1){
  105. $customMsgPrams['batch_no'] = $batch_no;
  106. $official_accounts = OfficialAccountService::getTrusteeshipOfficialAccounts();
  107. \Log::info('getTrusteeshipOfficialAccounts');
  108. \Log::info($official_accounts);
  109. // return true;
  110. foreach($official_accounts as $official_account){
  111. $customMsgPrams['appid'] = $official_account->appid;
  112. $customMsgPrams['distribution_channel_id'] = $official_account->distribution_channel_id;
  113. \Log::info('getTrusteeshipOfficialAccounts:appid:'.$official_account->appid.' distribution_channel_id:'.$official_account->distribution_channel_id);
  114. //先判断在发送时间段1小时以内,有没有发起过相同的模板消息,如果有发送过,就提示用户已经创建过相同模板消息,不创建新的模板消息
  115. $isSendCustomer = CustomMsgService::isSendCustomerAtSameTimeAndSex($customMsgPrams);
  116. if (!empty($isSendCustomer)) {
  117. \Log::info('isSendCustomerAtSameTime already_send:appid:'.$customMsgPrams['appid'].' distribution_channel_id:'.$customMsgPrams['distribution_channel_id']);
  118. continue;
  119. }
  120. $customMsgService = CustomMsgService::addCustomSendMsgs($customMsgPrams);
  121. }
  122. }
  123. }
  124. }