package com.game.business.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.game.business.domain.AppGameBetting;
import com.game.business.domain.AppUser;
import com.game.business.domain.FinTranRecord;
import com.game.business.service.IAppGameBettingService;
import com.game.business.service.IAppUserService;
import com.game.business.service.IFinTranRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class AppGameBettingTask {

    @Autowired
    private IAppGameBettingService appGameBettingService;

    @Autowired
    private IAppUserService appUserService;

    @Autowired
    private IFinTranRecordService finTranRecordService;

    public void gameBettingTask(Long classId, Long gameId, String gameDate, String bettingItem){

        List<AppGameBetting> list = appGameBettingService.getIsWinning(classId, gameId, gameDate, 0);

        if(list == null || list.isEmpty()){
            return;
        }

        for (int i = 0; i < list.size(); i++) {
            AppGameBetting appGameBetting = list.get(i);
            appGameBetting.setUpdateTime(new Date());
            if(!appGameBetting.getBettingItem().equals(bettingItem)){
                appGameBetting.setIsWinning(2);
                appGameBettingService.updateById(appGameBetting);
                continue;
            }

            AppUser appUser = appUserService.selectAppUserByUserid(appGameBetting.getUserId());
            if(appUser == null){
                appGameBetting.setIsWinning(1);
                appGameBettingService.updateById(appGameBetting);
                continue;
            }

            // 更新用户余额
            appUser.setDiamondCoin(appUser.getDiamondCoin() + (appGameBetting.getBettingAmount() * appGameBetting.getBettingMultiple()) );
            appUser.setDiamondCoinTotal(appUser.getDiamondCoinCashTotal() + (appGameBetting.getBettingAmount() * appGameBetting.getBettingMultiple()));
            appUserService.updateAppUser(appUser);

            appGameBetting.setIsWinning(1);
            appGameBettingService.updateById(appGameBetting);

            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(appGameBetting.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(appGameBetting.getId());
            finTranRecord.setTranType1(17L);
            finTranRecord.setTranType2(200L);
            finTranRecord.setTranType3(20001L);

            finTranRecord.setRemarks("游戏中奖");

            finTranRecord.setConsumptionCoin(appGameBetting.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(appGameBetting.getId());

            finTranRecordService.insertFinTranRecord(finTranRecord);
        }
    }
}
