<?php

namespace app\dao;

use App\model\Users;
use App\model\BankLog;
use App\Utils\FunctionResponse;
use App\model\User;
use App\model\UserReward;
class UserDao
{
    /**
     * 构造首页信息
     *
     * @param int $user_id 用户id
     * @return mixed
     */
    public static function get_index_userInfo($user_id)
    {
        $user = User::find($user_id);
        $userData = [
            "id" => 4,
            "username" => "3",
            "invite_code" => "06192",
            "is_active" => 0,
            "login_ip" => "192.168.2.143",
            "join_ip" => "192.168.2.143",
            "login_time" => "2025-02-16 21:57:50",
            "prev_time" => null,
            "status" => 1,
            "money" => 2,
            "admin_money" => 0,
            "all_team_money" => 0,
            "task_income_money" => 0,
            "task_status" => 0,
            //今天任务收入
            // "today_task_income" => 0,
            //今天推广收益
            // "today_team_income" => 0,
            "growth_value" => 0,
            "vip_id" => 0,
            "withdraw_money" => 0,
            "f_id" => 0,
            "ff_id" => 0,
            "fff_id" => 0,
            "top_id" => 0,
            "path" => null,
            "remark" => null,
            "created_at" => "2025-02-15T14:38:09.000000Z",
            "updated_at" => "2025-02-16T13:57:50.000000Z",
        ];
        foreach($userData as $key =>$value){
            $userData[$key]=$user->$key;
        }
        $userData['today_team_income']=self::today_team_income($user_id);
        $userData['today_task_income']=self::today_task_income($user_id);
        return $userData;
    }
    /**
     * 获取今天推广收益
     */
    public static function today_team_income($user_id){
        $today = date('Y-m-d');
        $query = UserReward::where('status', 5)->where('user_id', $user_id)->where('created_at', '>=', $today." 00:00:00")->sum('money');
        return $query;
    }
    /**
     * 获取昨天推广收益
     */
    public static function today_team_income_old($user_id){
        $endOfYesterday =  date('Y-m-d', strtotime('-1 day'));   // 昨天的结束时间（23:59:59）
        $query = UserReward::where('status', 5)->where('user_id', $user_id)->whereBetween('created_at', [ $endOfYesterday." 00:00:00", $endOfYesterday." 23:59:59"])->sum('money');
        return $query;
    }
    /**
     * 获取今天任务收益
     */
    public static function today_task_income($user_id){
        $today = date('Y-m-d');
        $query = UserReward::where('status', 6)->where('user_id', $user_id)->where('created_at', '>=', $today." 00:00:00")->sum('money');
        return $query;
    }
    /**
     * 获取昨天任务收益
     */
    public static function today_task_income_old($user_id){
        $endOfYesterday =  date('Y-m-d', strtotime('-1 day'));
        $query = UserReward::where('status', 6)->where('user_id', $user_id)->whereBetween('created_at', [ $endOfYesterday." 00:00:00", $endOfYesterday." 23:59:59"])->sum('money');
        return $query;
    }
    /**
     * 获取所有任务收益
     */
    public static function all_task_income($user_id){
        $query = UserReward::where('status', 6)->where('user_id', $user_id)->sum('money');
        return $query;
    }
    /**
     * 获取直属活跃人数
     */
    public static function level_1($user_id){
        $startOfDay = strtotime('today');  // 今天的开始时间（00:00:00）
        $query = User::where('f_id', $user_id)->where('login_time', '>=', $startOfDay)->count();
        return $query;
    }

     /**
     * 获取活跃人数
     */
    public static function active_users($user_id){
        $startOfDay = strtotime('today');  // 今天的开始时间（00:00:00）
        $query = User::where('f_id', $user_id)->orWhere('ff_id', $user_id)->where('login_time', '>=', $startOfDay)->count();
        return $query;
    }
    /**
     * 获取累计佣金
     */
    public static function all_income($user_id){
        $query = UserReward::where('status', 5)->where('user_id', $user_id)->sum('money');
        return $query;
    }
}
