123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496 |
- <?php
- /**
- * Created by PhpStorm.
- * User: shensong
- * Date: 2023/1/3
- * Time: 16:55
- */
- namespace App\Console\Repair;
- use App\Log;
- use App\Models\BatchTransferCustomer;
- use App\Models\CustomerAssignmentTotal;
- use App\Models\CustomerDetails;
- use App\Models\DjUser;
- use App\RedisModel;
- use App\Support\EmailQueue;
- use Illuminate\Console\Command;
- class CustomerAssignmentDataRepair extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'CustomerAssignmentDataRepair';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = '客户迁移历史数据处理';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- public function handle()
- {
- # 需要处理的迁移列表
- $assignmentData = BatchTransferCustomer::query()
- ->where('create_time', '>=', '2022-12-01')
- ->where('enable', 1)
- ->orderBy('id', 'asc')
- ->get();
- $this->info(date('H:i') . ' 开始执行, 待处理的数据有'.$assignmentData->count());
- foreach($assignmentData as $data) {
- // 判断迁移类型并分别处理
- if(1 == $data['type']) {
- $this->customerAssignmentOnJob($data);
- } else {
- $this->customerAssignmentQuit($data);
- }
- }
- }
- public function customerAssignmentOnJob($data)
- {
- $params = json_decode($data['params'], 1);
- $customerName = isset($params['customer_name']) ? $params['customer_name'] : null;
- $userIdList = isset($params['user_id_list']) ? $params['user_id_list'] : null;
- $addDateStart = isset($params['add_date_start']) ? $params['add_date_start'] : null;
- $addDateEnd = isset($params['add_date_end']) ? $params['add_date_end'] : null;
- $addWay = isset($params['add_way']) ? $params['add_way'] : null;
- $gender = isset($params['gender']) ? $params['gender'] : null;
- $payStatus = isset($params['pay_status']) ? $params['pay_status'] : null;
- $payNumMin = isset($params['pay_num_min']) ? $params['pay_num_min'] : null;
- $payNumMax = isset($params['pay_num_max']) ? $params['pay_num_max'] : null;
- $tagType = isset($params['tag_type']) ? $params['tag_type'] : null;
- $tagIdList = isset($params['tag_id_list']) ? $params['tag_id_list'] : null;
- $lossStatus = 0;
- $corpid = $data['corpid'];
- $page = 1;
- $pageSize = 500;
- $filterCustomerList = json_decode($data['filter_customer_list'], 1);
- $filterData = [];
- foreach($filterCustomerList as $value) {
- $filterData[] = $value['external_userid'].'-'.$value['handover_userid'];
- }
- // 判断是否筛选了客服
- if(empty($userIdList)) {
- // 没有筛选客服时,从系统中获取所有客服
- $userIdList = DjUser::query()
- ->where('corpid', $data['corpid'])
- ->where('enable', 1)
- ->get();
- $userIdList = $userIdList->isNotEmpty() ? $userIdList->toArray() : [];
- $userIdList = array_column($userIdList, 'user_id');
- }
- foreach($userIdList as $userId) {
- $flag = true;
- $customerDataList = [];
- while($flag) {
- list($customerList, $count) = $this->customerListOnJob($customerName, $userId, $addDateStart,
- $addDateEnd, $addWay, $gender, $payStatus, $payNumMin, $payNumMax, $tagType, $tagIdList,
- $lossStatus, $corpid, $page, $pageSize);
- if($count > 0) {
- foreach($customerList as $customer) {
- $kk = $customer['external_userid'].'-'.$customer['user_id'];
- if(!in_array($kk, $filterData)) {
- $customerDataList[] = $customer['external_userid'];
- }
- }
- $pages = ceil($count/500);
- if($page >= $pages) {
- $flag = false;
- } else {
- $page++;
- }
- } else {
- $flag = false;
- }
- }
- if(count($customerDataList) > 0) {
- if($userId != $data['takeover_userid']) {
- // 将数据写入
- $result = CustomerAssignmentTotal::query()
- ->insert([
- 'handover_userid' => $userId,
- 'takeover_userid' => $data['takeover_userid'],
- 'transfer_success_msg' => $data['transfer_success_msg'],
- 'external_userid' => json_encode($customerDataList),
- 'type' => 1,
- 'status' => 1,
- 'portrait_inheritance' => $data['portrait_inheritance'],
- 'enable' => 1,
- 'corpid' => $data['corpid'],
- ]);
- } else {
- $result = true;
- }
- if(!$result) {
- // 日志记录
- Log::logError($this->signature, [
- 'errmsg' => '客户在职迁移-插入数据失败',
- 'params' => $data,
- 'tmp' => [
- 'filter_data' => $filterData,
- 'user_id_list' => $userIdList,
- 'user_id' => $userId,
- 'customer_data_list' => $customerDataList,
- ],
- 'result' => $result,
- ], 'script_error');
- // 邮件报警
- $this->exceptionHandler($this->signature, json_encode([
- 'errmsg' => '客户在职迁移-插入数据失败',
- 'params' => $data,
- 'tmp' => [
- 'filter_data' => $filterData,
- 'user_id_list' => $userIdList,
- 'user_id' => $userId,
- 'customer_data_list' => $customerDataList,
- ],
- 'result' => $result,
- ]), '客户在职迁移-'.$this->signature);
- }
- } else {
- // 日志记录
- Log::logError($this->signature, [
- 'errmsg' => '客户在职迁移-查询客户列表为空',
- 'params' => $data,
- 'tmp' => [
- 'filter_data' => $filterData,
- 'user_id_list' => $userIdList,
- 'user_id' => $userId,
- 'customer_data_list' => $customerDataList,
- ],
- ], 'script_error');
- }
- }
- }
- public function customerAssignmentQuit($data)
- {
- $params = json_decode($data['params'], 1);
- $customerName = isset($params['customer_name']) ? $params['customer_name'] : null;
- $userIdList = isset($params['user_id_list']) ? json_decode($params['user_id_list'], 1) : null;
- $addDateStart = isset($params['add_date_start']) ? $params['add_date_start'] : null;
- $addDateEnd = isset($params['add_date_end']) ? $params['add_date_end'] : null;
- $addWay = isset($params['add_way']) ? $params['add_way'] : null;
- $payStatus = isset($params['pay_status']) ? $params['pay_status'] : null;
- $payNumMin = isset($params['pay_num_min']) ? $params['pay_num_min'] : null;
- $payNumMax = isset($params['pay_num_max']) ? $params['pay_num_max'] : null;
- $tagType = isset($params['tag_type']) ? $params['tag_type'] : null;
- $tagIdList = isset($params['tag_id_list']) ? json_decode($params['tag_id_list']) : null;
- $corpid = $data['corpid'];
- $page = 1;
- $pageSize = 500;
- $filterCustomerList = json_decode($data['filter_customer_list'], 1);
- $filterData = [];
- foreach($filterCustomerList as $value) {
- $filterData[] = $value['external_userid'].'-'.$value['handover_userid'];
- }
- // 判断是否筛选了客服
- if(empty($userIdList)) {
- // 没有筛选客服时,从系统中获取所有客服
- $userIdList = DjUser::query()
- ->where('corpid', $data['corpid'])
- ->where('enable', 1)
- ->get();
- $userIdList = $userIdList->isNotEmpty() ? $userIdList->toArray() : [];
- $userIdList = array_column($userIdList, 'user_id');
- }
- foreach($userIdList as $userId) {
- $flag = true;
- $customerDataList = [];
- while($flag) {
- list($customerList, $count) = $this->customerListQuit($customerName, $userId, $addDateStart,
- $addDateEnd, $addWay, $payStatus, $payNumMin, $payNumMax, $tagType, $tagIdList, $corpid, $page,
- $pageSize);
- if($count > 0) {
- foreach($customerList as $customer) {
- $kk = $customer['external_userid'].'-'.$customer['user_id'];
- if(!in_array($kk, $filterData)) {
- $customerDataList[] = [
- 'external_userid' => $customer['external_userid']
- ];
- }
- }
- $pages = ceil($count/500);
- if($page >= $pages) {
- $flag = false;
- } else {
- $page++;
- }
- } else {
- $flag = false;
- }
- }
- if(count($customerDataList) > 0) {
- // 将数据写入
- $result = CustomerAssignmentTotal::query()
- ->insert([
- 'handover_userid' => $userId,
- 'takeover_userid' => $data['takeover_userid'],
- 'transfer_success_msg' => $data['transfer_success_msg'],
- 'external_userid' => json_encode($customerDataList),
- 'type' => 2,
- 'status' => 1,
- 'portrait_inheritance' => $data['portrait_inheritance'],
- 'corpid' => $data['corpid']
- ]);
- if(!$result) {
- // 日志记录
- Log::logError($this->signature, [
- 'errmsg' => '客户在职迁移-插入数据失败',
- 'params' => $data,
- 'tmp' => [
- 'filter_data' => $filterData,
- 'user_id_list' => $userIdList,
- 'user_id' => $userId,
- 'customer_data_list' => $customerDataList,
- ],
- 'result' => $result,
- ], 'script_error');
- // 邮件报警
- $this->exceptionHandler($this->signature, json_encode([
- 'errmsg' => '客户在职迁移-插入数据失败',
- 'params' => $data,
- 'tmp' => [
- 'filter_data' => $filterData,
- 'user_id_list' => $userIdList,
- 'user_id' => $userId,
- 'customer_data_list' => $customerDataList,
- ],
- 'result' => $result,
- ]), '客户在职迁移-'.$this->signature);
- }
- } else {
- // 日志记录
- Log::logError($this->signature, [
- 'errmsg' => '客户离职迁移-查询客户列表为空',
- 'params' => $data,
- 'tmp' => [
- 'filter_data' => $filterData,
- 'user_id_list' => $userIdList,
- 'user_id' => $userId,
- 'customer_data_list' => $customerDataList,
- ],
- ], 'script_error');
- }
- }
- }
- // 客户管理列表
- public function customerListOnJob($customerName, $userId, $addDateStart, $addDateEnd,
- $addWay, $gender, $payStatus, $payNumMin, $payNumMax, $tagType, $tagIdList,
- $lossStatus, $corpid, $page, $pageSize)
- {
- $customerQuery = CustomerDetails::suffix($corpid)
- // ->where('loss_status', 1)
- ->where('corpid', $corpid);
- if(!empty($customerName)) {
- $customerQuery = $customerQuery->where('name', 'like', '%'.$customerName.'%');
- }
- if(!empty($userId)) {
- $customerQuery = $customerQuery->where('user_id', $userId);
- }
- if(!empty($addDateStart)) {
- $customerQuery = $customerQuery->where('createtime', '>=', strtotime($addDateStart . ' 00:00:00'));
- }
- if(!empty($addDateEnd)) {
- $customerQuery = $customerQuery->where('createtime', '<=', strtotime($addDateEnd . ' 23:59:59'));
- }
- if(is_numeric($addWay)) {
- $customerQuery = $customerQuery->where('add_way', $addWay);
- }
- if(is_numeric($gender)) {
- $customerQuery = $customerQuery->where('gender', $gender);
- }
- // 付款状态,付款次数筛选
- if(is_numeric($payStatus)) {
- if(0 == $payStatus) {
- $payNumMax=0;
- $payNumMin=0;
- }else {
- if(empty($payNumMin)) {
- $payNumMin = 1;
- }
- }
- if(is_numeric($payNumMax)) {
- $customerQuery = $customerQuery->where('pay_num', '<=', $payNumMax);
- }
- if(is_numeric($payNumMin)) {
- $customerQuery = $customerQuery->where('pay_num', '>=', $payNumMin);
- }
- }
- if(1 == $tagType) {
- $tagRaw = '';
- foreach($tagIdList as $k=>$tagId) {
- $tagRaw .= $tagId;
- if($k < count($tagIdList) -1){
- $tagRaw = $tagRaw.' ';
- }
- }
- $customerQuery = $customerQuery->whereRaw('match(`tag_list`) against ("'.$tagRaw.'" in boolean mode)');
- }
- if(2 == $tagType) {
- $tagRaw = '';
- foreach($tagIdList as $k=>$tagId) {
- $tagRaw .= '+'.$tagId;
- if($k < count($tagIdList) -1){
- $tagRaw = $tagRaw.' ';
- }
- }
- $customerQuery = $customerQuery->whereRaw('match(`tag_list`) against ("'.$tagRaw.'" in boolean mode)');
- }
- if(3 == $tagType) {
- $customerQuery = $customerQuery->whereNull('tag_list');
- }
- $customerQueryCount = clone $customerQuery;
- $count = $customerQueryCount->selectRaw('count(distinct(`external_userid`)) as count')->first();
- $count = $count->count;
- $offset = ($page - 1) * $pageSize;
- $data = $customerQuery->select(['customer_id', 'name', 'gender', 'external_userid', 'user_id', 'remark',
- 'createtime', 'add_way', 'corpid', 'pay_num', 'enable'])
- ->groupBy('con_user_cus')
- ->offset($offset)
- ->limit($pageSize)
- ->get()
- ->toArray();
- return [$data, $count];
- }
- public function customerListQuit($customerName, $userId, $addDateStart, $addDateEnd, $addWay,
- $payStatus, $payNumMin, $payNumMax, $tagType, $tagIdList, $corpid,
- $page, $pageSize)
- {
- $customerQuery = CustomerDetails::suffix($corpid)
- // ->where('loss_status', 2)
- ->where('corpid', $corpid);
- if(!empty($customerName)) {
- $customerQuery = $customerQuery->where('name', 'like', '%'.$customerName.'%');}
- if(!empty($userId)) {
- $customerQuery = $customerQuery->where('user_id', $userId);}
- if(!empty($addDateStart)) {
- $customerQuery = $customerQuery->where('createtime', '>=', strtotime($addDateStart . ' 00:00:00'));}
- if(!empty($addDateEnd)) {
- $customerQuery = $customerQuery->where('createtime', '<=', strtotime($addDateEnd . ' 23:59:59'));}
- if(is_numeric($addWay)) {
- $customerQuery = $customerQuery->where('add_way', $addWay);}
- // 付款状态,付款次数筛选
- if(is_numeric($payStatus)) {
- if(0 == $payStatus) {
- $payNumMax=0;
- $payNumMin=0;
- } else {
- if(empty($payNumMin)) {
- $payNumMin = 1;
- }
- }
- // $customerQuery = $customerQuery->where('pay_num', '>=', $payNumMin)
- // ->where('pay_num', '<=', $payNumMax);
- if(is_numeric($payNumMax)) {
- $customerQuery = $customerQuery->where('pay_num', '<=', $payNumMax);
- }
- if(is_numeric($payNumMin)) {
- $customerQuery = $customerQuery->where('pay_num', '>=', $payNumMin);
- }
- }
- if(1 == $tagType) {
- $tagRaw = '';
- foreach($tagIdList as $k=>$tagId) {
- $tagRaw .= $tagId;
- if($k < count($tagIdList) -1){
- $tagRaw = $tagRaw.' ';
- }
- }
- $customerQuery = $customerQuery->whereRaw('match(`tag_list`) against ("'.$tagRaw.'" in boolean mode)');
- }
- if(2 == $tagType) {
- $tagRaw = '';
- foreach($tagIdList as $k=>$tagId) {
- $tagRaw .= '+'.$tagId;
- if($k < count($tagIdList) -1){
- $tagRaw = $tagRaw.' ';
- }
- }
- $customerQuery = $customerQuery->whereRaw('match(`tag_list`) against ("'.$tagRaw.'" in boolean mode)');
- }
- if(3 == $tagType) {
- $customerQuery = $customerQuery->whereNull('tag_list');}
- $customerQueryCount = clone $customerQuery;
- $count = $customerQueryCount->selectRaw('count(distinct(`external_userid`)) as count')->first();
- $count = $count->count;
- $offset = ($page - 1) * $pageSize;
- $data = $customerQuery->select(['customer_id', 'name', 'gender',
- 'external_userid', 'user_id', 'remark', 'createtime', 'add_way', 'corpid', 'pay_num'])
- ->groupBy('con_user_cus')
- ->offset($offset)
- ->limit($pageSize)
- ->get()
- ->toArray();
- return [$data, $count];
- }
- public function exceptionHandler($key, $message, $title)
- {
- $val = RedisModel::get($key);
- if(empty($val)) {
- EmailQueue::rPush($title, $message, ['song.shen@kuxuan-inc.com'], []);
- RedisModel::set($key, 1);
- RedisModel::expire($key, 10*60);
- }
- }
- }
|