package com.ff.quartz.task;

import com.ff.base.constant.Constants;
import com.ff.base.datasource.DynamicDataSourceContextHolder;
import com.ff.base.enums.GamePlatforms;
import com.ff.base.utils.DateUtils;
import com.ff.game.api.IGamesService;
import com.ff.game.api.request.BetRecordByTimeDTO;
import com.ff.game.api.request.GamesBaseRequestDTO;
import com.ff.game.api.request.GetFreeSpinDashflowRequestDTO;
import com.ff.game.domain.GameSecretKey;
import com.ff.game.service.IGameSecretKeyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 游戏任务
 *
 * @author shi
 * @date 2024/10/22
 */
@Slf4j
@Component("gameTask")
public class GameTask {


    @Autowired
    private Map<String, IGamesService> gamesService;

    @Resource
    private IGameSecretKeyService gameSecretKeyService;

    /**
     * 插入游戏投注详细信息
     */

    public void insertGameBettingDetails(Integer backTime) {

        //捞取指定分钟前的数据
        Long startTime = DateUtils.addOrSubtractMinutes(DateUtils.getNowDate(), backTime);
        Long endTime = DateUtils.getNowDate();

        Map<Object, DataSource> resolvedDataSources = DynamicDataSourceContextHolder.getAllDataSource();
        for (Map.Entry<Object, DataSource> entry : resolvedDataSources.entrySet()) {
            Object key = entry.getKey();
            // 设置数据源类型
            DynamicDataSourceContextHolder.setDataSourceType(key.toString());
            for (String gameKey : gamesService.keySet()) {
                List<GameSecretKey> gameSecretKeys = gameSecretKeyService.selectGameSecretKeyList(GameSecretKey.builder().platform(gameKey.replace(Constants.SERVICE, "")).build());
                for (GameSecretKey gameSecretKey : gameSecretKeys) {
                    try {
                        gamesService.get(gameKey).getGameList(GamesBaseRequestDTO.builder()
                                .agentId(gameSecretKey.getCode())
                                .agentKey(gameSecretKey.getKey())
                                .build());
                        BetRecordByTimeDTO betRecordByTimeDTO = new BetRecordByTimeDTO();
                        betRecordByTimeDTO.setGamePlatform(gameSecretKey.getPlatform());
                        betRecordByTimeDTO.setStartTime(startTime);
                        betRecordByTimeDTO.setEndTime(endTime);
                        betRecordByTimeDTO.setPage(1);
                        betRecordByTimeDTO.setPageLimit(1000);
                        betRecordByTimeDTO.setAgentId(gameSecretKey.getCode());
                        betRecordByTimeDTO.setAgentKey(gameSecretKey.getKey());
                        gamesService.get(gameKey).getBetRecordByTime(betRecordByTimeDTO);
                    } catch (Exception e) {
                        log.error("查询 币种 {} 投注记录失败，错误信息 {}", gameSecretKey.getCode(), e.getMessage());
                    }
                }

            }
        }
    }

    /**
     * 插入免费游戏局数情况
     */
    public void insertFreeSpinDashflow() {
        Map<Object, DataSource> resolvedDataSources = DynamicDataSourceContextHolder.getAllDataSource();
        for (Map.Entry<Object, DataSource> entry : resolvedDataSources.entrySet()) {
            Object key = entry.getKey();
            // 设置数据源类型
            DynamicDataSourceContextHolder.setDataSourceType(key.toString());
            Long startTime = DateUtils.addOrSubtractMinutes(DateUtils.getNowDate(), -70);
            for (String gameKey : gamesService.keySet()) {
                List<GameSecretKey> gameSecretKeys = gameSecretKeyService.selectGameSecretKeyList(GameSecretKey.builder().platform(gameKey.replace(Constants.SERVICE, "")).build());
                for (GameSecretKey gameSecretKey : gameSecretKeys) {
                    try {
                        GetFreeSpinDashflowRequestDTO getFreeSpinDashflowRequestDTO = new GetFreeSpinDashflowRequestDTO();
                        getFreeSpinDashflowRequestDTO.setStartTime(startTime);
                        getFreeSpinDashflowRequestDTO.setAgentId(gameSecretKey.getCode());
                        getFreeSpinDashflowRequestDTO.setAgentKey(gameSecretKey.getKey());
                        gamesService.get(gameKey).getFreeSpinDashflow(getFreeSpinDashflowRequestDTO);
                    } catch (Exception e) {
                        log.error("查询 币种 {} 免费游戏投注记录失败，错误信息 {}", gameSecretKey.getCode(), e.getMessage());
                    }
                }

            }
        }
    }
}






