Files
fast_response/admin/application/index/controller/Tasktimer.php
2025-03-28 14:13:19 +08:00

141 lines
4.4 KiB
PHP
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/*
* description
* authorwh
* email
* createTime{2025/3/24} {17:23}
*/
namespace app\index\controller;
use app\api\logic\MessageLogic;
use app\api\logic\TokenLogic;
use think\Controller;
use think\Db;
use wanghua\general_utility_tools_php\Mmodel;
use wanghua\general_utility_tools_php\tool\Tools;
//定时任务
class Tasktimer extends Controller
{
/**
* desc按分钟执行
* authorwh
*
*/
function runMinutes(){
//客户回访计划
Mmodel::catchJson(function (){
$this->returnPlan();
});
}
/**
* desc客户回访计划
* authorwh
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
function returnPlan(){
Tools::log_to_write_txt(['客户回访计划,启动']);
//回访记录
$return_record = Db::table('fa_firmcustomerfollowuprecord')
->where('status','no')
->select();
if(empty($return_record)){
Tools::log_to_write_txt(['无回访记录计划,无需回访']);
return '';
}
foreach ($return_record as $k=>$item){
Tools::log_to_write_txt(['客户回访计划,开始:$item'=>$item]);
$ret = $this->postText($item);
//dump($ret);
Tools::log_to_write_txt(['客户回访计划,结束:$res'=>$ret]);
}
//Tools::log_to_write_txt(['runMinutes出参$res'=>$ret]);
//dump($ret);
return '';
}
/**
* desc发送文字消息
* authorwh
* @return array
*/
private function postText($return_record){
//$firmcustomerfollowuprecord_id = input('firmcustomerfollowuprecord_id',1);
//查询客户
$cust = Db::table('fa_firmcustomer')
->where('id',$return_record['firmcustomer_id'])
->find();
if(empty($cust)){
return Tools::set_fail('有回访计划,客户不存在');
}
//查询客户所绑定的ai客服
$ai_config = Db::table('fa_aicustomerservice')
->where('id',$cust['aicustomerservice_ids'])
->find();
if(empty($ai_config)){
return Tools::set_fail('有回访计划无ai客服服务该客户');
}
$base_url = $ai_config['server_url'];
//查询回访计划
$plan = Db::table('fa_firmcustomerfollowuptimelist')
->where('firmcustomerfollowuprecord_id',$return_record['id'])
->order('followup_time asc')
->find();
if(empty($plan)){
return Tools::set_fail('该客户没有回访计划');
}
//记录创建时间
$create_time = $return_record['create_time'];
$day = (int)$plan['day'];
//在记录创建后的N天N点执行
$day_time = strtotime($create_time) + ($day * 86400);//N天后
if($day_time > time()){
return Tools::set_fail('未到回访时间');
}
$date = date('Y-m-d ',$day_time).$plan['followup_time'];
$exc_time = strtotime($date);//最终的执行时间
dump(date('Y-m-d H:i:s',$exc_time));
//校验时间是否达到执行计划时间
if($exc_time > time()){
return Tools::set_fail('未到计划回访时间');
}
$logic = new MessageLogic();
$logic->setBaseUrl($base_url);
$tokenArr = (new TokenLogic())->getToken();
$data = [
'appId'=>$tokenArr['appId'],
'toWxid'=>$cust['rel_group'],//是群则发送群消息,否则发个人私人消息
'content'=>"@".$cust['wx_nickname']." ".$return_record['visit_msg'],
//@的好友,多个英文逗号分隔。群主或管理员@全部的人,则填写'notify@all'
'ats'=>$cust['rel_wx'],
];
Tools::log_to_write_txt(['回访客户,开始:$post_data'=>$data]);
$res = $logic->postGroupText($data);
Tools::log_to_write_txt(['回访客户,结束:$res'=>$res]);
//修改回访记录回访状态
Db::table('fa_firmcustomerfollowuprecord')
->where('id',$return_record['id'])
->update([
'status'=>'yes',
'send_time'=>Tools::get_now_date(),
]);
return Tools::set_ok('发送成功');
}
}