diff --git a/app/admin/service/UserService.php b/app/admin/service/UserService.php index 34457ed..62e4d44 100644 --- a/app/admin/service/UserService.php +++ b/app/admin/service/UserService.php @@ -58,7 +58,7 @@ class UserService $config['where']['id'] = $p['id']; } - $config['fields'] = ['id', 'name', 'email', 'sex', 'phone', 'avatar', 'corp_id', 'country_id', 'position', 'status', 'created_at']; + $config['fields'] = ['id','sn', 'name', 'email', 'sex', 'phone', 'avatar', 'corp_id', 'country_id', 'position', 'status', 'created_at']; break; case 'show': $config['where']['id'] = $p['id']; diff --git a/app/command/CustomerCodeCommand.php b/app/command/CustomerCodeCommand.php new file mode 100644 index 0000000..2d3e447 --- /dev/null +++ b/app/command/CustomerCodeCommand.php @@ -0,0 +1,61 @@ +setDescription('为crm_customer表所有记录生成唯一code码,分批处理'); + } + + protected function execute(InputInterface $input, OutputInterface $output): int + { + $batchSize = 1000; + $total = Customer::withTrashed()->where('code','')->count(); + $output->writeln("总计需处理:$total 条记录"); + + $processed = 0; + do { + // 查询一批未生成code的记录 + $customers = Customer::withTrashed()->where('code','')->limit($batchSize)->get(); + if ($customers->isEmpty()) { + break; + } + + // Eloquent 批量处理 + foreach ($customers as $customer) { + // 生成唯一16位code,CM+14位 + $unique = strtoupper(substr(md5($customer->id . microtime(true) . mt_rand(1000, 9999)), 0, 14)); + $code = 'CM' . $unique; + + // 防止极小概率重复 + while (Customer::where('code', $code)->exists()) { + $unique = strtoupper(substr(md5($customer->id . microtime(true) . mt_rand(1000, 9999)), 0, 14)); + $code = 'CM' . $unique; + } + + $obtain = $customer->created_at>strtotime('2025-07-29')?$customer->created_at:strtotime('2025-07-29'); + + $customer->code = $code; + $customer->obtain = $obtain; + $customer->save(); + $processed++; + } + + $output->writeln("已处理:$processed / $total"); + } while (true); + + $output->writeln('全部处理完成!'); + return self::SUCCESS; + } +} \ No newline at end of file diff --git a/app/crm/controller/CustomerController.php b/app/crm/controller/CustomerController.php index dba4ae7..2efbceb 100644 --- a/app/crm/controller/CustomerController.php +++ b/app/crm/controller/CustomerController.php @@ -3,6 +3,7 @@ namespace app\crm\controller; use app\common\model\Corp; +use app\common\model\InquiryLog; use app\common\model\User; use app\crm\model\Channel; use app\crm\model\Country; @@ -12,6 +13,7 @@ use app\crm\model\Follow; use app\crm\service\CustomerService; use app\crm\utils\Util; use config\constants\Constants; +use support\Db; use support\Request; class CustomerController extends BaseController @@ -454,22 +456,38 @@ class CustomerController extends BaseController $data = [ 'user_id' => $request->user->uid ]; - $customer= Customer::withTrashed()->find($customerId); - if(empty($customer) || $customer->user_id != 0){ - return $this->error( trans('claim_fail')); + + try { + Db::beginTransaction(); + $customer= Customer::withTrashed()->find($customerId); + + if(empty($customer) || $customer->user_id != 0){ + return $this->error( trans('claim_fail')); + } + + $customer->restore(); + $res = $customer->update($data); + + //记录客户关系变化 + $his = CustomerHis::create([ + 'customer_id' => $customerId, + 'user_his' => 0, + 'user_id' => $userId, + ]); + if (!empty($customer->sn) && $customer->type==1){ + $inquiryLog = InquiryLog::where('order_no', $customer->sn)->first(); + if (!empty($inquiryLog)) { + $inquiryLog->recipient = $userId; + $inquiryLog->save(); + } + } + Db::commit(); + } catch (\Throwable $e) { + Db::rollback(); + return $this->error($e->getMessage()); } - $customer->restore(); - $res = $customer->update($data); - - //记录客户关系变化 - $his = CustomerHis::create([ - 'customer_id' => $customerId, - 'user_his' => 0, - 'user_id' => $userId, - ]); - if ($res) { return $this->success($res, trans('success')); } else { @@ -506,17 +524,44 @@ class CustomerController extends BaseController }else{ $query= Customer::whereIn('id',explode(',',$customerId)); } - $customer = $query->get(); +// $customer = $query->get(); +// +// foreach ($customer as $item) { +// //记录客户关系变化 +// $his = CustomerHis::create([ +// 'customer_id' => $item->id, +// 'user_his' => $item->user_id, +// 'user_id' => $user['id'], +// ]); +// } +// $res = $query->update(['user_id' => $user->id]); - foreach ($customer as $item) { - //记录客户关系变化 - $his = CustomerHis::create([ - 'customer_id' => $item->id, - 'user_his' => $item->user_id, - 'user_id' => $user['id'], - ]); + try { + $customer = $query->get(); + + foreach ($customer as $item) { + //记录客户关系变化 + $his = CustomerHis::create([ + 'customer_id' => $item->id, + 'user_his' => $item->user_id, + 'user_id' => $user['id'], + ]); + if (!empty($customer->sn) && $customer->type==1){ + $inquiryLog = InquiryLog::where('order_no', $customer->sn)->first(); + if (!empty($inquiryLog)) { + $inquiryLog->recipient = $userId; + $inquiryLog->save(); + } + } + } + $res = $query->update(['user_id' => $user->id]); + + Db::commit(); + } catch (\Throwable $e) { + Db::rollback(); + return $this->error($e->getMessage()); } - $res = $query->update(['user_id' => $user->id]); + if ($res) { diff --git a/app/crmAdmin/service/CustomerService.php b/app/crmAdmin/service/CustomerService.php index d003a0b..79c9193 100644 --- a/app/crmAdmin/service/CustomerService.php +++ b/app/crmAdmin/service/CustomerService.php @@ -2,6 +2,7 @@ namespace app\crmAdmin\service; +use app\common\model\InquiryLog; use app\common\model\User; use app\crm\model\CustomerHis; use app\crmAdmin\logic\CustomerLogic; @@ -306,6 +307,13 @@ class CustomerService 'user_his' => $item->user_id, 'user_id' => $userId, ]); + if (!empty($item->sn) && $item->type==1){ + $inquiryLog = InquiryLog::where('order_no', $item->sn)->first(); + if (!empty($inquiryLog)) { + $inquiryLog->recipient = $userId; + $inquiryLog->save(); + } + } } $res = $query->update(['user_id' => $userId]);