企微短剧业务系统

CircleMassMsgService.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\CircleMassMsg;
  5. use App\Models\DjUser;
  6. use App\Models\System\Users;
  7. class CircleMassMsgService
  8. {
  9. /**
  10. * 设置群发消息规则
  11. * */
  12. public static function setRule($ruleId, $params)
  13. {
  14. try {
  15. # 校验附件信息合法性
  16. $attachmentsVerifyCode = MassMsgRuleService::attachmentsVerify($params['attachments'], $params['corpid']);
  17. if($attachmentsVerifyCode) return $attachmentsVerifyCode;
  18. if($ruleId){ // 编辑群发规则
  19. $ruleInfo = CircleMassMsg::where('enable', 1)->where('id', $ruleId)->first();
  20. if(empty($ruleInfo)) return 5221;
  21. $params['next_send_time'] = CircleMassMsgService::getNextSendTime(
  22. $params['interval_type'], $params['time_points'], $params['interval'], $ruleInfo->last_send_time
  23. );
  24. if(!$params['next_send_time']) return 5220;
  25. $result = CircleMassMsg::where('id', $ruleId)->update($params);
  26. } else {
  27. # 获取下一次发送时间
  28. $lastSendTime = date('Y-m-d H:i:s');
  29. $nextSendTime = CircleMassMsgService::getNextSendTime(
  30. $params['interval_type'], $params['time_points'], $params['interval'], $lastSendTime
  31. );
  32. if(!$nextSendTime) return 5220;
  33. # 设置新群发规则
  34. $massMsgModel = new CircleMassMsg();
  35. $massMsgModel->admin_id = $params['admin_id'];
  36. $massMsgModel->corpid = $params['corpid'];
  37. $massMsgModel->name = $params['name'];
  38. $massMsgModel->last_send_time = $lastSendTime;
  39. $massMsgModel->next_send_time = $nextSendTime;
  40. $massMsgModel->is_all = $params['is_all'];
  41. $massMsgModel->senders = $params['senders'];
  42. $massMsgModel->content = $params['content'];
  43. $massMsgModel->attachments = html_entity_decode($params['attachments']);
  44. $massMsgModel->gender = $params['gender'];
  45. $massMsgModel->interval_type = $params['interval_type'];
  46. $massMsgModel->interval_time = $params['interval_time'];
  47. $massMsgModel->interval = $params['interval'];
  48. $massMsgModel->time_points = $params['time_points'];
  49. $massMsgModel->tag_screen_type = $params['tag_screen_type'];
  50. $massMsgModel->tag_list = $params['tag_list'];
  51. $massMsgModel->exclude_tag_list = $params['exclude_tag_list'];
  52. // 付费情况
  53. $massMsgModel->pay_status = $params['pay_status'];
  54. $massMsgModel->pay_num_min = $params['pay_num_min'];
  55. $massMsgModel->pay_num_max = $params['pay_num_max'];
  56. $result = $massMsgModel->save();
  57. // $ruleId = isset($massMsgModel->id) ? $massMsgModel->id : '';
  58. }
  59. if(!$result) return 5222;
  60. } catch (\Exception $e) {
  61. Log::logError('智能群发规则设置发生异常', [
  62. 'line' => $e->getLine(),
  63. 'msg' => $e->getMessage(),
  64. 'params' => $params
  65. ], 'SetCircleMassMsgRule');
  66. return 5223;
  67. }
  68. return 0;
  69. }
  70. /**
  71. * 获取智能群发列表
  72. * @param $corpid string 企业ID
  73. * @param $creatorId integer 创建人ID集合
  74. * @param $createTimeStart string 创建时间查询-结束时间
  75. * @param $createTimeEnd string 创建时间查询-结束时间
  76. * @param $page integer 当前页码数
  77. * @param $pageSize integer 每页显示条数
  78. * */
  79. public static function ruleList($corpid, $creatorId, $createTimeStart,$createTimeEnd, $page, $pageSize, &$errno)
  80. {
  81. try {
  82. list($list, $count) = CircleMassMsg::getRuleLists($corpid, $creatorId, $createTimeStart, $createTimeEnd, $page, $pageSize);
  83. # 获取创建人信息
  84. $adminIds = $list->pluck('admin_id');
  85. $adminData = Users::select(['id','name'])->whereIn('id', $adminIds)->get();
  86. # 处理数据
  87. foreach($list as $datum) {
  88. # 创建人信息
  89. $adminInfo = $adminData->where('id', $datum->admin_id)->first();
  90. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  91. # 描述信息
  92. $days = floor($datum->interval_time / 60 / 24);
  93. $hour = ($datum->interval_time / 60) % 24;
  94. $minute = $datum->interval_time % 60;
  95. $datum->desc = '客户加客服' .$days. '天' .$hour. '小时' .$minute. '分钟后,';
  96. if($datum->interval_type == 1) {
  97. $datum->desc .= '当天';
  98. $timePoints = json_decode($datum->time_points, true);
  99. foreach ($timePoints as $points) {
  100. $datum->desc .= $points . ' ';
  101. }
  102. $datum->desc .= "提醒发送";
  103. } elseif ($datum->interval_type == 2) {
  104. $datum->desc .= '后台每'.$datum->interval.'小时分批提醒发送';
  105. }
  106. unset($datum->admin_id);
  107. }
  108. } catch (\Exception $e) {
  109. Log::logError('获取智能群发列表过程发生异常', [
  110. 'line' => $e->getLine(),
  111. 'msg' => $e->getMessage(),
  112. ], 'CircleMassMsgRuleList');
  113. $errno = 2105;
  114. return [[], 0];
  115. }
  116. return [$list, $count];
  117. }
  118. /**
  119. * 获取群发详情
  120. * @param $corpid string 企业ID
  121. * @param $ruleId integer 群发规则ID
  122. * */
  123. public static function ruleDetail($corpid, $ruleId, &$errno)
  124. {
  125. try{
  126. $detail = CircleMassMsg::selectRaw('id as rule_id, admin_id, corpid, name, interval_type, interval_time, is_all, senders, content, attachments, status, gender, `interval`, time_points, tag_screen_type, tag_list, exclude_tag_list, pay_status, pay_num_min, pay_num_max')
  127. ->where('corpid', $corpid)->where('id', $ruleId)
  128. ->first();
  129. if(empty($detail)) return [];
  130. # 发送人
  131. if(!$detail->is_all) {
  132. $detail->sender_name = DjUser::query()->where("corpid",$corpid)
  133. ->whereIn("user_id", explode(',',$detail->senders))
  134. ->pluck("name");
  135. } else {
  136. $detail->sender_name = [];
  137. }
  138. # 获取创建人信息
  139. $detail->creator = Users::where('id', $detail->admin_id)->value('name');
  140. $attachments = json_decode($detail->attachments, true);
  141. if(!empty($attachments)) {
  142. foreach ($attachments as $key=>&$attachment) {
  143. if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显
  144. $radarId = $attachment['radar']['radar_id'] ?? 0;
  145. $radarInfo = RadarService::getRadarContent($corpid, $radarId);
  146. if(empty($radarInfo)) {
  147. unset($attachment[$key]);
  148. continue;
  149. }
  150. $attachment['radar'] = $radarInfo;
  151. }
  152. }
  153. }
  154. $detail->time_points = json_decode($detail->time_points);
  155. $detail->attachments = json_encode($attachments, 256);
  156. } catch (\Exception $e) {
  157. Log::logError('智能群发详情获取过程发生异常', [
  158. 'line' => $e->getLine(),
  159. 'msg' => $e->getMessage(),
  160. 'rule_id' => $ruleId
  161. ], 'CircleMassMsgRuleDetail');
  162. $errno = 2104;
  163. return [];
  164. }
  165. return $detail;
  166. }
  167. /**
  168. * 更新群发状态
  169. * */
  170. public static function updateRuleStatus($corpid, $ruleId, $status)
  171. {
  172. # 验证规则是否存在
  173. $isExist = CirCleMassMsg::where('corpid', $corpid)->where('id', $ruleId)->exists();
  174. if(!$isExist) return 2106;
  175. # 变更规则状态
  176. $result = CircleMassMsg::where('corpid', $corpid)->where('id', $ruleId)->update(['status' => $status]);
  177. if(!$result) return 2107;
  178. return 0;
  179. }
  180. /**
  181. * 获取下一次推送时间
  182. * */
  183. public static function getNextSendTime($intervalType, $timePoints, $interval, $lastSendTime)
  184. {
  185. $lastSendTime = strtotime($lastSendTime);
  186. $nextSendTime = null;
  187. if($intervalType == 1) { // 指定时间点检索
  188. $timePointsData = json_decode($timePoints, true);
  189. foreach ($timePointsData as &$timePoint){
  190. $timePoint = strtotime($timePoint);
  191. }
  192. sort($timePointsData);
  193. foreach ($timePointsData as $time) {
  194. if($time>$lastSendTime && $time>=time()) {
  195. $nextSendTime = date('Y-m-d H:i:s', $time);
  196. break;
  197. }
  198. }
  199. if(!$nextSendTime) {
  200. $nextSendTime = date('Y-m-d H:i:s', strtotime('+1 day', $timePointsData[0]));
  201. Log::logInfo('下次推送时间需要到隔天', [
  202. 'intervalType' => $intervalType,
  203. 'timePoints' => $timePoints,
  204. 'interval' => $interval,
  205. 'lastSendTime' => $lastSendTime,
  206. 'nextSendTime' => $nextSendTime
  207. ], 'NextSendTimeTrace');
  208. }
  209. } elseif ($intervalType == 2) {
  210. $nextSendTime = strtotime('+'.$interval.' hour', $lastSendTime);
  211. if($nextSendTime < time())
  212. $nextSendTime = strtotime('+'.$interval.' hour');
  213. $nextSendTime = date('Y-m-d H:i:s', $nextSendTime);
  214. }
  215. Log::logInfo('下次推送时间运算日志', [
  216. 'intervalType' => $intervalType,
  217. 'timePoints' => $timePoints,
  218. 'interval' => $interval,
  219. 'lastSendTime' => $lastSendTime,
  220. 'nextSendTime' => $nextSendTime
  221. ], 'NextSendTimeTrace');
  222. return $nextSendTime;
  223. }
  224. /**
  225. * 获取下一次补充邀请入群时间
  226. * */
  227. public static function getNextSupplementaryInviteTime($intervalType, $timePoints, $interval, $lastSendTime)
  228. {
  229. $lastSendTime = strtotime($lastSendTime);
  230. $nextSendTime = null;
  231. if($intervalType == 1) { // 指定时间点检索
  232. $timePointsData = json_decode($timePoints, true);
  233. foreach ($timePointsData as &$timePoint){
  234. $timePoint = strtotime($timePoint);
  235. }
  236. sort($timePointsData);
  237. foreach ($timePointsData as $time) {
  238. if($time>$lastSendTime && $time>=time()) {
  239. $nextSendTime = date('Y-m-d H:i:s', $time);
  240. break;
  241. }
  242. }
  243. if(!$nextSendTime) {
  244. $nextSendTime = date('Y-m-d H:i:s', strtotime('+'.$interval.' day', $timePointsData[0]));
  245. Log::logInfo('下次推送时间需要到隔天', [
  246. 'intervalType' => $intervalType,
  247. 'timePoints' => $timePoints,
  248. 'interval' => $interval,
  249. 'lastSendTime' => $lastSendTime,
  250. 'nextSendTime' => $nextSendTime
  251. ], 'getNextSupplementaryInviteTimeTrace');
  252. }
  253. } elseif ($intervalType == 2) {
  254. $nextSendTime = strtotime('+'.$interval.' hour', $lastSendTime);
  255. if($nextSendTime < time())
  256. $nextSendTime = strtotime('+'.$interval.' hour');
  257. $nextSendTime = date('Y-m-d H:i:s', $nextSendTime);
  258. }
  259. Log::logInfo('下次推送时间运算日志', [
  260. 'intervalType' => $intervalType,
  261. 'timePoints' => $timePoints,
  262. 'interval' => $interval,
  263. 'lastSendTime' => $lastSendTime,
  264. 'nextSendTime' => $nextSendTime
  265. ], 'getNextSupplementaryInviteTimeTrace');
  266. return $nextSendTime;
  267. }
  268. }