package com.sckw.product.service;

import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.product.model.KwpGoods;
import com.sckw.product.model.KwpGoodsPriceRange;
import com.sckw.product.model.vo.res.WorkbenchGoodsRes;
import com.sckw.system.api.RemoteSystemService;
import com.sckw.system.api.model.dto.res.EntCacheResDto;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

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

/**
 * @desc: 工作台商品统计相关接口
 * @author: yzc
 * @date: 2023-09-13 17:57
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class KwpGoodsStatisticsService {

    private final KwpGoodsService service;
    private final KwpGoodsPriceRangeService priceRangeService;

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    /**
     * @desc: 获取工作台商品列表
     * @author: yzc
     * @date: 2023-09-13 18:55
     * @return: java.util.List<com.sckw.product.model.vo.res.WorkbenchGoodsRes>
     */
    public List<WorkbenchGoodsRes> workbenchList() {
        List<KwpGoods> goodsList = service.getWorkbenchList();
        if (CollectionUtils.isEmpty(goodsList)) {
            return Collections.emptyList();
        }
        List<WorkbenchGoodsRes> result = new ArrayList<>();
        List<Long> goodsIds = goodsList.stream().map(KwpGoods::getId).toList();
        List<Long> supplyEntIds = goodsList.stream().map(KwpGoods::getSupplyEntId).toList();
        Map<Long, List<KwpGoodsPriceRange>> priceRangeMap = priceRangeService.getByGoodsIds(goodsIds).stream()
                .collect(Collectors.groupingBy(KwpGoodsPriceRange::getGoodsId));
        //供应企业信息
        List<EntCacheResDto> entList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(supplyEntIds)) {
            entList = remoteSystemService.queryEntCacheByIds(supplyEntIds);
        }
        Map<String, Map<String, String>> dict = remoteSystemService.queryDictByType(List.of(DictTypeEnum.PRODUCT_NAME_TYPE.getType(),
                DictTypeEnum.UNIT_TYPE.getType()));
        Map<String, String> productNameMap, unitMap;
        if (CollectionUtils.isEmpty(dict)) {
            productNameMap = new HashMap<>();
            unitMap = new HashMap<>();
        } else {
            productNameMap = dict.get(DictTypeEnum.PRODUCT_NAME_TYPE.getType());
            unitMap = dict.get(DictTypeEnum.UNIT_TYPE.getType());
        }
        Map<Long, String> entMap = entList.stream().collect(Collectors.toMap(EntCacheResDto::getId, EntCacheResDto::getFirmName, (k1, k2) -> k1));
        goodsList.forEach(e -> {
            WorkbenchGoodsRes goods = BeanUtils.copyProperties(e, WorkbenchGoodsRes.class);
            goods.setGoodsTypeLabel(CollectionUtils.isNotEmpty(productNameMap) ? productNameMap.get(e.getGoodsType()) : null)
                    .setUnitLabel(CollectionUtils.isNotEmpty(unitMap) ? unitMap.get(e.getUnit()) : null)
                    .setSupplyFireName(entMap.get(e.getSupplyEntId()));
            List<KwpGoodsPriceRange> priceRanges = priceRangeMap.get(e.getId());
            if (CollectionUtils.isNotEmpty(priceRanges)) {
                goods.setLowestPrice(priceRanges.get(0).getPrice());
            }
            result.add(goods);
        });
        return result;
    }
}
