package com.game.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.game.business.domain.*;
import com.game.business.service.*;
import com.game.common.core.controller.BaseController;
import com.game.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/game/betting")
@Api(value = "AppGameBettingController", description = "游戏投注接口", tags = {"游戏投注、查询记录等"})
public class AppGameBettingController extends BaseController{

    @Autowired
    private IAppGameBettingService appGameBettingService;

    @Autowired
    private IAppGameService appGameService;

    @Autowired
    private IAppGameClassifyService appGameClassifyService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private IFinTranRecordService finTranRecordService;

    @Autowired
    private IAppGameLotteryService appGameLotteryService;

    /**
     * 下注
     */
    @PostMapping(value = "/create")
    public AjaxResult betting(@RequestBody AppGameBetting gameBetting)
    {

        if(gameBetting.getClassId() == null){
            return error("游戏平台ID为空。");
        }

        if(gameBetting.getGameId() == null){
            return error("游戏ID为空。");
        }

        if(gameBetting.getUserId()== null){
            return error("用户ID为空。");
        }

        AppGameClassify appGameClassify = appGameClassifyService.selectAppGameClassifyById(gameBetting.getClassId());
        if(appGameClassify == null){
            return error("游戏平台不存在。");
        }

        AppGame appGame = appGameService.selectAppGameById(gameBetting.getGameId());
        if(appGame == null){
            return error("游戏不存在。");
        }

        if(StringUtils.isBlank(appGame.getGameDate())){
            return error("游戏期号不存在，无法下单。");
        }

        if(StringUtils.isBlank(appGame.getGameTime())){
            return error("游戏倒计时不存在，无法下单。");
        }

        if(!appGame.getGameTime().contains(":")){
            return error("游戏已封盘，无法下单。");
        }

        String[] timeArry = appGame.getGameTime().substring(0, 4).split(":");
        int m = Integer.parseInt(timeArry[1]);

        if(m <= 10){
            return error("游戏已封盘，无法下单。");
        }

        long count = appGameLotteryService.selectCount(appGame.getClassifyId(), appGame.getId(), appGame.getGameDate());

        if(count > 0){
            return error("游戏已开奖，无法下单。");
        }

        synchronized (this){

            AppUser appUser = appUserService.selectAppUserByUserid(gameBetting.getUserId());

            if(appUser == null){
                return error("用户不存在。");
            }

            // 获取用户金额，判断投注金额是否大于余额
            if(gameBetting.getBettingAmount() > appUser.getDiamondCoin()){
                return error("余额不足，投注失败。");
            }

            // 更新用户余额
            appUser.setDiamondCoin(appUser.getDiamondCoin() - gameBetting.getBettingAmount());
            appUser.setDiamondCoinTotal(appUser.getDiamondCoinCashTotal() - gameBetting.getBettingAmount());
            appUserService.updateAppUser(appUser);

            gameBetting.setGameDate(appGame.getGameDate());
            // 插入投注记录
            appGameBettingService.save(gameBetting);

            FinTranRecord finTranRecord = new FinTranRecord();

            finTranRecord.setAfterCoin(appUser.getCoin());
            finTranRecord.setCoinChange(0.00);

            finTranRecord.setAfterMoney(0.00);
            finTranRecord.setMoneyChange(0.00);

            finTranRecord.setAfterDiamondCoin(appUser.getDiamondCoin());
            finTranRecord.setDiamondCoinChange(gameBetting.getBettingAmount());

            finTranRecord.setAfterTicket(0.00);
            finTranRecord.setTicketChange(0.00);

            finTranRecord.setToUid(appUser.getUserid());
            finTranRecord.setUid(appUser.getUserid());

            finTranRecord.setSceneId1(0L);
            finTranRecord.setSceneId2(null);
            finTranRecord.setSceneType(0L);

            finTranRecord.setTranGroupId(gameBetting.getId());
            finTranRecord.setTranType1(16L);
            finTranRecord.setTranType2(200L);
            finTranRecord.setTranType3(20001L);

            finTranRecord.setRemarks("游戏下注");

            finTranRecord.setConsumptionCoin(gameBetting.getBettingAmount());
            finTranRecord.setConsumptionMoney(0.00);
            finTranRecord.setCommissionRelatedUid(0L);
            finTranRecord.setAgentId(appUser.getAgentId());

            finTranRecord.setCreateTime(new Date());

            finTranRecord.setCurrencyType(4L);

            finTranRecord.setFromUid(0L);
            finTranRecord.setGoodsId(0L);
            finTranRecord.setGuildId(0L);
            finTranRecord.setManagerCoId(0L);
            finTranRecord.setManagerId(0L);

            finTranRecord.setPerc(0.00);
            finTranRecord.setProId(0L);
            finTranRecord.setProCount(0L);

            finTranRecord.setOrderId(gameBetting.getId());

            finTranRecordService.insertFinTranRecord(finTranRecord);
        }

        return success();
    }
}
