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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.product.feign.SkuStockFeignClient;
import com.tmerclub.cloud.common.cache.constant.CacheNames;
import com.tmerclub.cloud.common.cache.constant.ProductCacheNames;
import com.tmerclub.cloud.common.cache.util.RedisUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.StatusEnum;
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.i18n.I18nMessage;
import com.tmerclub.cloud.common.product.constant.SpuMold;
import com.tmerclub.cloud.common.product.constant.SupplierSpuType;
import com.tmerclub.cloud.common.product.vo.GiveawaySpuVO;
import com.tmerclub.cloud.common.product.vo.GiveawayVO;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.product.dto.GiveawaySpuDTO;
import com.tmerclub.cloud.product.mapper.GiveawaySpuMapper;
import com.tmerclub.cloud.product.model.GiveawaySpu;
import com.tmerclub.cloud.product.service.GiveawaySpuService;
import com.tmerclub.cloud.product.service.SpuStockService;
import org.apache.commons.compress.utils.Lists;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 赠品套装商品项
 *
 * @author tmerclub
 * @date 2022-08-16 11:11:59
 */
@Service
public class GiveawaySpuServiceImpl implements GiveawaySpuService {

    @Autowired
    private GiveawaySpuMapper giveawaySpuMapper;

    @Autowired
    private SpuStockService spuStockService;
    @DubboReference
    private SkuStockFeignClient skuStockFeignClient;

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

    @Override
    public List<GiveawaySpuVO> getBySpuId(Long spuId) {
        return giveawaySpuMapper.getBySpuId(spuId);
    }

    @Override
    public PageVO<SpuVO> getMainSpuPage(PageDTO pageDTO, GiveawaySpuDTO giveawaySpuDTO) {
        PageVO<SpuVO> spuPage = PageUtil.doPage(pageDTO, () -> giveawaySpuMapper.getMainSpuPage(giveawaySpuDTO, I18nMessage.getLang()));
        spuStockService.setSpuStock(spuPage.getList());
        List<Long> comboSpuList = Lists.newArrayList();
        if (CollUtil.isNotEmpty(spuPage.getList())) {
            for (SpuVO spuVO : spuPage.getList()) {
                if(Objects.equals(spuVO.getSpuMold(), SpuMold.COMBO.value())) {
                    comboSpuList.add(spuVO.getSpuId());
                }
            }
        }
        // 如果是组合商品,需要重新计算一下库存
        if (CollUtil.isNotEmpty(comboSpuList)) {
            ServerResponseEntity<Map<Long, Integer>> spuIdStockMapResp = skuStockFeignClient.calculateStockByComboSpuList(comboSpuList);
            Map<Long, Integer> spuStockMap = spuIdStockMapResp.getData();
            for (SpuVO spuVO : spuPage.getList()) {
                if(!spuStockMap.containsKey(spuVO.getSpuId()) && !spuStockMap.containsKey(spuVO.getSupplierSpuId())){
                    continue;
                }
                spuVO.setTotalStock(spuStockMap.get(spuVO.getSpuId()));
                if (Objects.equals(spuVO.getSupplierSpuType(), SupplierSpuType.CONSIGNMENT.value()) && Objects.nonNull(spuVO.getSupplierSpuId())) {
                    spuVO.setTotalStock(spuStockMap.get(spuVO.getSupplierSpuId()));
                }
            }
        }
        return spuPage;
    }

    @Override
    public List<GiveawaySpuVO> getByGiveawayId(Long giveawayId) {
        return giveawaySpuMapper.getByGiveawayId(giveawayId, I18nMessage.getLang(), StatusEnum.DELETE.value());
    }

    @Override
    @Cacheable(cacheNames = ProductCacheNames.GIVEAWAY_BY_SPU_ID, key = "#spuId")
    public GiveawayVO getGiveawayBySpuId(Long spuId) {
        return giveawaySpuMapper.getGiveawayBySpuId(spuId);
    }

    @Override
    public void save(GiveawaySpu giveawaySpu) {
        giveawaySpuMapper.save(giveawaySpu);
    }

    @Override
    public boolean updateStatus(Long giveawayId, Integer status) {
        return giveawaySpuMapper.updateStatus(giveawayId, status);
    }

    @Override
    public void deleteById(Long giveawaySpuId) {
        giveawaySpuMapper.deleteById(giveawaySpuId);
    }

    @Override
    public void insertBatch(List<GiveawaySpuDTO> giveawaySpuList) {
        if (CollUtil.isEmpty(giveawaySpuList)) {
            return;
        }
        giveawaySpuList.forEach(s -> s.setStatus(1));
        giveawaySpuMapper.insertBatch(giveawaySpuList);
    }

    @Override
    public void batchDeleteByIds(List<Long> giveawaySpuIds) {
        if (CollUtil.isEmpty(giveawaySpuIds)) {
            return;
        }
        giveawaySpuMapper.batchDeleteByIds(giveawaySpuIds);
    }

    @Override
    public void changeGiveawayActivityStatusByGiveawayIdList(List<Long> giveawayIdList) {
        giveawaySpuMapper.changeGiveawayActivityStatusByGiveawayIdList(giveawayIdList);
    }


    @Override
    @Caching(evict = {
        @CacheEvict(cacheNames = ProductCacheNames.GIVEAWAY_BY_SPU_ID, key = "#spuId"),
            // spu中有赠品信息，要一起清除缓存
        @CacheEvict(cacheNames = ProductCacheNames.SPU_KEY, key = "#spuId")
    })
    public void removeGiveawayCacheBySupId(Long spuId) {
    }

    @Override
    public void removeGiveawayCacheBatch(List<Long> spuIds) {
        if (CollUtil.isEmpty(spuIds)) {
            return;
        }
        List<String> key = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (Long spuId : spuIds) {
            //removeGiveawayCacheBySupId(spuId);
            key.add(ProductCacheNames.GIVEAWAY_BY_SPU_ID + CacheNames.UNION + spuId);
            // spu中有赠品信息，要一起清除缓存
            key.add(ProductCacheNames.SPU_KEY + CacheNames.UNION + spuId);
        }
        RedisUtil.del(key);
    }
}
