webman/process/Task3.php

126 lines
4.4 KiB
PHP
Raw Normal View History

2025-02-18 09:42:26 +08:00
<?php
namespace process;
2025-02-22 21:04:05 +08:00
2025-02-18 09:42:26 +08:00
use app\model\UserPhone;
2025-02-22 21:04:05 +08:00
use app\model\ActiveUsers;
2025-02-18 09:42:26 +08:00
use app\dao\UserRewardDao;
use Workerman\Crontab\Crontab;
use app\model\User;
2025-02-23 18:26:45 +08:00
use Carbon\Carbon;
2025-02-18 09:42:26 +08:00
/**
2025-02-18 12:36:42 +08:00
* 获取今天在线时间超过6小时的用户
2025-02-18 09:42:26 +08:00
*/
class Task3
{
public function onWorkerStart()
{
// // 每秒钟执行一次
// new Crontab('*/1 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每5秒执行一次
// new Crontab('*/5 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每分钟执行一次
// new Crontab('0 */1 * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每5分钟执行一次
// new Crontab('0 */5 * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每分钟的第一秒执行
// new Crontab('1 * * * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
// // 每天的7点50执行注意这里省略了秒位
// new Crontab('50 7 * * *', function(){
// echo date('Y-m-d H:i:s')."\n";
// });
2025-02-23 18:26:45 +08:00
// // 每1小时执行一次
2025-02-23 18:27:22 +08:00
new Crontab('0 0 * * * *', function () {
2025-02-18 21:11:29 +08:00
//获取在线时间超过6小时的用户
2025-02-18 09:42:26 +08:00
$today = date('Y-m-d');
2025-02-23 18:26:45 +08:00
$UserPhone = UserPhone::where('day_score', '>=', 120)->get();
2025-02-18 09:42:26 +08:00
//判断用户绑定Whatsapp首次登录
foreach ($UserPhone as $key => $value) {
$user_id = $value->user_id;
2025-02-19 11:47:46 +08:00
//判定有没有父级 这里父级没有默认是0
2025-02-18 09:42:26 +08:00
$user = User::find($user_id);
2025-02-19 11:47:46 +08:00
if ($user->f_id==0) {
2025-02-18 10:33:03 +08:00
continue;
2025-02-23 18:26:45 +08:00
}
//获取该用户的父级
$parent = User::find($user->f_id);
2025-02-18 09:42:26 +08:00
//满足则加80积分
2025-02-23 18:26:45 +08:00
// UserRewardDao::base($parent->id, 5, 80, '有效用户奖励');
2025-02-18 09:42:26 +08:00
//vip_id等级加1,并更新数据库
2025-02-23 23:19:58 +08:00
if($value->day_score<=140){
2025-02-23 22:48:16 +08:00
$parent->vip_id += 1;
//今天邀请人数加1
$parent->today_num += 1;
// $parent->today_team_income+=80;
$parent->save();
}
2025-02-23 18:26:45 +08:00
//判断今天有没有数据
$todayStart = Carbon::now()->startOfDay(); // 今天00:00:00
$todayEnd = Carbon::now()->endOfDay(); // 今天23:59:59
$existingRecord = ActiveUsers::where('user_id', $parent->id)
->where('phone', $value->phone)
->whereBetween('created_at', [$todayStart, $todayEnd])
->first();
if ($existingRecord) {
$existingRecord->update([
'income' => $value->day_score * 0.2,
]);
} else {
// 如果记录不存在,则创建新记录
ActiveUsers::create([
'user_id' => $parent->id,
'phone' => $value->phone,
'income' => $value->day_score * 0.2,
]);
}
2025-02-22 15:11:59 +08:00
//判定有没有父级的父级 这里父级没有默认是0
if($parent->f_id==0){
continue;
}
2025-02-23 18:26:45 +08:00
2025-02-22 15:11:59 +08:00
//获取父级的父级
$ancestor = User::find($parent->f_id);
2025-02-23 18:26:45 +08:00
$existingRecord1 = ActiveUsers::where('user_id', $ancestor->id)
->where('phone', $value->phone)
->whereBetween('created_at', [$todayStart, $todayEnd])
->first();
if ($existingRecord1) {
$existingRecord1->update([
'income' => $value->day_score * 0.1,
]);
} else {
// 如果记录不存在,则创建新记录
ActiveUsers::create([
'user_id' => $ancestor->id,
'phone' => $value->phone,
'income' => $value->day_score*0.1,
]);
}
2025-02-22 15:11:59 +08:00
//今天活跃人数加1
2025-02-23 23:37:55 +08:00
if($value->day_score<=140){
$ancestor->active_figures += 1;
}
2025-02-22 15:11:59 +08:00
$ancestor->save();
2025-02-18 09:42:26 +08:00
}
});
}
}