<?php


namespace app\api\controller;


use app\models\point_plan\PointPlan;
use app\models\point_plan\UserPointPlan;
use app\models\trade\CashTradeOrder;
use app\models\vote\Vote;
use app\Request;
use crmeb\services\CacheService;
use crmeb\services\UtilService;

class PointPlanController
{
    public function lst(Request $request)
    {
        $list = PointPlan::where('start_time', '<=', time())->where('is_del', 0)->select()->each(function ($item) {
            $item['request_num'] = UserPointPlan::where('plan_id', $item['id'])->where('status', 'in', [0, 1])->where('check_end_time', '>', time())->count();
        })->toArray();
        return app('json')->success('ok', $list);
    }

    public function request_point($id, Request $request)
    {
        $user = $request->user();
        $uid = $request->uid();
        $info = PointPlan::where('start_time', '<=', time())->where('is_del', 0)->where('id', $id)->find();
        if (!$info) {
            return app('json')->fail('找不到计划');
        }
        if (UserPointPlan::where('plan_id', $info['id'])->where('status', 'in', [0, 1])->where('check_end_time', '>', time())->count() >= $info['num']) {
            return app('json')->fail('节点已经认购完');
        }
        if (UserPointPlan::where('uid', $uid)->where('status', 'in', [0, 1])->where('check_end_time', '>', time())->find()) {
            return app('json')->fail('已经认购节点了');
        }
        list(, $captcha) = UtilService::postMore(
            [
                ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
                    return md5(md5($item)) == $user['trade_pwd'];
                }], ['请输入交易密码', '交易密码错误']],
                ['captcha', '']
            ], $request, true);
        $type = $info['buy_money_type'];
        $price = 0;
        $list = sys_data('money_type');
        foreach ($list as $v) {
            if ($v['code'] == $type) {
                $price = $v['price'] ? $v['price'] : CashTradeOrder::averagePrice($v['code']);
            }
        }
        if (bcmul(bcmul($info['buy_num'], $info['buy_price'], 8), $price, 2) >= 10000) {
            $verifyCode = CacheService::get('code_' . $user['account']);
            if (!$verifyCode)
                return app('json')->fail('请先获取验证码');
            $verifyCode = substr($verifyCode, 0, 6);
            if ($verifyCode != $captcha)
                return app('json')->fail('验证码错误');
        }
        $res = PointPlan::joinPlan($id, $uid);
        if ($res) {
            return app('json')->success('认购完成');
        } else {
            return app('json')->fail('认购失败:' . PointPlan::getErrorInfo('认购错误'));
        }
    }

    public function my_point(Request $request)
    {
        $res = UserPointPlan::where('uid', $request->uid())
            ->where('status', 'in', [0, 1])
//            ->where('check_end_time', '>', time())
            ->find()->toArray();
        if ($res)
            $res['plan'] = PointPlan::get($res['plan_id']);
        return app('json')->success('ok', $res);
    }
}