package com.prolog.rdc.tms.proxy;

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

import com.prolog.rdc.tms.model.dto.intelligent.BranchDTO;
import com.prolog.rdc.tms.model.dto.intelligent.ConsigneeDTO;
import com.prolog.rdc.tms.model.dto.intelligent.CustomerDTO;
import com.prolog.rdc.tms.model.dto.intelligent.VehicleDTO;
import com.prolog.rdc.tms.model.dto.intelligent.VehicleTypeDTO;
import com.prolog.rdc.tms.model.entity.SysParamConfigModel;
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.basicData.ItemCheckDTO;
import com.prolog.rdc.tms.model.dto.message.SendMessageDTO;
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();
	}

	@Async("messageAndImportExecutor")
	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 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 List<VehicleDTO> findVehicleByPlateNumberList(List<String> plateNumberList) {
		RestMessage<List<VehicleDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findVehicleByPlateNumberList(plateNumberList);
		} catch (Exception e) {
			throw new BizException("获取车辆信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findVehicleByPlateNumberList", plateNumberList, 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<VehicleDTO> findVehicleByIdList(List<Long> idList) {
		RestMessage<List<VehicleDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findVehicleByIdList(idList);
		} catch (Exception e) {
			throw new BizException("获取车辆信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findVehicleByIdList", idList, 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<VehicleTypeDTO> findVehicleTypeByIdList(List<Long> idList) {
		RestMessage<List<VehicleTypeDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findVehicleTypeByIdList(idList);
		} catch (Exception e) {
			throw new BizException("获取车型信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findVehicleTypeByIdList", idList, 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<BranchDTO> findBranchByIdList(List<Long> idList) {
		RestMessage<List<BranchDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findBranchByIdList(idList);
		} catch (Exception e) {
			throw new BizException("获取仓库信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findBranchByIdList", idList, 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<CustomerDTO> findCustomerByIdList(List<Long> idList) {
		RestMessage<List<CustomerDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findCustomerByIdList(idList);
		} catch (Exception e) {
			throw new BizException("获取客户信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findCustomerByIdList", idList, 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<ConsigneeDTO> findConsigneeByIdList(List<Long> idList) {
		RestMessage<List<ConsigneeDTO>> restMessage = null;
		try {
			restMessage = maindataFeign.findConsigneeByIdList(idList);
		} catch (Exception e) {
			throw new BizException("获取收货方信息异常");
		} finally {
			log.info("topic={},title={},request={},response={}", TOPIC, "findConsigneeByIdList", idList, JSON.toJSONString(restMessage));
		}
		if (ObjectUtil.isNull(restMessage) || !restMessage.isSuccess()) {
			throw new BizException("获取收货方信息失败");
		}
		if (ObjectUtil.isNull(restMessage.getData())) {
			throw new BizException("获取收货方信息为空");
		}
		return restMessage.getData();
	}
}
