<?php
/**
 *------Create thems Model------
 *------SCWPHP  Version 1.0.0------
 *------Dev Model Jions------
 *------Create Time 2017-06-06 07:45:06------
 */

namespace App\Api\Model;

use App\Commons\Model\Money_recharge;
use Biz\Account\AccountManager;
use \System\Model;

class Account extends Model
{

    protected $table = 'account';

    protected function getMoneyRechargeRec(string $account_identity, $activeId, string $type = '')
    {
        $m = new Money_recharge();
        $where = [
            'account_identity' => $account_identity,
            'status' => 1
        ];
        $betTime = '';
        switch ($type) {
            case 'month':
                $where[] = ['complete_time', '>=', $betTime = date ('Y-m-1')];
                break;
            case 'week':
                $time = date ('Y-m-d');
                $t = strtotime ($time . ' Sunday');
                $where[] = ['complete_time', '>=', $betTime = date ('Y-m-d H:i:s', $t - 86400 * 6)];
                break;
            default:

                $where[] = ['complete_time', '>=', $betTime = '2018-01-01 00:00:00'];
        }
        self::chkBettingRecord ($account_identity, $betTime);
        return $m->where ($where)->whereIn ('recharge_type', ['后台充值', '汇款', '在线充值'])->orderBy ('apply_time')->first (['money', 'order_id', 'complete_time']);
    }

    public static function chkFirstRecharge($account_identity, $activeId, $type = '')
    {
        $active = Lottery_money::where ('id', $activeId)->first ();
        // dd($activeId,$active);
        if (!$active)
            Render ('', -50016); //找不到该彩金活动
        $ret1 = self::getMoneyRechargeRec ($account_identity, $activeId, $type);
        if (!$ret1)
            Render ('', -50031);//没找到首充记录
        $content = json_decode ($active->conent, 1);
        $timeout = $content['fr_time'] ?? 0;
        $timeouted = strtotime ($ret1->complete_time) >= time () - $timeout * 3600;
        if (!$timeouted && $timeout)
            Render ('', -50032);//过期
        $ret2 = Lottery_money_log::where ('order_id', $ret1->order_id)->first ();
        if ($ret2) {
            if ($ret2->status = 0)
                Render ('', -50035);//申请审核中
            elseif ($ret2->status = 0)
                Render ('', -50034);//该订单已参加过活动
        }

        //验证是否有下注
        return $ret1;
    }

    protected function chkBettingRecord($account_identity, $time)
    {
        $mb = MoneyBuy::where ('account_identity', $account_identity)->where ('money_time', '>=', $time)->first ();
        $mb6 = SixmoneyBuy::where ('account_identity', $account_identity)->where ('money_time', '>=', $time)->first ();
        if ($mb || $mb6)
            Render ('', -50033);//有下注
        return 1;
    }

    public function getAccount($account_identity)
    {
        return self::where('identity', $account_identity)->pluck('account');
    }

    /**
     * @param string $token
     * @param string $payPass
     * @return bool
     * @throws \Exception
     */
    public static function checkPayPwd(string $token, string $payPass): int
    {
        $accountManager = new AccountManager();
        $userId = $accountManager->getCurrentUser()->account_identity;
        $res = lm('account_bank', 'Commons')->where(['account_identity' => $userId])->first();
        if (!$res) {
            // Render('', -2046, lang()->get('missing bankCard'));
            return -2046;
        }

        $return = $accountManager->repository->checkOldPayPassword($token, $payPass);
        return $status = $return['status'];

    }

}
