<?php

namespace app\dao;

use App\model\UserReward;
use App\model\BankLog;
use app\model\User;

/**
 * 用户收益明细控制类
 */
class UserRewardDao
{
    /**
     * 注册赠送金额
     */
    public static function Register_for_free($userid)
    {
        self::f_bounty($userid, 4, 88, '注册赠送');
    }
    /**
     * 挂机获得的奖励
     */
    public static function Onhookincome($userid, $money)
    {
        self::f_bounty($userid, 6, $money, '挂机收益');
    }
    /**
     * 加粉赏金奖励
     */
    public static function bounty($userid, $money)
    {
        self::base($userid, 5, $money, '加粉赏金');
    }
    /**
     * 抽奖
     */
    public static function lottery($userid, $money)
    {
        self::base($userid, 9, $money, '抽奖');
    }
    /**
     * 中奖
     */
    public static function winning($userid, $money)
    {
        self::base($userid, 8, $money, '中奖');
    }
    /**
     * 签到
     */
    public static function sing($userid, $money)
    {
        self::base($userid, 7, $money, '签到');
    }
    /**
     * 转账函数
     */
    public static function base($userid, $status, $money, $memo)
    {
        $UserReward = new UserReward();
        $user = User::find($userid);
        $UserReward->user_id = $user->id;
        $UserReward->username = $user->username;
        $UserReward->uu_id  = $user->invite_code;
        $UserReward->status  = $status;
        $UserReward->money  = $money;
        $UserReward->before = $user->money;
        $UserReward->after = $user->money + $money;
        $UserReward->memo = $memo;
        $UserReward->createtime2 = date('Y-m-d H:i:s');
        $user->money = $UserReward->after;
        $user->save();
        $UserReward->save();
    }
    /**
     * 父级 父父级分红
     * 函数
     */
    public static function f_bounty($userid, $status, $money, $memo)
    {
        //父分红
        $f_dividend = floor($money * 0.2);
        //父父分红
        $ff_dividend = floor($money * 0.1);
        $user = User::find($userid);
        //分红大于0才会记录 这里还有类型 有些类型需要分红吗
        if ($user->f_id != 0&&$money>0&&$f_dividend>0&&$ff_dividend>0) {
            $f = User::find($user->f_id);
            self::base($f->id, 5, $f_dividend, '加粉赏金');
            if ($user->ff_id != 0) {
                $ff = User::find($user->ff_id);
                self::base($ff->id, 5, $ff_dividend, '加粉赏金');
            }
        }
        self::base($userid, $status, $money, $memo);
    }
    /**
     * 搜索函数
     * 
     * @param int $userid 用户ID
     * @param int $status 类型
     * @param int $time 时间段（0: 全部时间，1: 今天，2: 昨天，3: 最近七天）
     * @return mixed 返回查询结果（可以是数组或查询对象）
     */
    public static function search($userid, $status, int $time)
    {
        // 初始化查询对象
        if ($status == 0) {
            $query = UserReward::where('user_id', $userid)->orderBy('id','DESC');
        } else {
            $query = UserReward::where('status', $status)->where('user_id', $userid);
        }


        switch ($time) {
            case 0:
                // 全部时间：查询没有时间限制的记录
                return $query->get();

            case 1:
                // 今天：查询今天的记录
                $today = date('Y-m-d');
                return $query->where('created_at', '>=', $today." 00:00:00")->orderBy('id','DESC')->get();

            case 2:
                // 昨天：查询昨天的记录
                $endOfYesterday =  date('Y-m-d', strtotime('-1 day'));
                return $query->whereBetween('created_at', [$endOfYesterday." 00:00:00", $endOfYesterday." 23:59:59"])->orderBy('id','DESC')->get();

            case 3:
                // 最近七天：查询过去七天的记录
                $sevenDaysAgo =  date('Y-m-d', strtotime('-7 days'));
                return $query->where('created_at', '>=', $sevenDaysAgo." 23:59:59")->orderBy('id','DESC')->get();

            default:
                // 如果没有匹配的时间段，返回空结果或其他处理方式
                return $query->get();
        }
    }
}
