package com.prolog.cs.biz.outbound.utils;

import com.prolog.cs.basic.item.model.vo.ItemMstQueryVO;
import com.prolog.cs.basic.item.model.vo.ItemMstVO2;
import com.prolog.cs.biz.outbound.core.SpringUtils;
import com.prolog.cs.biz.outbound.resource.ItemClient;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import com.prolog.framework.utils.AssertUtils;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ItemUtils {
    private Map<String, ItemMstVO2> itemIdMap;

    //拼装商品明细字段值
    static public Map<String, ItemMstVO2> getItemsInfo(Set<String> itemIds) {

        List<ItemMstVO2> itemList = getItemMstVO2s(itemIds);
        Map<String, ItemMstVO2> itemMap = new ConcurrentHashMap<>();
        for (ItemMstVO2 itemMstVO2 : itemList) {
            String itemId = itemMstVO2.getItemId();
            itemMap.putIfAbsent(itemId, itemMstVO2);
        }
         /*
        return itemList.stream().
                collect(Collectors.toMap(ItemMstVO2::getItemId, Function.identity()));
          */
        return itemMap;
    }

    private static List<ItemMstVO2> getItemMstVO2s(Set<String> itemIds) {
        AssertUtils.isNotEmpty(itemIds);
        ItemClient itemClient = getItemClient();
        ItemMstQueryVO vo = new ItemMstQueryVO();
        vo.setItemIds(itemIds);

        RestMessage<Page<ItemMstVO2>> restMessage = itemClient.pagebyRelated(vo);
        if (!restMessage.isSuccess()) {
            throw new PrologException("调用查询商品接口出错！" + restMessage.getMessage());
        }
        AssertUtils.isNotEmpty(restMessage.getData().getList());
        return restMessage.getData().getList();
    }

    //拼装商品明细字段值
    static public ItemMstVO2 getItemsInfo(String itemId,String enterpriseId) {
        HashSet<String> hashSet = new HashSet(Arrays.asList(itemId));
        ItemClient itemClient = getItemClient();
        ItemMstQueryVO vo = new ItemMstQueryVO();
        vo.setItemIds(hashSet);
        vo.setEnterpriseId(enterpriseId);
        RestMessage<Page<ItemMstVO2>> restMessage = itemClient.pagebyRelated(vo);
        if (!restMessage.isSuccess()) {
            throw new PrologException("调用查询商品接口出错！");
        }
        List<ItemMstVO2> itemList = restMessage.getData().getList();
        if (itemList.isEmpty()) {
            throw new PrologException("调用查询商品接口出错,找不到此商品信息");
        }
        return itemList.get(0);
    }

    private static ItemClient getItemClient() {
        return SpringUtils.getBeansByType(ItemClient.class);
    }

    public static ItemMstVO2 getItemInfo(String itemId) {
        ItemUtils itemUtils = new ItemUtils(itemId);
        return itemUtils.getByItemId(itemId);
    }

    public ItemUtils(Set<String> itemIds) {
        itemIdMap = getItemsInfo(itemIds);
    }

    public ItemUtils(String itemId) {
        HashSet<String> itemIds = new HashSet(Arrays.asList(itemId));
        itemIdMap = getItemsInfo(itemIds);
    }

    public ItemMstVO2 getByItemId(String itemId) {
        ItemMstVO2 itemMstVO2 = itemIdMap.get(itemId);
        if (itemMstVO2 == null) {
            throw new PrologException("找不到对应的商品信息");
        }
        return itemMstVO2;
    }

}
