123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2022/6/7
- * Time: 11:35
- */
- namespace App\Service;
- use App\Log;
- use App\Models\BatchAddCustomerConfig;
- use App\Models\Customer\BatchAddCustomerDetails;
- use App\Models\Customer\BatchAddCustomerRecord;
- use App\Models\CustomerDetails;
- use App\Models\DjUser;
- use App\Models\System\Users;
- use App\Models\Tag;
- use App\RedisModel;
- class AddCustomerService
- {
- // 批量添加客户导入方法
- public static function batchAddCustomerImport($originalFileName, $newFileName, $ext, $corpid, $userIdList, $customerTagList,
- $sysGroupId)
- {
- $admin = \Auth::user();
- $adminId = $admin->id;
- $requestData = [
- 'admin_id' => $adminId,
- 'user_id_list' => $userIdList,
- 'corpid' => $corpid,
- 'customer_tag_list' => $customerTagList,
- 'original_file_name' => $originalFileName,
- 'new_file_name' => $newFileName,
- 'ext' => $ext,
- 'sys_group_id' => $sysGroupId,
- ];
- $fileName = $newFileName;
- // $fileName = ImportService::getFileNameByType(1, $corpid, $adminId, $ext);
- if(empty($fileName)) {
- Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
- 'message' => '未找到本地文件',
- ], 'interface');
- return ['code' => 3303, 'info' => '系统异常,读取缓存数据失败'];
- }
- $filePath = '../storage/uploads/'.$fileName;
- try{
- // 读取文件内容
- $objPHPExcel = @\PHPExcel_IOFactory::load($filePath);
- $sheet = $objPHPExcel->getSheet(0); // 读取第一个工作表
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- $phoneArr = []; // 已经导入的手机号,判断是否重复导入使用
- $data = [];// 导入的数据
- $preg_phone='/^1[345789]\d{9}$/ims';
- $errorArr = [];
- $breakCount = 0;
- for ($row = 6; $row <= $highestRow; $row ++) { // 从第6行开始读取数据,第1行为表头,无需处理
- $item = [];
- $item['phone'] = trim($sheet->getCell('A' . $row)->getFormattedValue());
- $item['remark'] = trim($sheet->getCell('B' . $row)->getFormattedValue());
- # 当获取手机号连续3行均为空时停止读取
- if(empty($item['phone'])) {
- if($breakCount <= 3) {
- $breakCount++;
- continue;
- } else {
- break;
- }
- } else {
- $breakCount = 0;
- }
- // 验证手机号格式
- if(!preg_match($preg_phone, $item['phone'])) {
- $errorArr[] = [
- 'row' => $row,
- 'error' => ['手机号格式不合法'],
- ];
- }
- // 本次导入的手机号去重
- if(!in_array($item['phone'], $phoneArr)) {
- $phoneArr[] = $item['phone'];
- $data[] = $item;
- }
- }
- if(!empty($errorArr)) {
- Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
- 'error_arr' => $errorArr,
- 'message' => '导入数据格式不合法',
- ], 'interface');
- return ['info' => $errorArr, 'code' => 3304];
- }
- // 将客户平均分配给客服
- $saveDetailsData = BatchAddCustomerDetails::distributionCustomer($data, $userIdList, $corpid, $sysGroupId);
- $count = count($data) - count($saveDetailsData);
- $cacheData['save_details_data'] = $saveDetailsData;
- $cacheData['original_file_name'] = $originalFileName;
- $cacheData['file_path'] = $filePath;
- $cacheData['ext'] = $ext;
- $cacheData['customer_tag_list'] = $customerTagList;
- $cacheData['user_id_list'] = $userIdList;
- if($count > 0) {
- // 与之前导入的手机号有重复,将本次导入的数据缓存入redis中,并返回提示
- RedisModel::set(BatchAddCustomerRecord::IMPORT_DATA.'-'.$fileName, json_encode($cacheData, 256));
- return ['info' => ['count' => $count, 'num' => count($saveDetailsData)], 'code' => 3301];
- } else {
- // 将文件上传oss,然后调用保存数据方法
- $oss = new OssService('playlet');
- $ossFile = $oss->upload($ext, $filePath, 'excel/'.$ext.'/'.date("Y-m-d",time()));
- $fileUrl = $ossFile['oss-request-url'];
- $importTime = date('Y-m-d H:i:s');
- \DB::begintransaction();// 开启事务
- # 调用保存数据方法
- // 保存导入记录
- $saveRecordResult = BatchAddCustomerRecord::insertData($importTime, $originalFileName, $userIdList,
- $customerTagList, $adminId, $corpid, $fileUrl, $sysGroupId);
- $recordId = isset($saveRecordResult->id) ? $saveRecordResult->id : null;
- if(!empty($recordId)) {
- // 保存导入客户
- $saveDetailsResult = BatchAddCustomerDetails::insertData($saveDetailsData, $corpid, $adminId,
- $importTime, $recordId, $customerTagList, $sysGroupId);
- } else {
- $saveDetailsResult = false;
- }
- if($saveDetailsResult && $saveRecordResult) {
- \DB::commit();
- // 删除本地文件
- unlink($filePath);
- // 在提醒队里中插入数据
- $remindUserList = array_unique(array_column($saveDetailsData, 'user_id'));
- foreach($remindUserList as $userId) {
- if(empty($userId)) {
- continue;
- }
- RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
- 'corpid' => $corpid,
- 'user_id' => $userId
- ]));
- }
- return ['info' => '导入成功', 'code' => 0];
- } else {
- Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
- 'massage' => '数据保存失败',
- 'record_result' => $saveRecordResult,
- 'details_result' => $saveDetailsResult,
- ], 'interface');
- \DB::rollBack();
- return ['info' => '导入失败', 'code' => 400];
- }
- }
- } catch (\Exception $exception) {
- # 打印错误日志
- Log::logError('batchAddCustomerImport 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['info' => '请求失败,请联系管理员', 'code' => 400];
- }
- }
- // 批量添加客户导入--二次确认
- public static function batchAddCustomerImportConfirm($corpid, $confirm, $newFileName, $sysGroupId)
- {
- $admin = \Auth::user();
- $adminId = $admin->id;
- $requestData = [
- 'corpid' => $corpid,
- 'confirm' => $confirm,
- 'admin_id' => $adminId,
- 'sys_group_id' => $sysGroupId,
- ];
- try{
- // 判断客户选择
- if( 1 == $confirm){// 确认跳过重复客户导入
- $importTime = date('Y-m-d H:i:s');
- // 从redis缓存中读取数据
- $cacheData = RedisModel::get(BatchAddCustomerRecord::IMPORT_DATA.'-'.$newFileName);
- if(empty($cacheData)) {
- Log::logError('batchAddCustomerImportConfirm 参数:'.json_encode($requestData, 256), ['读取缓存数据失败'], 'interface');
- return ['code' => 3303, 'info' => '系统异常,读取缓存数据失败'];
- }
- $cacheData = json_decode($cacheData, true);
- $ext = $cacheData['ext'];
- $filePath = $cacheData['file_path'];
- $originalFileName = $cacheData['original_file_name'];
- $customerTagList = $cacheData['customer_tag_list'];
- $userIdList = $cacheData['user_id_list'];
- $saveDetailsData = $cacheData['save_details_data'];
- // 本地文件上传到阿里云
- $oss = new OssService('playlet');
- $ossFile = $oss->upload($ext, $filePath, 'excel/'.$ext.'/'.date("Y-m-d",time()));
- $fileUrl = $ossFile['oss-request-url'];
- // 保存缓存数据到本地
- \DB::begintransaction();// 开启事务
- # 调用保存数据方法
- // 保存导入记录
- $saveRecordResult = BatchAddCustomerRecord::insertData($importTime, $originalFileName, $userIdList,
- $customerTagList, $adminId, $corpid, $fileUrl, $sysGroupId);
- $recordId = isset($saveRecordResult->id) ? $saveRecordResult->id : null;
- if(!empty($recordId)) {
- // 保存导入客户
- $saveDetailsResult = BatchAddCustomerDetails::insertData($saveDetailsData, $corpid, $adminId, $importTime,
- $recordId, $customerTagList, $sysGroupId);
- } else {
- $saveDetailsResult = false;
- }
- if($saveDetailsResult && $saveRecordResult) {
- \DB::commit();
- // 删除本地文件
- unlink($filePath);
- RedisModel::expire(BatchAddCustomerRecord::IMPORT_DATA.'-'.$newFileName, 0);
- // 在提醒队里中插入数据
- $remindUserList = array_unique(array_column($saveDetailsData, 'user_id'));
- foreach($remindUserList as $userId) {
- if(empty($userId)) {
- continue;
- }
- RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
- 'corpid' => $corpid,
- 'user_id' => $userId
- ]));
- }
- return ['info' => '导入成功', 'code' => 0];
- } else {
- Log::logError('batchAddCustomerImportConfirm 参数:'.json_encode($requestData, 256), [
- 'massage' => '数据保存失败',
- 'record_result' => $saveRecordResult,
- 'details_result' => $saveDetailsResult,
- ], 'interface');
- \DB::rollBack();
- return ['info' => '', 'code' => 400];
- }
- } else {// 取消本次导入
- $filePath = '../storage/uploads/'.$newFileName;
- // 删除本地文件
- unlink($filePath);
- // 删除redis缓存
- RedisModel::expire(BatchAddCustomerRecord::IMPORT_DATA.'-'.$newFileName, 0);
- return ['info' => '操作成功', 'code' => 0];
- }
- } catch (\Exception $exception) {
- # 打印错误日志
- Log::logError('batchAddCustomerImportConfirm 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['info' => '请求失败,请联系管理员', 'code' => 400];
- }
- }
- // 导入客户列表
- public static function batchAddCustomerDetailsList($corpid, $addStatus, $keyword, $startTime, $endTime, $page,
- $pageSize, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'add_status' => $addStatus,
- 'keyword' => $keyword,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'sys_group_id' => $sysGroupId
- ];
- try{
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
- $startTime, $endTime, $keyword, null, null, null);
- $count = $batchAddCustomerDetailsQuery->count();
- $data = $batchAddCustomerDetailsQuery->select(['id', 'phone', 'remark', 'customer_tag_list', 'user_id',
- 'add_status', 'import_time', 'distribution_num', 'add_time', 'admin_id', 'external_userid'])
- ->orderBy('id', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- // 查询企业下所有标签列表
- $tagList = Tag::query()
- ->where('corpid', $corpid)
- ->where('enable', 1)
- ->get();
- // 提取所有客服id
- $userIdList = array_unique(array_column($data->toArray(), 'user_id'));
- $userInfoList = DjUser::query()
- ->where('corpid', $corpid)
- ->where('enable', 1)
- ->whereIn('user_id', $userIdList)
- ->get();
- // 操作人信息
- $adminIdList = array_unique(array_column($data->toArray(), 'admin_id'));
- $adminInfoList = Users::query()
- ->whereIn('id', $adminIdList)
- ->where('enable', 1)
- ->get();
- // 提取客户详情信息
- $external_user_list = [];
- foreach($data as $value) {
- if(!empty($value->external_userid)) {
- $external_user_list[] = $value->user_id.$value->external_userid;
- }
- }
- if(!empty($external_user_list)) {
- $customerDetailList = CustomerDetails::suffix($corpid)
- ->where('corpid', $corpid)
- ->whereIn('con_user_cus', $external_user_list)
- ->get();
- } else {
- $customerDetailList = null;
- }
- foreach($data as &$value) {
- $operator = $adminInfoList->where('id', $value->admin_id)->first();
- $value->operator_name = !empty($operator->name) ? $operator->name : null;
- if(4 == $value->add_status) {
- // 待分配的客户不展示第一次分配的客服
- $value->user_name = null;
- } else {
- $user = $userInfoList->where('user_id', $value->user_id)->first();
- $value->user_name = !empty($user->name) ? $user->name : null;
- }
- if(!empty($value->external_userid)) {
- $con_user_cus = $value->user_id.$value->external_userid;
- $customerDetails = $customerDetailList->where('con_user_cus', $con_user_cus)
- ->first();
- $value->customer_id = !empty($customerDetails->customer_id) ? $customerDetails->customer_id : 0;
- } else {
- $value->customer_id = 0;
- }
- if(empty($value->customer_tag_list)) {
- $value->tag_list = [];
- } else {
- $customerTagList = explode(',', $value->customer_tag_list);
- $value->tag_list = $tagList->whereIn('tag_md5', $customerTagList)->pluck('tag_name');
- }
- }
- return [$data, $count];
- } catch (\Exception $exception) {
- Log::logError('batchAddCustomerDetailsList 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return [[], 0];
- }
- }
- // 导入记录列表
- public static function batchAddCustomerRecordList($corpid, $page, $pageSize, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'sys_group_id' => $sysGroupId,
- ];
- try{
- $batchAddCustomerRecordQuery = BatchAddCustomerRecord::query()
- ->where('group_admin_id', $sysGroupId)
- ->where('corpid', $corpid)
- ->where('enable', 1);
- $count = $batchAddCustomerRecordQuery->count();
- $data = $batchAddCustomerRecordQuery
- ->select(['id', 'import_time', 'file_name', 'user_list', 'tag_list'])
- ->orderBy('id', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- // 查询企业下所有标签列表
- $tagList = Tag::query()
- ->where('corpid', $corpid)
- ->where('enable', 1)
- ->get();
- // 提取所有客服id
- $userInfoList = DjUser::query()
- ->where('corpid', $corpid)
- ->where('enable', 1)
- ->get();
- // 提取所有记录id列表
- $recordIdList = array_unique(array_column($data->toArray(), 'id'));
- $recordData = BatchAddCustomerDetails::customerCountStatistics($recordIdList, null);
- foreach ($data as &$value) {
- if(empty($value->tag_list)) {
- $value->tag_list = [];
- } else {
- $customerTagList = explode(',', $value->tag_list);
- $value->tag_list = $tagList->whereIn('tag_md5', $customerTagList)->pluck('tag_name');
- }
- if(empty($value->user_list)) {
- $value->user_list = [];
- $value->user_id_list = [];
- } else {
- $userList = explode(',', $value->user_list);
- $value->user_list = $userInfoList->whereIn('user_id', $userList)->pluck('name');
- $value->user_id_list = $userList;
- }
- $recordInfo = $recordData->where('record_id', $value->id)->first();
- $value->total_count = isset($recordInfo->total_count) ? $recordInfo->total_count : 0;
- $value->complete_count = isset($recordInfo->complete_count) ? $recordInfo->complete_count : 0;
- $value->complete_rate = $value->total_count > 0 ? round($value->complete_count / $value->total_count, 4) * 100 . '%' : '0%';
- }
- return [$data, $count];
- } catch (\Exception $exception) {
- Log::logError('batchAddCustomerRecordList 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return [[], 0];
- }
- }
- // 导入记录详情
- public static function batchAddCustomerRecordDetail($corpid, $addStatus, $keyword, $recordId, $page, $pageSize, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'add_status' => $addStatus,
- 'keyword' => $keyword,
- 'record_id' => $recordId,
- 'page' => $page,
- 'page_size' => $pageSize,
- 'sys_group_id' => $sysGroupId
- ];
- try{
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
- null, null, $keyword, null, null, $recordId);
- $count = $batchAddCustomerDetailsQuery->count();
- $data = $batchAddCustomerDetailsQuery->select(['id', 'phone', 'remark', 'customer_tag_list', 'user_id',
- 'add_status', 'import_time', 'distribution_num', 'add_time', 'admin_id', 'external_userid'])
- ->orderBy('id', 'desc')
- ->offset(($page - 1) * $pageSize)
- ->limit($pageSize)
- ->get();
- // 提取所有客户id
- $userIdList = array_unique(array_column($data->toArray(), 'user_id'));
- $userInfoList = DjUser::query()
- ->where('corpid', $corpid)
- ->where('enable', 1)
- ->whereIn('user_id', $userIdList)
- ->get();
- if(empty($addStatus)) {
- // 计算已添加客户数
- $recordData = BatchAddCustomerDetails::customerCountStatistics([$recordId]);
- $recordInfo = $recordData->where('record_id', $recordId)->first();
- $completeCount = !empty($recordInfo) ? $recordInfo->complete_count : 0;
- } else if(3 == $addStatus) {
- $completeCount = $count;
- } else {
- $completeCount = 0;
- }
- // 提取客户详情信息
- $external_user_list = [];
- foreach($data as $value) {
- if(!empty($value->external_userid)) {
- $external_user_list[] = $value->user_id.$value->external_userid;
- }
- }
- if(!empty($external_user_list)) {
- $customerDetailList = CustomerDetails::suffix($corpid)
- ->where('corpid', $corpid)
- ->whereIn('con_user_cus', $external_user_list)
- ->get();
- } else {
- $customerDetailList = null;
- }
- foreach($data as &$value) {
- if(4 == $value->add_status) {
- // 待分配的客户不展示第一次分配的客服
- $value->user_name = null;
- } else {
- $user = $userInfoList->where('user_id', $value->user_id)->first();
- $value->user_name = !empty($user->name) ? $user->name : null;
- }
- if(!empty($value->external_userid)) {
- $con_user_cus = $value->user_id.$value->external_userid;
- $customerDetails = $customerDetailList->where('con_user_cus', $con_user_cus)->first();
- $value->customer_id = !empty($customerDetails->customer_id) ? $customerDetails->customer_id : 0;
- } else {
- $value->customer_id = 0;
- }
- }
- return [['list' => $data, 'total' => $count, 'complete_total' => $completeCount], $count];
- } catch (\Exception $exception) {
- Log::logError('batchAddCustomerRecordDetail 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return [['list' => [], 'total' => 0, 'complete_total' => 0], 0];
- }
- }
- // 删除记录
- public static function deleteRecord($corpid, $recordId, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'record_id' => $recordId,
- 'sys_group_id' => $sysGroupId,
- ];
- try{
- $res = BatchAddCustomerRecord::query()
- ->where('id', $recordId)
- ->where('corpid', $corpid)
- ->update(['enable' => 0]);
- if($res) {
- return ['操作成功', 0];
- } else {
- Log::logError('deleteRecord 参数:'.json_encode($requestData, 256), [
- 'res' => $res,
- 'message' => '操作失败',
- ], 'interface');
- return ['操作失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('deleteRecord 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['操作失败', 400];
- }
- }
- public static function deleteDetail($corpid, $addStatus, $keyword, $startTime, $endTime, $selectAll, $selectIdList,
- $excludeIdList, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'add_status' => $addStatus,
- 'keyword' => $keyword,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- 'select_all' => $selectAll,
- 'select_id_list' => $selectIdList,
- 'exclude_id_list' => $excludeIdList,
- 'sys_group_id' => $sysGroupId,
- ];
- try{
- // 判断是否全选
- if(isset($selectAll) && 1 == $selectAll) {// 全选
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
- $startTime, $endTime, $keyword, $excludeIdList, null, null);
- } else {// 单个勾选
- $batchAddCustomerDetailsQuery = $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList(
- $corpid, $sysGroupId, null, null, null, null, null,
- $selectIdList, null);
- }
- $res = $batchAddCustomerDetailsQuery->update(['enable' => 0]);
- if($res) {
- return ['操作成功', 0];
- } else {
- Log::logError('deleteDetail 参数:'.json_encode($requestData, 256), [
- 'res' => $res,
- 'message' => '操作失败',
- ], 'interface');
- return ['操作失败', 400];
- }
- } catch (\Exception $exception) {
- Log::logError('deleteDetail 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['操作失败', 400];
- }
- }
- public static function batchRemind($corpid, $addStatus, $keyword, $startTime, $endTime, $selectAll, $selectIdList,
- $excludeIdList, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'add_status' => $addStatus,
- 'keyword' => $keyword,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- 'select_all' => $selectAll,
- 'select_id_list' => $selectIdList,
- 'exclude_id_list' => $excludeIdList,
- 'sys_group_id' => $sysGroupId,
- ];
- try{
- // 判断是否全选
- if(isset($selectAll) && 1 == $selectAll) {// 全选
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
- $startTime, $endTime, $keyword, $excludeIdList, null, null);
- } else {// 单个勾选
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, null,
- null, null, null, null, $selectIdList, null);
- }
- $userList = $batchAddCustomerDetailsQuery->whereIn('add_status', [1, 2])
- ->distinct()->pluck('user_id');
- if(!empty($userList)) {
- foreach ($userList->toArray() as $userId) {
- RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
- 'corpid' => $corpid,
- 'user_id' => $userId
- ]));
- }
- }
- return ['提醒成功', 0];
- } catch (\Exception $exception) {
- Log::logError('batchRemind 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['操作失败', 400];
- }
- }
- public static function batchDistributionCustomer($corpid, $addStatus, $keyword, $startTime, $endTime, $selectAll,
- $selectIdList, $excludeIdList, $userIdList, $sysGroupId)
- {
- $requestData = [
- 'corpid' => $corpid,
- 'add_status' => $addStatus,
- 'keyword' => $keyword,
- 'start_time' => $startTime,
- 'end_time' => $endTime,
- 'select_all' => $selectAll,
- 'select_id_list' => $selectIdList,
- 'exclude_id_list' => $excludeIdList,
- 'user_id_list' => $userIdList,
- 'sys_group_id' => $sysGroupId,
- ];
- try{
- // 判断是否全选
- if(isset($selectAll) && 1 == $selectAll) {// 全选
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, $addStatus,
- $startTime, $endTime, $keyword, $excludeIdList, null, null);
- } else {// 单个勾选
- $batchAddCustomerDetailsQuery = BatchAddCustomerDetails::getCustomerList($corpid, $sysGroupId, null,
- null, null, null, null, $selectIdList, null);
- }
- // 获取所有待分配的id列表
- $list = $batchAddCustomerDetailsQuery->select(['id', 'phone', 'remark', 'corpid', 'admin_id', 'group_admin_id',
- 'customer_tag_list', 'import_time', 'record_id', 'distribution_num'])
- ->where('add_status', 4)->get();
- if(!empty($list->toArray())) {
- // 重新分配
- $newData = BatchAddCustomerDetails::reassignCustomer($list->toArray(), $userIdList);
- \DB::begintransaction();
- // 将重新分配的数据写入数据库
- $insertRes = BatchAddCustomerDetails::query()->insert($newData);
- // 修改本次待分配数据状态
- $ids = array_column($list->toArray(), 'id');
- $updateRes = BatchAddCustomerDetails::query()->whereIn('id', $ids)->update(['add_status' => 0]);
- if($insertRes && $updateRes) {
- \DB::commit();
- // 提取客服列表,将数据塞入提醒队列
- $remindUserList = array_unique(array_column($newData, 'user_id'));
- foreach($remindUserList as $userId) {
- if(empty($userId)) {
- continue;
- }
- RedisModel::lPush(BatchAddCustomerConfig::BATCH_ADD_CUSTOMER_RECORD_RDS, json_encode([
- 'corpid' => $corpid,
- 'user_id' => $userId,
- ]));
- }
- return ['操作成功', 0];
- } else {
- \DB::rollBack();
- Log::logError('batchDistributionCustomer 参数:'.json_encode($requestData, 256), [
- 'message' => '数据库操作失败',
- 'insert_res' => $insertRes,
- 'update_res' => $updateRes,
- 'new_data' => $newData,
- 'list' => $list->toArray(),
- ], 'interface');
- return ['操作失败', 400];
- }
- } else {
- Log::logError('batchDistributionCustomer 参数:'.json_encode($requestData, 256), [
- 'message' => '没有筛选到待分配的客户',
- ], 'interface');
- return ['没有筛选到待分配的客户', 3305];
- }
- } catch (\Exception $exception) {
- Log::logError('batchDistributionCustomer 参数:'.json_encode($requestData, 256), [
- 'file' => $exception->getFile(),
- 'line' => $exception->getLine(),
- 'message' => $exception->getMessage(),
- 'trace' => $exception->getTraceAsString(),
- ], 'interface');
- return ['操作失败', 400];
- }
- }
- }
|