package com.prolog.rdc.tms.app.proxy;

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

import cn.hutool.core.collection.CollectionUtil;
import com.prolog.rdc.tms.model.dto.basicData.ProviderShipperCheckDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseBranchDTO;
import com.prolog.rdc.tms.model.dto.basicData.TmsBaseCustomerReceiverNewDTO;
import com.prolog.rdc.tms.model.dto.fleetManagement.ContainerDTO;
import com.prolog.rdc.tms.model.vo.basicData.ProviderShipperCheckVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

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.product.core.framework.entity.UserEntity;
import com.prolog.rdc.tms.model.dto.basicConfigure.ShipbillMatchCarrierDTO;
import com.prolog.rdc.tms.model.dto.basicData.ItemCheckDTO;
import com.prolog.rdc.tms.model.dto.message.SendMessageDTO;
import com.prolog.rdc.tms.model.entity.SysParamConfigModel;
import com.prolog.rdc.tms.model.vo.basicConfigure.ShipbillMatchCarrierVO;
import com.prolog.rdc.tms.model.vo.basicData.ItemCheckVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;

import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @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();
	}

	@Async("messageExecutor")
	public void sendMessageList(List<SendMessageDTO> sendMessageDTOList, UserEntity user) {
		log.info("topic={},title={},request={}", TOPIC, "sendMessageList", JSON.toJSONString(sendMessageDTOList));
		sendMessageDTOList.forEach(dto -> {
			try {
				dto.setUser(user);
				sendMessage(dto);
			} catch (Exception e) {
				log.error("topic={},title={},request={}", TOPIC, "sendMessageList", JSON.toJSONString(dto), e);
			}
		});
	}

	private void sendMessage(SendMessageDTO dto) {
		RestMessage<Object> restMessage = null;
		try {
			restMessage = maindataFeign.sendMessage(dto);
		} catch (Exception e) {
			log.error("topic={},title={}", TOPIC, "sendMessage", e);
			throw new BizException("发送消息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "sendMessage", JSON.toJSONString(dto), JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("发送消息失败:" + restMessage.getMessage());
		}
	}

	public Map<String, ShipbillMatchCarrierDTO> shipbillMatchCarrierExecute(List<ShipbillMatchCarrierVO> matchCarrierVOs) {
		RestMessage<Map<String, ShipbillMatchCarrierDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.shipbillMatchCarrierExecute(matchCarrierVOs);
		} catch (Exception e) {
			throw new BizException("承运商策略匹配异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "shipbillMatchCarrierExecute", JSON.toJSONString(matchCarrierVOs), JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("承运商策略匹配失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("承运商策略匹配为空");
		}
		return restMessage.getData();
	}

	public TmsBaseBranchDTO viewBranch(Long branchId) {
		RestMessage<TmsBaseBranchDTO> restMessage = null;
		try {
			restMessage = maindataFeign.viewBranch(branchId);
		} catch (Exception e) {
			throw new BizException("获取仓库信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "viewBranch", branchId, JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取仓库信息失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取仓库信息为空");
		}
		return restMessage.getData();
	}

	public TmsBaseCustomerReceiverNewDTO viewCustomerReceiver(Long id) {
		RestMessage<TmsBaseCustomerReceiverNewDTO> restMessage = null;
		try {
			restMessage = maindataFeign.viewCustomerReceiver(id);
		} catch (Exception e) {
			throw new BizException("获取收货方信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "viewCustomerReceiver", id, JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取收货方信息失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取收货方信息为空");
		}
		return restMessage.getData();
	}

	public ProviderShipperCheckDTO getShipperExecute(Long shipperId) {
		RestMessage<ProviderShipperCheckDTO> restMessage = null;
		ProviderShipperCheckVO vo = new ProviderShipperCheckVO();
		vo.setShipperId(shipperId);
		try {
			restMessage = maindataFeign.getShipperExecute(vo);
		} catch (Exception e) {
			throw new BizException("获取发货方信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "getShipperExecute", vo, JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取发货方信息失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取发货方信息为空");
		}
		return restMessage.getData();
	}

	public List<ContainerDTO> findContainerByIdList(List<Long> idList) {
		RestMessage<List<ContainerDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findContainerByIdList(idList);
		} catch (Exception e) {
			throw new BizException("获取容器信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findContainerByIdList", JSON.toJSONString(idList), JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取容器信息失败");
		}
		if (CollectionUtil.isEmpty(restMessage.getData())) {
			throw new BizException("获取容器信息为空");
		}
		return restMessage.getData();
	}
}
