/*
package com.red.circle.console.app.service.app.sys;

import com.sugartime.app.common.mybatis.service.impl.BaseServiceImpl;
import com.sugartime.app.common.tools.StringUtils;
import com.sugartime.app.common.tools.collection.CollectionUtils;
import com.sugartime.back.infrastructure.constant.PageConstant;
import com.sugartime.back.infrastructure.framework.enums.ErrorCodeEnum;
import com.sugartime.back.infrastructure.framework.util.ApiAssert;
import com.sugartime.back.infrastructure.mapper.sys.SysProductConfigMapper;
import com.sugartime.back.infrastructure.mapper.sys.dto.ProductConfigAssociationDTO;
import com.sugartime.back.infrastructure.mapper.sys.dto.PurchaseProductAssociationQuery;
import com.sugartime.back.infrastructure.model.sys.dto.FruitDTO;
import com.sugartime.back.infrastructure.model.sys.dto.FruitTopDTO;
import com.sugartime.back.infrastructure.model.sys.entity.SysProductConfig;
import com.sugartime.back.infrastructure.model.sys.param.ProductConfigSortParam;
import com.sugartime.back.infrastructure.service.sys.ISysProductConfigService;
import lombok.AllArgsConstructor;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.collectingAndThen;

*/
/**
 * <p>
 * 内购产品列表 服务实现类
 * </p>
 *
 * @author houyong
 * @since 2020-08-05
 *//*

@AllArgsConstructor
@Service
public class SysProductConfigServiceImpl implements SysProductConfigService {

  private final SysProductConfigMapper sysProductConfigMapper;

  @Override
  public SysProductConfig getByPlatformAndProductId(String sysOrigin, String platform,
      String productId) {
    return query()
        .eq(SysProductConfig::getSysOrigin, sysOrigin)
        .eq(SysProductConfig::getPlatform, platform)
        .eq(SysProductConfig::getProductId, productId)
        .last(PageConstant.LIMIT_ONE).getOne();
  }

  @Override
  public List<SysProductConfig> listSysProductConfig(SysProductConfig sysProductConfig) {
    return query()
        .eq(SysProductConfig::getSysOrigin, sysProductConfig.getSysOrigin())
        .eq(SysProductConfig::getPlatform, sysProductConfig.getPlatform())
        .eq(StringUtils.isNotBlank(sysProductConfig.getGroupName()), SysProductConfig::getGroupName,
            sysProductConfig.getGroupName())
        .eq(StringUtils.isNotBlank(sysProductConfig.getProductType()),
            SysProductConfig::getProductType, sysProductConfig.getProductType())
        .eq(Objects.nonNull(sysProductConfig.getShowcase()), SysProductConfig::getShowcase,
            sysProductConfig.getShowcase())
        .like(StringUtils.isNotBlank(sysProductConfig.getProductId()),
            SysProductConfig::getProductId, sysProductConfig.getProductId())
        .orderByDesc(SysProductConfig::getSort)
        .list();
  }

  @Override
  public void addBatchProductConfig(List<SysProductConfig> sysProductConfigs) {
    if (CollectionUtils.isEmpty(sysProductConfigs)) {
      return;
    }
    try {
      saveBatch(sysProductConfigs);
    } catch (DuplicateKeyException ignore) {
      ApiAssert.failure(ErrorCodeEnum.BUSINESS_EXISTS);
    }
  }

  @Override
  public void updateProductConfig(SysProductConfig param, Boolean sync) {
    param.setRecommend(null);
    param.setSort(null);
    param.setPlatform(null);
    if (Objects.equals(sync, Boolean.TRUE)) {
      update().set(SysProductConfig::getObtainCandy, param.getObtainCandy())
          .set(SysProductConfig::getDiscountRate, param.getDiscountRate())
          .set(SysProductConfig::getShowcase, param.getShowcase())
          .set(SysProductConfig::getTags, param.getTags())
          .eq(SysProductConfig::getProductId, param.getProductId())
          .eq(SysProductConfig::getSysOrigin, param.getSysOrigin())
          .execute(param);
      return;
    }

    updateSelectiveById(param);
  }

  @Override
  public void deleteByProductId(String sysOrigin, String productId) {
    if (StringUtils.isNotEmpty(productId)) {
      delete()
          .eq(SysProductConfig::getSysOrigin, sysOrigin)
          .eq(SysProductConfig::getProductId, productId)
          .eq(SysProductConfig::getShowcase, Boolean.FALSE)
          .execute();
    }
  }


  @Override
  public void syncAllPlatformRecommend(String sysOrigin, String productId, String productType) {
    cleanSysOriginPlatformRecommend(sysOrigin, null, productType);
    update().set(SysProductConfig::getRecommend, Boolean.TRUE)
        .eq(SysProductConfig::getSysOrigin, sysOrigin)
        .eq(SysProductConfig::getProductId, productId)
        .execute();
  }


  @Override
  public void updateSort(ProductConfigSortParam param) {
    sysProductConfigMapper.updateSort(param);
  }

  @Override
  public boolean existsProductId(String productId) {
    return Optional.ofNullable(query()
            .select(SysProductConfig::getId)
            .eq(SysProductConfig::getProductId, productId)
            .last(PageConstant.LIMIT_ONE).getOne())
        .map(sysProductConfig -> Objects.nonNull(sysProductConfig.getId()))
        .orElse(Boolean.FALSE);
  }

  @Override
  public List<Map<String, Object>> getGiftPackGrade() {
    List<Map<String, Object>> list = new ArrayList<>();
    List<SysProductConfig> productConfigs = sysProductConfigMapper.getPackGrade();
    for (SysProductConfig productConfig : productConfigs) {
      Map<String, Object> map = new HashMap<>();
      map.put("value", productConfig.getId());
      map.put("label", productConfig.getSysOrigin());
      List<SysProductConfig> children = query()
          .eq(SysProductConfig::getSysOrigin, productConfig.getSysOrigin())
          .eq(SysProductConfig::getGroupName, "GIFT_PACK")
          .list();
      List<SysProductConfig> childrenTop = children.stream().collect(
          collectingAndThen(Collectors.toCollection(
                  () -> new TreeSet<>(Comparator.comparing(SysProductConfig::getPlatform))),
              ArrayList::new));
      Map<String, List<SysProductConfig>> sysProductConfigMap = children.stream()
          .collect(Collectors.groupingBy(SysProductConfig::getPlatform));
      map.put("children", childrenTop.stream().map(f -> new FruitTopDTO(f.getId(), f.getPlatform(),
          sysProductConfigMap.get(f.getPlatform()).stream()
              .map(t -> new FruitDTO(t.getId(), t.getDescription())).collect(
                  Collectors.toList()))));
      list.add(map);
    }
    return list;
  }

  @Override
  public List<SysProductConfig> pageSysProductGiftPackConfig(SysProductConfig sysProductConfig) {
    return sysProductConfigMapper.pageSysProductGiftPackConfig(sysProductConfig);
  }

  private void cleanSysOriginPlatformRecommend(String sysOrigin, String platform,
      String productType) {
    update().set(SysProductConfig::getRecommend, Boolean.FALSE)
        .eq(SysProductConfig::getRecommend, Boolean.TRUE)
        .eq(SysProductConfig::getSysOrigin, sysOrigin)
        .eq(StringUtils.isNotBlank(platform), SysProductConfig::getPlatform, platform)
        .eq(StringUtils.isNotBlank(productType), SysProductConfig::getProductType, productType)
        .execute();
  }

  @Override
  public List<SysProductConfig> listCandyGroupBySysOrigin(String sysOrigin) {
    ApiAssert.isFalse(ErrorCodeEnum.SYSTEM_NOT_SELECTED_ERROR, StringUtils.isBlank(sysOrigin));
    return query()
        .eq(SysProductConfig::getGroupName, "CANDY")
        .eq(SysProductConfig::getSysOrigin, sysOrigin)
        .orderByDesc(SysProductConfig::getCreateTime)
        .list();
  }

  @Override
  public List<ProductConfigAssociationDTO> listProduct(PurchaseProductAssociationQuery query) {
    return sysProductConfigMapper.listProduct(query);
  }

  @Override
  public List<SysProductConfig> listSysOriginByProductId(String sysOrigin, String productId) {
    return query()
        .eq(SysProductConfig::getSysOrigin, sysOrigin)
        .eq(SysProductConfig::getProductId, productId)
        .list();
  }


}
*/
