<?php


namespace app\api\controller;


use app\models\point_plan\PointPlan;
use app\models\point_plan\UserPointPlan;
use app\models\user\UserBill;
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('status', 1)->select()->each(function ($item) {
            $item['request_num'] = UserPointPlan::where('plan_id', $item['id'])->sum('buy_num');
            $item['buy_money_type'] = get_money_name($item['buy_money_type']);
        })->toArray();
        return app('json')->success('ok', $list);
    }

    public function request_point($id, Request $request)
    {
        $user = $request->user();
        $uid = $request->uid();
//        $num = $request->post('num', 1);
        $info = PointPlan::where('start_time', '<=', time())->where('status', 1)->where('id', $id)->find();
        if (!$info) {
            return app('json')->fail('找不到合伙人节点');
        }

        list($num,) = UtilService::postMore(
            [
                ['num', 0],
                ['trade_psw', '', '', '', ['not_empty_check', function ($item) use ($user) {
                    return md5(md5($item)) == $user['trade_pwd'];
                }], ['请输入交易密码', '交易密码错误']],
            ], $request, true);
        if ($num <= 0) return app('json')->fail('认购股数大于0');
        if ($info['stock'] < $num) {
            return app('json')->fail('节点库存不足');
        }
        $res = PointPlan::joinPlan($id, $uid, $num);
        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())
            ->select()->each(function ($item) {
                $item['plan'] = PointPlan::get($item['plan_id']);
            })->toArray();
        return app('json')->success('ok', ['list' => $res,
            'all' => UserBill::where('uid', $request->uid())->where('category', 'LALA')->where('status', 1)->where('type', 'release')->value('SUM(number)'),
            'yesterday' => UserBill::where('uid', $request->uid())->where('category', 'LALA')->where('status', 1)->where('type', 'release')->whereTime('add_time', 'yesterday')->value('SUM(number)'),
            'yesterday_award' => UserBill::where('uid', $request->uid())->where('category', 'LALA')->where('status', 1)->where('type', 'lala_point_award')->whereTime('add_time', 'yesterday')->value('SUM(number)'),
            'all_award' => UserBill::where('uid', $request->uid())->where('category', 'LALA')->where('status', 1)->where('type', 'lala_point_award')->value('SUM(number)'),
            'yesterday_service' => UserBill::where('uid', $request->uid())->where('status', 1)->where('type', 'service_point_award')->group('category')->whereTime('add_time', 'yesterday')->field('SUM(number) as sum,category')->select(),
            'all_service' => UserBill::where('uid', $request->uid())->where('status', 1)->where('type', 'service_point_award')->group('category')->field('SUM(number) as sum,category')->select(),
        ]);
    }
}