<?php
/**
 * Created by PhpStorm.
 * User: fr*d
 * Date: 2018/9/25
 * Time: 18:09
 * 业务逻辑：
 * 时间起始点：用户自上次成功提现起首次资金变化成功发后时间点（>=上次提现时间后的首笔成功资金发生时间（含充值、彩金、活动）)
 * 时间结束点：当前审核时间
 * 彩金、充值活动发生或审核通过时：加入对应的目标流水差额(父订单金额*(要求倍数-1))到提现规则表中的目标流水字段里
 * 提现前：计算 目标流水=所有充值成功金额*倍数+目标流水差额的值;下注流水=动态查询；
 * donate_start:上次提现时间
 * curr_start=getFirstTimeFromMoneyRecharge($idd,$donate_start);
 * 提现（审核通过）后：目标流水清零；
 */

namespace App\Models;

use Illuminate\Support\Facades\DB;
class Withdraw_rule extends BaseModel
{
    protected $table = 'withdraw_rule';

    /**
     * 增加用户流水增量记录
     * @param string $idd 用户账户UUID
     * @param mixed $startTime  unuse
     * @param mixed $endTime unuse
     * @param $bettingMoney unuse
     * @param $needBettingMoney 差额流水金额
     * @return mixed 成功返回记录编号，失败返回错误码
     */
    protected function setRule($idd, $startTime, $endTime, $bettingMoney, $needBettingMoney)
    {
        $data = [
            'account_identity' => $idd,
            'start_time' => is_int ($startTime) ? $startTime : strtotime ($startTime) ?? time (),
            'end_time' => is_int ($endTime) ? $endTime : strtotime ($endTime) ?? time (),
            'bettingMoney' => $bettingMoney,
            'updated_at' => date ('Y-m-d H:i:s'),
            'needBettingMoney' => $needBettingMoney,
            'donate_start' => date ('Y-m-d H:i:s'),
        ];
        return self::insertGetId ($data);
    }

    /**
     * 更新用户增量流水
     * @param string $idd 用户账户UUID
     * @param  $money 父订单金额
     * @param int $rate 增量倍数
     * @return int
     */
    protected function updateRule($idd, $money, $rate)
    {
        $withdrawRule = new self();
        $with = $withdrawRule->where ('account_identity', $idd)->first ();
        $time = date ('Y-m-d H:i:s');
        $ret = 1;
        $need = (float)$money * ($rate - 1);//目标流水差额
        if (!$with) {
            self::setRule ($idd, $time, $time, 0, $need);
        } else {
            $with->increment ('needBettingMoney', $need);//目标流水差额
            $with->updated_at = date ('Y-m-d H:i:s');
            $ret = $with->update ();
        }
        return $ret;
    }

    /**
     * 提现时验证用户的流水记录
     * @param string $idd 用户账户编号uuid
     * @return null 没有记录返回NULL,成功返回用户当前有效下注总流水和目标总流水
     */
    protected function check($idd)
    {
        $where = ['account_identity' => $idd];
        $data = self::where ($where)->first (['bettingMoney', 'needBettingMoney', 'donate_start']);
        if (!$data) return null;
        $time = $data->donate_start;
        $bet = (new MoneyBuy())->getBetMoney ($time, $idd);//常规投注
        $sixBet = (new SixMoneyBuy())::getBetMoney ($time, $idd);//六合投注
        $data->bettingMoney = $bet + $sixBet;//总投注流水
        $recharge = Money_recharge::where ('account_identity', $idd)
            ->where ('complete_time', '>=', $time) //首笔金额发生时间
            ->where ('status', 1) //订单状态为成功
            //->whereIn ('trade_type', [11, 14])
            ->sum (DB::Raw ('money * rate'));//资金表中的流水倍数和
        $data->needBettingMoney += $recharge;//差额流水+资金表中流水要求=总目标流水
        return $data;
    }

    protected function rmRule($idd)
    {
        return self::where ('account_identity', $idd)->delete ();
    }

    protected function getRule($id)
    {
        return self::where ('id', $id)->first ();
    }

    /**
     * 提现成功后清零下注记录和流水差额记录并更新上次提现时间
     * @param $idd
     * @return int
     */
    protected function withDraw($idd)
    {
        $data = self::where ('account_identity', $idd)->first ();
        $ret = 1;
        if ($data) {
            $data->donate_start = date ('Y-m-d H:i:s');
            $data->bettingMoney = 0;
            $data->needBettingMoney = 0;
            $ret = $data->update ();
        }
        return $ret;
    }

}