企微短剧业务系统

ServiceDataService.php 7.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace App\Service\Report;
  3. use App\Models\ChatGroupMassMsg;
  4. use App\Models\ChatGroupMassMsgLog;
  5. use App\Models\ChatGroupMassMsgRecord;
  6. use App\Models\Es\ChatGroupMassMsgLogEs;
  7. use App\Models\MassMsg;
  8. use App\Models\MassMsgRecord;
  9. use App\Models\PeriodMassMsg\PeriodMassMsg;
  10. use App\Models\PeriodMassMsg\PeriodMassMsgGroup;
  11. use App\Models\PeriodMassMsg\PeriodMassMsgRecord;
  12. use App\Models\System\Users;
  13. use App\Service\StatisticsService;
  14. class ServiceDataService
  15. {
  16. # 客户群发消息送达数据
  17. public static function massMsgServiceData($creatorId, $sendTimeStart, $sendTimeEnd, $keyword, $sysGroupId, $page
  18. , $pageSize) {
  19. $corpids = StatisticsService::getAuthCorpids($sysGroupId);
  20. if(empty($corpids)) {
  21. return [];
  22. }
  23. # 查询总计数据
  24. $totalData = MassMsg::getServiceDataTotal($corpids, $creatorId, $sendTimeStart, $sendTimeEnd, $keyword);
  25. # 查询列表数据
  26. list($list, $count) = MassMsg::getServiceDataLists($corpids, $creatorId, $sendTimeStart, $sendTimeEnd, $keyword
  27. , $page, $pageSize);
  28. # 获取创建人信息
  29. $adminIds = $list->pluck('admin_id');
  30. $adminData = Users::query()->select(['id','name'])->whereIn('id', $adminIds)->get();
  31. # 统计发送信息
  32. $ruleIds = $list->pluck('rule_id');
  33. $sendStatData = MassMsgRecord::query()->selectRaw("rule_id, sum(send_success) as send_success, "
  34. ."sum(send_fail) as send_fail, sum(send_total) as send_total, count(CASE WHEN status =-1 AND errcode != 41048"
  35. ." AND errcode!= 84061 THEN 1 END) as send_fail_user")
  36. ->where('type', 1)->whereIn('rule_id', $ruleIds)->groupBy(['rule_id'])->get();
  37. # 列表数据格式化
  38. foreach($list as $info) {
  39. # 创建人信息
  40. $adminInfo = $adminData->where('id', $info->admin_id)->first();
  41. $info->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  42. # 统计发送情况
  43. $sendStatInfo = $sendStatData->where('rule_id', $info->rule_id)->first();
  44. $info->send_success = isset($sendStatInfo->send_success) ? $sendStatInfo->send_success : 0;
  45. $info->send_fail = isset($sendStatInfo->send_fail) ? $sendStatInfo->send_fail : 0;
  46. $info->send_total = isset($sendStatInfo->send_total) ? $sendStatInfo->send_total : 0;
  47. $info->send_fail_user = isset($sendStatInfo->send_fail_user) ? $sendStatInfo->send_fail_user : 0;
  48. }
  49. return [['total' => $totalData, 'list' => $list], $count];
  50. }
  51. # 客户群群发消息送达数据
  52. public static function chatGroupMassMsgServiceData($creatorId, $sendTimeStart, $sendTimeEnd, $title, $sysGroupId
  53. , $page, $pageSize) {
  54. $corpids = StatisticsService::getAuthCorpids($sysGroupId);
  55. if(empty($corpids)) {
  56. return [];
  57. }
  58. # 查询总计数据
  59. $totalData = ChatGroupMassMsg::getServiceDataTotal($corpids, $creatorId, $sendTimeStart, $sendTimeEnd, $title);
  60. # 查询列表数据
  61. list($list, $count) = ChatGroupMassMsg::getServiceDataList($corpids, $creatorId, $sendTimeStart, $sendTimeEnd
  62. , $title, $page, $pageSize);
  63. # 获取创建人信息
  64. $adminIds = $list->pluck('admin_id');
  65. $adminData = Users::query()->select(['id','name'])->whereIn('id', $adminIds)->get();
  66. # 统计发送信息
  67. $ruleIds = $list->pluck('rule_id');
  68. $sendStatData = ChatGroupMassMsgRecord::query()->selectRaw("rule_id, count(CASE WHEN status=2 THEN 1 END) AS "
  69. ."send_user_success, count(CASE WHEN status !=2 THEN 1 END) AS send_user_fail")
  70. ->whereIn('rule_id', $ruleIds)->groupBy(['rule_id'])->get();
  71. // $sendChatData = ChatGroupMassMsgLog::query()->selectRaw("rule_id, count(CASE WHEN status=1 THEN 1 END) AS "
  72. // ."send_chat_success, count(CASE WHEN status !=1 THEN 1 END) AS send_chat_fail, sum(member_count) as member_count")
  73. // ->whereIn('rule_id', $ruleIds)->groupBy(['rule_id'])->get();
  74. $sendChatData = ChatGroupMassMsgLogEs::getChatGroupData($ruleIds);
  75. # 列表数据格式化
  76. foreach($list as $info) {
  77. # 创建人信息
  78. $adminInfo = $adminData->where('id', $info->admin_id)->first();
  79. $info->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  80. # 统计发送情况
  81. $sendStatInfo = $sendStatData->where('rule_id', $info->rule_id)->first();
  82. $info->send_user_success = isset($sendStatInfo->send_user_success) ? $sendStatInfo->send_user_success : 0;
  83. $info->send_user_fail = isset($sendStatInfo->send_user_fail) ? $sendStatInfo->send_user_fail : 0;
  84. // $sendChatInfo = $sendChatData->where('rule_id', $info->rule_id)->first();
  85. $sendChatInfo = $sendChatData[$info->rule_id] ?? [];
  86. $info->member_count = isset($sendChatInfo['member_count']) ? $sendChatInfo['member_count'] : 0;
  87. $info->send_chat_success = isset($sendChatInfo['send_chat_success']) ? $sendChatInfo['send_chat_success'] : 0;
  88. $info->send_chat_fail = isset($sendChatInfo['send_chat_fail']) ? $sendChatInfo['send_chat_fail'] : 0;
  89. }
  90. return [['total' => $totalData, 'list' => $list], $count];
  91. }
  92. # 智能群发消息送达数据
  93. public static function periodMassMsgServiceData($creatorId, $sendTimeStart, $sendTimeEnd, $keyword, $sysGroupId
  94. , $page, $pageSize) {
  95. $corpids = StatisticsService::getAuthCorpids($sysGroupId);
  96. if(empty($corpids)) {
  97. return [];
  98. }
  99. $ruleIdList = [];
  100. if(!empty($keyword) || !empty($creatorId)) {
  101. $ruleIdList = PeriodMassMsg::getServiceDataList($corpids, $creatorId, $keyword);
  102. if(empty($ruleIdList)) {
  103. return [['total' => ['send_success' => 0, 'send_fail' => 0], 'list' => []], 0];
  104. }
  105. }
  106. # 查询总计数据
  107. $totalData = PeriodMassMsgRecord::getServiceDataTotal($corpids, $sendTimeStart, $sendTimeEnd, $ruleIdList);
  108. # 查询列表数据
  109. list($list, $count) = PeriodMassMsgRecord::getServiceDataList($corpids, $sendTimeStart, $sendTimeEnd
  110. , $ruleIdList, $page, $pageSize);
  111. # 获取创建人信息
  112. $adminData = Users::query()->select(['id','name'])->get();
  113. # 获取分组信息
  114. $ruleIds = $list->pluck('rule_id');
  115. $ruleData = PeriodMassMsg::getInfoById($ruleIds);
  116. $groupIds = array_column($ruleData->toArray(), 'group_id');
  117. $groupData = PeriodMassMsgGroup::getInfoById($groupIds);
  118. # 列表数据格式化
  119. foreach($list as $info) {
  120. # 获取群发规则信息以及创建人信息
  121. $ruleInfo = $ruleData->where('id', $info->rule_id)->first();
  122. $adminId = isset($ruleInfo->admin_id) ? $ruleInfo->admin_id : null;
  123. if($adminId) {
  124. $adminInfo = $adminData->where('id', $adminId)->first();
  125. }
  126. $info->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  127. $info->name = isset($ruleInfo->name) ? $ruleInfo->name : '';
  128. # 获取群发分组信息
  129. $groupId = isset($ruleInfo->group_id) ? $ruleInfo->group_id : 0;
  130. $ruleGroupInfo = $groupData->where('id', $groupId)->first();
  131. $info->group_name = isset($ruleGroupInfo->title) ? $ruleGroupInfo->title : '';
  132. }
  133. return [['total' => $totalData, 'list' => $list], $count];
  134. }
  135. }