<?php

namespace app\api\controller\auction;


use app\models\auction\Auction;
use app\models\auction\AuctionBooking;
use app\models\user\User;
use app\models\user\UserBill;
use app\Request;
use Monolog\Handler\Curl\Util;
use think\facade\Cache;
use crmeb\services\{
    CacheService,
    ExpressService,
    SystemConfigService,
};
use crmeb\services\UtilService;
use crmeb\repositories\OrderRepository;


class AuctionController
{
    /**
     * 场馆列表
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $data = UtilService::getMore([
            [['page', 'd'], 0],
            [['limit', 'd'], 0],
            ['advance']
        ], $request);

        $auctionModel = new \app\models\auction\Auction();

        return app('json')->successful($auctionModel->list($data, $request->uid()));

    }

    /**
     * 预约场馆
     * @param Request $request
     * @return void
     */
    public function subscribe(Request $request)
    {
        $data = UtilService::postMore([
            ['id']
        ]);
        if (!$data['id']) return app('json')->fail('数据传入错误');
        $auctionModel = new Auction();
        $auction = $auctionModel->find($data['id']);
        if (!$auction)return app('json')->fail('没有此数据');

        if (time() < strtotime($auction['add_time'])){
            return app('json')->fail('预约未开始');
        }
        if (time() > strtotime($auction['end_time'])){
            return app('json')->fail('预约时间已过');
        }
        if (AuctionBooking::where([['uid', '=', $request->uid()], ['auction_id' , '=', $auction['id']], ['frequency', '=', $auction['frequency']]])->find()){
            return app('json')->fail('当前场馆已预约');
        }

        $userModel = new User();
        $user = $userModel->find($request->uid());

        if ($user['anticipate'] < $auction['anticipate']) return app('json')->fail('预约卷不够');
        $user['anticipate'] = $user['anticipate'] - $auction['anticipate'];// 扣除预约卷

        User::rollbackTrans();
        $res = $user->save();

        if ($res){
            AuctionBooking::booking($user['uid'], $auction);
            UserBill::income('预约场馆', $user['uid'], $user['spread_uid'],'预约', '预约卷'); // 写入记录

            User::commitTrans();
            return app('json')->successful('预约成功');
        }else{

            User::rollbackTrans();
            return app('json')->fail('预约失败');
        }

    }

    /**
     * 进入场馆
     * @param Request $request
     * @return void
     */
    public function advance(Request $request)
    {
        $data = UtilService::getMore([
            ['id']
        ]);
        if (!$data['id']) return app('json')->fail('数据传入错误');
        $auction = Auction::find($data['id']);
        $booking = AuctionBooking::where([['auction_id', '=',$auction['id']], ['frequency', '=', $auction['frequency']]])->find();
        $user = $request->user();
        $time = strtotime(date('Y-m-d', time()));// 今天
        $today = strtotime(date('Y-m-d', strtotime('+1day')));// 明天

        if ($user['is_new'] == 1 or ($user['green_time'] > $time and $user['green_time'] < $today)){
            // 新人或者绿色通道提前三分钟入场
            if (strtotime($auction['radd_time']) -3000 > time()){
                return app('json')->fail('未到进入时间');
            }
            if (strtotime($auction['rend_time']) < time()){
                return app('json')->fail('进场时间已过');
            }

        }else{
            if (strtotime($auction['radd_time']) > time()){
                return app('json')->fail('未到进入时间');
            }
            if (strtotime($auction['rend_time']) < time()){
                return app('json')->fail('进场时间已过');
            }
        }

        if ($booking){
            return app('json')->successful('可进入');
        }else{
            return app('json')->fail('未预约');
        }
    }




}