<?php

class paymentModel extends CI_Model
{
    /**
     * 获得用户充值总值
     * @param $uid
     * @param $day
     * @return mixed
     * Created by: owen
     * Created on: 2020/2/6 16:56
     */
    public function getUserTotalPayNum($uid, $day = 0)
    {
        $tb = otable::paidStat();
        $ret = oo::commonOprDb('slavedb')->getOne("Select `amount` From {$tb} Where `uid` = {$uid}");

        return $ret['amount'];
    }

    /**
     * 用戶最後購買時間
     * @param $uid
     * @return mixed
     * Created by: Owen
     * Created on: 2020/6/30 14:40
     */
    public function getUserLastPayTime($uid)
    {
        $cacheKey = okeys::UserLastPayTime($uid);
        $cache = oo::commonOprRedis('games')->get($cacheKey);
        if ($cache !== false) {
            return intval($cache);
        }
        $tb = otable::payment($uid);
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid} AND status=2 ORDER BY pid DESC LIMIT 1";
        $ret = oo::commonOprDb('slavedb')->getOne($sql, MYSQLI_ASSOC);
        $lastTime = max(intval($ret['pay_time']), 0);
        oo::commonOprRedis('games')->setex($cacheKey, $lastTime, 7 * 24 * 60 * 60);
        return $lastTime;
    }

    /**
     * 用戶最後購買時間
     * @param $uid
     * @return mixed
     * Created by: Owen
     * Created on: 2020/6/30 14:40
     */
    public function getUserFirstPayTime($uid)
    {
        $cacheKey = okeys::UserFirstPayTime($uid);
        $cache = oo::commonOprRedis('games')->get($cacheKey);
        if ($cache !== false) {
            return intval($cache);
        }
        $tb = otable::payment($uid);
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid} AND status=2 ORDER BY pid ASC LIMIT 1";
        $ret = oo::commonOprDb('slavedb')->getOne($sql, MYSQLI_ASSOC);
        $lastTime = max(intval($ret['pay_time']), 0);
        oo::commonOprRedis('games')->setex($cacheKey, $lastTime, 7 * 24 * 60 * 60);
        return $lastTime;
    }

    /**
     * 获得用户充值总次数
     * @param $uid
     * @return false|int
     * Created by: Owen
     * Created on: 2020/12/1 15:06
     */
    public function getUserPayTimes($uid, $day = 0)
    {
        $cacheKey = okeys::UserPayTimes($uid);
        $cache = oo::commonOprRedis('games')->hGet($cacheKey, $day);
        if ($cache !== false) {
            return intval($cache);
        }
        $tb = otable::payment($uid);
        if ($day) {
            $time = time() - $day * 24 * 60 * 60;
            $ret = oo::commonOprDb('slavedb')->getOne("SELECT COUNT(*) AS count FROM {$tb} WHERE uid={$uid} AND status=2 AND pay_time>{$time}", MYSQLI_ASSOC);
        } else {
            $ret = oo::commonOprDb('slavedb')->getOne("SELECT COUNT(*) AS count FROM {$tb} WHERE uid={$uid} AND status=2", MYSQLI_ASSOC);
        }
        oo::commonOprRedis('games')->hSet($cacheKey, $day, intval($ret['count']));
        oo::commonOprRedis('games')->expire($cacheKey, 7 * 24 * 60 * 60);
        return intval($ret['count']);
    }

    /**
     * 用户购买订单数
     * @param $uid
     * @return mixed
     * Created by: Hss
     * Created on: 2020/9/22 9:55
     */
    public function getUserPayCount($uid)
    {
        $tb = otable::payment($uid);
        $sql = "SELECT count(*) AS count FROM {$tb} WHERE uid={$uid} AND status=2";
        $ret = oo::commonOprDb('slavedb')->getOne($sql, MYSQLI_ASSOC);
        return empty($ret) ? 0 : count($ret['count']);
    }

    /**
     * 上次支付时间
     * @param $uid
     * @return mixed
     * Created by: Hss
     * Created on: 2020/9/22 9:55
     * 为什么用up 因为 last被占用了
     */
    public function getUserUpPayTime($uid)
    {
        $tb = otable::payment($uid);
        $sql = "SELECT * FROM {$tb} WHERE uid={$uid} AND status=2 ORDER BY pid DESC LIMIT 1,1";
        $ret = oo::commonOprDb('slavedb')->getOne($sql, MYSQLI_ASSOC);
        return empty($ret) ? 0 : $ret['pay_time'];
    }

    /**
     * 上次支付时间
     * @param $uid
     * @return mixed
     * Created by: Hss
     * Created on: 2020/9/22 9:55
     */
    public function getSkuPrice()
    {
        $config = [
            'com.unioncity.crazycoin.0.99' => '0.99',
            'com.unioncity.crazycoin.1.99' => '1.99',
            'com.unioncity.crazycoin.2.99' => '2.99',
            'com.unioncity.crazycoin.3.99' => '3.99',
            'com.unioncity.crazycoin.4.99' => '4.99',
            'com.unioncity.crazycoin.5.99' => '5.99',
            'com.unioncity.crazycoin.7.99' => '7.99',
            'com.unioncity.crazycoin.8.99' => '8.99',
            'com.unioncity.crazycoin.9.99' => '9.99',
            'com.unioncity.crazycoin.10.99' => '10.99',
            'com.unioncity.crazycoin.11.99' => '11.99',
            'com.unioncity.crazycoin.14.99' => '14.99',
            'com.unioncity.crazycoin.19.99' => '19.99',
            'com.unioncity.crazycoin.29.99' => '29.99',
            'com.unioncity.crazycoin.49.99' => '49.99',
            'com.unioncity.crazycoin.99.99' => '99.99',
            'goldenChest' => '2100000',
            'magicalChest' => '3100000',
            'woodenChest' => '1100000',
            '{"coins":"coins01","bet":0}' => '0',
        ];
        $out = [];
        foreach ($config as $key => $price) {
            $sku = str_replace("com.sa.crazygod", "com.unioncity.crazycoin.", $key);
            $out[$sku] = $price;
        }
        asort($out);
        return $out;
    }

    /**
     * 支付白名单
     * @return mixed
     * Created by: Hss
     * Created on: 2020/11/23 16:19
     */
    public function payWhiteList()
    {
        $key = okeys::payWhiteList();
        $content = oo::commonOprRedis('games')->get($key);
        if (empty($content)) {
            $tb = otable::payWhiteList();
            $sql = "SELECT * FROM {$tb}";
            $data = oo::commonOprDb('slavedb')->getOne($sql, 1);
            if (!empty($data['content'])) {
                $content = $data['content'];
                oo::commonOprRedis('games')->set($key, $content);
            }
        }
        $content = explode(",", $content);
        return $content;
    }
}
