package com.zanxiang.game.gs.data.serve.service.impl;


import com.google.common.base.CaseFormat;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.row.Row;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.zanxiang.game.gs.data.serve.dao.mapper.game.parent.AdsRoleAmountDataParentMapper;
import com.zanxiang.game.gs.data.serve.pojo.dto.BigRDTO;
import com.zanxiang.game.gs.data.serve.pojo.dto.RoleManageDTO;
import com.zanxiang.game.gs.data.serve.pojo.dto.RoleRemoveGameMonitorDTO;
import com.zanxiang.game.gs.data.serve.pojo.entity.game.parent.AdsRoleAmountDataParent;
import com.zanxiang.game.gs.data.serve.pojo.entity.game.parent.AdsRoleAmountDataTodayParent;
import com.zanxiang.game.gs.data.serve.pojo.vo.BigRVO;
import com.zanxiang.game.gs.data.serve.pojo.vo.PageVO;
import com.zanxiang.game.gs.data.serve.pojo.vo.RoleManageVO;
import com.zanxiang.game.gs.data.serve.pojo.vo.RoleRemoveGameMonitorVO;
import com.zanxiang.game.gs.data.serve.service.*;
import com.zanxiang.module.util.DateUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static com.mybatisflex.core.query.QueryMethods.*;
import static com.zanxiang.game.gs.data.serve.pojo.entity.game.parent.table.AdsRoleAmountDataParentTableDef.ADS_ROLE_AMOUNT_DATA_PARENT;
import static com.zanxiang.game.gs.data.serve.pojo.entity.game.parent.table.AdsRoleAmountDataTodayParentTableDef.ADS_ROLE_AMOUNT_DATA_TODAY_PARENT;

/**
 * 角色父游戏充值数据 服务层实现。
 *
 * @author shaosong
 * @since 1.0
 */
@Service
public class AdsRoleAmountDataParentServiceImpl extends ServiceImpl<AdsRoleAmountDataParentMapper, AdsRoleAmountDataParent> implements IAdsRoleAmountDataParentService {
    @Resource
    private IAdsRoleAmountDataTodayParentService adsRoleAmountDataTodayParentService;
    @Resource
    private IRoleOperateService roleOperateService;
    @Resource
    private ISysUserService sysUserService;
    @Resource
    private IGameService gameService;
    @Resource
    private IGameServerSonMergeService serverSonMergeService;
    @Resource
    private IGameServerService gameServerService;
    @Resource
    private IGameAuthRoleGroupService authRoleGroupService;
    @Resource
    private IGameAuthGroupService authGroupService;

    @Override
    public PageVO<BigRVO, Row> bigRListOfPage(BigRDTO dto) {
        if (StringUtils.isBlank(dto.getSortFiled())) {
            dto.setSortFiled(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT.getName());
        } else {
            String column = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, dto.getSortFiled());
            dto.setSortFiled(column);
        }
        QueryWrapper bigRWrapper = getBigRWrapper(dto);

