This commit is contained in:
2026-01-05 14:33:21 +08:00
commit ee348b4925
451 changed files with 39954 additions and 0 deletions
@@ -0,0 +1,51 @@
<?php
namespace app\crmAdmin\controller;
use support\Request;
use support\Response;
use support\exception\BusinessException;
use app\common\traits\ResponseTrait;
use think\Validate;
abstract class BaseController
{
use ResponseTrait;
protected mixed $validator = null;
/**
* 验证数据
*
* @param Validate $validator 验证器实例
* @param array $data 待验证的数据
* @param string $scene 验证场景
* @return array 验证后的数据
* @throws BusinessException 验证失败时抛出业务异常
*/
public function validate(Validate $validator, array $data, string $scene = ''): array
{
// 如果指定了验证场景,并且验证器支持场景方法,则设置验证场景
if ($scene && method_exists($validator, 'scene')) {
$validator->scene($scene);
}
// 执行数据验证
if (!$validator->check($data)) {
// 验证失败,抛出业务异常,异常信息为验证器返回的错误信息
throw new BusinessException($validator->getError());
}
// 验证成功,返回原始数据
return $data;
}
public function page($result){
$current = $result->currentPage();
$total = $result->total();
$size = $result->perPage();
$result = $result->toArray();
$result['current'] = $current;
$result['total'] = $total;
$result['size'] = $size;
return $result;
}
}
@@ -0,0 +1,270 @@
<?php
namespace app\crmAdmin\controller;
use app\common\model\Corp;
use app\crm\model\Customer;
use app\crm\utils\Util;
use app\crmAdmin\service\CustomerService;
use support\Request;
class CustomerController extends BaseController
{
/**
* 不需要头信息的方法
*/
protected $noNeedHeader = [];
protected $noNeedHeaderData = ['export'];
protected $noNeedLogin = ['export'];
/**
* 客户列表
* @param Request $request
* @return \support\Response
*/
public function list(Request $request)
{
try {
$param = $request->all();
$corp = Corp::where(['abbr' => $request->basic_data['corp_abbr']])->first();
$param['corp_id'] = $corp->id;
// $param['corp_abbr'] = $request->basic_data['corp_abbr'];
$result = (new CustomerService())->list($param);
return $this->success($this->page($result));
} catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getFile().$e->getLine().$e->getTraceAsString());
}
}
/**
* 客户导出
* @param Request $request
* @return \support\Response
*/
public function export(Request $request)
{
try {
$param = $request->all();
// $corp = Corp::where(['abbr' => $request->basic_data['corp_abbr']])->first();
// $param['corp_id'] = $corp->id;
$result = (new CustomerService())->export($param);
$excelContent = file_get_contents($result['filepath']);
// 生成文件名
$filename = '客户数据_' . date('Y-m-d_H-i-s') . '.xlsx';
return response()
->withHeader('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
->withHeader('Content-Disposition', 'attachment; filename="'.$filename.'"')
->withBody($excelContent);
/*$result = (new CustomerService())->export($param);
// 检查文件是否存在
if (!file_exists($result['filepath'])) {
return $this->error('导出文件生成失败');
}
// 获取文件信息
$fileSize = filesize($result['filepath']);
$filename = $result['filename'];
// 设置响应头
$response = response()->file($result['filepath']);
$response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
$response->header('Content-Disposition', 'attachment; filename="' . $filename . '"');
$response->header('Content-Length', $fileSize);
$response->header('Cache-Control', 'no-cache, must-revalidate');
$response->header('Pragma', 'no-cache');
return $response;*/
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}
/**
* 审核客户
* @param Request $request
* @return \support\Response
*/
public function audit(Request $request)
{
try {
$param = $request->all();
if (empty($param['id'])) {
return $this->error('ID'.trans('not_null'));
}
if (!in_array($param['scene'],['delete','report'])) {
return $this->error('scene 不合法');
}
$result = (new CustomerService())->audit($param);
return $this->success($result);
} catch (\Throwable $e) {
return $this->error($e->getMessage().$e->getFile().$e->getLine().$e->getTraceAsString());
}
}
/**
* 批量转让
* @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 delete(Request $request)
{
try {
$param = $request->all();
$scene = $param['scene'] ?? '';
$status_desc = $param['status_desc'] ?? '';
if($scene != 'force' && empty($param['status_desc'])){
return $this->error( '移除原因不能为空');
}
if (empty($param['customer_id'])) {
return $this->error('customer_id'.trans('not_null'));
}
$customer_id = explode(',', $param['customer_id']);
$query = Customer::whereIn('id',$customer_id)->withTrashed();
if(!$query->exists()){
return $this->error( trans('fail'));
}
if ($scene === 'list') { //管理端客户列表移除
$res = $query->update(['status' => 4, 'status_desc' => $status_desc, 'deleted_at' => time()]);
} elseif ($scene === 'force') { //强删除
$res = $query->forceDelete();
} else { //公海软删除
$res = $query->update(['status' => 3, 'status_desc' => $status_desc, 'deleted_at' => time()]);
}
return $this->success($res);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
/**
* 客户恢复
* @param Request $request
* @return \support\Response
*/
public function restore(Request $request)
{
try {
$id = $request->input('id', '');
if (Util::isEmpty($id)) {
return $this->error(trans('id_empty'));
}
$ids = explode(',', $id);
$query = Customer::withTrashed()->whereIn('id', $ids);
if (!$query->exists()) {
return $this->error(trans('fail'));
}
// 恢复软删除(清空 deleted_at)
$query->restore();
// 恢复为在库状态,放入公海
$res = Customer::whereIn('id', $ids)->update(['status' => 1, 'user_id' => 0]);
return $this->success($res, trans('success'));
} 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());
}
}
/**
* 客户详情
* @param Request $request
* @return \support\Response
*/
public function follow(Request $request)
{
try {
$param = $request->all();
if (empty($param['id'])) {
return $this->error('id'.trans('not_null'));
}
$result = (new CustomerService())->follow($param);
return $this->success($result);
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
/**
* 离职员工客户分配
* @param Request $request
* @return \support\Response
*/
public function allocation(Request $request)
{
try {
$param = $request->all();
if (empty($param['corp_id'])) {
return $this->error('corp_id'.trans('not_null'));
}
if (empty($param['user_id'])) {
return $this->error('user_id'.trans('not_null'));
}
$result = (new CustomerService())->allocation($param);
return $this->success($result,'成功分配'.$result['userCount'].'位离职用户的'.$result['customerCount'].'位客户');
} catch (\Throwable $th) {
return $this->error($th->getMessage());
}
}
}
@@ -0,0 +1,187 @@
<?php
namespace app\crmAdmin\controller;
use app\common\controller\BaseController;
use app\common\model\Country;
use app\common\model\InquiryChannel;
use app\common\model\InquiryLog;
use app\common\traits\crud\CrudControllerTrait;
use app\crm\model\Customer;
use app\inquiry\service\CountryService;
use support\Request;
use support\Db;
use Illuminate\Support\Carbon;
class StatisticsController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new CountryService();
}
public function whole()
{
$totals = Customer::count('id');
$channelCount = InquiryChannel::count('id');
$todayCount = Customer::where('created_at', '>=', strtotime(date('Y-m-d')))->count('id');
$releaseCount = Customer::distinct('enter_user_id')->count('id');
$countryCount = Country::count('id');
$trackCount = Customer::distinct('user_id')->count();
$data = [
'totals' => $totals,
'channelCount' => $channelCount,
'todayCount' => $todayCount,
'releaseCount' => $releaseCount,
'countryCount' => $countryCount,
'trackCount' => $trackCount
];
return $this->success($data);
}
// 各渠道计数
public function channelCount()
{
$inquiryChannelList = InquiryChannel::all(['id', 'name']);
$channels = [];
foreach ($inquiryChannelList as $key => $val) {
$channels[$val->id] = $val->name;
}
$list = Customer::select('channel_id', Db::raw('COUNT(*) as totals'))
->groupBy('channel_id')
->get()->toArray();
$channelCounts = [];
foreach ($list as $vo) {
$channelCounts[] = [
"name" => $channels[$vo["channel_id"]],
"value" => $vo["totals"]
];
}
return $this->success($channelCounts);
}
// 各国家计数
public function countryCount()
{
$list = Customer::with("countryInfo")
->select('country_id', Db::raw('COUNT(*) as totals'))
->where('country_id','<>',0)
->groupBy('country_id')
->get();
$arr = [];
foreach ($list as $val) {
$arr[] = [
"value" => $val->totals,
"name" => $val->countryInfo->cn_addr
];
}
return $this->success($arr);
}
// 跟单人计数
public function trackCount()
{
$list = Customer::with(["user"])
->where("user_id", ">", 0)
->whereNotIn('user_id', [1,5,10,11,13,155,16,17,19,74,75,78,141,147])
->select('user_id', Db::raw('COUNT(*) as totals'))
->groupBy('user_id')
->get();
$optionType = [];
$counts = [];
foreach ($list as $val) {
if (isset($val->user) && $val->user) {
$optionType[] = $val->user->name;
$counts[] = $val->totals;
}
}
$arr = [
"optionType" => $optionType,
"counts" => $counts
];
return $this->success($arr);
}
// 每日录单数
public function dayCount()
{
$list = Db::table('crm_customer')
->where('created_at', '>=', Carbon::now()->subDay(7)->timestamp)
->select(
Db::raw('DATE(FROM_UNIXTIME(created_at)) as date'),
Db::raw('COUNT(*) as order_count')
)
->groupBy('date')
->pluck('order_count', 'date')->toArray();
$optionType = [];
$counts = [];
foreach ($list as $key=>$val) {
$optionType[] = $key;
$counts[] = $val;
}
$arr = [
"optionType" => $optionType,
"counts" => $counts
];
return $this->success($arr);
}
public function customerCount(Request $request)
{
$companyId = $request->get('company_id') ?? 0;
// 1. 计算时间范围
$now = Carbon::now();
$sixMonthsAgo = $now->copy()->subMonths(5)->startOfMonth(); // 包含6个月
// 2. 获取关联渠道ID
$channelIds = $this->getChannelIdsByCompany($companyId);
if (empty($channelIds)) {
return collect();
}
// 3. 执行查询
$list = Customer::select([
DB::raw("DATE_FORMAT(FROM_UNIXTIME(created_at), '%Y-%m') AS month"),
DB::raw('COUNT(*) AS total_count')
])
->whereIn('channel_id', $channelIds)
->where('created_at', '>', 0)
->where('created_at', '>=', $sixMonthsAgo->timestamp) // 添加时间限制
->groupBy('month')
->orderBy('month')
->get();
$optionType = [];
$counts = [];
foreach ($list as $item) {
$optionType[] = $item->month;
$counts[] = $item->total_count;
}
$arr = [
"optionType" => $optionType,
"counts" => $counts
];
return $this->success($arr);
}
/**
* 获取公司与渠道的映射
*/
private function getChannelIdsByCompany($companyId)
{
$data = [
0 => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
1 => [1, 2, 3, 4, 5, 6, 7, 8],
3 => [9, 10, 11]
];
return $data[$companyId] ?? [];
}
}