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

import com.red.circle.common.business.core.enums.SysOriginPlatformEnum;
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.service.sys.EnumConfigClientService;
import com.red.circle.other.infra.convertor.sys.EnumConfigInfraConvertor;
import com.red.circle.other.infra.database.cache.service.other.EnumConfigCacheService;
import com.red.circle.other.infra.database.rds.entity.sys.EnumConfig;
import com.red.circle.other.infra.database.rds.service.sys.EnumConfigService;
import com.red.circle.other.inner.enums.config.EnumConfigKey;
import com.red.circle.other.inner.model.cmd.sys.SysEnumConfigQryCmd;
import com.red.circle.other.inner.model.dto.sys.EnumConfigDTO;
import com.red.circle.other.inner.model.dto.sys.SysEnumConfigSortDTO;
import java.math.BigDecimal;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * 枚举配置服务 实现.
 *
 * @author pengliang on 2023/5/25
 */
@Service
@RequiredArgsConstructor
public class EnumConfigClientServiceImpl implements EnumConfigClientService {

  private final EnumConfigService enumConfigService;
  private final EnumConfigCacheService enumConfigCacheService;
  private final EnumConfigInfraConvertor enumConfigAppConvertor;

  @Override
  public List<EnumConfigDTO> listAppConfig(String sysOrigin) {
    return enumConfigCacheService.listAppConfig(sysOrigin);
  }

  @Override
  public EnumConfigDTO getByCode(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.getByCode(key, sysOrigin);
  }

  @Override
  public EnumConfigDTO getByCode(String key, String sysOrigin) {
    return enumConfigCacheService.getByCode(key, sysOrigin);
  }

  @Override
  public String getValue(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.getValue(key, sysOrigin);
  }

  @Override
  public Integer getValueInt(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.getValueInt(key, sysOrigin);
  }

  @Override
  public Long getValueLong(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.getValLong(key, sysOrigin);
  }

  @Override
  public BigDecimal getValueBigDecimal(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.getValueBigDecimal(key, sysOrigin);
  }

  @Override
  public Boolean getValueBool(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.getValueBool(key, sysOrigin);
  }

  @Override
  public BigDecimal giftGoldRatioFormula(BigDecimal ratio, BigDecimal gold) {
    return enumConfigCacheService.giftGoldRatioFormula(ratio, gold);
  }

  @Override
  public BigDecimal getGiftGoldRatio(SysOriginPlatformEnum sysOrigin) {
    return enumConfigCacheService.getGiftGoldRatio(sysOrigin);
  }

  @Override
  public BigDecimal getLuckyGiftGoldRatio(SysOriginPlatformEnum sysOrigin) {
    return enumConfigCacheService.getLuckyGiftGoldRatio(sysOrigin);
  }

  @Override
  public BigDecimal getGiftGoldRatioSelf(SysOriginPlatformEnum sysOrigin) {
    return enumConfigCacheService.getGiftGoldRatioSelf(sysOrigin);
  }

  @Override
  public BigDecimal toDiamondQuantity(EnumConfigKey key, String sysOrigin) {
    return enumConfigCacheService.toDiamondQuantity(key, sysOrigin);
  }

  @Override
  public BigDecimal goldsToDiamondQuantity(BigDecimal gold, String sysOrigin) {
    return enumConfigCacheService.goldsToDiamondQuantity(gold, sysOrigin);
  }

  @Override
  public BigDecimal goldsToDiamondQuantity(BigDecimal gold, BigDecimal exchangeRatio) {
    return enumConfigCacheService.goldsToDiamondQuantity(gold, exchangeRatio);
  }

  @Override
  public BigDecimal getDiamondEqGold(String sysOrigin) {
    return enumConfigCacheService.getDiamondEqGold(sysOrigin);
  }

  @Override
  public BigDecimal getDiamondExchangeMultiple(String sysOrigin) {
    return enumConfigCacheService.getDiamondExchangeMultiple(sysOrigin);
  }

  @Override
  public BigDecimal getDiamondExchangeMaxGold(String sysOrigin) {
    return enumConfigCacheService.getDiamondExchangeMaxGold(sysOrigin);
  }


  @Override
  public PageResult<EnumConfigDTO> pageSysEnumConfig(SysEnumConfigQryCmd query) {
    return enumConfigService.pageSysEnumConfig(query)
        .convert(enumConfigAppConvertor::toEnumConfigDTO);
  }

  @Override
  public List<EnumConfigDTO> sysEnumConfigList(String groupName) {
    return enumConfigAppConvertor.toListEnumConfigDTO(
        enumConfigService.sysEnumConfigListByGroupName(groupName));
  }

  @Override
  public String getViolationAvatar(EnumConfigKey violationPicture) {
    return enumConfigService.getViolationAvatar(violationPicture);
  }

  @Override
  public void saveSysEnumConfig(EnumConfigDTO sysEnumConfig) {
    enumConfigCacheService.removeAll(sysEnumConfig.getSysOrigin());
    sysEnumConfig.setInoperable(Boolean.FALSE);
    enumConfigService.save(enumConfigAppConvertor.toEnumConfig(sysEnumConfig));
  }

  @Override
  public void updateSysEnumConfig(EnumConfigDTO sysEnumConfig) {
    enumConfigCacheService.removeAll(sysEnumConfig.getSysOrigin());
    //enumConfigCacheService.putValue(sysEnumConfig.getName(), sysEnumConfig, sysEnumConfig.getSysOrigin());
    enumConfigService.updateSelectiveById(enumConfigAppConvertor.toEnumConfig(sysEnumConfig));
  }

  @Override
  public void deleteBySysOriginAndName(Long id) {
    EnumConfig sysEnumConfig = enumConfigService.getById(id);
    ResponseAssert.notNull(CommonErrorCode.NOT_FOUND_RECORD_INFO, sysEnumConfig);
    enumConfigCacheService.removeAll(sysEnumConfig.getSysOrigin());
    //enumConfigCacheService.remove(sysEnumConfig.getName(), sysEnumConfig.getSysOrigin());
    enumConfigService.deleteBySysOriginAndName(sysEnumConfig.getName(),
        sysEnumConfig.getSysOrigin());
  }

  @Override
  public void updateSort(List<SysEnumConfigSortDTO> params) {
    enumConfigService.updateSort(params);
  }

}
