package com.prolog.eis.service.feign.impl;

import com.prolog.cs.basic.item.model.vo.ItemBaseInfoVO;
import com.prolog.cs.basic.item.model.vo.ItemMstQueryVO;
import com.prolog.cs.basic.item.model.vo.ItemSyncQueryByIdVO;
import com.prolog.eis.dto.item.ItemSyncVO;
import com.prolog.eis.feign.ItemFeignService;
import com.prolog.eis.service.feign.FeignGoodsService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * @author dengj
 * @version 1.0
 * @date 2023/2/17 15:34
 */

@Service
@RequiredArgsConstructor
@Slf4j
public class FeignGoodsServiceImpl implements FeignGoodsService {

    private final ItemFeignService itemFeignService;


    @Override
    public ItemSyncVO getItemInfo(String itemId) {
        ItemSyncQueryByIdVO itemSyncQueryByIdVO = new ItemSyncQueryByIdVO();
        itemSyncQueryByIdVO.setItemIds(Arrays.asList(itemId));
        itemSyncQueryByIdVO.setPageSize(1);
        RestMessage<Page<ItemSyncVO>> result = itemFeignService.getItemByVo(itemSyncQueryByIdVO);
        if (!result.isSuccess()) {
            log.error("itemFeignService -> getItemByVo 根据【{}】查询商品信息失败:" + result.getMessage(), itemId);
            throw new PrologException(String.format("itemFeignService -> getItemByVo 根据【%s】查询商品信息失败:" + result.getMessage(), itemId));
        }
        if (result.getData().getList().isEmpty()) {
            log.error("itemFeignService -> getItemByVo 未查到商品【{}】信息:" + result.getMessage(), itemId);
            throw new PrologException(String.format("itemFeignService -> getItemByVo 未查到商品【%s】信息:" + result.getMessage(), itemId));
        }
        return result.getData().getList().get(0);

    }

    @Override
    public List<ItemSyncVO> getItemByBarcode(String barCode) {
        final RestMessage<List<ItemSyncVO>> result = itemFeignService.getItemByBarcode(barCode);
        if (!result.isSuccess()) {
            log.error("itemFeignService -> getItemByVo 根据【{}】查询商品信息失败:" + result.getMessage(), barCode);
            throw new PrologException(String.format("itemFeignService -> getItemByVo 根据【%s】查询商品信息失败:" + result.getMessage(), barCode));
        }
        if (Objects.isNull(result.getData())) {
            log.error("itemFeignService -> getItemByVo 未查到商品【{}】信息:" + result.getMessage(), barCode);
            throw new PrologException(String.format("itemFeignService -> getItemByVo 未查到商品【%s】信息:" + result.getMessage(), barCode));
        }
        return result.getData();
    }

    @Override
    public List<ItemBaseInfoVO> getItemByName(String itemName) {
        final RestMessage<List<ItemBaseInfoVO>> result = itemFeignService.getItemByName(itemName);
        if (!result.isSuccess()) {
            log.error("itemFeignService -> getItemByVo 根据【{}】查询商品信息失败:" + result.getMessage(), itemName);
            throw new PrologException(String.format("itemFeignService -> getItemByVo 根据【%s】查询商品信息失败:" + result.getMessage(), itemName));
        }
        if (Objects.isNull(result.getData())) {
            log.error("itemFeignService -> getItemByVo 未查到商品【{}】信息:" + result.getMessage(), itemName);
            throw new PrologException(String.format("itemFeignService -> getItemByVo 未查到商品【%s】信息:" + result.getMessage(), itemName));
        }
        return result.getData();
    }

    @Override
    public List<ItemSyncVO> queryItemMst(List<String> itemIds) throws Exception {
        ItemSyncQueryByIdVO itemSyncQueryByIdVO = new ItemSyncQueryByIdVO();
        itemSyncQueryByIdVO.setItemIds(itemIds);
        itemSyncQueryByIdVO.setPageNum(0);
        itemSyncQueryByIdVO.setPageSize(itemIds.size());
        RestMessage<Page<ItemSyncVO>> pageRestMessage = itemFeignService.queryItemMst(itemSyncQueryByIdVO);
        if (!pageRestMessage.isSuccess()) {
            throw new Exception("查询商品服务报错<queryItemMst>" + pageRestMessage.getMessage());
        }
        final Page<ItemSyncVO> data = pageRestMessage.getData();
        if (null == data) {
            throw new Exception(String.format("%s查询商品不存在"));
        }
        return data.getList();
    }
}