package com.red.circle.other.app.inner.service.game.impl;

import com.google.common.collect.Lists;
import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.framework.core.response.CommonErrorCode;
import com.red.circle.framework.dto.PageResult;
import com.red.circle.other.app.inner.convertor.game.GameLudoInnerConvertor;
import com.red.circle.other.app.inner.convertor.live.RoomProfileInnerConvertor;
import com.red.circle.other.app.inner.service.game.GameLuckyBoxClientService;
import com.red.circle.other.infra.database.cache.service.other.GameNewLuckyBoxCacheService;
import com.red.circle.other.infra.database.mongo.service.live.RoomProfileManagerService;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxAwardConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxBountyDetailsConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxFortuneValueConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxGiftConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxProbabilityConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxStandardConfig;
import com.red.circle.other.infra.database.rds.entity.game.GameLuckyBoxTimeConfig;
import com.red.circle.other.infra.database.rds.entity.gift.GiftConfig;
import com.red.circle.other.infra.database.rds.entity.props.PropsSourceRecord;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxAwardConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxAwardDetailsConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxBountyConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxBountyDetailsConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxCountService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxFortuneValueConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxGiftConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxProbabilityConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxStandardConfigService;
import com.red.circle.other.infra.database.rds.service.game.GameLuckyBoxTimeConfigService;
import com.red.circle.other.infra.database.rds.service.gift.GiftBackpackService;
import com.red.circle.other.infra.database.rds.service.gift.GiftConfigService;
import com.red.circle.other.infra.database.rds.service.props.PropsSourceRecordService;
import com.red.circle.other.inner.enums.material.ConsolePropsTypeEnum;
import com.red.circle.other.inner.model.cmd.game.GameLuckBoxAwardConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckBoxBountyConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckBoxFortuneValueConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckBoxStandardConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckyBoxAwardDetailsConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckyBoxBountyDetailsConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.GameLuckyBoxStandardDetailsConfigQryCmd;
import com.red.circle.other.inner.model.cmd.game.LuckBoxGameQryCmd;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxAwardConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxAwardDetailsConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxBountyConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxBountyDetailsConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxCountDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxCountInfoDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxFortuneValueConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxGiftConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxProbabilityConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxStandardConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxStandardDetailsConfigDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxTicketCountInfoDTO;
import com.red.circle.other.inner.model.dto.game.GameLuckyBoxTimeConfigDTO;
import com.red.circle.other.inner.model.dto.game.LuckyBoxGiftMapDTO;
import com.red.circle.other.inner.model.dto.live.RoomProfileDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import com.red.circle.tool.core.date.TimestampUtils;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 游戏砸金蛋服务.
 *
 * @author pengliang on 2023/6/10
 */
@Service
@RequiredArgsConstructor
public class GameLuckyBoxClientServiceImpl implements GameLuckyBoxClientService {

  private final GiftConfigService giftConfigService;
  private final GiftBackpackService giftBackpackService;
  private final GameLudoInnerConvertor gameLudoInnerConvertor;
  private final GameLuckyBoxCountService gameLuckyBoxCountService;
  private final PropsSourceRecordService propsSourceRecordService;
  private final RoomProfileManagerService roomProfileManagerService;
  private final RoomProfileInnerConvertor roomProfileInnerConvertor;
  private final GameNewLuckyBoxCacheService gameNewLuckyBoxCacheService;
  private final GameLuckyBoxTimeConfigService gameLuckyBoxTimeConfigService;
  private final GameLuckyBoxGiftConfigService gameLuckyBoxGiftConfigService;
  private final GameLuckyBoxAwardConfigService gameLuckyBoxAwardConfigService;
  private final GameLuckyBoxBountyConfigService gameLuckyBoxBountyConfigService;
  private final GameLuckyBoxStandardConfigService gameLuckyBoxStandardConfigService;
  private final GameLuckyBoxProbabilityConfigService gameLuckyBoxProbabilityConfigService;
  private final GameLuckyBoxAwardDetailsConfigService gameLuckyBoxAwardDetailsConfigService;
  private final GameLuckyBoxFortuneValueConfigService gameLuckyBoxFortuneValueConfigService;
  private final GameLuckyBoxBountyDetailsConfigService gameLuckyBoxBountyDetailsConfigService;


