<?php

namespace process;


use app\model\UserPhone;
use app\model\ActiveUsers;
use app\dao\UserRewardDao;
use Workerman\Crontab\Crontab;
use app\model\User;
use Carbon\Carbon;

/**
 * 获取今天在线时间超过6小时的用户
 */
class Task3
{
    public function onWorkerStart()
    {

        // // 每1小时执行一次
        new Crontab('0 0 * * * *', function () {
            //获取在线时间超过6小时的用户
            $today = date('Y-m-d');
            $UserPhone = UserPhone::where('day_score', '>=', 120)->get();
            //判断用户绑定Whatsapp首次登录
            foreach ($UserPhone as $key => $value) {
                $user_id = $value->user_id;
                //判定有没有父级 这里父级没有默认是0
                $user = User::find($user_id);
                if (!empty($user)&&$user->f_id==0) {
                    continue;
                }             
                //获取该用户的父级
                $parent = User::find($user->f_id);

                //vip_id等级加1,并更新数据库
                if($value->day_score<140){
                    $parent->vip_id += 1;
                    //今天邀请人数加1
                    $parent->today_num += 1;
                    $existingPhone = ActiveUsers::where('phone', $value->phone)
                    ->first();
                    //不存在,代表首次邀请
                    if (!$existingPhone) {
                        if($value->score>=120 && $value->score<140){
                            $parent->today_team_income+=80;
                            ActiveUsers::create([
                                'user_id' => $parent->id,
                                'phone' => $value->phone,
                                'income' => 80,
                            ]);
                            //满足则加80积分
                            // UserRewardDao::base($parent->id, 5, 80, '有效用户奖励');
                        }
                    } 
                    $parent->save();
                }
                //判断今天有没有数据
                $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,
                    ]);
                }
                //判定有没有父级的父级 这里父级没有默认是0
                if($parent->f_id==0){
                    continue;
                }
               
                //获取父级的父级
                $ancestor = User::find($parent->f_id);
                $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,
                    ]);
                }
                //今天活跃人数加1
                if($value->day_score<140){
                   $ancestor->active_figures += 1;
                }
                $ancestor->save();
            }
        });
    }
}