package com.prolog.rdc.tms.proxy;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.BizException;
import com.prolog.rdc.tms.model.dto.SySParameterDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCheckDTO;
import com.prolog.rdc.tms.model.entity.SysParamConfigModel;
import com.prolog.rdc.tms.model.vo.basicData.ItemCheckVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author hongbo.pan
 * @date 2022/10/18
 */
@Slf4j
@Component
public class MainDataProxy {
	private static final String TOPIC = "MainDataProxy";

	@Autowired
	private MaindataFeign maindataFeign;

	public String getCodeNumberByCode(String code) {
		RestMessage<String> restMessage = null;
		try {
			restMessage = maindataFeign.getCodeNumberByCode(code, "L");
		} catch (Exception e) {
			throw new BizException("获取单号异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "getCodeNumberByCode", code, JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取单号失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取单号为空");
		}
		return restMessage.getData();
	}

	public Map<Long, ItemCheckDTO> getItemByIdList(List<Long> itemIdList) {
		Map<Long, ItemCheckDTO> map = Maps.newHashMap();
		itemIdList.forEach(itemId -> map.put(itemId, getItemById(itemId)));
		return map;
	}

	public ItemCheckDTO getItemById(Long itemId) {
		RestMessage<ItemCheckDTO> restMessage = null;
		ItemCheckVO itemCheckVO = new ItemCheckVO();
		itemCheckVO.setItemId(itemId);
		try {
			restMessage = maindataFeign.getItem(itemCheckVO);
		} catch (Exception e) {
			throw new BizException("获取商品异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "getItemById", JSON.toJSONString(itemCheckVO), JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取商品失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取商品为空");
		}
		return restMessage.getData();
	}

	public String getSystemParamByCode(String code) {
		RestMessage<SysParamConfigModel> restMessage = null;
		try {
			restMessage = maindataFeign.getValue(code);
		} catch (Exception e) {
			throw new BizException("获取系统参数异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "getSystemParamByCode", code, JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取系统参数失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取系统参数为空");
		}
		return restMessage.getData().getValue();
	}

	public SySParameterDTO queryParameter() {
		RestMessage<SySParameterDTO> restMessage = null;
		try {
			restMessage = maindataFeign.queryParameter();
		} catch (Exception e) {
			throw new BizException("获取全部系统参数异常");
		} finally {
			log.info("topic={},title={},response={}", TOPIC, "queryParameter", JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取全部系统参数为空");
		}
		if (!restMessage.isSuccess()) {
			throw new BizException("获取全部系统参数失败");
		}
		return restMessage.getData();
	}

}