  @Override
  public PageResult<GameLuckyBoxCountDTO> pageLuckyBoxGameRecord(LuckBoxGameQryCmd query) {
    PageResult<GameLuckyBoxCountDTO> iPage = gameLuckyBoxCountService.pageByCondition(query)
        .convert(gameLudoInnerConvertor::toGameLuckyBoxCountDTO);

    if (CollectionUtils.isEmpty(iPage.getRecords())) {
      iPage.convert(gameLuckyBoxCount -> gameLuckyBoxCount);
    }

    Map<Long, RoomProfileDTO> roomProfileMap = getRoomProfileMap(
        iPage.getRecords().stream().toList());

    return iPage.convert(gameLuckyBoxCount -> {
      RoomProfileDTO roomProfile = roomProfileMap.get(gameLuckyBoxCount.getRoomId());

      if (Objects.isNull(roomProfile)) {
        return null;
      }

      return new GameLuckyBoxCountDTO()
          .setId(gameLuckyBoxCount.getId())
          .setFrequency(gameLuckyBoxCount.getFrequency())
          .setGiftAmount(gameLuckyBoxCount.getGiftAmount())
          .setUserId(gameLuckyBoxCount.getUserId())
          .setPayAmount(gameLuckyBoxCount.getPayAmount())
          .setSysOrigin(gameLuckyBoxCount.getSysOrigin())
          .setRoomProfile(new RoomProfileDTO()
              .setId(roomProfile.getId())
              .setRoomAccount(roomProfile.getRoomAccount())
              .setRoomCover(roomProfile.getRoomCover())
              .setRoomName(roomProfile.getRoomName()))
          .setCreateTime(gameLuckyBoxCount.getCreateTime());
    });
  }


  @Override
  public GameLuckyBoxCountInfoDTO countLuckyBoxGame(LuckBoxGameQryCmd query) {
    // TODO LuckyBox切换系统时需要优化
    GameLuckyBoxCountInfoDTO gameLuckyBoxCountInfoVO = gameLuckyBoxCountService.countLuckyBoxGame(
        query);
    GiftConfig giftConfig = giftConfigService.getByCode("lucky_box_gift-choujiangquan-3");
    if (Objects.nonNull(giftConfig)) {
      GameLuckyBoxTicketCountInfoDTO ticketCountInfo = giftBackpackService.ticketCount(
          giftConfig.getId(), Objects.nonNull(query.getUserId()) ? query.getUserId() : null);
      if (Objects.nonNull(ticketCountInfo)) {
        gameLuckyBoxCountInfoVO.setTicketCount(ticketCountInfo.getTicketCount());
        gameLuckyBoxCountInfoVO.setConsumeTicketCount(ticketCountInfo.getConsumeTicketCount());
        if (Objects.nonNull(gameLuckyBoxCountInfoVO.getAwardCount())) {
          gameLuckyBoxCountInfoVO.setAwardNotTicketCount(
              gameLuckyBoxCountInfoVO.getAwardCount() - ticketCountInfo.getTicketCount() * 330);
        }
      }
    }
    return gameLuckyBoxCountInfoVO;
  }

  @Override
  public GameLuckyBoxFortuneValueConfigDTO getLuckyBoxFortuneValueConfig(
      GameLuckBoxFortuneValueConfigQryCmd query) {
    GameLuckyBoxFortuneValueConfig fortuneValueConfig = gameLuckyBoxFortuneValueConfigService.getLuckyBoxFortuneValueConfig(
        query.getSysOrigin());
    return gameLudoInnerConvertor.toGameLuckyBoxFortuneValueConfigDTO(fortuneValueConfig);
  }


