<?php

namespace app\api\controller\auction;


use app\models\auction\Auction;
use app\models\auction\AuctionBooking;
use app\models\auction\AuctionGu;
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;
use think\facade\Db;


class AuctionController
{
    /**
     * 场馆列表
     * @param Request $request
     * @return mixed
     */
    public function list(Request $request)
    {
        $data = UtilService::getMore([
            [['page', 'd'], 0],
            [['limit', 'd'], 0],
            ['advance'],
            ['auction_gu_id']
        ], $request);
        if (!$data['auction_gu_id']) return app('json')->fail('数据传入错误');
        $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::expend('预约场馆', $user['uid'], 'anticipate','reduce_anticipate', $auction['anticipate'], 0, $user['anticipate'], '预约扣除预约卷'); // 写入记录

            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('未预约');
        }
    }


    /**
     * 用户下级
     * @param Request $request
     * @return mixed
     */
    public function lower(Request $request){
        $data = UtilService::getMore([
            [['page', 'd'], 0],
            [['limit', 'd'], 0],
        ], $request);

        $user = User::where('spread_uid', $request->uid())->page($data['page'], $data['limit'])->order('uid DESC')->select()->toArray();
        return app('json')->successful($user);
    }


    /**
     * 转预约卷给下级
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function transfer_accounts(Request $request){
        $data = UtilService::getMore([
            ['uid'],
            ['anticipate']
        ], $request);
        if (!$data['uid'] or !$data['anticipate'])  return app('json')->fail('数据传入错误');
        $user = User::find($request->uid());
        if ($user['anticipate'] < $data['anticipate']) return app('json')->fail('预约卷不够');
        $me = User::find($data['uid']);
        $user['anticipate'] = $user['anticipate'] - $data['anticipate'];// 扣除预约卷
        $me['anticipate'] = $me['anticipate'] + $data['anticipate'];// 增加预约卷

        try {
            Db::startTrans();
            UserBill::expend('预约卷减少',$user['uid'], 'anticipate', 'reduce_anticipate', $data['anticipate'], 0, $user['anticipate'], '转账给用户'.$me['nickname'].$data['anticipate'].'预约卷');
            UserBill::income('预约卷增加',$me['uid'], 'anticipate', 'add_anticipate', $data['anticipate'], 0, $me['anticipate'], $me['nickname'].'转账'.$data['anticipate'].'预约卷');
            $user->save();
            $me->save();

            Db::commit();
            return app('json')->successful('成功');
        } catch (\Exception $e) {
            Db::rollback();
            return app('json')->fail('失败');
        }


    }

    /**
     * 场馆
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function auction_gu(Request  $request)
    {
        $data = UtilService::getMore([
            [['page', 'd'], 0],
            [['limit', 'd'], 0],
        ], $request);

        $uid = getParent($request->uid());
        $list = AuctionGu::where('uid', 'in', $uid)->page($data['page'], $data['limit'])->select();
        $list = count($list) ? $list->toArray() : [];
        return app('json')->successful($list);

    }








}