This commit is contained in:
2025-08-01 17:18:08 +08:00
parent d016376687
commit ebfec84fc3
7 changed files with 564 additions and 22 deletions
+162 -12
View File
@@ -3,6 +3,7 @@
namespace app\crmAdmin\service;
use app\common\model\User;
use app\crm\model\CustomerHis;
use app\crmAdmin\logic\CustomerLogic;
use app\crm\model\CarType;
use app\crm\model\Channel;
@@ -11,11 +12,28 @@ use app\crm\model\Customer;
use app\crm\model\Follow;
use app\crm\utils\Util;
use config\constants\Constants;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
class CustomerService
{
public function list($params)
{
$customer = $this->search($params);
return $customer;
}
/**
* 查询列表数据
* @param $params
* @param $export
* @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){
// $page = $request->input('page', 1);
// $perPage = $request->input('per_page', 10);
$perPage = $params['size'] ?? 10;
@@ -31,9 +49,9 @@ class CustomerService
$where = [];
if ($scene == 'audit') {
$where = ['status', 2];
$where[] = ['status', '=', 2];
}else {
$where = ['status', 1];
$where[] = ['status', '=', 1];
}
if($seas == 'seas' || $scene == 'seas'){
$where[] = ['user_id', 0];
@@ -92,11 +110,22 @@ class CustomerService
$query->onlyTrashed();
}
$customer = $query->paginate($perPage, ['*'], 'page', $page);
foreach ($customer as &$item) {
$item = $this->getCustomer($item);
if($seas == 'seas' || $scene == 'seas'){
$item['seas_time'] = time()-strtotime($item['deleted_at']);
if(!$export){
$customer = $query->orderBy('id','desc')->paginate($perPage, ['*'], 'page', $page);
}else{
$customer = $query->orderBy('id','desc')->get();
return $customer;
}
if(!$export){
foreach ($customer as &$item) {
$item = $this->getCustomer($item);
if($seas == 'seas' || $scene == 'seas'){
$item['seas_time'] = time()-strtotime($item['deleted_at']);
}
if ($scene == 'audit') { //审核时查询举报人姓名
$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');
}
}
}
unset($item);
@@ -104,6 +133,130 @@ class CustomerService
return $customer;
}
public function export($params)
{
$customers = $this->search($params, true);
// 创建新的Spreadsheet对象
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
// 设置表头
$headers = [
'A1' => '客户ID',
'B1' => '联系人',
'C1' => '电话',
'D1' => '邮箱',
'E1' => 'WhatsApp',
'F1' => '性别',
'G1' => '国家/地区',
'H1' => '渠道来源',
'I1' => '描述',
'J1' => '所属人',
'K1' => '录入人',
];
// 设置表头样式
foreach ($headers as $cell => $value) {
$sheet->setCellValue($cell, $value);
}
// 设置表头样式
$headerStyle = [
'font' => [
'bold' => true,
'color' => ['rgb' => 'FFFFFF'],
],
'fill' => [
'fillType' => Fill::FILL_SOLID,
'startColor' => ['rgb' => '4472C4'],
],
'alignment' => [
'horizontal' => Alignment::HORIZONTAL_CENTER,
'vertical' => Alignment::VERTICAL_CENTER,
],
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
'color' => ['rgb' => '000000'],
],
],
];
$sheet->getStyle('A1:K1')->applyFromArray($headerStyle);
// 填充数据
$row = 2;
foreach ($customers as $customer) {
// 获取完整的客户信息
$customerData = $this->getCustomer($customer);
// 填充数据行
$sheet->setCellValue('A' . $row, $customerData['id']);
$sheet->setCellValue('B' . $row, $customerData['contacts']);
$sheet->setCellValue('C' . $row, $customerData['phone']);
$sheet->setCellValue('D' . $row, $customerData['email']);
$sheet->setCellValue('E' . $row, $customerData['WhatsApp']);
$sheet->setCellValue('F' . $row, $customerData['sex_txt']);
$sheet->setCellValue('G' . $row, $customerData['country_name']);
$sheet->setCellValue('H' . $row, $customerData['channel_name']);
$sheet->setCellValue('I' . $row, $customerData['describe']);
$sheet->setCellValue('J' . $row, $customerData['user_name']); // 所属人姓名
$sheet->setCellValue('K' . $row, $customerData['enter_user_name']);
$row++;
}
// 设置数据行样式
$dataStyle = [
'borders' => [
'allBorders' => [
'borderStyle' => Border::BORDER_THIN,
'color' => ['rgb' => '000000'],
],
],
'alignment' => [
'vertical' => Alignment::VERTICAL_CENTER,
],
];
$lastRow = $row - 1;
if ($lastRow >= 2) {
$sheet->getStyle('A2:K' . $lastRow)->applyFromArray($dataStyle);
}
// 自动调整列宽
foreach (range('A', 'K') as $column) {
$sheet->getColumnDimension($column)->setAutoSize(true);
}
// 设置行高
$sheet->getRowDimension(1)->setRowHeight(25);
// 生成文件名
$filename = '客户数据_' . date('Y-m-d_H-i-s') . '.xlsx';
// 创建输出目录
$exportDir = runtime_path() . '/export/';
if (!is_dir($exportDir)) {
mkdir($exportDir, 0755, true);
}
$filepath = $exportDir . $filename;
// 创建Excel写入器
$writer = new Xlsx($spreadsheet);
$writer->save($filepath);
// 返回文件信息
return [
'filename' => $filename,
'filepath' => $filepath,
'size' => filesize($filepath),
'count' => count($customers)
];
}
/**
* @param $params
* @return bool
@@ -118,13 +271,10 @@ class CustomerService
$logic = new CustomerLogic();
if($params['scene'] == 'delete'){
return $logic->delete($params['id']);
return $logic->forceDelete($params['id']);
}else{
return $logic->status($params['id'], 2);
return $logic->status($params['id'], 1);
}
}
/**