        Page<BigRVO> paginate = getMapper().paginateAs(dto.getPageNum(), dto.getPageSize(), bigRWrapper, BigRVO.class);
        if (0 == paginate.getTotalRow()) {
            return PageVO.empty(dto);
        }
        List<BigRVO> records = paginate.getRecords();
        Set<String> serverIdList = records.stream().map(BigRVO::getEndServerId).collect(Collectors.toSet());
        Set<Integer> parentGameIdList = records.stream().map(BigRVO::getParentGameId).collect(Collectors.toSet());
        Set<Long> gsIdList = records.stream().map(BigRVO::getGsId).collect(Collectors.toSet());
        //父游戏名称
        Map<Integer, String> parentGameMap = gameService.gameNameMap(dto.getSourceSystem(), parentGameIdList);
        //区服名称
        Map<Long, Map<String, String>> serverMap = serverSonMergeService.gameServerMap(dto.getSourceSystem(), serverIdList);
        //GS名称查询
        Map<Long, String> sysUserMap = CollectionUtils.isEmpty(gsIdList) ? null : sysUserService.getSysUserMap(gsIdList);
        //GS组名称
//        Map<Long, List<String>> gsGroupMap = authRoleGroupService.getGsGroupMap(dto.getSourceSystem(), gsIdList);
        records.forEach(record -> {
            Long gsId = record.getGsId();
            record.setGsName(0 == gsId ? null : sysUserMap != null ? sysUserMap.get(gsId) : null);
//            record.setGsGroupName(null == gsId ? null : gsGroupMap.get(gsId));
            record.setParentGameName(parentGameMap.get(record.getParentGameId()));
            record.setEndServerName(null == serverMap.get(record.getSuperGameId()) ? null : serverMap.get(record.getSuperGameId()).get(record.getEndServerId()));
            if (null != record.getGsStartTime()) {
                record.setGsServeDays(record.getGsEndTime().isBefore(LocalDate.now()) ? DateUtil.intervalOfDays(record.getGsStartTime(), record.getGsEndTime())
                        : DateUtil.intervalOfDays(record.getGsStartTime(), LocalDate.now()));
                record.setServeStatus(record.getGsEndTime().isBefore(LocalDate.now()));
            }
            record.setRoleSource(0 == record.getGsId() ? 1 : 2);
        });
        return new PageVO<>(paginate);
    }

    @Override
    public PageVO<RoleRemoveGameMonitorVO, Row> roleRemoveGameMonitor(RoleRemoveGameMonitorDTO dto) {
        if (StringUtils.isBlank(dto.getSortFiled())) {
            dto.setSortFiled(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT.getName());
        } else {
            String column = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, dto.getSortFiled());
            dto.setSortFiled(column);
        }
        QueryWrapper bigRWrapper = getRoleRemoveGameWrapper(dto);

        Page<RoleRemoveGameMonitorVO> paginate = getMapper().paginateAs(dto.getPageNum(), dto.getPageSize(), bigRWrapper, RoleRemoveGameMonitorVO.class);
        if (0 == paginate.getTotalRow()) {
            return PageVO.empty(dto);
        }
        List<RoleRemoveGameMonitorVO> records = paginate.getRecords();
        Set<String> serverIdList = records.stream().map(RoleRemoveGameMonitorVO::getEndServerId).collect(Collectors.toSet());
        Set<Integer> parentGameIdList = records.stream().map(RoleRemoveGameMonitorVO::getParentGameId).collect(Collectors.toSet());
        Set<Long> gsIdList = records.stream().map(RoleRemoveGameMonitorVO::getGsId).collect(Collectors.toSet());
        //父游戏名称
        Map<Integer, String> parentGameMap = gameService.gameNameMap(dto.getSourceSystem(), parentGameIdList);
        //区服名称
        Map<Long, Map<String, String>> serverMap = serverSonMergeService.gameServerMap(dto.getSourceSystem(), serverIdList);
        //GS名称查询
        Map<Long, String> sysUserMap = CollectionUtils.isEmpty(gsIdList) ? null : sysUserService.getSysUserMap(gsIdList);
        records.forEach(record -> {
            Long gsId = record.getGsId();
            record.setGsName(0 == gsId ? null : sysUserMap != null ? sysUserMap.get(gsId) : null);
            record.setParentGameName(parentGameMap.get(record.getParentGameId()));
            record.setEndServerName(null == serverMap.get(record.getSuperGameId()) ? null : serverMap.get(record.getSuperGameId()).get(record.getEndServerId()));
            if (null != record.getGsStartTime()) {
                record.setGsServeDays(record.getGsEndTime().isBefore(LocalDate.now()) ? DateUtil.intervalOfDays(record.getGsStartTime(), record.getGsEndTime())
                        : DateUtil.intervalOfDays(record.getGsStartTime(), LocalDate.now()));
                record.setServeStatus(record.getGsEndTime().isBefore(LocalDate.now()));
            }
            record.setRoleSource(0 == record.getGsId() ? 1 : 2);
        });
        return new PageVO<>(paginate);
    }

    @Override
    public PageVO<RoleManageVO, Row> roleManage(RoleManageDTO dto) {
        if (StringUtils.isBlank(dto.getSortFiled())) {
            dto.setSortFiled(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT.getName());
        } else {
            String column = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, dto.getSortFiled());
            dto.setSortFiled(column);
        }
        QueryWrapper wrapper = getRoleManageWrapper(dto);

        Page<RoleManageVO> paginate = getMapper().paginateAs(dto.getPageNum(), dto.getPageSize(), wrapper, RoleManageVO.class);
        if (0 == paginate.getTotalRow()) {
            return PageVO.empty(dto);
        }
        List<RoleManageVO> records = paginate.getRecords();
        Set<String> serverIdList = records.stream().map(RoleManageVO::getEndServerId).collect(Collectors.toSet());
        Set<Integer> parentGameIdList = records.stream().map(RoleManageVO::getParentGameId).collect(Collectors.toSet());
        Set<Long> gsIdList = records.stream().map(RoleManageVO::getGsId).collect(Collectors.toSet());
        //父游戏名称
        Map<Integer, String> parentGameMap = gameService.gameNameMap(dto.getSourceSystem(), parentGameIdList);
        //区服名称
        Map<Long, Map<String, String>> serverMap = serverSonMergeService.gameServerMap(dto.getSourceSystem(), serverIdList);
        //GS名称查询
        Map<Long, String> sysUserMap = CollectionUtils.isEmpty(gsIdList) ? null : sysUserService.getSysUserMap(gsIdList);
        records.forEach(record -> {
            Long gsId = record.getGsId();
            record.setGsName(0 == gsId ? null : sysUserMap != null ? sysUserMap.get(gsId) : null);
            record.setParentGameName(parentGameMap.get(record.getParentGameId()));
            record.setEndServerName(null == serverMap.get(record.getSuperGameId()) ? null : serverMap.get(record.getSuperGameId()).get(record.getEndServerId()));
            if (null != record.getGsStartTime()) {
                record.setGsServeDays(record.getGsEndTime().isBefore(LocalDate.now()) ? DateUtil.intervalOfDays(record.getGsStartTime(), record.getGsEndTime())
                        : DateUtil.intervalOfDays(record.getGsStartTime(), LocalDate.now()));
                record.setServeStatus(record.getGsEndTime().isBefore(LocalDate.now()));
            }
        });
        return new PageVO<>(paginate);
    }

    private static QueryWrapper getRoleManageWrapper(RoleManageDTO dto) {
        return QueryWrapper.create()
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GS_START_TIME)
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_NAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_NAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PARENT_GAME_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.PARENT_GAME_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ASSOCIATION_USER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.ASSOCIATION_USER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_LEVEL).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_LEVEL.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_VIP).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_VIP.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.COMBAT_NUM).as(ADS_ROLE_AMOUNT_DATA_PARENT.COMBAT_NUM.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.COUNTRY).as(ADS_ROLE_AMOUNT_DATA_PARENT.COUNTRY.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME.getName()))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME)).as("role_create_day_diff"))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME)).as("role_active_day_diff"))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME)).as("last_order_day_diff"))
                .select(dateDiff(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME), max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME)).as("role_create_pay_day_diff"))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.FIRST_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.FIRST_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_HOUR_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_HOUR_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PRODUCT_NAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.PRODUCT_NAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_CNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_CNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.APP_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.APP_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PROPORTION).as(ADS_ROLE_AMOUNT_DATA_PARENT.PROPORTION.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.CONCAT_GAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.CONCAT_GAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_COUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_COUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_END_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_END_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SOURCE_SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SOURCE_SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.END_SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.END_SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SUPER_GAME_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SUPER_GAME_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID_SERVER).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID_SERVER.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_COUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_COUNT.getName()))
                .from(ADS_ROLE_AMOUNT_DATA_PARENT)
                .ge(AdsRoleAmountDataParent::getRoleTime, null == dto.getRoleCreateDayBegin() ? null : LocalDateTime.of(dto.getRoleCreateDayBegin(), LocalTime.MIDNIGHT), null != dto.getRoleCreateDayBegin())
                .le(AdsRoleAmountDataParent::getRoleTime, null == dto.getRoleCreateDayEnd() ? null : LocalDateTime.of(dto.getRoleCreateDayEnd(), LocalTime.MAX), null != dto.getRoleCreateDayEnd())
                .in(AdsRoleAmountDataParent::getGsId, dto.getGsIdList(), CollectionUtils.isNotEmpty(dto.getGsIdList()))
                .eq(AdsRoleAmountDataParent::getParentGameId, dto.getParentGameId(), null != dto.getParentGameId())
                .like(AdsRoleAmountDataParent::getRoleName, dto.getRoleName(), StringUtils.isNotBlank(dto.getRoleName()))
                .eq(AdsRoleAmountDataParent::getSuperGameId, dto.getSuperGameId(), null != dto.getSuperGameId())
                .in(AdsRoleAmountDataParent::getEndServerId, dto.getServerIdList(), CollectionUtils.isNotEmpty(dto.getServerIdList()))
                .ge(AdsRoleAmountDataParent::getGsStartTime, dto.getServeDayBegin(), null != dto.getServeDayBegin())
                .le(AdsRoleAmountDataParent::getGsEndTime, dto.getServeDayEnd(), null != dto.getServeDayEnd())
                .ne(AdsRoleAmountDataParent::getGsId, 0)
                .ge(AdsRoleAmountDataParent::getGsEndTime, LocalDate.now(), null != dto.getServeStatus() && 1 == dto.getServeStatus())
                .le(AdsRoleAmountDataParent::getGsEndTime, LocalDate.now(), null != dto.getServeStatus() && 2 == dto.getServeStatus())
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GS_START_TIME)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID)
                .orderBy(dto.getSortFiled(), dto.getSortAsc());
    }

    private static QueryWrapper getRoleRemoveGameWrapper(RoleRemoveGameMonitorDTO dto) {
        return QueryWrapper.create()
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GS_START_TIME)
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_NAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_NAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PARENT_GAME_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.PARENT_GAME_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ASSOCIATION_USER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.ASSOCIATION_USER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_LEVEL).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_LEVEL.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_VIP).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_VIP.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.COMBAT_NUM).as(ADS_ROLE_AMOUNT_DATA_PARENT.COMBAT_NUM.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.COUNTRY).as(ADS_ROLE_AMOUNT_DATA_PARENT.COUNTRY.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME.getName()))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME)).as("role_create_day_diff"))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME)).as("role_active_day_diff"))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME)).as("last_order_day_diff"))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.FIRST_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.FIRST_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_HOUR_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_HOUR_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PRODUCT_NAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.PRODUCT_NAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_CNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_CNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.APP_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.APP_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PROPORTION).as(ADS_ROLE_AMOUNT_DATA_PARENT.PROPORTION.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.CONCAT_GAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.CONCAT_GAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_COUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_COUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_END_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_END_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SOURCE_SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SOURCE_SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.END_SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.END_SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SUPER_GAME_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SUPER_GAME_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.IS_REMOVE_GAME).as("human_remove_game_status"))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID_SERVER).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID_SERVER.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_COUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_COUNT.getName()))
                .select("IF(TIMESTAMPDIFF(HOUR, max(ads_role_amount_data_parent.active_time), NOW()) > 72,IF(TIMESTAMPDIFF(HOUR, max(ads_role_amount_data_parent.last_order_time), NOW()) < 72 , 0, 1) , 0) as system_remove_game_status")
                .from(ADS_ROLE_AMOUNT_DATA_PARENT)
                .ge(AdsRoleAmountDataParent::getRoleTime, null == dto.getRoleCreateDayBegin() ? null : LocalDateTime.of(dto.getRoleCreateDayBegin(), LocalTime.MIDNIGHT), null != dto.getRoleCreateDayBegin())
                .le(AdsRoleAmountDataParent::getRoleTime, null == dto.getRoleCreateDayEnd() ? null : LocalDateTime.of(dto.getRoleCreateDayEnd(), LocalTime.MAX), null != dto.getRoleCreateDayEnd())
                .in(AdsRoleAmountDataParent::getGsId, dto.getGsIdList(), CollectionUtils.isNotEmpty(dto.getGsIdList()))
                .eq(AdsRoleAmountDataParent::getParentGameId, dto.getParentGameId(), null != dto.getParentGameId())
                .like(AdsRoleAmountDataParent::getRoleName, dto.getRoleName(), StringUtils.isNotBlank(dto.getRoleName()))
                .eq(AdsRoleAmountDataParent::getSuperGameId, dto.getSuperGameId(), null != dto.getSuperGameId())
                .in(AdsRoleAmountDataParent::getEndServerId, dto.getServerIdList(), CollectionUtils.isNotEmpty(dto.getServerIdList()))
                .ge(AdsRoleAmountDataParent::getGsStartTime, dto.getServeDayBegin(), null != dto.getServeDayBegin())
                .le(AdsRoleAmountDataParent::getGsEndTime, dto.getServeDayEnd(), null != dto.getServeDayEnd())
                .ge(AdsRoleAmountDataParent::getGsEndTime, LocalDate.now())
                .ne(AdsRoleAmountDataParent::getGsId, 0, null != dto.getRoleSource() && 2 == dto.getRoleSource())
                .isNotNull(AdsRoleAmountDataParent::getGsIdServer, null != dto.getRoleSource() && 1 == dto.getRoleSource())
                .and(ADS_ROLE_AMOUNT_DATA_PARENT.IS_REMOVE_GAME.ne(1).or(ADS_ROLE_AMOUNT_DATA_PARENT.IS_REMOVE_GAME.isNull()))
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GS_START_TIME)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID)
                .having(QueryCondition.createEmpty().and("system_remove_game_status = ?", 1))
                .orderBy(dto.getSortFiled(), dto.getSortAsc());
    }

    private static QueryWrapper getBigRWrapper(BigRDTO dto) {
        return QueryWrapper.create()
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID)
                .select(ADS_ROLE_AMOUNT_DATA_PARENT.GS_START_TIME)
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_NAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_NAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PARENT_GAME_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.PARENT_GAME_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ASSOCIATION_USER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.ASSOCIATION_USER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_LEVEL).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_LEVEL.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_VIP).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_VIP.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.COMBAT_NUM).as(ADS_ROLE_AMOUNT_DATA_PARENT.COMBAT_NUM.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.COUNTRY).as(ADS_ROLE_AMOUNT_DATA_PARENT.COUNTRY.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME.getName()))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_TIME)).as("role_create_day_diff"))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME)).as("role_active_day_diff"))
                .select(dateDiff(currentDate(), max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME)).as("last_order_day_diff"))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.FIRST_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.FIRST_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_HOUR_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_HOUR_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.LAST_ORDER_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PRODUCT_NAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.PRODUCT_NAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_CNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.TOTAL_CNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.ACTIVE_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.APP_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.APP_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.PROPORTION).as(ADS_ROLE_AMOUNT_DATA_PARENT.PROPORTION.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.CONCAT_GAME).as(ADS_ROLE_AMOUNT_DATA_PARENT.CONCAT_GAME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_COUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_COUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_END_TIME).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_END_TIME.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SOURCE_SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SOURCE_SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.END_SERVER_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.END_SERVER_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SUPER_GAME_ID).as(ADS_ROLE_AMOUNT_DATA_PARENT.SUPER_GAME_ID.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.IS_REMOVE_GAME).as("human_remove_game_status"))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID_SERVER).as(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID_SERVER.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_AMOUNT.getName()))
                .select(max(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_COUNT).as(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_COUNT.getName()))
                .select(sum(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.TODAY_COUNT).as(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.TODAY_COUNT.getName()))
                .select(sum(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.TODAY_AMOUNT).as(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.TODAY_AMOUNT.getName()))
                .select(sum(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.TODAY_AMOUNT).divide(sum(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.SERVER_AMOUNT)).as(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.SERVER_PROPORTION.getName()))
                .select(sum(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.TODAY_AMOUNT).divide(sum(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.GAME_AMOUNT)).as(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.GAME_PROPORTION.getName()))
                .select("IF(TIMESTAMPDIFF(HOUR, max(ads_role_amount_data_parent.active_time), NOW()) > 72,IF(TIMESTAMPDIFF(HOUR, max(ads_role_amount_data_parent.last_order_time), NOW()) < 72 , 0, 1) , 0) as system_remove_game_status")
                .from(ADS_ROLE_AMOUNT_DATA_PARENT)
                .leftJoin(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT)
                .on(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID.eq(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.GAME_ID)
                        .and(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID.eq(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.ROLE_ID)))
                .and(ADS_ROLE_AMOUNT_DATA_PARENT.SERVER_ID.isNotNull().or(ADS_ROLE_AMOUNT_DATA_TODAY_PARENT.SERVER_ID.isNotNull()))
                .ge(AdsRoleAmountDataTodayParent::getDay, dto.getDtBegin(), null != dto.getDtBegin())
                .le(AdsRoleAmountDataTodayParent::getDay, dto.getDtBegin(), null != dto.getDtEnd())
                .ge(AdsRoleAmountDataParent::getRoleTime, null == dto.getRoleCreateDayBegin() ? null : LocalDateTime.of(dto.getRoleCreateDayBegin(), LocalTime.MIDNIGHT), null != dto.getRoleCreateDayBegin())
                .le(AdsRoleAmountDataParent::getRoleTime, null == dto.getRoleCreateDayEnd() ? null : LocalDateTime.of(dto.getRoleCreateDayEnd(), LocalTime.MAX), null != dto.getRoleCreateDayEnd())
                .in(AdsRoleAmountDataParent::getGsId, dto.getGsIdList(), CollectionUtils.isNotEmpty(dto.getGsIdList()))
                .eq(AdsRoleAmountDataParent::getParentGameId, dto.getParentGameId(), null != dto.getParentGameId())
                .like(AdsRoleAmountDataParent::getRoleName, dto.getRoleName(), StringUtils.isNotBlank(dto.getRoleName()))
                .eq(AdsRoleAmountDataParent::getSuperGameId, dto.getSuperGameId(), null != dto.getSuperGameId())
                .in(AdsRoleAmountDataParent::getEndServerId, dto.getServerIdList(), CollectionUtils.isNotEmpty(dto.getServerIdList()))
                .ge(AdsRoleAmountDataParent::getGsStartTime, dto.getServeDayBegin(), null != dto.getServeDayBegin())
                .le(AdsRoleAmountDataParent::getGsEndTime, dto.getServeDayEnd(), null != dto.getServeDayEnd())
                .ge(AdsRoleAmountDataParent::getGsEndTime, LocalDate.now(), null != dto.getServeStatus() && 1 == dto.getServeStatus())
                .le(AdsRoleAmountDataParent::getGsEndTime, LocalDate.now(), null != dto.getServeStatus() && 2 == dto.getServeStatus())
                .ne(AdsRoleAmountDataParent::getGsId, 0, null != dto.getRoleSource() && 2 == dto.getRoleSource())
                .isNotNull(AdsRoleAmountDataParent::getGsIdServer, null != dto.getRoleSource() && 1 == dto.getRoleSource())
                .eq(AdsRoleAmountDataParent::getIsRemoveGame, dto.getHumanRemoveGameStatus(), null != dto.getHumanRemoveGameStatus() && 1 == dto.getHumanRemoveGameStatus())
                .and(wrapper -> wrapper.ne(AdsRoleAmountDataParent::getIsRemoveGame,1).or(ADS_ROLE_AMOUNT_DATA_PARENT.IS_REMOVE_GAME.isNull()), null != dto.getHumanRemoveGameStatus() && 0 == dto.getHumanRemoveGameStatus())
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GAME_ID)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GS_ID)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.GS_START_TIME)
                .groupBy(ADS_ROLE_AMOUNT_DATA_PARENT.ROLE_ID)
                .having(QueryCondition.createEmpty().and("system_remove_game_status = ?", dto.getSystemRemoveGameStatus()).when(null != dto.getSystemRemoveGameStatus()))
                .orderBy(dto.getSortFiled(), dto.getSortAsc());
    }
}