企微短剧业务系统

PeriodMassMsgService.php 46KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\CustomerDetails;
  5. use App\Models\DjUser;
  6. use App\Models\Es\PeriodMassMsgSendDetailEs;
  7. use App\Models\PeriodMassMsg\PeriodMassMsg;
  8. use App\Models\PeriodMassMsg\PeriodMassMsgConf;
  9. use App\Models\PeriodMassMsg\PeriodMassMsgGroup;
  10. use App\Models\PeriodMassMsg\PeriodMassMsgRecord;
  11. use App\Models\PeriodMassMsg\PeriodMassMsgTemplate;
  12. use App\Models\PeriodMassMsg\PeriodTempGroup;
  13. use App\Models\System\Users;
  14. use App\Support\EmailQueue;
  15. use Illuminate\Support\Facades\DB;
  16. use PhpOffice\PhpSpreadsheet\IOFactory;
  17. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  18. class PeriodMassMsgService
  19. {
  20. public static function setRule($ruleId, $params)
  21. {
  22. try {
  23. # 获取分组对应的客服配置
  24. $userConfig = PeriodMassMsgGroup::where('id', $params['group_id'])->first();
  25. if(empty($userConfig)) return 5224;
  26. if(!$userConfig->corpid) {
  27. $ruleDetail = PeriodMassMsg::where('enable', 1)->where('status', 1)->where('group_id', $params['group_id'])
  28. ->where('corpid', '>', '')->orderBy('updated_at', 'desc')->first();
  29. if(!empty($ruleDetail)) {
  30. $userConfig->operate_type = $ruleDetail->operate_type;
  31. $userConfig->is_operation = $ruleDetail->is_operation;
  32. $userConfig->operator_group_id = $ruleDetail->operator_group_id;
  33. $userConfig->corpid = $ruleDetail->corpid;
  34. $userConfig->is_all = $ruleDetail->is_all;
  35. $userConfig->senders = $ruleDetail->senders;
  36. }
  37. }
  38. $params['operate_type'] = $userConfig->operate_type;
  39. $params['is_operation'] = $userConfig->is_operation;
  40. $params['operator_group_id'] = $userConfig->operator_group_id;
  41. $params['multiple_senders'] = $userConfig->senders;
  42. $params['is_all'] = $userConfig->is_all;
  43. $params['corpid'] = $userConfig->corpid;
  44. if(2 == $params['operate_type']){
  45. if(1 == $params['is_operation']) {
  46. $senders = MassMsgRuleService::getSenderListByOperatorGroupId($params['operator_group_id']);
  47. $params['senders'] = '';
  48. } else {
  49. $senders = json_decode($params['multiple_senders'], 1);
  50. $params['senders'] = $params['multiple_senders'];
  51. }
  52. $corpIdList = MassMsgRuleService::getCorpIdListBySenders($senders);
  53. $params['corpid'] = implode(',', $corpIdList);
  54. } else {
  55. # 校验附件信息合法性
  56. $attachmentsVerifyCode = MassMsgRuleService::attachmentsVerify($params['attachments'], $params['corpid']);
  57. if($attachmentsVerifyCode) return $attachmentsVerifyCode;
  58. $params['senders'] = $userConfig->senders;
  59. }
  60. // if(!$params['is_all'] && !$params['senders'] && !$params['is_operation']) {
  61. // return 2204;
  62. // }
  63. DB::beginTransaction();
  64. # 写入数据到period_mass_msg表
  65. $relationId = PeriodMassMsg::setMsgRelation($ruleId, $params);
  66. if(!$relationId) {
  67. DB::rollBack();
  68. return 5222;
  69. }
  70. # 设置时间规则period_mass_msg_conf表
  71. $confData = json_decode($params['send_conf'], true);
  72. foreach ($confData as $conf) {
  73. $confId = isset($conf['conf_id']) ? $conf['conf_id'] : 0;
  74. $naturalDay = $conf['natural_day'];
  75. $sendTime = $conf['send_time'];
  76. $addStartTime = null;
  77. $addEndTime = null;
  78. if($naturalDay == 1) { // 处理用户添加的时间段设置
  79. $addStartTime = $conf['add_start_time'] ?? null;
  80. $addEndTime = $conf['add_end_time'] ?? null;
  81. }
  82. # 获取下次发送时间
  83. if(strtotime($sendTime) >= time()) {
  84. $nextSendTime = date('Y-m-d') . ' ' . $sendTime;
  85. } else {
  86. $date = date('Y-m-d', strtotime('+1 day'));
  87. $nextSendTime = $date . ' ' . $sendTime;
  88. }
  89. $status = $conf['status'] ?? 1;
  90. $result = PeriodMassMsgConf::setMsg(
  91. $params['group_id'], $params['operate_type'], $confId, $naturalDay, $sendTime, $status, $relationId, $nextSendTime, $addStartTime, $addEndTime
  92. );
  93. if(!$result) {
  94. DB::rollBack();
  95. return 5222;
  96. }
  97. }
  98. DB::commit();
  99. } catch (\Exception $e) {
  100. DB::rollBack();
  101. Log::logError('设置智能群发规则过程发生异常', [
  102. 'line' => $e->getLine(),
  103. 'msg' => $e->getMessage(),
  104. 'data' => $params
  105. ], 'PeriodMassMsgRuleSet-Exception');
  106. return 5223;
  107. }
  108. return 0;
  109. }
  110. /**
  111. * 配置智能群发模板
  112. * */
  113. public static function setTemplate($templateId, $params)
  114. {
  115. # 写入数据到period_mass_msg_template表
  116. $errno = PeriodMassMsgTemplate::setMsgRelation($templateId, $params);
  117. return $errno;
  118. }
  119. /**
  120. * 获取智能群发列表
  121. * @param $groupId string 分组ID
  122. * @param $corpid string 企业ID
  123. * @param $creatorId integer 创建人ID集合
  124. * @param $createTimeStart string 创建时间查询-结束时间
  125. * @param $createTimeEnd string 创建时间查询-结束时间
  126. * @param $page integer 当前页码数
  127. * @param $pageSize integer 每页显示条数
  128. * */
  129. public static function ruleList($groupId, $keyword, $corpid, $creatorId, $createTimeStart,$createTimeEnd, $page, $pageSize, &$errno)
  130. {
  131. try {
  132. list($list, $count) = PeriodMassMsg::getRuleLists(
  133. $groupId, $keyword, $corpid, $creatorId, $createTimeStart, $createTimeEnd, $page, $pageSize
  134. );
  135. # 获取创建人信息
  136. $adminIds = $list->pluck('admin_id');
  137. $adminData = Users::select(['id','name'])->whereIn('id', $adminIds)->get();
  138. # 配置信息
  139. $ruleIds = $list->pluck('rule_id');
  140. $confList = PeriodMassMsgConf::whereIn('rule_id', $ruleIds)->where('status', 1)
  141. ->where('enable', 1)->get();
  142. # 处理数据
  143. foreach($list as $datum) {
  144. # 创建人信息
  145. $adminInfo = $adminData->where('id', $datum->admin_id)->first();
  146. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  147. # 发送配置描述
  148. $desc = '';
  149. $sendConf = $confList->whereIn('rule_id', $datum->rule_id)->all();
  150. foreach ($sendConf as $conf) {
  151. if($conf->natural_day == 1 && $conf->add_start_time && $conf->add_end_time) {
  152. $desc .= '客户加客服第'.$conf->natural_day.'天,添加时间段在【'.$conf->add_start_time . '~' . $conf->add_end_time .'】的用户,当天'.$conf->send_time.'提醒发送;';
  153. } else {
  154. $desc .= '客户加客服第'.$conf->natural_day.'天,当天'.$conf->send_time.'提醒发送;';
  155. }
  156. }
  157. $datum->desc = $desc;
  158. unset($datum->admin_id);
  159. }
  160. } catch (\Exception $e) {
  161. Log::logError('获取智能群发列表过程发生异常', [
  162. 'line' => $e->getLine(),
  163. 'msg' => $e->getMessage(),
  164. ], 'PeriodMassMsgRuleList');
  165. $errno = 2105;
  166. return [[], 0];
  167. }
  168. return [$list, $count];
  169. }
  170. /**
  171. * 智能群发模板列表
  172. * */
  173. public static function templateList($adminId, $sysGroupId, $groupId, $title, $createTimeStart,$createTimeEnd, $status, $page, $pageSize)
  174. {
  175. list($list, $count) = PeriodMassMsgTemplate::getTemplateLists($adminId, $sysGroupId, $groupId, $title, $createTimeStart, $createTimeEnd, $status, $page, $pageSize);
  176. # 处理数据
  177. foreach ($list as $item) {
  178. # 发送配置描述
  179. $desc = '';
  180. $sendConf = json_decode($item->send_conf, true);
  181. foreach ($sendConf as $conf) {
  182. if($conf['natural_day'] == 1 && $conf['add_start_time'] && $conf['add_end_time']) {
  183. $desc .= '客户加客服第'.$conf['natural_day'].'天,添加时间段在【'.$conf['add_start_time'] . '~' . $conf['add_end_time'] .'】的用户,当天'.$conf['send_time'].'提醒发送;';
  184. } else {
  185. $desc .= '客户加客服第'.$conf['natural_day'].'天,当天'.$conf['send_time'].'提醒发送;';
  186. }
  187. }
  188. $item->desc = $desc;
  189. unset($item->send_conf);
  190. }
  191. return [$list, $count];
  192. }
  193. /**
  194. * 获取群发详情
  195. * @param $corpid string 企业ID
  196. * @param $ruleId integer 群发规则ID
  197. * */
  198. public static function ruleDetail($corpid, $ruleId, &$errno)
  199. {
  200. try{
  201. $detail = PeriodMassMsg::selectRaw('id as rule_id, admin_id, corpid, name, is_all, '
  202. .'senders, content, attachments, status, gender, tag_screen_type, tag_list, exclude_tag_list, '
  203. .'pay_status, pay_num_min, pay_num_max, operate_type, operator_group_id, is_operation, group_id')
  204. ->where('id', $ruleId)->where('enable', 1)
  205. ->first();
  206. if(empty($detail)) return [];
  207. # 发送人
  208. if(!$detail->is_all) {
  209. $detail->sender_name = DjUser::query()->where("corpid",$corpid)
  210. ->whereIn("user_id", explode(',',$detail->senders))
  211. ->pluck("name");
  212. } else {
  213. $detail->sender_name = [];
  214. }
  215. # 获取创建人信息
  216. $detail->creator = Users::where('id', $detail->admin_id)->value('name');
  217. $attachments = json_decode($detail->attachments, true);
  218. if(!empty($attachments)) {
  219. foreach ($attachments as $key=>&$attachment) {
  220. if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显
  221. $radarId = $attachment['radar']['radar_id'] ?? 0;
  222. $radarInfo = RadarService::getRadarContent($corpid, $radarId);
  223. if(empty($radarInfo)) {
  224. unset($attachment[$key]);
  225. continue;
  226. }
  227. $attachment['radar'] = $radarInfo;
  228. }
  229. }
  230. }
  231. # 获取发送时间配置
  232. $confList = PeriodMassMsgConf::query()->selectRaw('id as conf_id, natural_day, send_time, status, add_start_time, add_end_time')
  233. ->where('rule_id', $detail->rule_id)->where('status', 1)->where('enable', 1)->get();
  234. $detail->send_conf = $confList;
  235. if($detail->operate_type == 2) {
  236. $detail->multiple_senders = $detail->senders;
  237. }
  238. } catch (\Exception $e) {
  239. Log::logError('智能群发详情获取过程发生异常', [
  240. 'line' => $e->getLine(),
  241. 'msg' => $e->getMessage(),
  242. 'rule_id' => $ruleId
  243. ], 'CircleMassMsgRuleDetail');
  244. $errno = 2104;
  245. return [];
  246. }
  247. return $detail;
  248. }
  249. /**
  250. * 获取模板详情
  251. * */
  252. public static function templateDetail($templateId, $sysGroupId, $adminId, &$errno)
  253. {
  254. $detail = PeriodMassMsgTemplate::selectRaw('id as template_id, title, send_conf, content, attachments, group_id')
  255. ->where('id', $templateId)->where('sys_group_id', $sysGroupId)
  256. ->where('enable', 1)
  257. ->first();
  258. if(empty($detail)) {
  259. $errno = 4831;
  260. return [];
  261. }
  262. return $detail;
  263. }
  264. /**
  265. * 更新群发状态
  266. * */
  267. public static function updateRuleStatus($corpid, $ruleId, $status, $adminId)
  268. {
  269. # 验证规则是否存在
  270. $ruleExist = PeriodMassMsg::whereRaw("FIND_IN_SET('".$corpid."', `corpid`)")->where('id', $ruleId)
  271. ->where('enable', 1)->exists();
  272. if(!$ruleExist) return 2106;
  273. $result = PeriodMassMsg::where('id', $ruleId)->where('status', '!=', $status)->update([
  274. 'status' => $status, 'admin_id' => $adminId
  275. ]);
  276. if(!$result) return 2107;
  277. if($status == 1) { // 分组启用时重置规则下次发送时间
  278. $periodMassMsgConfList = PeriodMassMsgConf::where('rule_id', $ruleId)->where('enable', 1)->get();
  279. foreach ($periodMassMsgConfList as $item) {
  280. $todaySendTime = date('Y-m-d') . ' ' . $item->send_time;
  281. if(strtotime($todaySendTime) >= time()) {
  282. $nextSendTime = $todaySendTime;
  283. } else {
  284. $nextSendTime = date('Y-m-d', strtotime('+1 day')) . ' ' . $item->send_time;
  285. }
  286. PeriodMassMsgConf::where('id', $item->id)->update(['next_send_time' => $nextSendTime, 'rule_status' => $status]);
  287. }
  288. } else {
  289. PeriodMassMsgConf::where('rule_id', $ruleId)->where('status', '!=', $status)->update(['rule_status' => $status]);
  290. }
  291. return 0;
  292. }
  293. /**
  294. * 智能群发模板状态修改
  295. * */
  296. public static function templateChangeStatus($templateId, $adminId, $sysGroupId, $status)
  297. {
  298. # 验证规则是否存在
  299. $templateInfo = PeriodMassMsgTemplate::where('id', $templateId)->where('sys_group_id', $sysGroupId)
  300. ->where('enable', 1)->first();
  301. if(empty($templateInfo)) return 4832;
  302. if($templateInfo->status == $status) return 4834;
  303. # 变更规则状态
  304. $result = PeriodMassMsgTemplate::where('id', $templateId)->where('admin_id', $adminId)
  305. ->update(['status' => $status]);
  306. if(!$result) return 4833;
  307. return 0;
  308. }
  309. /**
  310. * 获取群发列表
  311. * @param $corpid string 企业ID
  312. * @param $ruleId integer
  313. * @param $sendTimeStart string 发送时间查询-起始时间
  314. * @param $sendTimeEnd string 发送时间查询-结束时间
  315. * @param $sortColumn string 排序字段
  316. * @param $sortMethod string 排序方式*
  317. * @param $page integer 当前页码数
  318. * @param $pageSize integer 每页显示条数
  319. * @return mixed
  320. * */
  321. public static function recordList($corpid, $ruleId, $sendTimeStart, $sendTimeEnd, $sortColumn, $sortMethod, $page, $pageSize, &$errno)
  322. {
  323. try {
  324. list($list, $count) = PeriodMassMsgRecord::getRecordLists($corpid, $ruleId, $sendTimeStart, $sendTimeEnd, $sortColumn, $sortMethod, $page, $pageSize);
  325. # 将该企微下的所有客服信息查询出来备用
  326. $adminData = Users::query()->select(['id','name'])->get();
  327. # 处理数据
  328. foreach($list as $datum) {
  329. # 解析json,读取发送规则以及标题
  330. $filterData = json_decode($datum->filter_data, 1);
  331. $adminId = $filterData['admin_id'] ?? 0;
  332. # 创建人信息
  333. $adminInfo = $adminData->where('id', $adminId)->first();
  334. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  335. # 群发标题
  336. $datum->name = $filterData['name'] ?? '';
  337. # 群发规则
  338. $datum->rule_detail = '客户加客服第'.($filterData['natural_day'] ?? '-').'天,当天'.($filterData['time_point'] ?? '-').'提醒发送;';
  339. unset($datum->filter_data);
  340. }
  341. } catch (\Exception $e) {
  342. Log::logError('获取群发列表过程发生异常', [
  343. 'line' => $e->getLine(),
  344. 'msg' => $e->getMessage(),
  345. ], 'CircleMassMsgRuleList');
  346. $errno = 3601;
  347. return [[], 0];
  348. }
  349. return [$list, $count];
  350. }
  351. /**
  352. * 获取群发详情
  353. * @param $corpid string 企业ID
  354. * @param $ruleId integer 群发规则ID
  355. * @param $sendNum integer
  356. * @param $sendDate string
  357. * @return mixed
  358. * */
  359. public static function recordDetail($corpid, $ruleId, $sendNum, $sendDate, &$errno)
  360. {
  361. try{
  362. $detail = [];
  363. # 获取发送记录
  364. $recordList = PeriodMassMsgRecord::selectRaw('corpid, rule_id, send_num, create_time
  365. , send_date, content, attachments, filter_data, status')->where('corpid', $corpid)
  366. ->where('rule_id', $ruleId)->where('send_num', $sendNum)
  367. ->where('send_date', $sendDate)->get();
  368. if($recordList->isEmpty()) return [];
  369. # 判断发送状态
  370. $statusList = array_column($recordList->toArray(), 'status');
  371. foreach($recordList as $recordInfo) {
  372. $detail = json_decode(json_encode($recordInfo), 1);
  373. }
  374. $detail['status'] = self::getRecordSendStatus($statusList);
  375. # 群发规则信息
  376. $ruleInfo = json_decode($detail['filter_data'], 1);
  377. $detail['rule_info'] = $ruleInfo;
  378. if($ruleInfo['is_all'] != 1 && !empty($ruleInfo['senders'])) {
  379. # 发送人
  380. $detail['sender_name'] = DjUser::query()->where("corpid",$corpid)
  381. ->whereIn("user_id",explode(',',$ruleInfo['senders']))
  382. ->pluck("name")->toArray();
  383. }
  384. # 群发规则
  385. $detail['rule_detail'] = '客户加客服第'.($ruleInfo['natural_day'] ?? '-').'天,当天'.($ruleInfo['time_point'] ?? '-').'提醒发送;';
  386. # 获取创建人信息
  387. $detail['creator'] = Users::query()->where('id', $ruleInfo['admin_id'])->value('name');
  388. # 处理附件
  389. $attachments = json_decode($detail['attachments'], 1);
  390. if(!empty($attachments)) {
  391. foreach ($attachments as $key=>&$attachment) {
  392. if(isset($attachment['msgtype']) && $attachment['msgtype'] == 'radar') { // 雷达附件信息回显
  393. $radarId = $attachment['radar']['radar_id'] ?? 0;
  394. $radarInfo = RadarService::getRadarContent($corpid, $radarId);
  395. if(empty($radarInfo)) {
  396. unset($attachment[$key]);
  397. continue;
  398. }
  399. $attachment['radar'] = $radarInfo;
  400. }
  401. }
  402. }
  403. # 消息条数
  404. $contentCount = empty($detail['content']) ? 0 : 1;
  405. $attachmentCount = empty($detail['attachments']) ? 0 : count($attachments);
  406. $detail['msg_count'] = $contentCount + $attachmentCount;
  407. $detail['attachments'] = json_encode($attachments, 256);
  408. unset($detail['filter_data']);
  409. } catch (\Exception $e) {
  410. Log::logError('群发V2详情获取过程发生异常', [
  411. 'line' => $e->getLine(),
  412. 'msg' => $e->getMessage(),
  413. 'trace' => $e->getTraceAsString(),
  414. 'rule_id' => $ruleId,
  415. 'send_date' => $sendDate,
  416. 'send_num' => $sendNum,
  417. ], 'PeriodRecordDetail');
  418. $errno = 3602;
  419. return [];
  420. }
  421. return $detail;
  422. }
  423. public static function overview($corpid, $ruleId, $sendNum, $sendDate)
  424. {
  425. $sendRecordTotal = PeriodMassMsgRecord::where("corpid",$corpid)
  426. ->where("rule_id",$ruleId)->where('send_num', $sendNum)->where('send_date', $sendDate)
  427. ->selectRaw("count(CASE WHEN status = 1 THEN 1 END) as un_sender_count") // 未发送成员
  428. ->selectRaw("count(CASE WHEN status = 2 THEN 1 END) as sender_count") // 已发送成员
  429. ->selectRaw("sum(send_success) as send_success") // 发送成功
  430. ->selectRaw("sum(send_fail) as send_fail") // 未送达人数
  431. ->first();
  432. $sendRecordTotal->send_success = empty($sendRecordTotal->send_success) ? 0 : $sendRecordTotal->send_success;
  433. $sendRecordTotal->send_fail = empty($sendRecordTotal->send_fail) ? 0 : $sendRecordTotal->send_fail;
  434. $retData = [];
  435. #已发送人员
  436. $retData['executed_sender_count'] = $sendRecordTotal->sender_count;
  437. #发送用户
  438. $retData['receive_customer_count'] = $sendRecordTotal->send_success;
  439. #未发送成员
  440. $retData['un_execute_sender_count'] = $sendRecordTotal->un_sender_count;
  441. #未送达客户
  442. $retData['un_receive_customer_count'] = $sendRecordTotal->send_fail ;
  443. #客户接收达上限
  444. $retData['receive_fail_with_limit'] = PeriodMassMsgSendDetailEs::massSendCustCount($ruleId, $sendNum, $sendDate, null,3);
  445. #因为不是好友发送失败
  446. $retData['receive_fail_with_not_friend'] = PeriodMassMsgSendDetailEs::massSendCustCount($ruleId, $sendNum, $sendDate, null,4);
  447. return $retData;
  448. }
  449. public static function senderList($corpid, $ruleId, $sendNum, $sendDate, $type, $keyword, $page, $pageSize)
  450. {
  451. /**已发送成员统计**/
  452. $sendRecord = PeriodMassMsgRecord::where("corpid",$corpid)
  453. ->where("rule_id",$ruleId)->where('send_num', $sendNum)->where('send_date', $sendDate)
  454. ->select(["sender", "filter_data"])
  455. ->selectRaw("min(send_time) as send_time") //发送时间
  456. ->selectRaw("sum(send_fail) as send_fail") //失败次数
  457. ->selectRaw("sum(send_success) as send_success") //成功次数
  458. ->groupBy("sender")
  459. ->get()->toArray();
  460. $ruleInfo = isset($sendRecord[0]['filter_data']) ? json_decode($sendRecord[0]['filter_data'], 1) : [];
  461. $lastSendTime = isset($ruleInfo['natural_day']) ? strtotime('-'.($ruleInfo['natural_day']-1) . ' day', strtotime($sendDate . ' 00:00:00')) : null;
  462. if($lastSendTime) {
  463. $nextSendTime = strtotime('-'.($ruleInfo['natural_day']-1) . ' day', strtotime($sendDate . ' 23:59:59'));
  464. } else {
  465. $nextSendTime = null;
  466. }
  467. $sendRecord = array_column($sendRecord, null, 'sender');
  468. #根据查询类型不同,获取当页展示的成员ID集合
  469. $allSendersIdArr = [];
  470. switch ($type){
  471. case "all": //全部成员
  472. $allSendersIdArr = PeriodMassMsgRecord::select('sender')
  473. ->where("rule_id",$ruleId)
  474. ->where('send_num', $sendNum)
  475. ->where('send_date', $sendDate)
  476. ->pluck('sender');
  477. break;
  478. case "sent": //已发送的成员
  479. $allSendersIdArr = PeriodMassMsgRecord::select('sender')
  480. ->where("rule_id",$ruleId)
  481. ->where('send_num', $sendNum)
  482. ->where('send_date', $sendDate)
  483. ->where('status', 2)
  484. ->pluck('sender');
  485. break;
  486. case "unsent": //未发送的成员
  487. $allSendersIdArr = PeriodMassMsgRecord::select('sender')
  488. ->where("rule_id",$ruleId)
  489. ->where('send_num', $sendNum)
  490. ->where('send_date', $sendDate)
  491. ->where('status', 1)
  492. ->pluck('sender');
  493. break;
  494. case "fail": //发送失败的成员
  495. $allSendersIdArr = PeriodMassMsgRecord::select('sender')
  496. ->where("rule_id",$ruleId)
  497. ->where('send_num', $sendNum)
  498. ->where('send_date', $sendDate)
  499. ->where('status', -1)
  500. ->pluck('sender');
  501. break;
  502. }
  503. if(!empty($allSendersIdArr) && !is_array($allSendersIdArr)) $allSendersIdArr = $allSendersIdArr->toArray();
  504. /**查询发送成员**/
  505. $query = DjUser::query()->where("corpid",$corpid)
  506. ->whereIn("user_id",$allSendersIdArr);
  507. /**关键词查询**/
  508. if(!empty($keyword)){
  509. $query->where("name","like","%$keyword%");
  510. }
  511. $total = $query->count(); //总计总数
  512. $list = $query->select("user_id","name","avatar")
  513. ->offset(($page-1)*$pageSize)
  514. ->limit($pageSize)
  515. ->get()->toArray();
  516. /**查询客服对应客户数**/
  517. $userCustomerCountList = CustomerDetails::suffix($corpid)
  518. ->selectRaw('user_id, count(1) as count')
  519. ->where('loss_status', 1)
  520. ->where('corpid', $corpid)
  521. ->whereIn('user_id', array_column($list,'user_id'))
  522. ->where(function($query) use ($lastSendTime, $nextSendTime) {
  523. if($lastSendTime && $nextSendTime){
  524. $query->where('createtime', '>=', $lastSendTime)
  525. ->where('createtime', '<', $nextSendTime);
  526. }
  527. })
  528. ->groupBy('user_id')
  529. ->get()
  530. ->keyBy('user_id')
  531. ->toArray();
  532. foreach ($list as $k => $item){
  533. #补充最早群发时间
  534. $list[$k]['send_time'] = isset($sendRecord[$item['user_id']]['send_time']) ? $sendRecord[$item['user_id']]['send_time'] : '';
  535. #补充好友数量
  536. $list[$k]['customer_num'] = isset($userCustomerCountList[$item['user_id']]['count']) ? $userCustomerCountList[$item['user_id']]['count'] : 0;
  537. #已送达人数
  538. $list[$k]['send_fail'] = isset($sendRecord[$item['user_id']]['send_fail']) ? $sendRecord[$item['user_id']]['send_fail'] : 0;
  539. #未送达人数
  540. $list[$k]['send_success'] = isset($sendRecord[$item['user_id']]['send_success']) ? $sendRecord[$item['user_id']]['send_success'] : 0;
  541. }
  542. return [$total,$list];
  543. }
  544. /*
  545. * 群发详情-客户详情列表
  546. */
  547. public static function massSendCustList($ruleId, $sendNum, $sendDate, $sender, $name, $type, $page, $pageSize)
  548. {
  549. return PeriodMassMsgSendDetailEs::massSendCustList($ruleId, $sendNum, $sendDate, null, $sender, $name, $type, $page, $pageSize);
  550. }
  551. public static function senderListExport($corpid, $ruleId, $sendNum, $sendDate, $type,$keyword)
  552. {
  553. try {
  554. list($total, $list) = self::senderList($corpid, $ruleId, $sendNum, $sendDate, $type, $keyword, 1, 100000);
  555. $spreadSheet = new Spreadsheet();
  556. $sheet = $spreadSheet->getActiveSheet();
  557. $rowIndex = 1;
  558. $columnIndex = 1;
  559. /**设置标题**/
  560. $title = ['成员昵称', '群发时间', '好友数量', '已送达人数', '发送失败次数'];
  561. foreach ($title as $name) {
  562. $sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $name);
  563. }
  564. /**列表内容**/
  565. foreach ($list as $row => $item) {
  566. $rowIndex++;
  567. $columnIndex = 1; //重置列索引
  568. $sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $item['name']);
  569. $sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $item['send_time']);
  570. $sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $item['customer_num']);
  571. $sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $item['send_success']);
  572. $sheet->setCellValueByColumnAndRow($columnIndex++, $rowIndex, $item['send_fail']);
  573. }
  574. $exportType = [
  575. 'all' => '全部成员',
  576. 'sent' => '已发送成员',
  577. 'unsent' => '未发送成员',
  578. 'fail' => '发送失败成员',
  579. ];
  580. $fileNameExt = isset($exportType[$type]) ? $exportType[$type] : "列表";
  581. $writer = IOFactory::createWriter($spreadSheet, 'Xlsx');
  582. header('Content-Type:application/vnd.ms-excel');
  583. header('Content-Disposition:attachment;filename=客户群发成员详情导出-' . $fileNameExt . '.xlsx');
  584. header('Cache-Control:max-age=0');
  585. $writer->save('php://output');
  586. } catch (\Exception $e) {
  587. $logContent = [
  588. 'params' => "corpid=$corpid , ruleId=$ruleId , sendNum=$sendNum, sendDate=$sendDate, type=$type, keyword=$keyword",
  589. 'msg' => $e->getMessage(),
  590. 'line' => $e->getLine()
  591. ];
  592. EmailQueue::rPush('导出客户群发V2成员详情出现错误', json_encode($logContent), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  593. return false;
  594. }
  595. }
  596. /*
  597. * 群发详情-客户详情列表
  598. **/
  599. public static function massSendCustListExport($ruleId, $sendNum, $sendDate, $sender, $name, $type)
  600. {
  601. try {
  602. $spreadSheet = new Spreadsheet();
  603. $sheet = $spreadSheet->getActiveSheet();
  604. $rowIndex = 1;
  605. $columnIndex = 1;
  606. /**设置标题**/
  607. $title = ['客户昵称','发送成员','送达时间','状态描述'];
  608. foreach ($title as $titleVal){
  609. $sheet->setCellValueByColumnAndRow($columnIndex++,$rowIndex,$titleVal);
  610. }
  611. $page = 1;
  612. $pageSize = 1000;
  613. while (1){
  614. list($list,$total) = PeriodMassMsgSendDetailEs::massSendCustList($ruleId, $sendNum, $sendDate, null
  615. , $sender, $name, $type, $page++, $pageSize);
  616. /**列表内容**/
  617. foreach ($list as $row =>$item){
  618. $rowIndex++;
  619. $columnIndex=1; //重置列索引
  620. $sheet->setCellValueByColumnAndRow($columnIndex++,$rowIndex,$item['external_username']);
  621. $sheet->setCellValueByColumnAndRow($columnIndex++,$rowIndex,$item['sender_name']);
  622. $sheet->setCellValueByColumnAndRow($columnIndex++,$rowIndex,$item['send_time']);
  623. $sheet->setCellValueByColumnAndRow($columnIndex++,$rowIndex,$item['status_description']);
  624. }
  625. if ($total<$pageSize) break; //停止游标获取完整列表
  626. }
  627. $writer = IOFactory::createWriter($spreadSheet, 'Xlsx');
  628. header('Content-Type:application/vnd.ms-excel');
  629. header('Content-Disposition:attachment;filename=客户群发详情导出.xlsx');
  630. header('Cache-Control:max-age=0');
  631. $writer->save('php://output');
  632. } catch(\Exception $e) {
  633. $logContent = [
  634. 'params' => " ruleId=$ruleId , sendNum=$sendNum, sendDate=$sendDate, sender=$sender , name=$name , type=$type",
  635. 'msg' => $e->getMessage(),
  636. 'line' => $e->getLine()
  637. ];
  638. EmailQueue::rPush('导出客户群发V2详情出现错误', json_encode($logContent), ['xiaohua.hou@kuxuan-inc.com'], []);
  639. return false;
  640. }
  641. }
  642. public static function getRecordSendStatus($statusList)
  643. {
  644. if(array_search('-1', $statusList)){// 含有-1(发送失败状态)
  645. return -1;
  646. } else if(array_search('1', $statusList)){// 含有1 (发送中状态)
  647. return 2;
  648. } else {// 全部为2(发送完成状态)
  649. return 3;
  650. }
  651. }
  652. public static function deleteRule($corpid, $ruleId) {
  653. # 验证规则是否存在
  654. $isExist = PeriodMassMsg::query()->where('corpid', $corpid)->where('id', $ruleId)
  655. ->where('enable', 1)->exists();
  656. if(!$isExist) return 2106;
  657. # 变更规则状态
  658. $result = PeriodMassMsg::query()->where('corpid', $corpid)->where('id', $ruleId)
  659. ->update(['enable' => 0, 'status' => 0]);
  660. if(!$result) {
  661. return 2107;
  662. }
  663. PeriodMassMsgConf::query()->where('rule_id', $ruleId)->where('enable', 1)
  664. ->where('status', 1)->update(['enable' => 0, 'status' => 0]);
  665. return 0;
  666. }
  667. /**
  668. * 批量删除智能群发模板
  669. * */
  670. public static function delTemplate($templateIds, $adminId)
  671. {
  672. $templateIds = explode(',', $templateIds);
  673. if(empty($templateIds)) return 4835;
  674. # 执行删除
  675. $result = PeriodMassMsgTemplate::whereIn('id', $templateIds)
  676. ->where('admin_id', $adminId)->where('enable', 1)
  677. ->update(['enable' => 0]);
  678. return $result ? 0 : 4836;
  679. }
  680. /**
  681. * 智能群发分组新建/编辑
  682. * */
  683. public static function editGroup(
  684. $groupId, $groupName, $sysGroupId, $adminId, $operateType, $isOperation, $operatorGroupId, $corpid,
  685. $isAll, $senders, $multipleSenders
  686. )
  687. {
  688. try {
  689. if(2 == $operateType){
  690. if(1 == $isOperation) {
  691. $senderList = MassMsgRuleService::getSenderListByOperatorGroupId($operatorGroupId);
  692. $senders = '';
  693. } else {
  694. $senderList = json_decode($multipleSenders, true);
  695. $senders = $multipleSenders;
  696. }
  697. $corpIdList = MassMsgRuleService::getCorpIdListBySenders($senderList);
  698. $corpid = implode(',', $corpIdList);
  699. }
  700. DB::beginTransaction();
  701. # 创建/编辑 素材组
  702. $errno = PeriodMassMsgGroup::editGroup(
  703. $groupId, $groupName, $sysGroupId, $adminId, $operateType, $isOperation, $operatorGroupId, $corpid, $isAll, $senders
  704. );
  705. if($errno) {
  706. DB::rollBack();
  707. return [0, $errno];
  708. }
  709. # 更新分组内的使用客服数据
  710. PeriodMassMsg::where('group_id', $groupId)->where('enable', 1)->update([
  711. 'operate_type' => $operateType,
  712. 'operator_group_id' => $operatorGroupId,
  713. 'is_operation' => $isOperation,
  714. 'corpid' => $corpid,
  715. 'is_all' => $isAll,
  716. 'senders' => $senders
  717. ]);
  718. # 更新配置表内的发送类型
  719. PeriodMassMsgConf::where('group_id', $groupId)->where('enable', 1)->update(['operate_type' => $operateType]);
  720. DB::commit();
  721. } catch (\Exception $e) {
  722. DB::rollBack();
  723. EmailQueue::rPush('智能群发分组编辑出现异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '猎羽');
  724. Log::logError('智能群发分组编辑出现异常', [
  725. 'line' => $e->getLine(),
  726. 'msg' => $e->getMessage(),
  727. 'group_id' => $groupId,
  728. 'operateType' => $operateType,
  729. 'isOperation' => $isOperation,
  730. 'operatorGroupId' => $operatorGroupId,
  731. 'corpid' => $corpid,
  732. 'isAll' => $isAll,
  733. 'senders' => $senders,
  734. ], 'PeriodEditGroup');
  735. return [0, 4849];
  736. }
  737. return [$groupId, $errno];
  738. }
  739. public static function groupDetail($groupId, $sysGroupId)
  740. {
  741. $detail = PeriodMassMsgGroup::selectRaw('id as group_id, operate_type, is_operation, operator_group_id, corpid, is_all, senders, title')
  742. ->where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)
  743. ->first();
  744. if(!$detail->corpid) { // 历史分组数据没有使用成员信息,需进行补充
  745. $ruleDetail = PeriodMassMsg::where('enable', 1)->where('status', 1)->where('group_id', $groupId)
  746. ->where('corpid', '>', '')->orderBy('updated_at', 'desc')->first();
  747. if(!empty($ruleDetail)) {
  748. $detail->operate_type = $ruleDetail->operate_type;
  749. $detail->is_operation = $ruleDetail->is_operation;
  750. $detail->operator_group_id = $ruleDetail->operator_group_id;
  751. $detail->corpid = $ruleDetail->corpid;
  752. $detail->is_all = $ruleDetail->is_all;
  753. $detail->senders = $ruleDetail->senders;
  754. }
  755. }
  756. return $detail;
  757. }
  758. /**
  759. * 智能群发模板分组新建/编辑
  760. * */
  761. public static function editTempGroup($groupId, $groupName, $sysGroupId, $adminId)
  762. {
  763. # 创建/编辑 素材组
  764. $errno = PeriodTempGroup::editGroup($groupId, $groupName, $sysGroupId, $adminId);
  765. return [$groupId, $errno];
  766. }
  767. /**
  768. * 智能群发分组禁用/启用
  769. * */
  770. public static function changeGroupStatus($groupId, $adminId, $sysGroupId, $status)
  771. {
  772. try {
  773. DB::beginTransaction();
  774. # 修改分组状态
  775. $groupInfo = PeriodMassMsgGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)->first();
  776. if(empty($groupInfo)) {
  777. DB::rollBack();
  778. return 4842;
  779. }
  780. if($groupInfo->status == $status) {
  781. DB::rollBack();
  782. return 4839;
  783. }
  784. $groupInfo->status = $status;
  785. if(!$groupInfo->save()) { // 状态修改失败
  786. DB::rollBack();
  787. return 4840;
  788. }
  789. # 修改分组内规则状态
  790. $ruleExist = PeriodMassMsg::where('group_id', $groupId)->where('status', '!=', $status)->exists();
  791. if($ruleExist) {
  792. PeriodMassMsg::where('group_id', $groupId)->where('status', '!=', $status)->update([
  793. 'status' => $status, 'admin_id' => $adminId
  794. ]);
  795. if($status == 1) { // 分组启用时重置规则下次发送时间
  796. $periodMassMsgConfList = PeriodMassMsgConf::where('group_id', $groupId)->where('enable', 1)->get();
  797. foreach ($periodMassMsgConfList as $item) {
  798. $todaySendTime = date('Y-m-d') . ' ' . $item->send_time;
  799. if(strtotime($todaySendTime) >= time()) {
  800. $nextSendTime = $todaySendTime;
  801. } else {
  802. $nextSendTime = date('Y-m-d', strtotime('+1 day')) . ' ' . $item->send_time;
  803. }
  804. PeriodMassMsgConf::where('id', $item->id)->update(['status' => $status, 'next_send_time' => $nextSendTime, 'rule_status' => $status]);
  805. }
  806. } else {
  807. PeriodMassMsgConf::where('group_id', $groupId)->where('status', '!=', $status)->update(['status' => $status, 'rule_status' => $status]);
  808. }
  809. }
  810. DB::commit();
  811. } catch (\Exception $e) {
  812. DB::rollBack();
  813. Log::logError('智能群发分组状态修改流程出现异常', [
  814. 'line' => $e->getLine(),
  815. 'msg' => $e->getTraceAsString()
  816. ], 'PeriodMsgChangeGroupStatus');
  817. return 4838;
  818. }
  819. return 0;
  820. }
  821. /**
  822. * 模板分组禁用/启用
  823. * */
  824. public static function changeTempGroupStatus($groupId, $adminId, $sysGroupId, $status)
  825. {
  826. try {
  827. DB::beginTransaction();
  828. # 修改分组状态
  829. $groupInfo = PeriodTempGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)->first();
  830. if(empty($groupInfo)) {
  831. DB::rollBack();
  832. return 4842;
  833. }
  834. if($groupInfo->status == $status) {
  835. DB::rollBack();
  836. return 4839;
  837. }
  838. $groupInfo->status = $status;
  839. if(!$groupInfo->save()) { // 状态修改失败
  840. DB::rollBack();
  841. return 4840;
  842. }
  843. # 修改分组内模板状态
  844. $ruleExist = PeriodMassMsgTemplate::where('group_id', $groupId)->where('status', '!=', $status)->exists();
  845. if($ruleExist) {
  846. PeriodMassMsgTemplate::where('group_id', $groupId)->where('status', '!=', $status)->update([
  847. 'status' => $status, 'admin_id' => $adminId
  848. ]);
  849. }
  850. DB::commit();
  851. } catch (\Exception $e) {
  852. DB::rollBack();
  853. Log::logError('智能群发模板分组状态修改流程出现异常', [
  854. 'line' => $e->getLine(),
  855. 'msg' => $e->getTraceAsString()
  856. ], 'PeriodTemplateChangeGroupStatus');
  857. return 4847;
  858. }
  859. return 0;
  860. }
  861. /**
  862. * 删除分组
  863. * */
  864. public static function delGroup($groupId, $adminId, $sysGroupId)
  865. {
  866. try {
  867. DB::beginTransaction();
  868. # 修改分组状态
  869. $groupInfo = PeriodMassMsgGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)->first();
  870. if(empty($groupInfo)) {
  871. DB::rollBack();
  872. return 4842;
  873. }
  874. $groupInfo->enable = 0;
  875. $groupInfo->admin_id = $adminId;
  876. if(!$groupInfo->save()) { // 分组删除失败
  877. DB::rollBack();
  878. return 4843;
  879. }
  880. # 删除分组内规则
  881. $ruleExist = PeriodMassMsg::where('group_id', $groupId)->where('enable', 1)->exists();
  882. if($ruleExist) {
  883. PeriodMassMsg::where('group_id', $groupId)->where('enable', 1)->update(['enable' => 0]);
  884. PeriodMassMsgConf::where('group_id', $groupId)->where('enable', 1)->update(['enable' => 0]);
  885. }
  886. DB::commit();
  887. } catch (\Exception $e) {
  888. DB::rollBack();
  889. Log::logError('智能群发分组状态删除流程出现异常', [
  890. 'line' => $e->getLine(),
  891. 'msg' => $e->getTraceAsString()
  892. ], 'PeriodMsgGroupDel');
  893. return 4844;
  894. }
  895. return 0;
  896. }
  897. /**
  898. * 删除分组
  899. * */
  900. public static function delTempGroup($groupId, $adminId, $sysGroupId)
  901. {
  902. try {
  903. DB::beginTransaction();
  904. # 修改分组状态
  905. $groupInfo = PeriodTempGroup::where('id', $groupId)->where('sys_group_id', $sysGroupId)->where('enable', 1)->first();
  906. if(empty($groupInfo)) {
  907. DB::rollBack();
  908. return 4842;
  909. }
  910. $groupInfo->enable = 0;
  911. $groupInfo->admin_id = $adminId;
  912. if(!$groupInfo->save()) { // 分组删除失败
  913. DB::rollBack();
  914. return 4843;
  915. }
  916. # 删除分组内模板
  917. $ruleExist = PeriodMassMsgTemplate::where('group_id', $groupId)->where('enable', 1)->exists();
  918. if($ruleExist) {
  919. PeriodMassMsgTemplate::where('group_id', $groupId)->where('enable', 1)->update(['enable' => 0, 'admin_id' => $adminId]);
  920. }
  921. DB::commit();
  922. } catch (\Exception $e) {
  923. DB::rollBack();
  924. Log::logError('智能群发分组状态删除流程出现异常', [
  925. 'line' => $e->getLine(),
  926. 'msg' => $e->getTraceAsString()
  927. ], 'PeriodMsgGroupDel');
  928. return 4844;
  929. }
  930. return 0;
  931. }
  932. /**
  933. * 获取分组列表
  934. * */
  935. public static function groupList($sysGroupId, $creatorId, $status, $keyword, $createTimeStart, $createTimeEnd, $page, $pageSize, &$errno)
  936. {
  937. list($list, $count) = PeriodMassMsgGroup::getGroupList(
  938. $sysGroupId, $creatorId, $status, $keyword, $createTimeStart, $createTimeEnd, $page, $pageSize
  939. );
  940. if(!$count) return [[], 0];
  941. # 获取创建人信息
  942. $adminIds = $list->pluck('admin_id');
  943. $adminData = Users::select(['id','name'])->whereIn('id', $adminIds)->get();
  944. foreach ($list as $datum) {
  945. # 创建人信息
  946. $adminInfo = $adminData->where('id', $datum->admin_id)->first();
  947. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  948. unset($datum->admin_id);
  949. }
  950. return [$list, $count];
  951. }
  952. /**
  953. * 获取分组列表
  954. * */
  955. public static function tempGroupList($sysGroupId, $creatorId, $status, $keyword, $createTimeStart, $createTimeEnd, $page, $pageSize, &$errno)
  956. {
  957. list($list, $count) = PeriodTempGroup::getGroupList(
  958. $sysGroupId, $creatorId, $status, $keyword, $createTimeStart, $createTimeEnd, $page, $pageSize
  959. );
  960. if(!$count) return [[], 0];
  961. # 获取创建人信息
  962. $adminIds = $list->pluck('admin_id');
  963. $adminData = Users::select(['id','name'])->whereIn('id', $adminIds)->get();
  964. foreach ($list as $datum) {
  965. # 创建人信息
  966. $adminInfo = $adminData->where('id', $datum->admin_id)->first();
  967. $datum->creator = isset($adminInfo->name) ? $adminInfo->name : '';
  968. unset($datum->admin_id);
  969. }
  970. return [$list, $count];
  971. }
  972. /**
  973. * 批量移动分组
  974. * */
  975. public static function changeGroupBatch($groupId, $sysGroupId, $adminId, $ruleIds)
  976. {
  977. $ruleIds = explode(',', $ruleIds);
  978. if(empty($ruleIds)) return 4841;
  979. # 判断分组是否存在
  980. $isExist = PeriodMassMsgGroup::where('sys_group_id', $sysGroupId)->where('enable', 1)->where('id', $groupId)->exists();
  981. if(!$isExist) return 4842;
  982. # 移动分组
  983. PeriodMassMsg::whereIn('id', $ruleIds)->update(['group_id' => $groupId, 'admin_id' => $adminId]);
  984. PeriodMassMsgConf::whereIn('rule_id', $ruleIds)->update(['group_id' => $groupId]);
  985. return 0;
  986. }
  987. /**
  988. * 批量移动模板分组
  989. * */
  990. public static function changeTempGroupBatch($groupId, $sysGroupId, $adminId, $ruleIds)
  991. {
  992. $ruleIds = explode(',', $ruleIds);
  993. if(empty($ruleIds)) return 4848;
  994. # 判断分组是否存在
  995. $isExist = PeriodTempGroup::where('sys_group_id', $sysGroupId)->where('enable', 1)->where('id', $groupId)->exists();
  996. if(!$isExist) return 4842;
  997. # 移动分组
  998. PeriodMassMsgTemplate::whereIn('id', $ruleIds)->update(['group_id' => $groupId, 'admin_id' => $adminId]);
  999. return 0;
  1000. }
  1001. }