增加询单发布&分配&用户全量

This commit is contained in:
mzl
2025-07-23 17:06:48 +08:00
parent b22db8ac35
commit 36288fa64d
9 changed files with 149 additions and 539 deletions
+29 -520
View File
@@ -4,6 +4,7 @@ namespace app\inquiry\controller;
use app\common\controller\BaseController;
use app\common\traits\crud\CrudControllerTrait;
use app\inquiry\service\InquiryLogService;
use support\Request;
class InquiryLogController extends BaseController
{
@@ -24,6 +25,11 @@ class InquiryLogController extends BaseController
*/
protected function indexFormat($data)
{
$data['countdown'] = time()-1;
if (isset($data['r_time'])) {
$countdown = 60 * 60;
$data['countdown'] = (time() - $data['r_time']) < $countdown ? $data['r_time'] + (60*60) : time()-1;
}
if (isset($data['created_at']))
$data['created_at'] = date('Y-m-d H:i:s', $data['created_at']);
if (isset($data['updated_at']))
@@ -32,538 +38,41 @@ class InquiryLogController extends BaseController
$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;
}
// 列表
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('请求方式错误');
}
// 立即发布
/**
* 发布
*
* @param Request $request
* @return mixed
*/
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());
$data = $this->validate($this->service->getValidator(), $request->post(), 'release');
$res = $this->service->serviceRelease($request, $data);
return $this->success([], '发布成功');
} catch (\Throwable $e) {
return $this->error($e->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');
}
// 客服分单给业务
/**
* 分配
*
* @param Request $request
* @return mixed
*/
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());
$data = $this->validate($this->service->getValidator(), $request->post(), 'allocation');
$res = $this->service->serviceAllocation($request, $data);
return $this->success([], '分配成功');
} catch (\Throwable $e) {
return $this->error($e->getMessage());
}
}