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

import cn.hutool.core.collection.CollectionUtil;
import com.prolog.cs.basic.item.model.vo.ItemBarCodeVO;
import com.prolog.cs.basic.item.model.vo.ItemBaseInfoVO;
import com.prolog.cs.basic.item.model.vo.ItemSyncQueryByIdVO;
import com.prolog.eis.dto.item.ItemSyncVO;
import com.prolog.eis.feign.ItemFeign;
import com.prolog.eis.service.shelves.ItemService;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.framework.core.pojo.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
@Slf4j
public class ItemServiceImpl implements ItemService {

    @Autowired
    ItemFeign itemFeign;

    @Override
    public List<ItemSyncVO> queryItemMst(List<String> itemIds) throws Exception{
         ItemSyncQueryByIdVO itemSyncQueryByIdVO = new ItemSyncQueryByIdVO();
         itemSyncQueryByIdVO.setItemIds(itemIds);
         itemSyncQueryByIdVO.setPageNum(1);
         itemSyncQueryByIdVO.setPageSize(itemIds.size());
         RestMessage<Page<ItemSyncVO>> pageRestMessage = itemFeign.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();
    }

    @Override
    public ItemBarCodeVO queryItemByBarCode(String code) throws Exception {
        RestMessage<List<ItemBarCodeVO>> listRestMessage = itemFeign.queryItemByBarcode(code);
        if (!listRestMessage.isSuccess()){
            throw new Exception("查询商品服务报错"+listRestMessage.getMessage());
        }
        List<ItemBarCodeVO> data = listRestMessage.getData();
        if (CollectionUtil.isEmpty(data)) {
            throw new Exception(String.format("查询商品不存在"));
        }
        return data.get(0);
    }

    @Override
    public List<ItemBarCodeVO> queryItemListByBarCode(String code) throws Exception {
        RestMessage<List<ItemBarCodeVO>> listRestMessage = itemFeign.queryItemByBarcode(code);
        if (!listRestMessage.isSuccess()){
            throw new Exception("查询商品服务报错"+listRestMessage.getMessage());
        }
        List<ItemBarCodeVO> data = listRestMessage.getData();
        if (CollectionUtil.isEmpty(data)) {
            throw new Exception(String.format("查询商品不存在"));
        }
        return data;
    }

    @Override
    public List<ItemBarCodeVO> queryItemListAllByBarCode(String code) throws Exception {
        RestMessage<List<ItemBarCodeVO>> listRestMessage = itemFeign.queryItemByBarcode(code);
        if (!listRestMessage.isSuccess()){
            throw new Exception("查询商品服务报错"+listRestMessage.getMessage());
        }
        List<ItemBarCodeVO> data = listRestMessage.getData();
        return data;
    }

    @Override
    public List<ItemSyncVO> getItemByBarcode(String barCode) {
        final RestMessage<List<ItemSyncVO>> result = itemFeign.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 = itemFeign.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();
    }
}
