package com.tmerclub.cloud.product.feign;

import com.tmerclub.cloud.api.product.feign.StockPointFeignClient;
import com.tmerclub.cloud.api.product.vo.SkuStockVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.product.dto.StockPointSkuDTO;
import com.tmerclub.cloud.common.product.vo.StockPointSkuVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.product.manager.StockManager;
import com.tmerclub.cloud.product.service.StockPointSkuService;
import com.tmerclub.cloud.product.util.StockUtil;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;

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

/**
 * @author zp
 */
@DubboService
public class StockPointFeignController implements StockPointFeignClient {

    @Autowired
    private StockPointSkuService stockPointSkuService;

    @Autowired
    private StockManager stockManager;

    @Override
    public ServerResponseEntity<Integer> countStockList(Long stockPointId) {
        return ServerResponseEntity.success(stockPointSkuService.listByPointId(stockPointId).size());
    }

    @Override
    public List<StockPointSkuVO> listStockPointSku(StockPointSkuDTO stockPointSkuDTO) {
        return stockPointSkuService.listStockPointSku(stockPointSkuDTO);
    }

    @Override
    public void updateStockPointSkuRelate(List<Long> stockPointIds, List<Long> skuIds) {
        // 查询关联数据
        StockPointSkuDTO stockPointSkuDTO = new StockPointSkuDTO();
        stockPointSkuDTO.setStockPointIds(stockPointIds);
        stockPointSkuDTO.setSkuIds(skuIds);
        List<StockPointSkuVO> stockPointSkus = stockPointSkuService.listStockPointSku(stockPointSkuDTO);
        if (CollectionUtils.isEmpty(stockPointSkus)) {
            return;
        }
        // 查询库存
        List<SkuStockVO> skuStocks = new ArrayList<>(stockPointSkus.size());
        for (StockPointSkuVO stockPointSkuVO : stockPointSkus) {
            skuStocks.add(new SkuStockVO(stockPointSkuVO.getSkuId(), stockPointSkuVO.getStockPointId()));
        }
        Map<String, SkuStockVO> stockMap = stockManager.mapSkuStockBySkuStocks(skuStocks);
        // 非默认仓库，且库存不存在 or 库存为0，删除关联数据
        List<Long> deleteData = new ArrayList<>(Constant.INITIAL_CAPACITY);
        for (StockPointSkuVO stockPointSkuVO : stockPointSkus) {
            if (Objects.equals(stockPointSkuVO.getType(), 0)) {
                continue;
            }
            SkuStockVO skuStockVO = stockMap.get(StockUtil.skuPointKey(stockPointSkuVO.getSkuId(), stockPointSkuVO.getStockPointId()));
            if (Objects.isNull(skuStockVO)
                    || Objects.equals(skuStockVO.getStock(), 0)) {
                deleteData.add(stockPointSkuVO.getStockPointSkuId());
            }
        }
        if (!CollectionUtils.isEmpty(deleteData)) {
            stockPointSkuService.deleteByIds(deleteData);
        }
    }

}
