Files
crm-server/app/process/Task.php
T

152 lines
6.4 KiB
PHP
Raw Normal View History

2025-03-22 10:26:52 +08:00
<?php
2025-03-22 18:30:38 +08:00
namespace app\process;
2025-03-22 10:26:52 +08:00
use app\controller\FeishuController;
use app\model\Customer;
2025-03-31 14:25:38 +08:00
use app\model\CustomerHis;
2025-03-22 10:26:52 +08:00
use app\model\FeishuUser;
2025-04-11 11:32:41 +08:00
use app\model\RequestTracks;
2025-03-22 10:26:52 +08:00
use app\model\User;
2025-03-22 20:08:01 +08:00
use support\Log;
2025-03-22 10:26:52 +08:00
use Webman\App;
use Workerman\Crontab\Crontab;
class Task
{
2025-03-22 18:30:38 +08:00
2025-03-22 10:26:52 +08:00
public function onWorkerStart()
{
// 每半小时执行一次
2025-03-22 20:09:17 +08:00
new Crontab('0 */30 * * * *', function(){
2025-03-22 10:26:52 +08:00
echo date('Y-m-d H:i:s') . "\n";
2025-03-22 20:08:01 +08:00
Log::info('send follow start',[time()]);
// FeishuController::send('xinyouhe','ou_e33bb3017ada6c4781398a68e55a4696', '跟进提醒', '您有新的跟进提醒', '');
2025-03-23 01:12:59 +08:00
// $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();
2025-03-22 10:26:52 +08:00
foreach ($customer as $item) {
$feishuUser = FeishuUser::where(['user_id' => $item->user_id])->first();
if ($feishuUser) {
// 获取凭证
2025-03-23 01:12:59 +08:00
$certificate = config('feishu.'.$feishuUser->sign);
2025-03-22 10:26:52 +08:00
// 使用parse_url解析URL
$parsedUrl = parse_url($certificate['rUrl']);
// 获取域名和端口
$host = $parsedUrl['host'];
$port = isset($parsedUrl['port']) ? ':' . $parsedUrl['port'] : '';
2025-03-23 01:12:59 +08:00
$url = 'http://'. $host .$port. '/pages/customer/detile?id='.$item->id;
2025-03-22 18:30:38 +08:00
$item->warn = 1;
$item->update();
2025-03-22 20:08:01 +08:00
Log::info('send',['open_id'=>$item['open_id'],'url'=>$url]);
2025-04-11 11:32:41 +08:00
$content = FeishuController::postMsg( '跟进提醒', '您有新的跟进提醒', $url);
FeishuController::send($feishuUser->sign, $feishuUser->open_id, $content, 'post');
2025-03-22 10:26:52 +08:00
}
}
2025-03-23 01:12:59 +08:00
2025-03-22 20:08:01 +08:00
Log::info('send follow start',[time()]);
2025-03-22 10:26:52 +08:00
});
2025-03-31 14:25:38 +08:00
2025-04-11 11:32:41 +08:00
//回收站客户转向公海 每小时执行一次
2025-03-31 14:25:38 +08:00
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) {
2025-04-01 13:50:46 +08:00
$item->update(['user_id' => 0, 'deleted_at' => time()]);
2025-03-31 14:25:38 +08:00
CustomerHis::create([
'customer_id' => $item->_id,
'user_his' => $item->user_id,
'user_id' => 0,
]);
}
2025-04-01 13:50:46 +08:00
Log::info('send high seas end',[time()]);
2025-03-31 14:25:38 +08:00
});
2025-04-11 11:32:41 +08:00
//总结日报, 每天执行一次
new Crontab('0 30 17 * * *', function () {
echo date('Y-m-d H:i:s') . "\n";
Log::info('start daily', [time()]);
$where = [
['created_at', '<=', time() - (3600 * 24)],
];
$requestTracks = RequestTracks::select('*, COUNT(*) as count')
->where($where)
->whereIn('path', ['customer/claim', 'customer/save', 'follow/save', 'customer/collect'])
->groupBy('user_id','path')
->orderBy('user_id', 'asc')
->get();
$statistics = [];
$users = [];
$daily = '';
foreach ($requestTracks as $item) {
$statistics[$item->user_id][$item->path] = $item->count;
$users[] = $item->user_id;
}
foreach ($users as $userId) {
$daily .= date('Y-m-d') . "\n";
$daily .= '领取客户'.$statistics[$userId]['customer/claim'].'位\n';
$daily .= '创建或修改客户'.$statistics[$userId]['customer/save'].'位\n';
$daily .= '添加跟进'.$statistics[$userId]['follow/save'].'次\n';
$daily .= '询盘跟单客户'.$statistics[$userId]['customer/collect'].'位\n';
$daily .= '请及时查看,30分钟后将向主管发送';
$feishuUser = FeishuUser::where(['user_id' => $userId])->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=';
$content = FeishuController::postMsg( '跟进提醒', $daily, $url);
FeishuController::send($feishuUser->sign, $feishuUser->open_id, $content, 'post');
}
}
Log::info('send high seas end', [time()]);
});
2025-03-22 10:26:52 +08:00
}
}