BatchCustomMsgService.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. $isSendCustomer = BatchCustomSendMsgs::isSendCustomerAtSameTimeSexName($customSendMsgsPrams['send_time'],$customSendMsgsPrams['sex'],$customSendMsgsPrams['name']);
  46. if (!empty($isSendCustomer)) {
  47. \Log::info('addBatchCustomSendMsgs already_send'.json_encode($customSendMsgsPrams));
  48. return 4;
  49. }
  50. $batchCustomSendMsgsPrams = array();
  51. $batchCustomSendMsgsPrams['name'] = $customSendMsgsPrams['name'];
  52. $batchCustomSendMsgsPrams['send_time'] = $customSendMsgsPrams['send_time'];
  53. $batchCustomSendMsgsPrams['content'] = $customSendMsgsPrams['content'];
  54. $batchCustomSendMsgsPrams['redirect_url'] = $customSendMsgsPrams['redirect_url'];
  55. $batchCustomSendMsgsPrams['sex'] = $customSendMsgsPrams['sex'];
  56. $batchCustomSendMsgsPrams['subscribe_time'] = $customSendMsgsPrams['subscribe_time'];
  57. $batchCustomSendMsgsPrams['balance'] = $customSendMsgsPrams['balance'];
  58. $batchCustomSendMsgsPrams['order_type'] = $customSendMsgsPrams['order_type'];
  59. $batchCustomSendMsgsPrams['category_id'] = $customSendMsgsPrams['category_id'];
  60. $batchCustomSendMsgsPrams['status'] = $customSendMsgsPrams['status'];
  61. $batchCustomSendMsgsPrams['del_flag'] = 0;
  62. $batch_create_res = BatchCustomSendMsgs::create($customSendMsgsPrams);
  63. $batch_no = isset($batch_create_res->id)?$batch_create_res->id:'';
  64. \Log::info('$batch_create_res:'.$batch_no);
  65. return 1;
  66. } catch (\Exception $e) {
  67. \Log::info('addBatchCustomSendMsgs_ept:'.$e->getMessage());
  68. return 0;
  69. }
  70. }
  71. /**
  72. * 获取批次客服消息列表
  73. */
  74. static function autoSendTrusteeShipCustomerMsg($sex)
  75. {
  76. \Log::info('autoSendTrusteeShipCustomerMsg:'.$sex);
  77. // 每天晚上19点
  78. $customMsgPrams['send_time'] = date('Y-m-d 19:00:00');
  79. $customMsgPrams['name'] = '自动发送托管客服消息:'.$customMsgPrams['send_time'];
  80. $books = [];
  81. // 男生
  82. if($sex == 'a'){
  83. $books = BookConfigService::getTrusteeShipChannelBook('','男频', 4);
  84. }
  85. // 女生
  86. else{
  87. $books = BookConfigService::getTrusteeShipChannelBook('','女频', 4);
  88. }
  89. $books = CustomMsgService::get_book_full_infos($books,$sex);
  90. // 随机推送内容
  91. $customMsgPrams['content'] = json_encode($books);
  92. $customMsgPrams['redirect_url'] = '';
  93. $customMsgPrams['sex'] = $sex;
  94. $customMsgPrams['subscribe_time'] = 'g';// 关注1天前,有过交互的
  95. $customMsgPrams['trusteeship'] = '1'; // 托管
  96. $customMsgPrams['balance'] = 'z';
  97. $customMsgPrams['order_type'] = 'z';
  98. $customMsgPrams['category_id'] = 'z';
  99. $customMsgPrams['status'] = '1';
  100. $customMsgPrams['is_show_list'] = '0';// 分销界面不展示
  101. \Log::info($customMsgPrams);
  102. $batch_no = '';
  103. $batch_res = BatchCustomMsgService::addBatchCustomSendMsgs($customMsgPrams,$batch_no);
  104. \Log::info('batch_res:'.$batch_res.' batch_no:'.$batch_no);
  105. if($batch_res == 1){
  106. $customMsgPrams['batch_no'] = $batch_no;
  107. $official_accounts = OfficialAccountService::getTrusteeshipOfficialAccounts();
  108. \Log::info('getTrusteeshipOfficialAccounts');
  109. \Log::info($official_accounts);
  110. // return true;
  111. foreach($official_accounts as $official_account){
  112. $customMsgPrams['appid'] = $official_account->appid;
  113. $customMsgPrams['distribution_channel_id'] = $official_account->distribution_channel_id;
  114. \Log::info('getTrusteeshipOfficialAccounts:appid:'.$official_account->appid.' distribution_channel_id:'.$official_account->distribution_channel_id);
  115. //先判断在发送时间段1小时以内,有没有发起过相同的模板消息,如果有发送过,就提示用户已经创建过相同模板消息,不创建新的模板消息
  116. $isSendCustomer = CustomMsgService::isSendCustomerAtSameTimeAndSex($customMsgPrams);
  117. if (!empty($isSendCustomer)) {
  118. \Log::info('isSendCustomerAtSameTime already_send:appid:'.$customMsgPrams['appid'].' distribution_channel_id:'.$customMsgPrams['distribution_channel_id']);
  119. continue;
  120. }
  121. $customMsgService = CustomMsgService::addCustomSendMsgs($customMsgPrams);
  122. }
  123. }
  124. }
  125. }