客户转移

This commit is contained in:
2025-08-04 11:17:38 +08:00
parent ebfec84fc3
commit 22e7db1053
2 changed files with 173 additions and 91 deletions
@@ -99,4 +99,50 @@ class CustomerController extends BaseController
} }
} }
/**
* 批量转让
* @param Request $request
* @return \support\Response
*/
public function transfer(Request $request)
{
try {
$param = $request->all();
if (empty($param['customer_id'])) {
return $this->error('customer_id'.trans('not_null'));
}
if (empty($param['user_id'])) {
return $this->error('user_id'.trans('not_null'));
}
$result = (new CustomerService())->transfer($param);
return $this->success($result);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
/**
* 客户详情
* @param Request $request
* @return \support\Response
*/
public function detail(Request $request)
{
try {
$param = $request->all();
if (empty($param['id'])) {
return $this->error('id'.trans('not_null'));
}
$result = (new CustomerService())->detail($param);
return $this->success($result);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
} }
+127 -91
View File
@@ -17,6 +17,7 @@ use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment; use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border; use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill; use PhpOffice\PhpSpreadsheet\Style\Fill;
use support\Db;
class CustomerService class CustomerService
{ {
@@ -32,7 +33,8 @@ class CustomerService
* @param $export * @param $export
* @return Customer[]|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Collection[]|\Illuminate\Pagination\LengthAwarePaginator * @return Customer[]|\Illuminate\Contracts\Pagination\LengthAwarePaginator|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Collection[]|\Illuminate\Pagination\LengthAwarePaginator
*/ */
public function search($params, $export = false){ public function search($params, $export = false)
{
// $page = $request->input('page', 1); // $page = $request->input('page', 1);
// $perPage = $request->input('per_page', 10); // $perPage = $request->input('per_page', 10);
@@ -50,10 +52,10 @@ class CustomerService
if ($scene == 'audit') { if ($scene == 'audit') {
$where[] = ['status', '=', 2]; $where[] = ['status', '=', 2];
}else { } else {
$where[] = ['status', '=', 1]; $where[] = ['status', '=', 1];
} }
if($seas == 'seas' || $scene == 'seas'){ if ($seas == 'seas' || $scene == 'seas') {
$where[] = ['user_id', 0]; $where[] = ['user_id', 0];
} }
// if($recycle == 'recycle'){ // if($recycle == 'recycle'){
@@ -80,18 +82,18 @@ class CustomerService
$query = Customer::where($where); $query = Customer::where($where);
if(!empty($user_id)){ if (!empty($user_id)) {
$query->whereIn('user_id', explode(',',$user_id)); $query->whereIn('user_id', explode(',', $user_id));
} }
if(!empty($tagId)){ if (!empty($tagId)) {
//查询有$tagId 里任意一个标签的客户 //查询有$tagId 里任意一个标签的客户
//$query->whereHas('tags', function($query) use ($tagId){ //$query->whereHas('tags', function($query) use ($tagId){
// $query->whereIn ('tag.id', $tagId); // $query->whereIn ('tag.id', $tagId);
//}); //});
//查询有$tagId 里所有标签的客户 //查询有$tagId 里所有标签的客户
foreach ($tagId as $id) { foreach ($tagId as $id) {
$query->whereHas('tags', function($query) use ($id){ $query->whereHas('tags', function ($query) use ($id) {
$query->where('tag.id', $id); $query->where('tag.id', $id);
}); });
} }
@@ -110,20 +112,20 @@ class CustomerService
$query->onlyTrashed(); $query->onlyTrashed();
} }
if(!$export){ if (!$export) {
$customer = $query->orderBy('id','desc')->paginate($perPage, ['*'], 'page', $page); $customer = $query->orderBy('id', 'desc')->paginate($perPage, ['*'], 'page', $page);
}else{ } else {
$customer = $query->orderBy('id','desc')->get(); $customer = $query->orderBy('id', 'desc')->get();
return $customer; return $customer;
} }
if(!$export){ if (!$export) {
foreach ($customer as &$item) { foreach ($customer as &$item) {
$item = $this->getCustomer($item); $item = $this->getCustomer($item);
if($seas == 'seas' || $scene == 'seas'){ if ($seas == 'seas' || $scene == 'seas') {
$item['seas_time'] = time()-strtotime($item['deleted_at']); $item['seas_time'] = time() - strtotime($item['deleted_at']);
} }
if ($scene == 'audit') { //审核时查询举报人姓名 if ($scene == 'audit') { //审核时查询举报人姓名
$customerHis = CustomerHis::where('customer_id',$item->id)->orderBy('id','desc')->first(['user_his']); $customerHis = CustomerHis::where('customer_id', $item->id)->orderBy('id', 'desc')->first(['user_his']);
$customer['user_name'] = empty($customerHis['user_his']) ? '' : User::find($customer['user_his'])->value('name'); $customer['user_name'] = empty($customerHis['user_his']) ? '' : User::find($customer['user_his'])->value('name');
} }
} }
@@ -136,31 +138,31 @@ class CustomerService
public function export($params) public function export($params)
{ {
$customers = $this->search($params, true); $customers = $this->search($params, true);
// 创建新的Spreadsheet对象 // 创建新的Spreadsheet对象
$spreadsheet = new Spreadsheet(); $spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(); $sheet = $spreadsheet->getActiveSheet();
// 设置表头 // 设置表头
$headers = [ $headers = [
'A1' => '客户ID', 'A1' => '客户ID',
'B1' => '联系人', 'B1' => '联系人',
'C1' => '电话', 'C1' => '电话',
'D1' => '邮箱', 'D1' => '邮箱',
'E1' => 'WhatsApp', 'E1' => 'WhatsApp',
'F1' => '性别', 'F1' => '性别',
'G1' => '国家/地区', 'G1' => '国家/地区',
'H1' => '渠道来源', 'H1' => '渠道来源',
'I1' => '描述', 'I1' => '描述',
'J1' => '所属人', 'J1' => '所属人',
'K1' => '录入人', 'K1' => '录入人',
]; ];
// 设置表头样式 // 设置表头样式
foreach ($headers as $cell => $value) { foreach ($headers as $cell => $value) {
$sheet->setCellValue($cell, $value); $sheet->setCellValue($cell, $value);
} }
// 设置表头样式 // 设置表头样式
$headerStyle = [ $headerStyle = [
'font' => [ 'font' => [
@@ -182,31 +184,31 @@ class CustomerService
], ],
], ],
]; ];
$sheet->getStyle('A1:K1')->applyFromArray($headerStyle); $sheet->getStyle('A1:K1')->applyFromArray($headerStyle);
// 填充数据 // 填充数据
$row = 2; $row = 2;
foreach ($customers as $customer) { foreach ($customers as $customer) {
// 获取完整的客户信息 // 获取完整的客户信息
$customerData = $this->getCustomer($customer); $customerData = $this->getCustomer($customer);
// 填充数据行 // 填充数据行
$sheet->setCellValue('A' . $row, $customerData['id']); $sheet->setCellValue('A' . $row, $customerData['id']);
$sheet->setCellValue('B' . $row, $customerData['contacts']); $sheet->setCellValue('B' . $row, $customerData['contacts']);
$sheet->setCellValue('C' . $row, $customerData['phone']); $sheet->setCellValue('C' . $row, $customerData['phone']);
$sheet->setCellValue('D' . $row, $customerData['email']); $sheet->setCellValue('D' . $row, $customerData['email']);
$sheet->setCellValue('E' . $row, $customerData['WhatsApp']); $sheet->setCellValue('E' . $row, $customerData['WhatsApp']);
$sheet->setCellValue('F' . $row, $customerData['sex_txt']); $sheet->setCellValue('F' . $row, $customerData['sex_txt']);
$sheet->setCellValue('G' . $row, $customerData['country_name']); $sheet->setCellValue('G' . $row, $customerData['country_name']);
$sheet->setCellValue('H' . $row, $customerData['channel_name']); $sheet->setCellValue('H' . $row, $customerData['channel_name']);
$sheet->setCellValue('I' . $row, $customerData['describe']); $sheet->setCellValue('I' . $row, $customerData['describe']);
$sheet->setCellValue('J' . $row, $customerData['user_name']); // 所属人姓名 $sheet->setCellValue('J' . $row, $customerData['user_name']); // 所属人姓名
$sheet->setCellValue('K' . $row, $customerData['enter_user_name']); $sheet->setCellValue('K' . $row, $customerData['enter_user_name']);
$row++; $row++;
} }
// 设置数据行样式 // 设置数据行样式
$dataStyle = [ $dataStyle = [
'borders' => [ 'borders' => [
@@ -219,35 +221,35 @@ class CustomerService
'vertical' => Alignment::VERTICAL_CENTER, 'vertical' => Alignment::VERTICAL_CENTER,
], ],
]; ];
$lastRow = $row - 1; $lastRow = $row - 1;
if ($lastRow >= 2) { if ($lastRow >= 2) {
$sheet->getStyle('A2:K' . $lastRow)->applyFromArray($dataStyle); $sheet->getStyle('A2:K' . $lastRow)->applyFromArray($dataStyle);
} }
// 自动调整列宽 // 自动调整列宽
foreach (range('A', 'K') as $column) { foreach (range('A', 'K') as $column) {
$sheet->getColumnDimension($column)->setAutoSize(true); $sheet->getColumnDimension($column)->setAutoSize(true);
} }
// 设置行高 // 设置行高
$sheet->getRowDimension(1)->setRowHeight(25); $sheet->getRowDimension(1)->setRowHeight(25);
// 生成文件名 // 生成文件名
$filename = '客户数据_' . date('Y-m-d_H-i-s') . '.xlsx'; $filename = '客户数据_' . date('Y-m-d_H-i-s') . '.xlsx';
// 创建输出目录 // 创建输出目录
$exportDir = runtime_path() . '/export/'; $exportDir = runtime_path() . '/export/';
if (!is_dir($exportDir)) { if (!is_dir($exportDir)) {
mkdir($exportDir, 0755, true); mkdir($exportDir, 0755, true);
} }
$filepath = $exportDir . $filename; $filepath = $exportDir . $filename;
// 创建Excel写入器 // 创建Excel写入器
$writer = new Xlsx($spreadsheet); $writer = new Xlsx($spreadsheet);
$writer->save($filepath); $writer->save($filepath);
// 返回文件信息 // 返回文件信息
return [ return [
'filename' => $filename, 'filename' => $filename,
@@ -261,7 +263,8 @@ class CustomerService
* @param $params * @param $params
* @return bool * @return bool
*/ */
public function audit($params){ public function audit($params)
{
$id = $params['id']; $id = $params['id'];
@@ -270,13 +273,45 @@ class CustomerService
]; ];
$logic = new CustomerLogic(); $logic = new CustomerLogic();
if($params['scene'] == 'delete'){ if ($params['scene'] == 'delete') {
return $logic->forceDelete($params['id']); return $logic->forceDelete($params['id']);
}else{ } else {
return $logic->status($params['id'], 1); return $logic->status($params['id'], 1);
} }
} }
/**
* @param $params
* @return bool
*/
public function transfer($params)
{
try {
Db::beginTransaction();
$customerId = $params['customer_id'];
$userId = $params['user_id'];
$query= Customer::whereIn('id',explode(',',$customerId));
$customer = $query->get();
foreach ($customer as $item) {
//记录客户关系变化
$his = CustomerHis::create([
'customer_id' => $item->id,
'user_his' => $item->user_id,
'user_id' => $userId,
]);
}
$res = $query->update(['user_id' => $userId]);
Db::commit();
return $res;
} catch (\Throwable $e) {
Db::rollback();
throw $e;
}
}
/** /**
* @param $customer * @param $customer
* @param $request * @param $request
@@ -289,21 +324,21 @@ class CustomerService
$customer['magnitude_txt'] = Util::language($language, Constants::MAGNITUDE_EN[$customer['magnitude']], Constants::MAGNITUDE[$customer['magnitude']], Constants::MAGNITUDE_RU[$customer['magnitude']]); $customer['magnitude_txt'] = Util::language($language, Constants::MAGNITUDE_EN[$customer['magnitude']], Constants::MAGNITUDE[$customer['magnitude']], Constants::MAGNITUDE_RU[$customer['magnitude']]);
$customer['sex_txt'] = Util::language($language, Constants::SEX_EN[$customer['sex']], Constants::SEX[$customer['sex']], Constants::SEX_RU[$customer['sex']]); $customer['sex_txt'] = Util::language($language, Constants::SEX_EN[$customer['sex']], Constants::SEX[$customer['sex']], Constants::SEX_RU[$customer['sex']]);
if ($customer['grade']<=2) { if ($customer['grade'] <= 2) {
$customer['follow'] = 0; $customer['follow'] = 0;
$customer['follow_txt'] = Util::language($language,'Following','跟进中','Продолжение'); $customer['follow_txt'] = Util::language($language, 'Following', '跟进中', 'Продолжение');
}elseif ($customer['grade']<5) { } elseif ($customer['grade'] < 5) {
$customer['follow'] = 1; $customer['follow'] = 1;
$customer['follow_txt'] = Util::language($language,'transacted','已成交','Транзакция завершена'); $customer['follow_txt'] = Util::language($language, 'transacted', '已成交', 'Транзакция завершена');
}else{ } else {
$customer['follow'] = 2; $customer['follow'] = 2;
$customer['follow_txt'] = Util::language($language,'lost','已流失','Потерянный'); $customer['follow_txt'] = Util::language($language, 'lost', '已流失', 'Потерянный');
} }
$where = [ $where = [
'customer_id' => $customer['id'] 'customer_id' => $customer['id']
]; ];
$begin_follow = Follow::select('id','created_at','updated_at') $begin_follow = Follow::select('id', 'created_at', 'updated_at')
->where($where) ->where($where)
->min('created_at'); ->min('created_at');
@@ -312,12 +347,12 @@ class CustomerService
// $customer['created_at'] = Util::timeTotDate($customer['created_at']); // $customer['created_at'] = Util::timeTotDate($customer['created_at']);
// $customer['updated_at'] = Util::timeTotDate($customer['updated_at']); // $customer['updated_at'] = Util::timeTotDate($customer['updated_at']);
$customer = Util::formatDate($customer,['follow_last']); $customer = Util::formatDate($customer, ['follow_last']);
$nameLang = $language=='en'?'name_en':($language=='ru'?'name_ru':'name'); $nameLang = $language == 'en' ? 'name_en' : ($language == 'ru' ? 'name_ru' : 'name');
$countryLang = $language=='en'?'en_addr':'cn_addr'; $countryLang = $language == 'en' ? 'en_addr' : 'cn_addr';
// $customer['car_type_name'] = empty($customer['car_type_id'])?'':CarType::where(['id'=>$customer['car_type_id']])->value($nameLang); // $customer['car_type_name'] = empty($customer['car_type_id'])?'':CarType::where(['id'=>$customer['car_type_id']])->value($nameLang);
$customer['country_name'] = empty($customer['country_id'])?'':Country::where(['id'=>$customer['country_id']])->value($countryLang); $customer['country_name'] = empty($customer['country_id']) ? '' : Country::where(['id' => $customer['country_id']])->value($countryLang);
$customer['channel_name'] = empty($customer['channel_id'])?'':Channel::where(['id'=>$customer['channel_id']])->value($nameLang); $customer['channel_name'] = empty($customer['channel_id']) ? '' : Channel::where(['id' => $customer['channel_id']])->value($nameLang);
// $customer['last_follow'] = $last_follow; // $customer['last_follow'] = $last_follow;
if ($tag) { if ($tag) {
@@ -336,10 +371,11 @@ class CustomerService
public function detail($params) public function detail($params)
{ {
$id =$params['id']; $id = $params['id'];
$customer = Customer::with('tags')->select('id', 'user_id', 'grade', 'channel_id', 'car_type_id', 'country_id', 'magnitude', 'email', 'phone', 'WhatsApp', 'contacts', 'describe', 'address', 'lat', 'lng', 'follow_next', 'follow_remark', 'sex') $customer = Customer::with('tags')
->where('id', $id) ->where('id', $id)
->first(); ->first();
$customer = $this->getCustomer($customer);
return $customer; return $customer;
} }