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

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.product.service.SkuStockService;
import com.tmerclub.cloud.product.service.SpuStockService;
import com.tmerclub.cloud.product.task.ProductTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 库存信息
 *
 * @author tmerclub
 * @date 2020-10-28 15:27:24
 */
@Service
public class SpuStockServiceImpl implements SpuStockService {
    private static final Logger log = LoggerFactory.getLogger(ProductTask.class);

    @Autowired
    private SkuStockService skuStockService;

    @Override
    public void setSpuStock(List<SpuVO> list) {
        if (CollUtil.isEmpty(list)) {
            return ;
        }
        List<Long> spuIds = list.stream().map(SpuVO::getSpuId).collect(Collectors.toList());
        log.info("SpuStockServiceImpl.setSpuStock(): spuIds: {}", spuIds);
        Map<Long, Integer> spuStockMap = skuStockService.spuStockMapBySpuIds(spuIds);
        for (SpuVO spuVO : list) {
            spuVO.setTotalStock(spuStockMap.getOrDefault(spuVO.getSpuId(), Constant.ZERO_INTEGER));
        }
    }
}
