<?php


namespace app\api\controller\mining;


use app\models\mining\MiningMachine;
use app\models\mining\UserMining;
use app\models\mining\UserMiningMachine;
use app\models\trade\CashTradeOrder;
use app\models\user\User;
use app\Request;
use crmeb\services\CacheService;
use crmeb\services\UtilService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class MiningController
{

    public function calculator(Request $request)
    {

        $machine = MiningMachine::valid()->where('get_money_type', 'in', ['XCH'])->find();
        if ($machine) {
            $machine_price = $machine['cost_money'];
            $machine_price_type = init_money_type()[$machine['cost_money_type']];
            $machine_get = $machine['day_get'];
            $machine_service = $machine['service'];
            $time = $machine['first_step_time'] + $machine['second_step_time'] + $machine['third_step_time'];
        } else {
            $machine_price = 0;
            $machine_price_type = 'FIL';
            $machine_get = 0;
            $machine_service = 0;
            $time = 0;
        }

        $money_type = sys_data('money_type');
        foreach ($money_type as $v) {
            if (explode('_', $v['code'])[0] == "USDT") {
                if ($v['price'] <= 0) {
                    //计算前一天成交的平均价格
                    $v['price'] = CashTradeOrder::averagePrice($v['code']);
                }
                $usdt_price = $v['price'];
            }
            if (explode('_', $v['code'])[0] == "XCH") {
                if ($v['price'] <= 0) {
                    //计算前一天成交的平均价格
                    $v['price'] = CashTradeOrder::averagePrice($v['code']);
                }
                $fli_price = $v['price'];
            }
            if (explode('_', $v['code'])[0] == $machine_price_type) {
                if ($v['price'] <= 0) {
                    //计算前一天成交的平均价格
                    $v['price'] = CashTradeOrder::averagePrice($v['code']);
                }
                $cost_price = $v['price'];
            }
        }

        $fli_usdt = (isset($usdt_price) && $usdt_price > 0) ? bcdiv($fli_price, $usdt_price, 8) : 0;//fli对usdt价格
        $cost_fli = (isset($fli_price) && $fli_price > 0) ? bcdiv($cost_price, $fli_price, 8) : 0;
        $cost_fli_price = (isset($cost_fli) && $cost_fli > 0) ? bcdiv($machine_price, $cost_fli, 8) : 0;
        $get_back = (isset($cost_fli_price) && $cost_fli_price > 0) ? bcdiv($cost_fli_price, $machine_get, 8) : 0;//回本天数
        $month_back = bcmul(bcmul($machine_get, 30, 8), bcsub(1, bcdiv($machine_service, 100, 2), 2), 8);
        $year = bcmul(bcmul($machine_get, 365, 8), bcsub(1, bcdiv($machine_service, 100, 2), 2), 8);
        $year_get = (isset($cost_fli_price) && $cost_fli_price > 0) ? bcdiv($year, $cost_fli_price, 8) : 0;
        return app('json')->success('ok', compact('fli_usdt', 'machine_price', 'machine_price_type', 'machine_get', 'time', 'get_back', 'month_back', 'year_get'));
    }

    public function mining_index(Request $request)
    {
        $type = $request->get('type', 'XCH');
        $all = UserMiningMachine::where('get_money_type', 'in', $type)->where('uid', $request->uid())->sum('num');
        $doing = UserMiningMachine::where('get_money_type', 'in', $type)->where('uid', $request->uid())->where('status', 'in', [1, 2])->sum('num');
        $stand = UserMiningMachine::where('get_money_type', 'in', $type)->where('uid', $request->uid())->where('status', 0)->sum('num');
        $umids = UserMiningMachine::where('uid', $request->uid())->column('id');
        $all_mining = UserMining::where('get_money_type', 'in', $type)->where('umid', 'in', $umids)->sum('get_money');
        $all_lock = UserMining::where('get_money_type', 'in', $type)->where('umid', 'in', $umids)->sum('lock_money');
        return app('json')->success('ok', compact('all', 'doing', 'stand', 'all_mining', 'all_lock'));
    }

    /**
     * 算力产品
     * @param Request $request
     * @return mixed
     */
    public function lst(Request $request)
    {
        $page = $request->get('page', 1);
        $limit = $request->get('limit', 10);
        $uid = $request->get('uid', $request->uid());
        $get_money_type = $request->get('get_money_type', '');
        $type = $request->get('type', '');
        return app('json')->success('ok', MiningMachine::getList($page, $limit, ['get_money_type' => $get_money_type, 'type' => $type, 'uid' => $uid]));
    }

    /**
     * @param $id
     * @param Request $request
     * @return mixed
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function detail($id, Request $request)
    {
        $res = MiningMachine::valid()->where('id', $id)->find()->toArray();
        $money_type = init_money_type();
        $res['_day_get'] = $res['day_get'] . $money_type[$res['get_money_type']] . '/T';
        $res['_cost_money'] = $res['cost_money'] . $money_type[$res['cost_money_type']];
        $res['_stand_money'] = $res['stand_money'] . $money_type[$res['get_money_type']];
        $res['tags'] = explode(',', $res['tags']);
        $res['_cost_money_type'] = $money_type[$res['cost_money_type']];
        return app('json')->success('ok', $res);
    }

    public function buy($id, Request $request)
    {
        $user = $request->user();
        list($num,) = UtilService::postMore(
            [
                ['num', 0,],
                ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
//                var_dump($user);
                    return md5(md5($item)) == $user['trade_pwd'];
                }], ['请输入交易密码', '交易密码错误']],
            ], $request, true);
        $res = MiningMachine::buyMachine($id, $request->uid(), $num);
        if ($res) {
            return app('json')->success('购买成功');
        } else {
            return app('json')->fail(MiningMachine::getErrorInfo());
        }
    }

    public function my(Request $request)
    {
        $where = UtilService::getMore([
            ['page', 1],
            ['limit', 10],
        ]);
        $where['uid'] = $request->uid();
        return app('json')->success('ok', UserMiningMachine::getList($where));
    }


    public function newList(Request $request)
    {
        $where = UtilService::getMore([
            ['page', 1],
            ['limit', 10],
        ]);

        $userStair = [];
        $users = [$request->uid()];
        while ($users) {
            $users = User::where('spread_uid', 'in', $users)->column('uid');
            if ($users) $userStair = array_merge($userStair, $users);
        }

        $where['uid'] = $userStair;
        return app('json')->success('ok', UserMiningMachine::getList($where));
    }


    public function mining_get(Request $request)
    {
        $where = UtilService::getMore([
            ['page', 1],
            ['limit', 10],
            ['type', 'XCH']
        ]);
        $where['uid'] = $request->uid();

        $umid = UserMiningMachine::where('uid', $where['uid'])->column('id');
        return app('json')->success('ok', [
            'all_get' => UserMining::where('get_money_type', $where['type'])->where('umid', 'in', $umid)->sum('get_money'),
            'all_unlock' => UserMining::where('get_money_type', $where['type'])->where('umid', 'in', $umid)->sum('unlock'),
            'all_unstand' => UserMining::where('get_money_type', $where['type'])->where('umid', 'in', $umid)->sum('unstand'),
            'all_lock' => UserMining::where('get_money_type', $where['type'])->where('umid', 'in', $umid)->sum('lock_money'),
            'stand' => UserMiningMachine::where('get_money_type', $where['type'])->where('uid', $where['uid'])->sum('stand_money'),
            'list' => UserMining::getList($where)
        ]);
    }


}