企微短剧业务系统

AddCustomerService.php 33KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: shensong
  5. * Date: 2022/6/7
  6. * Time: 11:35
  7. */
  8. namespace App\Service;
  9. use App\Log;
  10. use App\Models\BatchAddCustomerConfig;
  11. use App\Models\Customer\BatchAddCustomerDetails;
  12. use App\Models\Customer\BatchAddCustomerRecord;
  13. use App\Models\CustomerDetails;
  14. use App\Models\DjUser;
  15. use App\Models\System\Users;
  16. use App\Models\Tag;
  17. use App\RedisModel;
  18. class AddCustomerService
  19. {
  20. // 批量添加客户导入方法
  21. public static function batchAddCustomerImport($originalFileName, $newFileName, $ext, $corpid, $userIdList, $customerTagList,
  22. $sysGroupId)
  23. {
  24. $admin = \Auth::user();
  25. $adminId = $admin->id;
  26. $requestData = [
  27. 'admin_id' => $adminId,
  28. 'user_id_list' => $userIdList,
  29. 'corpid' => $corpid,
  30. 'customer_tag_list' => $customerTagList,
  31. 'original_file_name' => $originalFileName,
  32. 'new_file_name' => $newFileName,
  33. 'ext' => $ext,
  34. 'sys_group_id' => $sysGroupId,
  35. ];
  36. $fileName = $newFileName;
  37. // $fileName = ImportService::getFileNameByType(1, $corpid, $adminId, $ext);
  38. if(empty($fileName)) {
  39. Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
  40. 'message' => '未找到本地文件',
  41. ], 'interface');
  42. return ['code' => 3303, 'info' => '系统异常,读取缓存数据失败'];
  43. }
  44. $filePath = '../storage/uploads/'.$fileName;
  45. try{
  46. // 读取文件内容
  47. $objPHPExcel = @\PHPExcel_IOFactory::load($filePath);
  48. $sheet = $objPHPExcel->getSheet(0); // 读取第一个工作表
  49. $highestRow = $sheet->getHighestRow(); // 取得总行数
  50. $phoneArr = []; // 已经导入的手机号,判断是否重复导入使用
  51. $data = [];// 导入的数据
  52. $preg_phone='/^1[345789]\d{9}$/ims';
  53. $errorArr = [];
  54. $breakCount = 0;
  55. for ($row = 6; $row <= $highestRow; $row ++) { // 从第6行开始读取数据,第1行为表头,无需处理
  56. $item = [];
  57. $item['phone'] = trim($sheet->getCell('A' . $row)->getFormattedValue());
  58. $item['remark'] = trim($sheet->getCell('B' . $row)->getFormattedValue());
  59. # 当获取手机号连续3行均为空时停止读取
  60. if(empty($item['phone'])) {
  61. if($breakCount <= 3) {
  62. $breakCount++;
  63. continue;
  64. } else {
  65. break;
  66. }
  67. } else {
  68. $breakCount = 0;
  69. }
  70. // 验证手机号格式
  71. if(!preg_match($preg_phone, $item['phone'])) {
  72. $errorArr[] = [
  73. 'row' => $row,
  74. 'error' => ['手机号格式不合法'],
  75. ];
  76. }
  77. // 本次导入的手机号去重
  78. if(!in_array($item['phone'], $phoneArr)) {
  79. $phoneArr[] = $item['phone'];
  80. $data[] = $item;
  81. }
  82. }
  83. if(!empty($errorArr)) {
  84. Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
  85. 'error_arr' => $errorArr,
  86. 'message' => '导入数据格式不合法',
  87. ], 'interface');
  88. return ['info' => $errorArr, 'code' => 3304];
  89. }
  90. // 将客户平均分配给客服
  91. $saveDetailsData = BatchAddCustomerDetails::distributionCustomer($data, $userIdList, $corpid, $sysGroupId);
  92. $count = count($data) - count($saveDetailsData);
  93. $cacheData['save_details_data'] = $saveDetailsData;
  94. $cacheData['original_file_name'] = $originalFileName;
  95. $cacheData['file_path'] = $filePath;
  96. $cacheData['ext'] = $ext;
  97. $cacheData['customer_tag_list'] = $customerTagList;
  98. $cacheData['user_id_list'] = $userIdList;
  99. if($count > 0) {
  100. // 与之前导入的手机号有重复,将本次导入的数据缓存入redis中,并返回提示
  101. RedisModel::set(BatchAddCustomerRecord::IMPORT_DATA.'-'.$fileName, json_encode($cacheData, 256));
  102. return ['info' => ['count' => $count, 'num' => count($saveDetailsData)], 'code' => 3301];
  103. } else {
  104. // 将文件上传oss,然后调用保存数据方法
  105. $oss = new OssService('playlet');
  106. $ossFile = $oss->upload($ext, $filePath, 'excel/'.$ext.'/'.date("Y-m-d",time()));
  107. $fileUrl = $ossFile['oss-request-url'];
  108. $importTime = date('Y-m-d H:i:s');
  109. \DB::begintransaction();// 开启事务
  110. # 调用保存数据方法
  111. // 保存导入记录
  112. $saveRecordResult = BatchAddCustomerRecord::insertData($importTime, $originalFileName, $userIdList,
  113. $customerTagList, $adminId, $corpid, $fileUrl, $sysGroupId);
  114. $recordId = isset($saveRecordResult->id) ? $saveRecordResult->id : null;
  115. if(!empty($recordId)) {
  116. // 保存导入客户
  117. $saveDetailsResult = BatchAddCustomerDetails::insertData($saveDetailsData, $corpid, $adminId,
  118. $importTime, $recordId, $customerTagList, $sysGroupId);
  119. } else {
  120. $saveDetailsResult = false;
  121. }
  122. if($saveDetailsResult && $saveRecordResult) {
  123. \DB::commit();
  124. // 删除本地文件
  125. unlink($filePath);
  126. // 在提醒队里中插入数据
  127. $remindUserList = array_unique(array_column($saveDetailsData, 'user_id'));
  128. foreach($remindUserList as $userId) {
  129. if(empty($userId)) {
  130. continue;
  131. }
  132. RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
  133. 'corpid' => $corpid,
  134. 'user_id' => $userId
  135. ]));
  136. }
  137. return ['info' => '导入成功', 'code' => 0];
  138. } else {
  139. Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
  140. 'massage' => '数据保存失败',
  141. 'record_result' => $saveRecordResult,
  142. 'details_result' => $saveDetailsResult,
  143. ], 'interface');
  144. \DB::rollBack();
  145. return ['info' => '导入失败', 'code' => 400];
  146. }
  147. }
  148. } catch (\Exception $exception) {
  149. # 打印错误日志
  150. Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
  151. 'file' => $exception->getFile(),
  152. 'line' => $exception->getLine(),
  153. 'message' => $exception->getMessage(),
  154. 'trace' => $exception->getTraceAsString(),
  155. ], 'interface');
  156. return ['info' => '请求失败,请联系管理员', 'code' => 400];
  157. }
  158. }
  159. // 批量添加客户导入--二次确认
  160. public static function batchAddCustomerImportConfirm($corpid, $confirm, $newFileName, $sysGroupId)
  161. {
  162. $admin = \Auth::user();
  163. $adminId = $admin->id;
  164. $requestData = [
  165. 'corpid' => $corpid,
  166. 'confirm' => $confirm,
  167. 'admin_id' => $adminId,
  168. 'sys_group_id' => $sysGroupId,
  169. ];
  170. try{
  171. // 判断客户选择
  172. if( 1 == $confirm){// 确认跳过重复客户导入
  173. $importTime = date('Y-m-d H:i:s');
  174. // 从redis缓存中读取数据
  175. $cacheData = RedisModel::get(BatchAddCustomerRecord::IMPORT_DATA.'-'.$newFileName);
  176. if(empty($cacheData)) {
  177. Log::logError('batchAddCustomerImportConfirm 参数:'.json_encode($requestData, 256), ['读取缓存数据失败'], 'interface');
  178. return ['code' => 3303, 'info' => '系统异常,读取缓存数据失败'];
  179. }
  180. $cacheData = json_decode($cacheData, true);
  181. $ext = $cacheData['ext'];
  182. $filePath = $cacheData['file_path'];
  183. $originalFileName = $cacheData['original_file_name'];
  184. $customerTagList = $cacheData['customer_tag_list'];
  185. $userIdList = $cacheData['user_id_list'];
  186. $saveDetailsData = $cacheData['save_details_data'];
  187. // 本地文件上传到阿里云
  188. $oss = new OssService('playlet');
  189. $ossFile = $oss->upload($ext, $filePath, 'excel/'.$ext.'/'.date("Y-m-d",time()));
  190. $fileUrl = $ossFile['oss-request-url'];
  191. // 保存缓存数据到本地
  192. \DB::begintransaction();// 开启事务
  193. # 调用保存数据方法
  194. // 保存导入记录
  195. $saveRecordResult = BatchAddCustomerRecord::insertData($importTime, $originalFileName, $userIdList,
  196. $customerTagList, $adminId, $corpid, $fileUrl, $sysGroupId);
  197. $recordId = isset($saveRecordResult->id) ? $saveRecordResult->id : null;
  198. if(!empty($recordId)) {
  199. // 保存导入客户
  200. $saveDetailsResult = BatchAddCustomerDetails::insertData($saveDetailsData, $corpid, $adminId, $importTime,
  201. $recordId, $customerTagList, $sysGroupId);
  202. } else {
  203. $saveDetailsResult = false;
  204. }
  205. if($saveDetailsResult && $saveRecordResult) {
  206. \DB::commit();
  207. // 删除本地文件
  208. unlink($filePath);
  209. RedisModel::expire(BatchAddCustomerRecord::IMPORT_DATA.'-'.$newFileName, 0);
  210. // 在提醒队里中插入数据
  211. $remindUserList = array_unique(array_column($saveDetailsData, 'user_id'));
  212. foreach($remindUserList as $userId) {
  213. if(empty($userId)) {
  214. continue;
  215. }
  216. RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
  217. 'corpid' => $corpid,
  218. 'user_id' => $userId
  219. ]));
  220. }
  221. return ['info' => '导入成功', 'code' => 0];
  222. } else {
  223. Log::logError('batchAddCustomerImportConfirm 参数:'.json_encode($requestData, 256), [
  224. 'massage' => '数据保存失败',
  225. 'record_result' => $saveRecordResult,
  226. 'details_result' => $saveDetailsResult,
  227. ], 'interface');
  228. \DB::rollBack();
  229. return ['info' => '', 'code' => 400];
  230. }
  231. } else {// 取消本次导入
  232. $filePath = '../storage/uploads/'.$newFileName;
  233. // 删除本地文件
  234. unlink($filePath);
  235. // 删除redis缓存
  236. RedisModel::expire(BatchAddCustomerRecord::IMPORT_DATA.'-'.$newFileName, 0);
  237. return ['info' => '操作成功', 'code' => 0];
  238. }
  239. } catch (\Exception $exception) {
  240. # 打印错误日志
  241. Log::logError('batchAddCustomerImportConfirm 参数:'.json_encode($requestData, 256), [
  242. 'file' => $exception->getFile(),
  243. 'line' => $exception->getLine(),
  244. 'message' => $exception->getMessage(),
  245. 'trace' => $exception->getTraceAsString(),
  246. ], 'interface');
  247. return ['info' => '请求失败,请联系管理员', 'code' => 400];
  248. }
  249. }
  250. // 导入客户列表
  251. public static function batchAddCustomerDetailsList($corpid, $addStatus, $keyword, $startTime, $endTime, $page,
  252. $pageSize, $sysGroupId)
  253. {
  254. $requestData = [
  255. 'corpid' => $corpid,
  256. 'add_status' => $addStatus,
  257. 'keyword' => $keyword,
  258. 'start_time' => $startTime,
  259. 'end_time' => $endTime,
  260. 'page' => $page,
  261. 'page_size' => $pageSize,
  262. 'sys_group_id' => $sysGroupId
  263. ];
  264. try{
  265. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
  266. $startTime, $endTime, $keyword, null, null, null);
  267. $count = $batchAddCustomerDetailsQuery->count();
  268. $data = $batchAddCustomerDetailsQuery->select(['id', 'phone', 'remark', 'customer_tag_list', 'user_id',
  269. 'add_status', 'import_time', 'distribution_num', 'add_time', 'admin_id', 'external_userid'])
  270. ->orderBy('id', 'desc')
  271. ->offset(($page - 1) * $pageSize)
  272. ->limit($pageSize)
  273. ->get();
  274. // 查询企业下所有标签列表
  275. $tagList = Tag::query()
  276. ->where('corpid', $corpid)
  277. ->where('enable', 1)
  278. ->get();
  279. // 提取所有客服id
  280. $userIdList = array_unique(array_column($data->toArray(), 'user_id'));
  281. $userInfoList = DjUser::query()
  282. ->where('corpid', $corpid)
  283. ->where('enable', 1)
  284. ->whereIn('user_id', $userIdList)
  285. ->get();
  286. // 操作人信息
  287. $adminIdList = array_unique(array_column($data->toArray(), 'admin_id'));
  288. $adminInfoList = Users::query()
  289. ->whereIn('id', $adminIdList)
  290. ->where('enable', 1)
  291. ->get();
  292. // 提取客户详情信息
  293. $external_user_list = [];
  294. foreach($data as $value) {
  295. if(!empty($value->external_userid)) {
  296. $external_user_list[] = $value->user_id.$value->external_userid;
  297. }
  298. }
  299. if(!empty($external_user_list)) {
  300. $customerDetailList = CustomerDetails::suffix($corpid)
  301. ->where('corpid', $corpid)
  302. ->whereIn('con_user_cus', $external_user_list)
  303. ->get();
  304. } else {
  305. $customerDetailList = null;
  306. }
  307. foreach($data as &$value) {
  308. $operator = $adminInfoList->where('id', $value->admin_id)->first();
  309. $value->operator_name = !empty($operator->name) ? $operator->name : null;
  310. if(4 == $value->add_status) {
  311. // 待分配的客户不展示第一次分配的客服
  312. $value->user_name = null;
  313. } else {
  314. $user = $userInfoList->where('user_id', $value->user_id)->first();
  315. $value->user_name = !empty($user->name) ? $user->name : null;
  316. }
  317. if(!empty($value->external_userid)) {
  318. $con_user_cus = $value->user_id.$value->external_userid;
  319. $customerDetails = $customerDetailList->where('con_user_cus', $con_user_cus)
  320. ->first();
  321. $value->customer_id = !empty($customerDetails->customer_id) ? $customerDetails->customer_id : 0;
  322. } else {
  323. $value->customer_id = 0;
  324. }
  325. if(empty($value->customer_tag_list)) {
  326. $value->tag_list = [];
  327. } else {
  328. $customerTagList = explode(',', $value->customer_tag_list);
  329. $value->tag_list = $tagList->whereIn('tag_md5', $customerTagList)->pluck('tag_name');
  330. }
  331. }
  332. return [$data, $count];
  333. } catch (\Exception $exception) {
  334. Log::logError('batchAddCustomerDetailsList 参数:'.json_encode($requestData, 256), [
  335. 'file' => $exception->getFile(),
  336. 'line' => $exception->getLine(),
  337. 'message' => $exception->getMessage(),
  338. 'trace' => $exception->getTraceAsString(),
  339. ], 'interface');
  340. return [[], 0];
  341. }
  342. }
  343. // 导入记录列表
  344. public static function batchAddCustomerRecordList($corpid, $page, $pageSize, $sysGroupId)
  345. {
  346. $requestData = [
  347. 'corpid' => $corpid,
  348. 'page' => $page,
  349. 'page_size' => $pageSize,
  350. 'sys_group_id' => $sysGroupId,
  351. ];
  352. try{
  353. $batchAddCustomerRecordQuery = BatchAddCustomerRecord::query()
  354. ->where('group_admin_id', $sysGroupId)
  355. ->where('corpid', $corpid)
  356. ->where('enable', 1);
  357. $count = $batchAddCustomerRecordQuery->count();
  358. $data = $batchAddCustomerRecordQuery
  359. ->select(['id', 'import_time', 'file_name', 'user_list', 'tag_list'])
  360. ->orderBy('id', 'desc')
  361. ->offset(($page - 1) * $pageSize)
  362. ->limit($pageSize)
  363. ->get();
  364. // 查询企业下所有标签列表
  365. $tagList = Tag::query()
  366. ->where('corpid', $corpid)
  367. ->where('enable', 1)
  368. ->get();
  369. // 提取所有客服id
  370. $userInfoList = DjUser::query()
  371. ->where('corpid', $corpid)
  372. ->where('enable', 1)
  373. ->get();
  374. // 提取所有记录id列表
  375. $recordIdList = array_unique(array_column($data->toArray(), 'id'));
  376. $recordData = BatchAddCustomerDetails::customerCountStatistics($recordIdList, null);
  377. foreach ($data as &$value) {
  378. if(empty($value->tag_list)) {
  379. $value->tag_list = [];
  380. } else {
  381. $customerTagList = explode(',', $value->tag_list);
  382. $value->tag_list = $tagList->whereIn('tag_md5', $customerTagList)->pluck('tag_name');
  383. }
  384. if(empty($value->user_list)) {
  385. $value->user_list = [];
  386. $value->user_id_list = [];
  387. } else {
  388. $userList = explode(',', $value->user_list);
  389. $value->user_list = $userInfoList->whereIn('user_id', $userList)->pluck('name');
  390. $value->user_id_list = $userList;
  391. }
  392. $recordInfo = $recordData->where('record_id', $value->id)->first();
  393. $value->total_count = isset($recordInfo->total_count) ? $recordInfo->total_count : 0;
  394. $value->complete_count = isset($recordInfo->complete_count) ? $recordInfo->complete_count : 0;
  395. $value->complete_rate = $value->total_count > 0 ? round($value->complete_count / $value->total_count, 4) * 100 . '%' : '0%';
  396. }
  397. return [$data, $count];
  398. } catch (\Exception $exception) {
  399. Log::logError('batchAddCustomerRecordList 参数:'.json_encode($requestData, 256), [
  400. 'file' => $exception->getFile(),
  401. 'line' => $exception->getLine(),
  402. 'message' => $exception->getMessage(),
  403. 'trace' => $exception->getTraceAsString(),
  404. ], 'interface');
  405. return [[], 0];
  406. }
  407. }
  408. // 导入记录详情
  409. public static function batchAddCustomerRecordDetail($corpid, $addStatus, $keyword, $recordId, $page, $pageSize, $sysGroupId)
  410. {
  411. $requestData = [
  412. 'corpid' => $corpid,
  413. 'add_status' => $addStatus,
  414. 'keyword' => $keyword,
  415. 'record_id' => $recordId,
  416. 'page' => $page,
  417. 'page_size' => $pageSize,
  418. 'sys_group_id' => $sysGroupId
  419. ];
  420. try{
  421. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
  422. null, null, $keyword, null, null, $recordId);
  423. $count = $batchAddCustomerDetailsQuery->count();
  424. $data = $batchAddCustomerDetailsQuery->select(['id', 'phone', 'remark', 'customer_tag_list', 'user_id',
  425. 'add_status', 'import_time', 'distribution_num', 'add_time', 'admin_id', 'external_userid'])
  426. ->orderBy('id', 'desc')
  427. ->offset(($page - 1) * $pageSize)
  428. ->limit($pageSize)
  429. ->get();
  430. // 提取所有客户id
  431. $userIdList = array_unique(array_column($data->toArray(), 'user_id'));
  432. $userInfoList = DjUser::query()
  433. ->where('corpid', $corpid)
  434. ->where('enable', 1)
  435. ->whereIn('user_id', $userIdList)
  436. ->get();
  437. if(empty($addStatus)) {
  438. // 计算已添加客户数
  439. $recordData = BatchAddCustomerDetails::customerCountStatistics([$recordId]);
  440. $recordInfo = $recordData->where('record_id', $recordId)->first();
  441. $completeCount = !empty($recordInfo) ? $recordInfo->complete_count : 0;
  442. } else if(3 == $addStatus) {
  443. $completeCount = $count;
  444. } else {
  445. $completeCount = 0;
  446. }
  447. // 提取客户详情信息
  448. $external_user_list = [];
  449. foreach($data as $value) {
  450. if(!empty($value->external_userid)) {
  451. $external_user_list[] = $value->user_id.$value->external_userid;
  452. }
  453. }
  454. if(!empty($external_user_list)) {
  455. $customerDetailList = CustomerDetails::suffix($corpid)
  456. ->where('corpid', $corpid)
  457. ->whereIn('con_user_cus', $external_user_list)
  458. ->get();
  459. } else {
  460. $customerDetailList = null;
  461. }
  462. foreach($data as &$value) {
  463. if(4 == $value->add_status) {
  464. // 待分配的客户不展示第一次分配的客服
  465. $value->user_name = null;
  466. } else {
  467. $user = $userInfoList->where('user_id', $value->user_id)->first();
  468. $value->user_name = !empty($user->name) ? $user->name : null;
  469. }
  470. if(!empty($value->external_userid)) {
  471. $con_user_cus = $value->user_id.$value->external_userid;
  472. $customerDetails = $customerDetailList->where('con_user_cus', $con_user_cus)->first();
  473. $value->customer_id = !empty($customerDetails->customer_id) ? $customerDetails->customer_id : 0;
  474. } else {
  475. $value->customer_id = 0;
  476. }
  477. }
  478. return [['list' => $data, 'total' => $count, 'complete_total' => $completeCount], $count];
  479. } catch (\Exception $exception) {
  480. Log::logError('batchAddCustomerRecordDetail 参数:'.json_encode($requestData, 256), [
  481. 'file' => $exception->getFile(),
  482. 'line' => $exception->getLine(),
  483. 'message' => $exception->getMessage(),
  484. 'trace' => $exception->getTraceAsString(),
  485. ], 'interface');
  486. return [['list' => [], 'total' => 0, 'complete_total' => 0], 0];
  487. }
  488. }
  489. // 删除记录
  490. public static function deleteRecord($corpid, $recordId, $sysGroupId)
  491. {
  492. $requestData = [
  493. 'corpid' => $corpid,
  494. 'record_id' => $recordId,
  495. 'sys_group_id' => $sysGroupId,
  496. ];
  497. try{
  498. $res = BatchAddCustomerRecord::query()
  499. ->where('id', $recordId)
  500. ->where('corpid', $corpid)
  501. ->update(['enable' => 0]);
  502. if($res) {
  503. return ['操作成功', 0];
  504. } else {
  505. Log::logError('deleteRecord 参数:'.json_encode($requestData, 256), [
  506. 'res' => $res,
  507. 'message' => '操作失败',
  508. ], 'interface');
  509. return ['操作失败', 400];
  510. }
  511. } catch (\Exception $exception) {
  512. Log::logError('deleteRecord 参数:'.json_encode($requestData, 256), [
  513. 'file' => $exception->getFile(),
  514. 'line' => $exception->getLine(),
  515. 'message' => $exception->getMessage(),
  516. 'trace' => $exception->getTraceAsString(),
  517. ], 'interface');
  518. return ['操作失败', 400];
  519. }
  520. }
  521. public static function deleteDetail($corpid, $addStatus, $keyword, $startTime, $endTime, $selectAll, $selectIdList,
  522. $excludeIdList, $sysGroupId)
  523. {
  524. $requestData = [
  525. 'corpid' => $corpid,
  526. 'add_status' => $addStatus,
  527. 'keyword' => $keyword,
  528. 'start_time' => $startTime,
  529. 'end_time' => $endTime,
  530. 'select_all' => $selectAll,
  531. 'select_id_list' => $selectIdList,
  532. 'exclude_id_list' => $excludeIdList,
  533. 'sys_group_id' => $sysGroupId,
  534. ];
  535. try{
  536. // 判断是否全选
  537. if(isset($selectAll) && 1 == $selectAll) {// 全选
  538. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
  539. $startTime, $endTime, $keyword, $excludeIdList, null, null);
  540. } else {// 单个勾选
  541. $batchAddCustomerDetailsQuery = $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList(
  542. $corpid, $sysGroupId, null, null, null, null, null,
  543. $selectIdList, null);
  544. }
  545. $res = $batchAddCustomerDetailsQuery->update(['enable' => 0]);
  546. if($res) {
  547. return ['操作成功', 0];
  548. } else {
  549. Log::logError('deleteDetail 参数:'.json_encode($requestData, 256), [
  550. 'res' => $res,
  551. 'message' => '操作失败',
  552. ], 'interface');
  553. return ['操作失败', 400];
  554. }
  555. } catch (\Exception $exception) {
  556. Log::logError('deleteDetail 参数:'.json_encode($requestData, 256), [
  557. 'file' => $exception->getFile(),
  558. 'line' => $exception->getLine(),
  559. 'message' => $exception->getMessage(),
  560. 'trace' => $exception->getTraceAsString(),
  561. ], 'interface');
  562. return ['操作失败', 400];
  563. }
  564. }
  565. public static function batchRemind($corpid, $addStatus, $keyword, $startTime, $endTime, $selectAll, $selectIdList,
  566. $excludeIdList, $sysGroupId)
  567. {
  568. $requestData = [
  569. 'corpid' => $corpid,
  570. 'add_status' => $addStatus,
  571. 'keyword' => $keyword,
  572. 'start_time' => $startTime,
  573. 'end_time' => $endTime,
  574. 'select_all' => $selectAll,
  575. 'select_id_list' => $selectIdList,
  576. 'exclude_id_list' => $excludeIdList,
  577. 'sys_group_id' => $sysGroupId,
  578. ];
  579. try{
  580. // 判断是否全选
  581. if(isset($selectAll) && 1 == $selectAll) {// 全选
  582. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
  583. $startTime, $endTime, $keyword, $excludeIdList, null, null);
  584. } else {// 单个勾选
  585. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, null,
  586. null, null, null, null, $selectIdList, null);
  587. }
  588. $userList = $batchAddCustomerDetailsQuery->whereIn('add_status', [1, 2])
  589. ->distinct()->pluck('user_id');
  590. if(!empty($userList)) {
  591. foreach ($userList->toArray() as $userId) {
  592. RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
  593. 'corpid' => $corpid,
  594. 'user_id' => $userId
  595. ]));
  596. }
  597. }
  598. return ['提醒成功', 0];
  599. } catch (\Exception $exception) {
  600. Log::logError('batchRemind 参数:'.json_encode($requestData, 256), [
  601. 'file' => $exception->getFile(),
  602. 'line' => $exception->getLine(),
  603. 'message' => $exception->getMessage(),
  604. 'trace' => $exception->getTraceAsString(),
  605. ], 'interface');
  606. return ['操作失败', 400];
  607. }
  608. }
  609. public static function batchDistributionCustomer($corpid, $addStatus, $keyword, $startTime, $endTime, $selectAll,
  610. $selectIdList, $excludeIdList, $userIdList, $sysGroupId)
  611. {
  612. $requestData = [
  613. 'corpid' => $corpid,
  614. 'add_status' => $addStatus,
  615. 'keyword' => $keyword,
  616. 'start_time' => $startTime,
  617. 'end_time' => $endTime,
  618. 'select_all' => $selectAll,
  619. 'select_id_list' => $selectIdList,
  620. 'exclude_id_list' => $excludeIdList,
  621. 'user_id_list' => $userIdList,
  622. 'sys_group_id' => $sysGroupId,
  623. ];
  624. try{
  625. // 判断是否全选
  626. if(isset($selectAll) && 1 == $selectAll) {// 全选
  627. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
  628. $startTime, $endTime, $keyword, $excludeIdList, null, null);
  629. } else {// 单个勾选
  630. $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, null,
  631. null, null, null, null, $selectIdList, null);
  632. }
  633. // 获取所有待分配的id列表
  634. $list = $batchAddCustomerDetailsQuery->select(['id', 'phone', 'remark', 'corpid', 'admin_id', 'group_admin_id',
  635. 'customer_tag_list', 'import_time', 'record_id', 'distribution_num'])
  636. ->where('add_status', 4)->get();
  637. if(!empty($list->toArray())) {
  638. // 重新分配
  639. $newData = BatchAddCustomerDetails::reassignCustomer($list->toArray(), $userIdList);
  640. \DB::begintransaction();
  641. // 将重新分配的数据写入数据库
  642. $insertRes = BatchAddCustomerDetails::query()->insert($newData);
  643. // 修改本次待分配数据状态
  644. $ids = array_column($list->toArray(), 'id');
  645. $updateRes = BatchAddCustomerDetails::query()->whereIn('id', $ids)->update(['add_status' => 0]);
  646. if($insertRes && $updateRes) {
  647. \DB::commit();
  648. // 提取客服列表,将数据塞入提醒队列
  649. $remindUserList = array_unique(array_column($newData, 'user_id'));
  650. foreach($remindUserList as $userId) {
  651. if(empty($userId)) {
  652. continue;
  653. }
  654. RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
  655. 'corpid' => $corpid,
  656. 'user_id' => $userId,
  657. ]));
  658. }
  659. return ['操作成功', 0];
  660. } else {
  661. \DB::rollBack();
  662. Log::logError('batchDistributionCustomer 参数:'.json_encode($requestData, 256), [
  663. 'message' => '数据库操作失败',
  664. 'insert_res' => $insertRes,
  665. 'update_res' => $updateRes,
  666. 'new_data' => $newData,
  667. 'list' => $list->toArray(),
  668. ], 'interface');
  669. return ['操作失败', 400];
  670. }
  671. } else {
  672. Log::logError('batchDistributionCustomer 参数:'.json_encode($requestData, 256), [
  673. 'message' => '没有筛选到待分配的客户',
  674. ], 'interface');
  675. return ['没有筛选到待分配的客户', 3305];
  676. }
  677. } catch (\Exception $exception) {
  678. Log::logError('batchDistributionCustomer 参数:'.json_encode($requestData, 256), [
  679. 'file' => $exception->getFile(),
  680. 'line' => $exception->getLine(),
  681. 'message' => $exception->getMessage(),
  682. 'trace' => $exception->getTraceAsString(),
  683. ], 'interface');
  684. return ['操作失败', 400];
  685. }
  686. }
  687. }