企微短剧业务系统

UserSopService.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\Customer;
  5. use App\Models\CustomerDetails;
  6. use App\Models\DjUser;
  7. use App\Models\System\Users;
  8. use App\Models\UserSop\UserSopMsg;
  9. use App\Models\UserSop\UserSopRecord;
  10. use App\Models\UserSop\UserSopRule;
  11. use App\Support\EmailQueue;
  12. use Illuminate\Support\Facades\DB;
  13. class UserSopService
  14. {
  15. /**
  16. * 配置个人SOP规则
  17. * */
  18. public static function setRule($params, $ruleId)
  19. {
  20. try {
  21. DB::beginTransaction();
  22. # 记录基本信息到dj_user_sop_rule表
  23. $errno = UserSopRule::editRule($params, $ruleId);
  24. if($errno || !$ruleId) {
  25. DB::rollBack();
  26. return $errno;
  27. }
  28. // 处理消息内容
  29. $msgData = json_decode($params['msg_data'], true);
  30. # 提取消息id
  31. $msgIdList = array_column($msgData, 'msg_id');
  32. # 删除用户去除的消息
  33. $count = UserSopMsg::where('rule_id', $ruleId)->where('enable', 1)
  34. ->whereNotIn('id', $msgIdList)->count();
  35. if($count > 0) {
  36. $result = UserSopMsg::where('rule_id', $ruleId)->where('enable', 1)
  37. ->whereNotIn('id', $msgIdList)->update(['enable' => 0]);
  38. if(!$result) {
  39. Log::logError('删除用户去除的消息失败', [
  40. 'params' => $params,
  41. 'rule_id' => $ruleId,
  42. 'result' => $result
  43. ], 'UserSopRuleCreate');
  44. DB::rollBack();
  45. return 5203;
  46. }
  47. }
  48. # 消息内容入库
  49. foreach ($msgData as $index=>$msgInfo) {
  50. $msgId = $msgInfo['msg_id'] ?? null;
  51. $params['sort'] = $index;
  52. $errno = UserSopMsg::editMsg($ruleId, $msgId, $msgInfo, $params);
  53. if($errno) {
  54. Log::logError('消息内容入库失败', [
  55. 'params' => $params,
  56. 'msg_info' => $msgInfo,
  57. 'rule_id' => $ruleId,
  58. 'result' => $errno
  59. ], 'UserSopRuleCreate');
  60. DB::rollBack();
  61. return $errno;
  62. }
  63. }
  64. DB::commit();
  65. } catch (\Exception $e) {
  66. EmailQueue::rPush('配置个人SOP规则发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '配置个人SOP规则发生异常');
  67. Log::logError('配置个人SOP规则发生异常', [
  68. 'line' => $e->getLine(),
  69. 'msg' => $e->getMessage(),
  70. 'param' => $params
  71. ], 'UserSopRuleCreate');
  72. DB::rollBack();
  73. return 5205;
  74. }
  75. return 0;
  76. }
  77. /**
  78. * 获取SOP规则列表
  79. * */
  80. public static function ruleList($corpid, $sysGroupId, $page, $pageSize)
  81. {
  82. # 查询SOP规则列表
  83. list($list, $count) = UserSopRule::getRuleList($corpid, $sysGroupId, $page, $pageSize);
  84. if(!$count) return [[], 0];
  85. # 获取创建成员信息
  86. $adminIds = $list->pluck('admin_id');
  87. $adminData = Users::query()
  88. ->select(['id','name'])
  89. ->whereIn('id', $adminIds)
  90. ->get();
  91. # 获取客服信息
  92. $userData = DjUser::select('user_id', 'name')->where('corpid', $corpid)->where('enable', 1)->get();
  93. foreach ($list as $item) {
  94. # 创建成员
  95. $adminInfo = $adminData->where('id', $item->admin_id)->first();
  96. $item->creator_name = $adminInfo->name ?? null;
  97. # 客服信息
  98. if($item->is_all == 1) {
  99. $item->user_list = '所有';
  100. } else {
  101. $userIds = explode(',', $item->user_ids);
  102. $userList = $userData->whereIn('user_id', $userIds)->pluck('name')->toArray();
  103. $item->user_list = implode(',', $userList);
  104. }
  105. # 规则描述
  106. if($item->type == 1) {
  107. $hour = floor($item->interval_time / 60);
  108. $minute = $item->interval_time % 60;
  109. $item->desc = "添加客服{$hour}小时{$minute}分后提醒发送消息";
  110. } elseif ($item->type == 2) {
  111. $days = floor($item->interval_time / 60 / 24);
  112. $item->desc = "添加客服{$days}天后,当天{$item->notice_time}提醒发送消息";
  113. }
  114. unset($item->corpid, $item->admin_id, $item->is_all, $item->user_ids, $item->type, $item->interval_time, $item->notice_time);
  115. }
  116. return [$list, $count];
  117. }
  118. /**
  119. * 获取配置详情
  120. * */
  121. public static function ruleDetail($corpid, $sysGroupId, $ruleId)
  122. {
  123. # 配置基础信息
  124. $ruleDetail = UserSopRule::select('id', 'title', 'is_all', 'user_ids', 'type', 'interval_time', 'notice_time', 'updated_at', 'status', 'admin_id', 'customer_filter', 'gender', 'tag_screen_type', 'tag_list', 'exclude_tag_list', 'pay_status', 'pay_num_min', 'pay_num_max')
  125. ->where('enable', 1)->where('corpid', $corpid)->where('sys_group_id', $sysGroupId)
  126. ->where('id', $ruleId)->first();
  127. if(empty($ruleDetail)) return [];
  128. # 获取创建者信息
  129. $ruleDetail->creator_name = Users::where('id', $ruleDetail->admin_id)->value('name');
  130. # 规则描述
  131. if($ruleDetail->type == 1) {
  132. $hour = floor($ruleDetail->interval_time / 60);
  133. $minute = $ruleDetail->interval_time % 60;
  134. $ruleDetail->desc = "添加客服{$hour}小时{$minute}分后提醒发送消息";
  135. } elseif ($ruleDetail->type == 2) {
  136. $days = floor($ruleDetail->interval_time / 60 / 24);
  137. $ruleDetail->desc = "添加客服{$days}天后,当天{$ruleDetail->notice_time}提醒发送消息";
  138. }
  139. if($ruleDetail->is_all == 1) {
  140. $ruleDetail->user_list = '所有';
  141. } else {
  142. $userIds = explode(',', $ruleDetail->user_ids);
  143. if(empty($userIds)) {
  144. $ruleDetail->user_list = '未配置';
  145. } else {
  146. $userList = DjUser::select('name')->whereIn('user_id', $userIds)->pluck('name')->toArray();
  147. $ruleDetail->user_list = implode(',', $userList);
  148. }
  149. }
  150. # SOP规则对应的消息内容
  151. $msgList = UserSopMsg::select('id', 'msg_type', 'content')
  152. ->where('rule_id', $ruleId)->where('enable', 1)->where('sys_group_id', $sysGroupId)
  153. ->where('corpid', $corpid)->orderBy('sort')
  154. ->get();
  155. $msgData = [];
  156. foreach ($msgList as $msg) {
  157. $msgData[] = [
  158. 'msg_id' => $msg->id,
  159. 'msg_type' => $msg->msg_type,
  160. 'content' => json_decode($msg->content, true)
  161. ];
  162. }
  163. $ruleDetail->msg_data = $msgData;
  164. return $ruleDetail;
  165. }
  166. /**
  167. * 配置状态变更
  168. * */
  169. public static function ruleStatusChange($corpid, $sysGroupId, $ruleId, $status)
  170. {
  171. $result = UserSopRule::where('enable', 1)->where('corpid', $corpid)->where('sys_group_id', $sysGroupId)
  172. ->where('id', $ruleId)->update(['status' => $status]);
  173. return $result ? 0 : 5206;
  174. }
  175. /**
  176. * 个人SOP规则详情
  177. * */
  178. public static function getDetail($corpid, $ruleId, &$errno)
  179. {
  180. # 规则基本信息
  181. $ruleInfo = UserSopRule::select('admin_id', 'type', 'interval_time', 'notice_time')
  182. ->where('corpid', $corpid)->where('id', $ruleId)
  183. ->where('enable', 1)->where('status', 1)
  184. ->first();
  185. if(empty($ruleInfo)) {
  186. $errno = 5207;
  187. return [];
  188. }
  189. # 获取创建者信息
  190. $creator = Users::where('id', $ruleInfo->admin_id)->value('name');
  191. if($ruleInfo->type == 1) {
  192. $hour = floor($ruleInfo->interval_time / 60);
  193. $minute = $ruleInfo->interval_time % 60;
  194. $desc = $creator . '创建了一条SOP任务,提醒你给已添加' . $hour . '小时'. $minute . '分的客户发送消息';
  195. } else {
  196. $days = floor($ruleInfo->interval_time / 60 / 24);
  197. $desc = $creator . '创建了一条SOP任务,提醒你给已添加' . $days . '天的客户发送消息';
  198. }
  199. # 推送内容
  200. $msgList = UserSopMsg::select('id', 'msg_type', 'content')
  201. ->where('rule_id', $ruleId)->where('corpid', $corpid)
  202. ->where('enable', 1)->orderBy('sort')
  203. ->get();
  204. $msgData = [];
  205. foreach ($msgList as $msg) {
  206. $content = json_decode($msg->content, true);
  207. $content['msg_id'] = $msg->id;
  208. $msgData[] = [
  209. 'msg_id' => $msg->id,
  210. 'msg_type' => $msg->msg_type,
  211. 'content' => $content
  212. ];
  213. }
  214. return ['desc' => $desc, 'msg_list' => $msgData];
  215. }
  216. /**
  217. * 待跟进客户信息
  218. * */
  219. public static function getCustomerList($corpid, $ruleId, $userId, $page, $pageSize)
  220. {
  221. # 判断SOP规则是否可用
  222. $isEnable = UserSopRule::where('id', $ruleId)->where('status', 1)->where('enable', 1)->count();
  223. if(!$isEnable) return [[], 0];
  224. # 待跟进客户
  225. $userSopModel = UserSopRecord::where('rule_id', $ruleId)->where('user_id', $userId)
  226. ->where('status', 2)->orderBy('id', 'desc')->where('enable', 1);
  227. $count = $userSopModel->count();
  228. $externalUserIds = $userSopModel->select('external_userid')
  229. ->limit($pageSize)->offset(($page-1) * $pageSize)
  230. ->pluck('external_userid');
  231. $customerData = CustomerDetails::suffix($corpid)->select('external_userid', 'name', 'createtime')
  232. ->where('corpid', $corpid)->whereIn('external_userid', $externalUserIds)
  233. ->where('user_id', $userId)
  234. ->get();
  235. if(empty($customerData)) return [[], 0];
  236. $customerSourceData = Customer::suffix($corpid)->select('external_userid', 'type', 'avatar')
  237. ->whereIn('external_userid', $externalUserIds)->where('corpid', $corpid)
  238. ->get();
  239. $customerType = Customer::CUSTOMER_TYPE;
  240. foreach ($customerData as $customer) {
  241. $customerInfo = $customerSourceData->where('external_userid', $customer->external_userid)->first();
  242. $customer->createtime = date('Y-m-d H:i:s', $customer->createtime);
  243. $customer->type = $customerInfo->type ?? 0;
  244. $customer->avatar = $customerInfo->avatar ?? '';
  245. $customer->source = isset($customerType[$customer->type]) ? $customerType[$customer->type] : '';
  246. }
  247. return [$customerData, $count];
  248. }
  249. /**
  250. * 跟进数据上报
  251. * */
  252. public static function report($corpid, $ruleId, $userId, $externalUserid)
  253. {
  254. $isExist = UserSopRecord::where('user_id', $userId)->where('corpid', $corpid)
  255. ->where('external_userid', $externalUserid)
  256. ->where('rule_id', $ruleId)->where('status', 2)->count();
  257. if($isExist) {
  258. $result = UserSopRecord::where('user_id', $userId)->where('corpid', $corpid)
  259. ->where('external_userid', $externalUserid)
  260. ->where('rule_id', $ruleId)->update(['status' => 3]);
  261. if(!$result) {
  262. Log::logError('SOP跟进数据上报,状态变更失败', [
  263. 'corpid' => $corpid,
  264. 'rule_id' => $ruleId,
  265. 'user_id' => $userId,
  266. 'external_userid' => $externalUserid
  267. ], 'SopUserReport');
  268. }
  269. }
  270. return 0;
  271. }
  272. }