
package com.tmerclub.cloud.distribution.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.feign.SearchSpuFeignClient;
import com.tmerclub.cloud.api.platform.feign.ConfigFeignClient;
import com.tmerclub.cloud.api.platform.vo.DistributionConfigApiVO;
import com.tmerclub.cloud.api.platform.vo.DistributionConfigSpuVO;
import com.tmerclub.cloud.api.platform.vo.DistributionRecruitConfigApiVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.product.vo.search.SpuSearchVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.common.util.Json;
import com.tmerclub.cloud.distribution.service.DistributionConfigService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author cl
 * @date 2021-08-12 10:06:38
 */
@Service
public class DistributionConfigServiceImpl implements DistributionConfigService {

    @DubboReference
    private ConfigFeignClient configFeignClient;

    @DubboReference
    private SearchSpuFeignClient searchSpuFeignClient;


    @Override
    public DistributionConfigApiVO getDistributionConfig() {
        ServerResponseEntity<String> config = configFeignClient.getConfig(Constant.DISTRIBUTION_CONFIG);
        if (!config.isSuccess()) {
            throw new LuckException(config.getMsg());
        }
        String value = config.getData();
        if (StrUtil.isBlank(value)) {
            throw new LuckException("分销基本设置没有配置");
        }
        DistributionConfigApiVO distributionConfigVO = Json.parseObject(value, DistributionConfigApiVO.class);
        List<Long> spuIdList = distributionConfigVO.getSpuIdList();
        if (CollUtil.isEmpty(spuIdList)) {
            return distributionConfigVO;
        }
        ServerResponseEntity<List<SpuSearchVO>> spuResponse = searchSpuFeignClient.listSpuBySpuIds(spuIdList);
        if (spuResponse.isSuccess() && CollUtil.isNotEmpty(spuResponse.getData())) {
            distributionConfigVO.setSpuList(BeanUtil.mapAsList(spuResponse.getData(), DistributionConfigSpuVO.class));
        }
        return distributionConfigVO;
    }

    @Override
    public DistributionRecruitConfigApiVO getDistributionRecruitConfig() {
        ServerResponseEntity<String> config = configFeignClient.getConfig(Constant.DISTRIBUTION_RECRUIT_CONFIG);
        if (!config.isSuccess()) {
            throw new LuckException(config.getMsg());
        }
        String value = config.getData();
        if (StrUtil.isBlank(value)) {
            throw new LuckException("分销推广设置没有配置");
        }
        return Json.parseObject(value, DistributionRecruitConfigApiVO.class);
    }
}