  @Override
  public void addLuckyBoxFortuneValueConfig(GameLuckyBoxFortuneValueConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          gameLuckyBoxFortuneValueConfigService.updateSelectiveById(
              gameLudoInnerConvertor.toGameLuckyBoxFortuneValueConfig(param)));
      gameNewLuckyBoxCacheService.removeFortuneValueConfigCache();
      return;
    }
    GameLuckyBoxFortuneValueConfig fortuneValueConfig = gameLudoInnerConvertor.toGameLuckyBoxFortuneValueConfig(
        param);
    gameLuckyBoxFortuneValueConfigService.save(fortuneValueConfig);
  }


  @Override
  public PageResult<GameLuckyBoxBountyConfigDTO> pageBountyConfig(
      GameLuckBoxBountyConfigQryCmd query) {
    PageResult<GameLuckyBoxBountyConfigDTO> iPage = gameLuckyBoxBountyConfigService.getBountyConfig(
        query).convert(gameLudoInnerConvertor::toGameLuckyBoxBountyConfigDTO);
    if (CollectionUtils.isEmpty(iPage.getRecords())) {
      return iPage;
    }

    Map<Long, GiftConfig> giftConfigMap = giftConfigService.mapByIds(
        iPage.getRecords().stream().map(GameLuckyBoxBountyConfigDTO::getGiftId)
            .collect(Collectors.toSet()));
    return iPage.convert(gameLuckyBoxBountyConfig -> {
      gameLuckyBoxBountyConfig.setPhotoUrl(
          giftConfigMap.get(gameLuckyBoxBountyConfig.getGiftId()).getGiftPhoto());
      return gameLuckyBoxBountyConfig;
    });
  }

  @Override
  public void addGameLuckyBoxBountyConfig(GameLuckyBoxBountyConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      gameLuckyBoxBountyConfigService.updateSelectiveById(
          gameLudoInnerConvertor.toGameLuckyBoxBountyConfig(param));
      return;
    }
    gameLuckyBoxBountyConfigService.save(gameLudoInnerConvertor.toGameLuckyBoxBountyConfig(param));
  }

  @Override
  public void deleteBountyConfig(Long id) {
    gameLuckyBoxBountyDetailsConfigService.deleteByBountyId(id);
    gameLuckyBoxBountyConfigService.removeById(id);
  }


  @Override
  public List<GameLuckyBoxBountyDetailsConfigDTO> getGameLuckyBoxBountyDetailsConfig(
      GameLuckyBoxBountyDetailsConfigQryCmd query) {
    List<GameLuckyBoxBountyDetailsConfig> list = gameLuckyBoxBountyDetailsConfigService.getGameLuckyBoxBountyDetailsConfig(
        query);
    if (CollectionUtils.isEmpty(list)) {
      return Lists.newArrayList();
    }
    Map<Long, PropsSourceRecord> propsSourceRecordMap = propsSourceRecordService.mapByIds(
        list.stream().map(GameLuckyBoxBountyDetailsConfig::getPropsId)
            .collect(Collectors.toSet()));
    return list.stream().map(
            bountyDetailsConfig -> {
              GameLuckyBoxBountyDetailsConfigDTO detailsConfigVO = gameLudoInnerConvertor.toGameLuckyBoxBountyDetailsConfigDTO(
                  bountyDetailsConfig);
              PropsSourceRecord propsSourceRecord = propsSourceRecordMap.get(
                  detailsConfigVO.getPropsId());
              detailsConfigVO.setCover(propsSourceRecord.getCover());
              detailsConfigVO.setSourceUrl(propsSourceRecord.getSourceUrl());
              detailsConfigVO.setPropsType(
                  ConsolePropsTypeEnum.getDescValue(propsSourceRecord.getType()));
              return detailsConfigVO;
            })
        .collect(Collectors.toList());
  }

  @Override
  public void addGameLuckyBoxBountyDetailsConfig(GameLuckyBoxBountyDetailsConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      gameLuckyBoxBountyDetailsConfigService.updateSelectiveById(
          gameLudoInnerConvertor.toGameLuckyBoxBountyDetailsConfig(param));
      return;
    }
    gameLuckyBoxBountyDetailsConfigService.save(
        gameLudoInnerConvertor.toGameLuckyBoxBountyDetailsConfig(param));
  }

  @Override
  public void deleteBountyDetailsConfig(Long id) {
    gameLuckyBoxBountyDetailsConfigService.removeById(id);
  }

  @Override
  public PageResult<GameLuckyBoxAwardConfigDTO> pageAwardConfig(
      GameLuckBoxAwardConfigQryCmd query) {
    return gameLuckyBoxAwardConfigService.pageAwardConfig(query)
        .convert(gameLudoInnerConvertor::toGameLuckyBoxAwardConfig);
  }


  @Override
  public void addGameLuckyBoxAwardConfig(GameLuckyBoxAwardConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      gameLuckyBoxAwardConfigService.updateSelectiveById(
          gameLudoInnerConvertor.toGameLuckyBoxAwardConfig(param));
      return;
    }
    gameLuckyBoxAwardConfigService.save(gameLudoInnerConvertor.toGameLuckyBoxAwardConfig(param));

  }


  @Override
  public void deleteAwardConfig(Long id) {
    GameLuckyBoxAwardConfig awardConfig = gameLuckyBoxAwardConfigService.getById(id);
    gameLuckyBoxAwardDetailsConfigService.deleteByAwardId(id);
    gameLuckyBoxAwardConfigService.removeById(id);
    gameNewLuckyBoxCacheService.removeAwardDetailsConfigCache(id, awardConfig.getLotteryType());
  }


  @Override
  public List<GameLuckyBoxAwardDetailsConfigDTO> listGameLuckyBoxAwardDetailsConfig(
      GameLuckyBoxAwardDetailsConfigQryCmd query) {

    List<GameLuckyBoxAwardDetailsConfigDTO> list = gameLudoInnerConvertor.toListGameLuckyBoxAwardDetailsConfigDTO(
        gameLuckyBoxAwardDetailsConfigService.listGameLuckyBoxAwardDetailsConfig(
            query));

    if (CollectionUtils.isEmpty(list)) {
      return Lists.newArrayList();
    }
    Map<Long, PropsSourceRecord> propsSourceRecordMap = propsSourceRecordService.mapByIds(
        list.stream().map(GameLuckyBoxAwardDetailsConfigDTO::getPropsId)
            .collect(Collectors.toSet()));
    return list.stream().peek(
            awardDetailsConfig -> {
              PropsSourceRecord propsSourceRecord = propsSourceRecordMap.get(
                  awardDetailsConfig.getPropsId());
              awardDetailsConfig.setCover(propsSourceRecord.getCover());
              awardDetailsConfig.setSourceUrl(propsSourceRecord.getSourceUrl());
              awardDetailsConfig.setPropsType(
                  ConsolePropsTypeEnum.getDescValue(propsSourceRecord.getType()));
            })
        .collect(Collectors.toList());
  }

  @Override
  public void addGameLuckyBoxAwardDetailsConfig(GameLuckyBoxAwardDetailsConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          gameLuckyBoxAwardDetailsConfigService.updateSelectiveById(
              gameLudoInnerConvertor.toGameLuckyBoxAwardDetailsConfig(param)));
      GameLuckyBoxAwardConfig awardConfig = gameLuckyBoxAwardConfigService.getById(
          param.getAwardId());
      gameNewLuckyBoxCacheService.removeAwardDetailsConfigCache(awardConfig.getId(),
          awardConfig.getLotteryType());
      return;
    }
    gameLuckyBoxAwardDetailsConfigService.save(
        gameLudoInnerConvertor.toGameLuckyBoxAwardDetailsConfig(param));
  }

  @Override
  public void deleteAwardDetailsConfig(Long id) {
    gameLuckyBoxAwardDetailsConfigService.removeById(id);
  }


  @Override
  public PageResult<GameLuckyBoxStandardConfigDTO> pageStandardConfig(
      GameLuckBoxStandardConfigQryCmd query) {
    return gameLuckyBoxStandardConfigService.getStandardConfig(query)
        .convert(gameLudoInnerConvertor::toGameLuckyBoxStandardConfigDTO);
  }


  @Override
  public void addStandardConfig(GameLuckyBoxStandardConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      gameLuckyBoxStandardConfigService.updateSelectiveById(
          gameLudoInnerConvertor.toGameLuckyBoxStandardConfig(param));
      return;
    }
    gameLuckyBoxStandardConfigService.save(
        gameLudoInnerConvertor.toGameLuckyBoxStandardConfig(param));
  }


  @Override
  public void switchStatus(Long id, Boolean status) {
    gameLuckyBoxStandardConfigService.switchStatus(id, status);
  }


  @Override
  public void deleteStandardConfig(Long id) {

    GameLuckyBoxStandardConfig standardConfig = gameLuckyBoxStandardConfigService.getById(id);

    // 删除礼物配置
    gameLuckyBoxGiftConfigService.deleteByStandardId(id);
    gameNewLuckyBoxCacheService.removeGiftInfoCache(id, standardConfig.getSysOrigin());

    List<GameLuckyBoxTimeConfigDTO> timeConfigs = gameLudoInnerConvertor.toListGameLuckyBoxTimeConfigDTO(
        gameLuckyBoxTimeConfigService.getByStandardId(
            new GameLuckyBoxStandardDetailsConfigQryCmd()
                .setStandardId(id)
        )
    );
    if (CollectionUtils.isNotEmpty(timeConfigs)) {
      for (GameLuckyBoxTimeConfigDTO timeConfigVO : timeConfigs) {
        // 清除概率模板
        gameNewLuckyBoxCacheService.removeInventoryTemplate(id, timeConfigVO.getSort());
        // 删除抽奖概率
        gameLuckyBoxProbabilityConfigService.deleteByTimeId(timeConfigVO.getId());
      }

    }
    // 删除抽奖频数
    gameLuckyBoxTimeConfigService.deleteByStandardId(id);
    gameNewLuckyBoxCacheService.removeTimeConfigCache(standardConfig.getId(),
        standardConfig.getSysOrigin());
    // 删除抽奖规格
    gameLuckyBoxStandardConfigService.removeById(id);
  }


  @Override
  public List<GameLuckyBoxTimeConfigDTO> getGameLuckyBoxStandardDetailsConfig(
      GameLuckyBoxStandardDetailsConfigQryCmd query) {
    List<GameLuckyBoxTimeConfigDTO> luckyBoxTimeConfigs = gameLudoInnerConvertor.toListGameLuckyBoxTimeConfigDTO(
        gameLuckyBoxTimeConfigService.getByStandardId(
            query));
    // 规格概率详情
    Map<Long, List<GameLuckyBoxProbabilityConfigDTO>> probabilityDetailsMap = gameLuckyBoxProbabilityConfigService.getLuckyBoxProbabilityDetails(
        query.getSysOrigin());

    if (CollectionUtils.isEmpty(luckyBoxTimeConfigs)) {
      return Lists.newArrayList();
    }
    return luckyBoxTimeConfigs.stream()
        .peek(luckyBoxTimeConfig -> luckyBoxTimeConfig.setGameLuckyBoxProbabilityConfigs(
            probabilityDetailsMap.get(luckyBoxTimeConfig.getId()))).collect(Collectors.toList());
  }


  @Override
  public List<GameLuckyBoxGiftConfigDTO> getGameLuckyBoxGiftConfig(
      GameLuckyBoxStandardDetailsConfigQryCmd query) {
    return gameLudoInnerConvertor.toListGameLuckyBoxGiftConfigDTO(
        gameLuckyBoxGiftConfigService.getByStandardId(query));
  }


  @Override
  public List<LuckyBoxGiftMapDTO> mapLuckyBoxGiftMap(
      GameLuckyBoxStandardDetailsConfigQryCmd query) {
    List<GameLuckyBoxGiftConfig> gameLuckyBoxGiftConfigs = gameLuckyBoxGiftConfigService.getByStandardId(
        query);
    if (CollectionUtils.isEmpty(gameLuckyBoxGiftConfigs)) {
      return Lists.newArrayList();
    }
    return gameLuckyBoxGiftConfigs.stream().map(gameLuckyBoxGiftConfigVO -> {
      LuckyBoxGiftMapDTO luckyBoxGiftMap = new LuckyBoxGiftMapDTO();
      luckyBoxGiftMap.setValue(gameLuckyBoxGiftConfigVO.getId());
      luckyBoxGiftMap.setName(gameLuckyBoxGiftConfigVO.getName());
      return luckyBoxGiftMap;
    }).collect(Collectors.toList());
  }

  @Override
  public void addGameLuckyBoxGiftConfig(GameLuckyBoxGiftConfigDTO param) {
    if (Objects.nonNull(param.getId())) {
      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          gameLuckyBoxGiftConfigService.updateSelectiveById(
              gameLudoInnerConvertor.toGameLuckyBoxGiftConfig(param)));
      gameNewLuckyBoxCacheService.removeGiftInfoCache(param.getStandardId(), param.getSysOrigin());
      return;
    }
    gameLuckyBoxGiftConfigService.save(gameLudoInnerConvertor.toGameLuckyBoxGiftConfig(param));
  }

  @Override
  public void addGameLuckyBoxStandardDetailsConfig(GameLuckyBoxStandardDetailsConfigDTO param) {
    GameLuckyBoxTimeConfigDTO gameLuckyBoxTimeConfig = addLuckyBoxTimeConfig(param);
    gameLuckyBoxProbabilityConfigService.add(gameLuckyBoxTimeConfig.getId(),
        getGameLuckyBoxProbabilityDetails(param,
            gameLuckyBoxTimeConfig));

  }

  private GameLuckyBoxTimeConfigDTO addLuckyBoxTimeConfig(
      GameLuckyBoxStandardDetailsConfigDTO param) {
    GameLuckyBoxTimeConfigDTO gameLuckyBoxTimeConfig = param.getGameLuckyBoxTimeConfig();
    if (Objects.nonNull(gameLuckyBoxTimeConfig.getId())) {
      ResponseAssert.isTrue(CommonErrorCode.UPDATE_FAILURE,
          gameLuckyBoxTimeConfigService.updateSelectiveById(
              gameLudoInnerConvertor.toGameLuckyBoxTimeConfig(gameLuckyBoxTimeConfig)));
      gameNewLuckyBoxCacheService.removeTimeConfigCache(gameLuckyBoxTimeConfig.getStandardId(),
          gameLuckyBoxTimeConfig.getSysOrigin());
      // 清除库存
      gameNewLuckyBoxCacheService.removeRemainingInventory(gameLuckyBoxTimeConfig.getStandardId(),
          gameLuckyBoxTimeConfig.getSort());
      return gameLuckyBoxTimeConfig;
    }
    GameLuckyBoxTimeConfig timeConfig = gameLudoInnerConvertor.toGameLuckyBoxTimeConfig(
        gameLuckyBoxTimeConfig);
    gameLuckyBoxTimeConfigService.save(timeConfig);
    gameLuckyBoxTimeConfig.setId(timeConfig.getId());
    return gameLuckyBoxTimeConfig;
  }


  private List<GameLuckyBoxProbabilityConfig> getGameLuckyBoxProbabilityDetails(
      GameLuckyBoxStandardDetailsConfigDTO param,
      GameLuckyBoxTimeConfigDTO gameLuckyBoxTimeConfigDTO) {
    return param.getGameLuckyBoxProbabilityConfigs().stream()
        .map(gameLuckyBoxProbabilityConfigDTO -> {
          GameLuckyBoxProbabilityConfig probabilityConfig = gameLudoInnerConvertor.toGameLuckyBoxProbabilityConfig(
              gameLuckyBoxProbabilityConfigDTO);
          probabilityConfig.setTimeId(gameLuckyBoxTimeConfigDTO.getId());
          probabilityConfig.setCreateTime(TimestampUtils.now());
          probabilityConfig.setUpdateTime(TimestampUtils.now());
          probabilityConfig.setSysOrigin(gameLuckyBoxTimeConfigDTO.getSysOrigin());
          probabilityConfig.setCreateUser(gameLuckyBoxTimeConfigDTO.getCreateUser());
          return probabilityConfig;
        }).collect(Collectors.toList());
  }

  private Map<Long, RoomProfileDTO> getRoomProfileMap(
      List<GameLuckyBoxCountDTO> gameLuckyBoxCounts) {
    return roomProfileInnerConvertor.toMapRoomProfileDTO(
        roomProfileManagerService.mapProfileByRoomIds(
            gameLuckyBoxCounts.stream().map(GameLuckyBoxCountDTO::getRoomId)
                .collect(Collectors.toSet()))
    );
  }
}
