Files
crm-server/app/process/Task.php
T
2025-04-17 10:14:01 +08:00

188 lines
8.0 KiB
PHP

<?php
namespace app\process;
use app\controller\FeishuController;
use app\model\Customer;
use app\model\CustomerHis;
use app\model\Daily;
use app\model\FeishuUser;
use app\model\RequestTracks;
use app\model\User;
use support\Log;
use Webman\App;
use Workerman\Crontab\Crontab;
class Task
{
public function onWorkerStart()
{
// 每半小时执行一次
new Crontab('0 */30 * * * *', function () {
echo date('Y-m-d H:i:s') . "\n";
Log::info('send follow start', [time()]);
// FeishuController::send('xinyouhe','ou_e33bb3017ada6c4781398a68e55a4696', '跟进提醒', '您有新的跟进提醒', '');
// $customer = Customer::where(['follow_next', '<=', time() + 3600])
// ->where(['follow_next', '>', time()])
// ->where(['warn', '<', 1])
// ->get();
// foreach ($customer as $item) {
// $feishuUser = FeishuUser::where(['user_id' => $item->user_id])->first();
// if ($feishuUser) {
// // 获取凭证
// $certificate = config($this->config);
// // 使用parse_url解析URL
// $parsedUrl = parse_url($certificate['rUrl']);
// // 获取域名和端口
// $host = $parsedUrl['host'];
// $port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '';
// $url = $host .$port. '/pages/customer/detile?id='.$customer->id;
// $item->warn = 1;
// $item->update();
// $user = User::first()->where('id', $item->user_id);
// Log::info('send',['open_id'=>$item['open_id'],'url'=>$url]);
// // FeishuController::send('xinyouhe','ou_e33bb3017ada6c4781398a68e55a4696', '跟进提醒', '您有新的跟进提醒', $url);
// FeishuController::send($user->sign,$feishuUser->open_id, '跟进提醒', '您有新的跟进提醒', $url);
// }
// }
$where = [
['follow_next', '<=', time() + 3600],
['follow_next', '>', time()],
['warn', '<', 1]
];
$customer = Customer::where($where)->get();
foreach ($customer as $item) {
$feishuUser = FeishuUser::where(['user_id' => $item->user_id])->first();
if ($feishuUser) {
// 获取凭证
$certificate = config('feishu.' . $feishuUser->sign);
// 使用parse_url解析URL
$parsedUrl = parse_url($certificate['rUrl']);
// 获取域名和端口
$host = $parsedUrl['host'];
$port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '';
$url = 'http://' . $host . $port . '/pages/customer/detile?id=' . $item->id;
$item->warn = 1;
$item->update();
Log::info('send', ['open_id' => $item['open_id'], 'url' => $url]);
$content = FeishuController::postMsg('跟进提醒', '您有新的跟进提醒', $url);
FeishuController::send($feishuUser->sign, $feishuUser->open_id, $content, 'post');
}
}
Log::info('send follow start', [time()]);
});
//回收站客户转向公海 每小时执行一次
new Crontab('0 10 * * * *', function () {
echo date('Y-m-d H:i:s') . "\n";
Log::info('send high seas start', [time()]);
$where = [
['deleted_at', '<=', time() - 3600],
['user_id', '<>', 0],
];
$customer = Customer::where($where)->onlyTrashed()->get();
foreach ($customer as $item) {
$item->update(['user_id' => 0, 'deleted_at' => time()]);
CustomerHis::create([
'customer_id' => $item->_id,
'user_his' => $item->user_id,
'user_id' => 0,
]);
}
Log::info('send high seas end', [time()]);
});
//总结日报, 每天执行一次
new Crontab('0 30 17 * * *', function () {
echo date('Y-m-d H:i:s') . " start daily \n";
Log::info('start daily', [time()]);
$where[] = ["user_id", ">", 0];
$where = [
['created_at', '<=', time() - (3600 * 24)],
];
$eventTracking = config('const.event_tracking');
$requestTracks = RequestTracks::selectRaw('user_id,path, COUNT(*) as count',)
// select('*',' COUNT(*) as count')
->where($where)
->whereIn('path', $eventTracking)
->groupBy('user_id', 'path')
// ->orderBy('user_id', 'asc')
->get();
$statistics = [];
$users = [];
$content = '';
foreach ($requestTracks as $item) {
$statistics[$item->user_id][$item->path] = $item->count;
$users[] = $item->user_id;
}
foreach ($users as $userId) {
$data = [
'user_id' => $userId,
'customer_claim' => $statistics[$userId]['customer/claim'] ?? 0,
'customer_save' => $statistics[$userId]['customer/save'] ?? 0,
'follow_add' => $statistics[$userId]['follow/save'] ?? 0,
'customer_collect' => $statistics[$userId]['customer/collect'] ?? 0,
'content' => '',
];
//判断领导
$mast = config('const.daily_group_2');
if (in_array($userId, $mast['member'])) {
$data['leader_id'] = $mast['master'];
}else{
$data['leader_id'] = 5;
}
$content .= date('Y-m-d') . "\n";
$data['content'] .= '领取客户' . ($statistics[$userId]['customer/claim'] ?? 0) . '位\n';
$data['content'] .= '创建或修改客户' . ($statistics[$userId]['customer/save'] ?? 0) . '位\n';
$data['content'] .= '添加跟进' . ($statistics[$userId]['follow/save'] ?? 0) . '次\n';
$data['content'] .= '询盘跟单客户' . ($statistics[$userId]['customer/collect'] ?? 0) . '位\n';
$content .= $data['content'];
$daily = Daily::create($data);
$content .= '请及时查看,30分钟后将向主管发送';
$feishuUser = FeishuUser::where(['user_id' => $userId])->first();
if ($feishuUser) {
$url = config('const.web_domain') . '/pages/mine/workreportdetil?id=' . $daily->id;
$content = FeishuController::postMsg(date('Y-m-d') . '日报', $content, $url);
FeishuController::send($feishuUser->sign, $feishuUser->open_id, $content, 'post');
}
}
Log::info('send high seas end', [time()]);
});
//向领导发送日报 每天执行一次
new Crontab('0 0 18 * * *', function () {
echo date('Y-m-d H:i:s') . "\n";
Log::info('send daily to leader', [time()]);
$where = [];
$where[] = ['send', '=', 0];
$where[] = ['created_at', '>=', strtotime(date('Y-m-d 00:00:00'))];
$daily = Daily::select('leader_id')->where($where)->groupBy('leader_id')->get();
foreach ($daily as $item) {
$feishuUser = FeishuUser::where(['user_id' => $item->leader_id])->first();
if ($feishuUser) {
$url = config('const.web_domain') . '/pages/mine/workreport';
$content = FeishuController::postMsg(date('Y-m-d') . '日报已汇总', '请点击查看', $url);
FeishuController::send($feishuUser->sign, $feishuUser->open_id, $content, 'post');
}
}
});
}
}