企微短剧业务系统

BatchAddCustomerService.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <?php
  2. namespace App\Service;
  3. use App\Log;
  4. use App\Models\BatchAddCustomerConfig;
  5. use App\Models\Customer\BatchAddCustomerDetails;
  6. use App\Models\DjUser;
  7. use App\Models\System\Users;
  8. use App\RedisModel;
  9. use App\Support\EmailQueue;
  10. use Illuminate\Support\Facades\DB;
  11. class BatchAddCustomerService
  12. {
  13. /**
  14. * 配置自动提醒及自动回收
  15. * @param $configId integer 配置id
  16. * @param $params array
  17. * */
  18. public static function editConfig($corpid, $adminId, $configList, $sysGroupId)
  19. {
  20. try {
  21. // # 获取登录用户所属sys_group_id
  22. // $isSystemAdmin = Users::where('id', $adminId)->where('is_system_admin', 1)->where('enable', 1)->count();
  23. // $sysGroupId = Users::where('enable', 1)
  24. // ->where(function ($query) use($isSystemAdmin, $adminId, $sysGroupId) {
  25. // if($isSystemAdmin) {
  26. // $query->where('group_admin_id', $sysGroupId);
  27. // } else {
  28. // $query->where('id', $adminId);
  29. // }
  30. // })
  31. // ->value('group_admin_id');
  32. if(!$sysGroupId) {
  33. return 1004;
  34. }
  35. DB::begintransaction();
  36. foreach ($configList as $config) {
  37. $configId = $config['config_id'] ?? '';
  38. $config['admin_id'] = $adminId;
  39. $config['corpid'] = $corpid;
  40. $config['sys_group_id'] = $sysGroupId;
  41. $type = $config['type'] ?? 0;
  42. if(!in_array($type, [1, 2, 3])) {
  43. Log::logError('type不合法', [
  44. 'config' => $config
  45. ], 'EditConfig');
  46. continue;
  47. }
  48. $errno = BatchAddCustomerConfig::editConfig($configId, $config);
  49. if($errno) {
  50. Log::logError('配置自动提醒及自动回收失败', [
  51. 'config_id' => $configId,
  52. 'params' => $config
  53. ], 'EditConfig');
  54. DB::rollBack();
  55. return $errno;
  56. }
  57. }
  58. DB::commit();
  59. } catch (\Exception $e) {
  60. Log::logError('配置自动提醒及自动回收过程发生异常', [
  61. 'corpid' => $corpid,
  62. 'admin_id' => $adminId,
  63. 'sys_group_id' => $sysGroupId,
  64. 'params' =>$configList,
  65. 'line' => $e->getLine(),
  66. 'msg' => $e->getMessage()
  67. ], 'EditConfig-Exception');
  68. DB::rollBack();
  69. return 2603;
  70. }
  71. return 0;
  72. }
  73. /**
  74. * 获取配置信息
  75. * */
  76. public static function configInfo($corpid, $sysGroupId, $adminId, &$configInfo)
  77. {
  78. try {
  79. // # 获取登录用户所属sys_group_id
  80. // $isSystemAdmin = Users::where('id', $adminId)->where('is_system_admin', 1)->where('enable', 1)->count();
  81. // $sysGroupId = Users::where('enable', 1)
  82. // ->where(function ($query) use($isSystemAdmin, $sysGroupId, $adminId) {
  83. // if($isSystemAdmin) {
  84. // $query->where('group_admin_id', $sysGroupId);
  85. // } else {
  86. // $query->where('id', $adminId);
  87. // }
  88. //
  89. // })
  90. // ->value('group_admin_id');
  91. if(!$sysGroupId) {
  92. return 1004;
  93. }
  94. $configInfo = array(
  95. 'assigned' => array('enable' => 0),
  96. 'charged' => array('enable' => 0),
  97. 'auto_recycle' => array('enable' => 0)
  98. );
  99. $data = BatchAddCustomerConfig::configInfo($corpid, $sysGroupId);
  100. foreach ($data as $datum) {
  101. switch($datum->type) {
  102. case 1:
  103. $configInfo['assigned'] = $datum;
  104. break;
  105. case 2:
  106. $configInfo['charged'] = $datum;
  107. break;
  108. case 3:
  109. $configInfo['auto_recycle'] = $datum;
  110. break;
  111. }
  112. }
  113. } catch (\Exception $e) {
  114. Log::logError('获取配置信息发生异常', [
  115. 'corpid' => $corpid,
  116. 'admin_id' => $adminId,
  117. 'group_admin_id' => $sysGroupId,
  118. 'line' => $e->getLine(),
  119. 'msg' => $e->getMessage()
  120. ], 'configInfo-Exception');
  121. return 2604;
  122. }
  123. return 0;
  124. }
  125. /**
  126. * 未完成客户添加工作的成员发送消息提醒
  127. * @param $corpid string 企业id
  128. * @param $userIds string 客服id集合
  129. * */
  130. public static function noticeUser($corpid, $userIds)
  131. {
  132. try {
  133. $userIds = array_unique(explode(',', $userIds));
  134. foreach($userIds as $userId) {
  135. $customerCount = BatchAddCustomerDetails::where('corpid', $corpid)
  136. ->where('user_id', $userId)->where('enable', 1)
  137. ->whereIn('add_status', [1,2])->count();
  138. if(!$customerCount) continue;
  139. $timestamps = time();
  140. $url = env('DOMAIN', 'http://dj.wenxingshuju.com/') . 'playlet/qwh5/dist/index.html#/?corpid='.$corpid.'&user_id='.$userId.'&token='.get_token($corpid, $userId, $timestamps) . '&timestamp='.$timestamps;
  141. $content = "【管理员提醒】您有客户未添加哦!\n\n提醒事项:添加客户\n\n客户数量:".$customerCount."名\n\n记得及时添加哦\n\n<a href=\"".$url."\">点击查看详情</a>\n";
  142. # 发送消息提醒
  143. $responseData = ApplicationMsgService::sendTextMsg($corpid, $content, [$userId]);
  144. if(isset($responseData['errcode']) && $responseData['errcode'] != 0) {
  145. $logData = [
  146. 'corpid' => $corpid,
  147. 'user_id' => $userIds,
  148. 'responseData' => $responseData
  149. ];
  150. EmailQueue::rPush('未完成客户添加工作的成员发送提醒失败', json_encode($logData, JSON_UNESCAPED_UNICODE), ['xiaohua.hou@kuxuan-inc.com'], '未完成客户添加工作的成员发送提醒失败');
  151. Log::logError('未完成客户添加工作的成员发送提醒失败', $logData, 'noticeUserFailed');
  152. return 2512;
  153. }
  154. # 记录发送日志
  155. Log::logInfo('未完成客户添加工作的成员消息提醒日志', [
  156. 'corpid' => $corpid,
  157. 'user_id' => $userIds,
  158. 'responseData' => $responseData
  159. ], 'noticeUserLog');
  160. }
  161. } catch (\Exception $e) {
  162. EmailQueue::rPush('未完成客户添加工作的成员消息提醒发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '未完成客户添加工作的成员消息提醒发生异常');
  163. Log::logError('未完成客户添加工作的成员消息提醒发生异常', [
  164. 'corpid' => $corpid,
  165. 'user_id' => $userIds,
  166. 'line' => $e->getLine(),
  167. 'msg' => $e->getMessage()
  168. ], 'noticeUserLog-Exception');
  169. return 2514;
  170. }
  171. return 0;
  172. }
  173. /**
  174. * 获取客服已分配的客户列表
  175. * @param $corpid string 企业ID
  176. * @param $userId string 客服人员ID
  177. * @param $token string 令牌
  178. * @param $timestamp string 时间戳
  179. * @param $addStatus int 添加状态 0已重新分配 1未添加 2待通过 3已添加 4待分配
  180. * */
  181. public static function getCustomerList($corpid, $userId, $token, $timestamp, $addStatus, $page, $pageSize, &$errno)
  182. {
  183. # 校验请求合法性
  184. $authToken = get_token($corpid, $userId, $timestamp);
  185. if($authToken != $token) {
  186. $errno = 1004;
  187. return [[], 0];
  188. }
  189. # 获取已分配的客户列表
  190. list($list, $count) = BatchAddCustomerDetails::getCustomerListOfUser($corpid, $userId, $addStatus, $page, $pageSize);
  191. # 获取用户未完成添加的客户数
  192. $customerTotal = BatchAddCustomerDetails::where('corpid', $corpid)->where('user_id', $userId)->where('enable', 1)
  193. ->whereIn('add_status', [1,2])->count();
  194. return [$list, $count, $customerTotal];
  195. }
  196. /**
  197. * 客服添加客户行为数据上报
  198. * @param $corpid string 企业id
  199. * @param $phone string 目标客户手机号
  200. * @param $userId string 客服id
  201. * @param $recordId int 导入记录ID
  202. * */
  203. public static function dataReporting($corpid, $phone, $userId, $recordId)
  204. {
  205. try {
  206. # 校验数据是否存在
  207. $isExist = BatchAddCustomerDetails::where('corpid', $corpid)
  208. ->where('phone', $phone)->where('record_id', $recordId)
  209. ->where('user_id', $userId)->where('enable', 1)->exists();
  210. if(!$isExist) return 2701;
  211. BatchAddCustomerDetails::where('corpid', $corpid)
  212. ->where('phone', $phone)->where('record_id', $recordId)
  213. ->where('user_id', $userId)->where('enable', 1)->update(['add_status' => 2]);
  214. } catch (\Exception $e) {
  215. EmailQueue::rPush('客户添加客户行为数据上报流程发生异常', $e->getTraceAsString(), ['xiaohua.hou@kuxuan-inc.com'], '客户添加客户行为数据上报流程发生异常');
  216. Log::logError('客户添加客户行为数据上报流程发生异常', [
  217. 'line' => $e->getLine(),
  218. 'msg' => $e->getMessage(),
  219. 'corpid' => $corpid,
  220. 'phone' => $phone,
  221. 'user_id' => $userId,
  222. 'record_id' => $recordId
  223. ], 'DataReporting-Exception');
  224. return 2702;
  225. }
  226. return 0;
  227. }
  228. /**
  229. * 数据概览
  230. * */
  231. public static function dataSummary($corpid, $startDate, $endDate, $sysGroupId)
  232. {
  233. $data = [
  234. 'import_count' => 0,
  235. 'assigned_count' => 0, // 待分配
  236. 'waiting_add_count' => 0, // 待添加
  237. 'passed_count' => 0, // 待通过
  238. 'added_count' => 0, // 已添加
  239. 'added_rate' => 0 . '%' // 添加完成率
  240. ];
  241. try {
  242. $condition = BatchAddCustomerDetails::selectRaw("count(1) as import_count, count(CASE WHEN add_status = 1 THEN 1 END) AS waiting_add_count, count(CASE WHEN add_status = 2 THEN 1 END) AS passed_count, count(CASE WHEN add_status = 3 THEN 1 END) AS added_count,count(CASE WHEN add_status = 4 THEN 1 END) AS assigned_count")
  243. ->where('corpid', $corpid)->where('enable', 1)
  244. ->where('group_admin_id', $sysGroupId)
  245. ->where('import_time', '>=', $startDate . ' 00:00:00')
  246. ->where('import_time', '<=', $endDate . ' 23:59:59')
  247. ->where('add_status', '>', 0)
  248. ->first();
  249. if(empty($condition)) return $data;
  250. $importCount = $condition->import_count ?? 0;
  251. $addedCount = $condition->added_count ?? 0;
  252. $data = [
  253. 'import_count' => $importCount,
  254. 'assigned_count' => $condition->assigned_count ?? 0,
  255. 'waiting_add_count' => $condition->waiting_add_count ?? 0,
  256. 'passed_count' => $condition->passed_count ?? 0,
  257. 'added_count' => $addedCount,
  258. 'added_rate' => $importCount ? (round($addedCount / $importCount, 4) * 100) . '%' : 0 . '%'
  259. ];
  260. } catch (\Exception $e) {
  261. Log::logError('数据概览统计过程发生异常', [
  262. 'line' => $e->getLine(),
  263. 'msg' => $e->getMessage(),
  264. 'param' => [
  265. 'corpid' => $corpid,
  266. 'start_date' => $startDate,
  267. 'end_date' => $endDate
  268. ]
  269. ], 'AddCustomerDataSummary-Exception');
  270. }
  271. return $data;
  272. }
  273. /**
  274. * 批量加好友统计列表
  275. * */
  276. public static function statistic($corpid, $userId, $startDate, $endDate, $sysGroupId, $page, $pageSize)
  277. {
  278. try {
  279. list($list, $count) = BatchAddCustomerDetails::getStatistic($corpid, $userId, $startDate, $endDate, $sysGroupId, $page, $pageSize);
  280. if(empty($list)) return [[], 0];
  281. $userIds = $list->pluck('user_id');
  282. # 获取客服信息
  283. $userList = DjUser::select(['name', 'user_id'])->whereIn('user_id', $userIds)->where('corpid', $corpid)->get();
  284. foreach ($list as $item) {
  285. # 计算添加完成率
  286. $assignCount = $item->assign_count ?? 0;
  287. $addedCount = $item->added_count ?? 0;
  288. $item->add_rate = $assignCount ? (round($addedCount / $assignCount, 4) * 100) . '%' : 0 . '%';
  289. # 客服名称
  290. $userInfo = $userList->where('user_id', $item->user_id)->first();
  291. $item->name = $userInfo->name ?? '-';
  292. }
  293. } catch (\Exception $e) {
  294. Log::logError('批量加好友列表统计过程发生异常', [
  295. 'line' => $e->getLine(),
  296. 'msg' => $e->getMessage(),
  297. 'param' => [
  298. 'corpid' => $corpid,
  299. 'user_id' => $userId,
  300. 'page' => $page,
  301. 'page_size' => $pageSize
  302. ]
  303. ], 'AddCustomerStatistic-Exception');
  304. return [[], 0];
  305. }
  306. return [$list, $count];
  307. }
  308. /**
  309. * 通过手机号添加的客户信息入打标签队列
  310. * */
  311. public static function addTagForCustomer($changeType, $corpid, $followInfo, $externalContact)
  312. {
  313. $changeTypeAllowList = ['add_external_contact', 'add_half_external_contact'];
  314. $addWay = isset($followInfo['add_way']) ? $followInfo['add_way'] : null;
  315. $remarkMobiles = isset($followInfo['remark_mobiles']) ? implode(',', $followInfo['remark_mobiles']) : null;
  316. if($addWay != 2 || empty($remarkMobiles)) {
  317. Log::logInfo('通过手机号添加的客户信息入打标签队列成功', [
  318. 'change_type' => $changeType,
  319. 'corpid' => $corpid,
  320. 'followInfo' => $followInfo,
  321. 'externalContact' => $externalContact,
  322. 'addWay' => $addWay,
  323. 'remark_mobiles' => $remarkMobiles
  324. ], 'addTagForCustomerQueue');
  325. return '';
  326. }
  327. try{
  328. if (!is_null($changeType) && in_array($changeType, $changeTypeAllowList)) {
  329. $data = [
  330. 'corpid' => $corpid,
  331. 'user_id' => $followInfo['userid'],
  332. 'external_userid' => $externalContact['external_userid'],
  333. 'remark_mobiles' => $remarkMobiles,
  334. 'createtime' => isset($followInfo['createtime']) ? $followInfo['createtime'] : null,
  335. ];
  336. RedisModel::lPush(BatchAddCustomerDetails::BATCH_ADD_CUSTOMER_TAG_CREATE_RDS, json_encode($data));
  337. Log::logInfo('通过手机号添加的客户信息入打标签队列成功', $data, 'addTagForCustomerQueue');
  338. }
  339. } catch(\Exception $e) {
  340. Log::logError('通过手机号添加的客户信息入打标签队列时发生异常', [
  341. 'msg' => $e->getMessage(),
  342. 'line' => $e->getLine(),
  343. 'changeType' => $changeType,
  344. 'corpid' => $corpid,
  345. 'followInfo' => $followInfo,
  346. 'externalContact' => $externalContact,
  347. ], 'addTagForCustomerQueue-Exception');
  348. }
  349. }
  350. }