Files
fdesk/app/inquiry/controller/InquiryLogController.php
T

698 lines
29 KiB
PHP
Raw Normal View History

2025-07-22 15:13:27 +08:00
<?php
namespace app\inquiry\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\inquiry\service\InquiryLogService;
class InquiryLogController extends BaseController
{
use CrudControllerTrait;
protected $service;
public function __construct()
{
$this->service = new InquiryLogService();
}
/**
* 列表页数据格式化
*
* @param array $data
* @return array
*/
2025-07-23 15:02:09 +08:00
protected function indexFormat($data)
{
if (isset($data['created_at']))
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
if (isset($data['updated_at']))
$data['updated_at'] = date('Y-m-d H:i:s', $data['updated_at']);
if (isset($data['r_time']))
$data['r_time'] = $data['r_time'] > 0 ? date('Y-m-d H:i:s', $data['r_time']) : 0;
if (isset($data['t_time']))
$data['t_time'] = $data['t_time'] > 0 ? date('Y-m-d H:i:s', $data['t_time']) : 0;
return $data;
}
2025-07-22 15:13:27 +08:00
// 列表
public function list(Request $request)
{
if ($request->method() == 'POST') {
$data = $request->all();
$limit = isset($data['limit']) && $data['limit'] ? $data['limit'] : 10;
// 状态
$roleId = $request->user->role_id;
$where = [];
if (in_array($roleId, [RoleRulesModel::ALL_ROLE_ID, RoleRulesModel::KEFU_ROLE_ID])) {
if (isset($data["status"]) && $data["status"] != "") {
$where[] = [
"status", "=", $data["status"]
];
} else {
$where[] = [
"status", ">", -1
];
}
} else {
$where[] = [
"status", "=", 1
];
}
// 检索渠道
if (isset($data["channel"]) && $data["channel"]) {
$where[] = [
"log_type", "=", $data["channel"]
];
}
// 检索国家
if (isset($data["country"]) && $data["country"]) {
$where[] = [
"log_country", "=", $data["country"]
];
}
// 检索时间
if (isset($data["ld_time"]) && $data["ld_time"]) {
$times = explode(' - ', $data["ld_time"]);
$specificDayStart = trim($times[0]);
$specificDayEnd = trim($times[1]);
$where[] = [
"c_time", ">", strtotime($specificDayStart)
];
$where[] = [
"c_time", "<=", strtotime($specificDayEnd)
];
}
// 检查渠道(不取缓存中的值)
// $channel = $request->user->channel;
$uinfos = UsersModel::where('id', $request->user->uid)->first();
$channel = $uinfos->channel;
if ($channel) {
$channel = explode(",", $channel);
$InquiryLogModelModel = InquiryLogModel::where($where)->whereIn("log_type", $channel);
} else {
$InquiryLogModelModel = InquiryLogModel::where($where);
}
// 关键字
if (isset($data["kwd"]) && $data["kwd"] != "") {
$InquiryLogModelModel->whereRaw("concat(`log_contact_type`,`log_contact_tel`,`log_contact_whatsapp`,`log_contacts`,`order_no`) like '%".$data['kwd']."%'");
}
$list = $InquiryLogModelModel
->with([
"eUserInfo"=>function($query) {
$query->where("id", ">", 0);
}, "rUserInfo"=>function($query) {
$query->where("id", ">", 0);
}, "countryInfo"
])
->orderBy("id", "desc")
->paginate($limit);
$configsModel = new ConfigsModel();
$logLevel = $configsModel->getConfig("log_level");
$countdown = (int)($configsModel->getConfig("score_countdown")) * 60;
$inquiryChannelList = ChannelModel::all(['id', 'name']);
$inquiryChannel = [];
foreach ($inquiryChannelList as $key => $val) {
$inquiryChannel[$val->id] = $val->name;
}
$arr = [];
foreach ($list->items() as $key => $val) {
$expiry_time = $val->r_time + $countdown;
$is_expiry = 0;
$expiry_ts = 0;
if ($expiry_time <= time()) {
$is_expiry = 1;
} else {
$diff = $expiry_time - (int)time();
$expiry_ts = $diff * 1000;
}
$rUinfo = "";
if ($val->rUserInfo) {
$rUinfo = $val->rUserInfo->name;
}
$eUinfo = "";
if ($val->eUserInfo) {
$eUinfo = $val->eUserInfo->name;
}
/**
* 显示联系方式条件:
* 1.已跟单并且当前用户是接单人
* 2.主权限
* 3.当前用户是录单人
*/
if (
($val->status == 2 && $request->user->uid == $val->recipient) ||
($roleId == RoleRulesModel::ALL_ROLE_ID) ||
($request->user->uid == $val->enter_user_id)
) {
$log_contact_type = $val->log_contact_type == "" ? " 无 " : $val->log_contact_type;
$log_contact_tel = $val->log_contact_tel == "" ? " 无 " : $val->log_contact_tel;
$log_contact_whatsapp = $val->log_contact_whatsapp == "" ? " 无 " : $val->log_contact_whatsapp;
} else {
$log_contact_type = "******";
$log_contact_tel = "******";
$log_contact_whatsapp = "******";
}
$arr[] = [
"id" => $val->id,
"order_no" => $val->order_no,
"enter_user_id" => $val->enter_user_id,
"recipient" => $val->recipient,
"log_quality" => $val->log_quality,
"log_quality_desc" => $logLevel[$val->log_quality],
"log_quality_color" => InquiryLogModel::LEVEL_BUTTON_COLOR[$val->log_quality],
"log_type" => $val->log_type,
"log_type_desc" => $inquiryChannel[$val->log_type],
"log_contact_type" => $log_contact_type,
"log_contact_tel" => $log_contact_tel,
"log_contact_whatsapp" => $log_contact_whatsapp,
"log_country" => $val->countryInfo->cn_addr."(".$val->countryInfo->two_letter.")",
"log_country_steering" => $val->countryInfo->steering,
"log_contacts" => $val->log_contacts,
"log_content" => $val->log_content,
"e_time" => $val->status==1?date("Y-m-d H:i:s", $expiry_time):"",
"status" => $val->status,
"status_color" => InquiryLogModel::STATUS_BUTTON_COLOR[$val->status],
"status_desc" => InquiryLogModel::STATUS[$val->status],
"is_expiry" => $is_expiry,
"expiry_ts" => $expiry_ts,
"rUinfo" => $rUinfo,
"eUinfo" => $eUinfo,
"r_time" => $val->r_time?date("Y-m-d H:i:s", $val->r_time):"",
"n_time" => date("Y-m-d H:i:s", time()),
"c_time" => date("Y-m-d H:i:s", strtotime($val->c_time)),
"u_time" => date("Y-m-d H:i:s", strtotime($val->u_time))
];
}
$data = [
'lastPage' => $list->lastPage(),
'currentPage' => $list->currentPage(),
'hasMorePages' => $list->hasMorePages(),
'list' => $arr,
'counts' => $list->total()
];
return $this->successResponse($data);
}
return view('inquiry-log/list');
}
// 添加
public function add(Request $request)
{
if ($request->method() == "POST") {
$enterUserId = $request->user->uid;
$data = $request->all();
if ($data["log_contact_type"]) {
$check1 = InquiryLogModel::where("log_contact_type", trim($data["log_contact_type"]))->first();
if ($check1 != NULL) {
return $this->errorResponse('邮箱-联系方式已存在');
}
}
if ($data["log_contact_tel"]) {
$check2 = InquiryLogModel::where("log_contact_tel", trim($data["log_contact_tel"]))->first();
if ($check2 != NULL) {
return $this->errorResponse('电话-联系方式已存在');
}
}
if ($data["log_contact_whatsapp"]) {
$check3 = InquiryLogModel::where("log_contact_whatsapp", trim($data["log_contact_whatsapp"]))->first();
if ($check3 != NULL) {
return $this->errorResponse('whatsapp-联系方式已存在');
}
}
if (!$data["log_contact_type"] && !$data["log_contact_tel"] && !$data["log_contact_whatsapp"]) {
$check4 = InquiryLogModel::where("log_contacts", trim($data["log_contacts"]))->where("log_country", $data["country"])->first();
if ($check4 != NULL) {
return $this->errorResponse('联系人已存在');
}
}
$model = new InquiryLogModel();
$model->order_no = orderNum();
$model->enter_user_id = $enterUserId;
$model->recipient = 0;
$model->log_type = $data["log_type"];
$model->log_contact_type = trim($data["log_contact_type"]);
$model->log_contact_tel = trim($data["log_contact_tel"]);
$model->log_contact_whatsapp = trim($data["log_contact_whatsapp"]);
$model->log_country = $data["country"];
$model->log_contacts = $data["log_contacts"];
$model->log_content = $data["log_content"];
$model->log_quality = $data["log_quality"];
$model->status = 0;
if ($model->save()) {
return $this->successResponse([], '添加成功');
} else {
return $this->errorResponse('添加失败,请重试');
}
}
return view('inquiry-log/add');
}
// 编辑
public function edit(Request $request)
{
if ($request->method() == "POST") {
$data = $request->all();
$model = new InquiryLogModel();
$info = $model->where("id", $data["id"])->first();
if ($data["log_contact_type"]) {
$check1 = InquiryLogModel::where("log_contact_type", trim($data["log_contact_type"]))->first();
if ($check1 != NULL && $check1->id != $data["id"]) {
return $this->errorResponse('邮箱-联系方式已存在');
}
}
if ($data["log_contact_tel"]) {
$check2 = InquiryLogModel::where("log_contact_tel", trim($data["log_contact_tel"]))->first();
if ($check2 != NULL && $check2->id != $data["id"]) {
return $this->errorResponse('电话-联系方式已存在');
}
}
if ($data["log_contact_whatsapp"]) {
$check3 = InquiryLogModel::where("log_contact_whatsapp", trim($data["log_contact_whatsapp"]))->first();
if ($check3 != NULL && $check3->id != $data["id"]) {
return $this->errorResponse('whatsapp-联系方式已存在');
}
}
if (!$data["log_contact_type"] && !$data["log_contact_tel"] && !$data["log_contact_whatsapp"]) {
$check4 = InquiryLogModel::where("log_contacts", trim($data["log_contacts"]))->where("log_country", $data["country"])->first();
if ($check4 != NULL && $check4->id != $data["id"]) {
return $this->errorResponse('联系人已存在');
}
}
$info->log_type = $data["log_type"];
$info->log_contact_type = $data["log_contact_type"];
$info->log_contact_tel = $data["log_contact_tel"];
$info->log_contact_whatsapp = $data["log_contact_whatsapp"];
$info->log_country = $data["country"];
$info->log_contacts = $data["log_contacts"];
$info->log_content = $data["log_content"];
$info->log_quality = $data["log_quality"];
if (!$info->save()) {
return $this->errorResponse('操作失败,请重试');
}
return $this->successResponse([], '操作成功');
}
return view('inquiry-log/edit');
}
// 删除
public function del(Request $request)
{
if ($request->method() == "POST") {
$id = $request->post("id");
$rst = InquiryLogModel::where("id", $id)->first();
if (!$rst) {
return $this->errorResponse('询单未找到');
}
if ($rst->enter_user_id != $request->user->uid) {
return $this->errorResponse('只能删除自己录入的询单');
}
$res = InquiryLogModel::where("id", $id)->where("enter_user_id", $request->user->uid)->delete();
if (!$res) {
return $this->errorResponse('操作失败,请重试');
}
fileLog('ID:'.$request->user->uid.' - 删除:询单ID:'.$id);
return $this->successResponse([], '操作成功');
}
return $this->errorResponse('请求方式错误');
}
// 单条信息
public function info(Request $request)
{
if ($request->method() == "POST") {
$id = $request->all("id");
$info = InquiryLogModel::where("id", $id)->first()->toArray();
$list = CountryModel::all(["cn_addr", "two_letter", "id"]);
$arr = [];
foreach ($list as $vo) {
$arr[] = [
"value" => $vo->id,
"name" => $vo->cn_addr."(".$vo->two_letter.")"
];
}
$info["guojia"] = $arr;
$info["guojia_id"] = [$info["log_country"]];
$configsModel = new ConfigsModel();
// $data = $configsModel->getConfig("inquiry_channel");
$inquiryChannelList = ChannelModel::all(['id', 'name']);
$data = [];
foreach ($inquiryChannelList as $key => $val) {
$data[$val->id] = $val->name;
}
$arr = [];
foreach ($data as $ky => $vo) {
$arr[] = [
"id" => $ky,
"name" => $vo
];
}
$info["qudao"] = $arr;
$data1 = $configsModel->getConfig("log_level");
$arr1 = [];
foreach ($data1 as $ky => $vo) {
$arr1[] = [
"id" => $ky,
"name" => $vo
];
}
$info["dengji"] = $arr1;
if (!$info) {
return $this->errorResponse('查询失败,请重试');
}
$info["log_content"] = strTh($info["log_content"]);
return $this->successResponse($info, 'success');
}
return $this->errorResponse('请求方式错误');
}
// 立即发布
public function release(Request $request)
{
try {
if ($request->method() == "POST") {
$id = $request->post("id");
if (!in_array($request->user->role_id, [RoleRulesModel::ALL_ROLE_ID, RoleRulesModel::KEFU_ROLE_ID])) {
return $this->errorResponse('无操作权限');
}
$info = InquiryLogModel::where("id", $id)->first();
if (!$info) {
return $this->errorResponse('询单信息未找到');
}
if ($info->status != 0) {
return $this->errorResponse('状态异常,无法发布');
}
$info->r_time = time();
$info->status = 1;
if (!$info->save()) {
return $this->errorResponse('发布失败,请重试');
}
// 发送飞书群消息通知
$data = ["order_id"=>$id];
RedisQueueRedis::send("release", $data);
// 发送飞书群消息通知
return $this->successResponse([], '发布成功');
}
return $this->errorResponse('电话-请求方式错误');
} catch (\Throwable $th) {
fileLog($th->getFile()."|".$th->getLine()."|".$th->getMessage());
return $this->errorResponse($th->getMessage());
}
}
// 立即跟单
public function track(Request $request)
{
// Db::beginTransaction();
try {
if ($request->method() == "POST") {
$id = $request->post("id");
$uid = $request->user->uid;
if (!in_array($request->user->role_id, [RoleRulesModel::ALL_ROLE_ID, RoleRulesModel::YEWU_ROLE_ID])) {
// Db::rollBack();
return $this->errorResponse('无操作权限');
}
$info = InquiryLogModel::where("id", $id)->first();
if (!$info) {
return $this->errorResponse('询单信息未找到');
}
if ($info->status != 1) {
// Db::rollBack();
return $this->errorResponse('状态异常,无法跟单');
}
// 更新询单
$info->recipient = $uid;
$info->t_time = time();
$info->status = 2;
// 记录跟单
$model = new UserOrderModel();
$model->uid = $request->user->uid;
$model->log_id = $id;
$model->score = (new InquiryLogModel())->score($info->log_quality, $info->r_time);
if (!$info->save() || !$model->save()) {
// Db::rollBack();
return $this->errorResponse('操作失败,请重试');
}
// Db::commit();
// // 发送飞书群消息通知
$data1 = ["order_id"=>$id];
RedisQueueRedis::send("track", $data1);
// // 发送飞书群消息通知
// // 同步询单到crm
$data2 = ["order_id"=>$id];
RedisQueueRedis::send("inquiry-sync", $data2);
// // 同步询单到crm
return $this->successResponse([], '跟单操作成功');
} else {
// Db::rollBack();
return $this->errorResponse('请求方式错误');
}
} catch (\Throwable $th) {
// Db::rollBack();
fileLog($th->getFile()."|".$th->getLine()."|".$th->getMessage());
return $this->errorResponse($th->getMessage());
}
}
// 详情
public function detail(Request $request)
{
if ($request->method() == "POST") {
$id = $request->all("id");
$res = InquiryLogModel::where("id", $id)->first()->toArray();
return $this->successResponse(["detail"=>$res["log_content"]], '操作成功');
}
return view('inquiry-log/detail');
}
// 客服分单给业务
public function allocation(Request $request)
{
// Db::beginTransaction();
try {
if ($request->method() == "POST") {
$data = $request->all();
$userInfo = UsersModel::where("id", $data["gd_user"])->first();
if (!$userInfo) {
// Db::rollBack();
return $this->errorResponse('用户未找到');
}
$model = new InquiryLogModel();
$info = $model->where("id", $data["id"])->first();
if (!$info) {
// Db::rollBack();
return $this->errorResponse('询单未找到');
}
$uomodel = new UserOrderModel();
$uoInfo = $uomodel->where("log_id", $data["id"])->first();
if (!$uoInfo) {
// 记录跟单
$HsUserOrderModel = new UserOrderModel();
$HsUserOrderModel->uid = $data["gd_user"];
$HsUserOrderModel->log_id = $data["id"];
$HsUserOrderModel->score = (new InquiryLogModel())->score($info->log_quality, $info->r_time);
if (!$HsUserOrderModel->save()) {
// Db::rollBack();
return $this->errorResponse('记录跟单操作失败,请重试');
}
} else {
$uoInfo->uid = $data["gd_user"];
if (!$uoInfo->save()) {
// Db::rollBack();
return $this->errorResponse('操作失败,请重试');
}
}
if ($info->status != 2) {
$info->t_time = time();
$info->status = 2;
}
$info->recipient = $data["gd_user"];
if (!$info->save()) {
// Db::rollBack();
return $this->errorResponse('操作失败,请重试');
}
// Db::commit();
// 发送飞书群消息通知
$data1 = ["order_id"=>$data["id"]];
RedisQueueRedis::send("allocation", $data1);
// 发送飞书群消息通知
// // 同步询单到crm
$data2 = ["order_id"=>$data["id"]];
RedisQueueRedis::send("inquiry-sync", $data2);
// // 同步询单到crm
return $this->successResponse([], '操作成功');
}
return view('inquiry-log/allocation');
} catch (\Throwable $th) {
// Db::rollBack();
fileLog($th->getFile()."|".$th->getLine()."|".$th->getMessage());
return $this->errorResponse($th->getMessage());
}
}
// 导出
public function export(Request $request)
{
if ($request->method() == "POST") {
$data = $request->all();
$where = [];
if (isset($data['log_lusers']) && $data['log_lusers']) {
$where[] = ['enter_user_id', '=', $data['log_lusers']];
}
if (isset($data['log_gusers']) && $data['log_gusers']) {
$where[] = ['recipient', '=', $data['log_gusers']];
}
if (isset($data['log_country']) && $data['log_country']) {
$where[] = ['log_country', '=', $data['log_country']];
}
if (isset($data['log_channel']) && $data['log_channel']) {
$where[] = ['log_type', '=', $data['log_channel']];
}
if (isset($data['log_times']) && $data['log_times']) {
$times = explode(' - ', $data["log_times"]);
$specificDayStart = trim($times[0]);
$specificDayEnd = trim($times[1]);
$where[] = [
"c_time", ">", strtotime($specificDayStart)
];
$where[] = [
"c_time", "<=", strtotime($specificDayEnd)
];
}
// 查询
$results = InquiryLogModel::with(['enterUser', 'recipientUser', 'country'])
->where($where)
->get()
->map(function ($log) use($data) {
$arr = [];
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['订单号'] = $log->order_no;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['录入人'] = $log->enterUser->name ?? null;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['接单人'] = $log->recipientUser->name ?? null;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['等级'] = $log->log_quality;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['邮箱'] = $log->log_contact_type;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['电话'] = $log->log_contact_tel;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['WhatsApp'] = $log->log_contact_whatsapp;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['联系人'] = $log->log_contacts;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['国家(中文)'] = $log->country->cn_addr ?? null;
}
if (isset($data['hsl_order_no']) && $data['hsl_order_no']) {
$arr['国家(英文)'] = $log->country->en_full_name ?? null;
}
if (isset($data['hsl_r_time']) && $data['hsl_r_time']) {
$arr['发单时间'] = $log->r_time ? date('Y-m-d H:i:s', $log->r_time) : null;
}
if (isset($data['hsl_t_time']) && $data['hsl_t_time']) {
$arr['跟单时间'] = $log->t_time ? date('Y-m-d H:i:s', $log->t_time) : null;
}
return $arr;
});
// 创建一个新的 Spreadsheet 对象
$spreadsheet = new Spreadsheet();
// 设置当前活动的工作表
$sheet = $spreadsheet->getActiveSheet();
// 设置工作表的标题
$sheet->setCellValue('A1', '订单号');
$sheet->setCellValue('B1', '录入人');
$sheet->setCellValue('C1', '接单人');
$sheet->setCellValue('D1', '等级');
$sheet->setCellValue('E1', '邮箱');
$sheet->setCellValue('F1', '电话');
$sheet->setCellValue('G1', 'WhatsApp');
$sheet->setCellValue('H1', '联系人');
$sheet->setCellValue('I1', '国家(中文)');
$sheet->setCellValue('J1', '国家(英文)');
$sheet->setCellValue('K1', '发单时间');
$sheet->setCellValue('L1', '跟单时间');
$data = $results->toArray();
// 将数据填充到工作表中
$row = 2; // 从第2行开始填充
foreach ($data as $item) {
$sheet->setCellValue('A' . $row, $item['订单号']);
$sheet->setCellValue('B' . $row, $item['录入人']);
$sheet->setCellValue('C' . $row, $item['接单人']);
$sheet->setCellValue('D' . $row, $item['等级']);
$sheet->setCellValue('E' . $row, $item['邮箱']);
$sheet->setCellValue('F' . $row, $item['电话']);
$sheet->setCellValue('G' . $row, $item['WhatsApp']);
$sheet->setCellValue('H' . $row, $item['联系人']);
$sheet->setCellValue('I' . $row, $item['国家(中文)']);
$sheet->setCellValue('J' . $row, $item['国家(英文)']);
$sheet->setCellValue('K' . $row, $item['发单时间']);
$sheet->setCellValue('L' . $row, $item['跟单时间']);
$row++;
}
// 创建 Excel 文件的写入器
$writer = new Xlsx($spreadsheet);
// 名称
$fileName = $request->user->name.'_'.date('Ymd').'_'.uniqid().'.xlsx';
// 路径
$filePath = '/export/'.$fileName;
// 将文件保存到本地
$writer->save(public_path().$filePath);
return $this->successResponse(['url'=>$filePath, 'name'=>$fileName]);
}
return view('inquiry-log/export');
}
}