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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.combo.vo.ComboSkuVo;
import com.tmerclub.cloud.api.product.feign.SkuFeignClient;
import com.tmerclub.cloud.api.product.feign.SpuFeignClient;
import com.tmerclub.cloud.combo.dto.ComboSpuSkuDTO;
import com.tmerclub.cloud.combo.mapper.ComboSpuSkuMapper;
import com.tmerclub.cloud.combo.model.ComboSpuSku;
import com.tmerclub.cloud.combo.service.ComboSpuSkuService;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.util.PageUtil;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.product.vo.ComboSpuSkuVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.app.SkuAppVO;
import com.tmerclub.cloud.common.util.BeanUtil;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 套装商品sku项
 *
 * @author tmerclub
 * @date 2022-08-16 09:18:06
 */
@Service
public class ComboSpuSkuServiceImpl implements ComboSpuSkuService {

    @Autowired
    private ComboSpuSkuMapper comboSpuSkuMapper;
    @DubboReference
    private SkuFeignClient skuFeignClient;
    @DubboReference
    private SpuFeignClient spuFeignClient;

    @Override
    public PageVO<ComboSpuSkuVO> page(PageDTO pageDTO) {
        return PageUtil.doPage(pageDTO, () -> comboSpuSkuMapper.list());
    }

    @Override
    public void save(List<ComboSpuSkuVO> comboSpuSkuList, Long comboSpuId) {
        List<ComboSpuSku> comboSpuSkus = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (ComboSpuSkuVO comboSpuSkuVO : comboSpuSkuList) {
            ComboSpuSku spuSku = BeanUtil.map(comboSpuSkuVO, ComboSpuSku.class);
            comboSpuSkus.add(spuSku);
        }
        comboSpuSkuMapper.save(comboSpuSkus, comboSpuId);
    }

    @Override
    public void update(ComboSpuSku comboSpuSku) {
        comboSpuSkuMapper.update(comboSpuSku);
    }

    @Override
    public void deleteById(Long comboSpuId) {
        comboSpuSkuMapper.deleteById(comboSpuId);
    }

    @Override
    public void insertBatch(List<ComboSpuSkuVO> skuList, Long comboSpuId) {
        if (CollUtil.isEmpty(skuList) || Objects.isNull(comboSpuId)) {
            return;
        }
        List<ComboSpuSku> comboSpuSkuList = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (ComboSpuSkuVO comboSpuSkuVO : skuList) {
            ComboSpuSku comboSpuSku = BeanUtil.map(comboSpuSkuVO, ComboSpuSku.class);
            comboSpuSkuList.add(comboSpuSku);
        }
        comboSpuSkuMapper.insertBatch(comboSpuSkuList, comboSpuId);
    }

    @Override
    public void batchUpdate(List<ComboSpuSkuVO> comboSpuSkuList) {
        List<ComboSpuSku> comboSpuSkus = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (ComboSpuSkuVO comboSpuSkuVO : comboSpuSkuList) {
            ComboSpuSku comboSpuSku = BeanUtil.map(comboSpuSkuVO, ComboSpuSku.class);
            comboSpuSkus.add(comboSpuSku);
        }
        comboSpuSkuMapper.batchUpdate(comboSpuSkus);
    }

    @Override
    public List<ComboSkuVo> comboVoListComboAndSkuList(Set<Long> comboIdList, List<Long> skuIdList) {
        return comboSpuSkuMapper.comboVoListComboAndSkuList(comboIdList, skuIdList);
    }

    @Override
    public List<ComboSpuSkuVO> comboVoListByComboSpuId(Long comboSpuId) {
        return comboSpuSkuMapper.comboVoListByComboSpuId(comboSpuId);
    }

    @Override
    public void deleteBatch(List<Long> comboSpuIdList) {
        comboSpuSkuMapper.deleteBatch(comboSpuIdList);
    }

    @Override
    public List<ComboSpuSkuVO> getByComboSpuId(Long comboSpuId) {
        return comboSpuSkuMapper.getByComboSpuId(comboSpuId);
    }

    @Override
    public List<ComboSpuSkuVO> comboSpuSkuList(List<Long> comboSpuIdList) {
        return comboSpuSkuMapper.comboSpuSkuList(comboSpuIdList);
    }

    @Override
    public List<ComboSpuSkuVO> comboSpuSkuListByComboIdAndSpuId(ComboSpuSkuDTO comboSpuSkuDTO) {
        List<ComboSpuSkuVO> comboSpuSkuList = comboSpuSkuMapper.comboSpuSkuListByComboIdAndSpuId(comboSpuSkuDTO.getComboId(), comboSpuSkuDTO.getSpuId());
        List<SkuAppVO> skuAppVOList = skuFeignClient.listBySpuId(comboSpuSkuDTO.getSpuId()).getData();
        Map<Long, SkuAppVO> skuAppMap = skuAppVOList.stream().collect(Collectors.toMap(SkuAppVO::getSkuId, e -> e));
        SpuVO spuVO = spuFeignClient.getById(comboSpuSkuDTO.getSpuId()).getData();
        for (ComboSpuSkuVO comboSpuSkuVO : comboSpuSkuList) {
            SkuAppVO skuAppVO = skuAppMap.get(comboSpuSkuVO.getSkuId());
            if (Objects.isNull(skuAppVO)) {
                continue;
            }
            if (Objects.isNull(skuAppVO.getMainImgUrl())) {
                skuAppVO.setMainImgUrl(spuVO.getMainImgUrl());
            }
            comboSpuSkuVO.setPic(skuAppVO.getMainImgUrl());
            if (Objects.isNull(skuAppVO.getSkuName())) {
                skuAppVO.setSkuName("");
            }
            comboSpuSkuVO.setSkuName(skuAppVO.getSkuName());
            comboSpuSkuVO.setPriceFee(skuAppVO.getPriceFee());
            comboSpuSkuVO.setProperties(skuAppVO.getProperties());
            comboSpuSkuVO.setStocks(skuAppVO.getStock());
        }
        return comboSpuSkuList;
    }
}
