package com.prolog.rdc.tms.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.prolog.common.util.ToolKits;
import com.prolog.framework.common.message.RestMessage;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.rdc.tms.constant.InterfaceConstant;
import com.prolog.rdc.tms.dao.mapper.CargoOwnerHtyMapper;
import com.prolog.rdc.tms.dao.mapper.CargoOwnerMapper;
import com.prolog.rdc.tms.dao.mapper.ErrorLogMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.CargoOwnerHtyModel;
import com.prolog.rdc.tms.model.entity.CargoOwnerModel;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.vo.IntfCargoOwnerSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.CargoOwnerService;

import cn.hutool.core.date.DateUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 
 *
 * 货主资料接口实现层
 *
 * @className :CargoOwnerServiceImpl
 * @author :fanzw
 * @creationTime :2022/11/28
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/11/28
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class CargoOwnerServiceImpl implements CargoOwnerService {

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private CargoOwnerMapper cargoOwnerMapper;

	@Autowired
	private CargoOwnerHtyMapper cargoOwnerHtyMapper;

	@Autowired
	private ErrorLogMapper errorLogMapper;

	/**
	 * 货主资料接口保存
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveCargoOwnerData(IntfCargoOwnerSaveVO intfCargoOwnerSaveVO) {
		ToolKits.isParamNull(intfCargoOwnerSaveVO, "请求参数不为空");

		Date date = DateUtil.date();

		// 校验非空项
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getCargoOwnerCode(), "货主编码不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getCargoOwnerName(), "货主名称不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getStatus(), "货主【" + intfCargoOwnerSaveVO.getCargoOwnerName() + "】状态不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getDepartmentId(), "货主【" + intfCargoOwnerSaveVO.getCargoOwnerName() + "】部门ID不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getDepartmentCode(), "货主【" + intfCargoOwnerSaveVO.getCargoOwnerName() + "】部门编码不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getDepartmentName(), "货主【" + intfCargoOwnerSaveVO.getCargoOwnerName() + "】部门名称不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getEnterpriseId(), "货主【" + intfCargoOwnerSaveVO.getCargoOwnerName() + "】企业ID不为空");
		ToolKits.isParamNull(intfCargoOwnerSaveVO.getEnterpriseName(), "货主【" + intfCargoOwnerSaveVO.getCargoOwnerName() + "】企业名称不为空");

		// 保存
		CargoOwnerModel cargoOwnerModel = new CargoOwnerModel();
		BeanUtils.copyProperties(intfCargoOwnerSaveVO, cargoOwnerModel);
		cargoOwnerModel.setCargoOwnerId(ToolKits.getUUID());
		cargoOwnerModel.setCreatetime(date);
		cargoOwnerModel.setUpdatetime(date);

		// 转业务请求集合
		List<IntfCargoOwnerSaveVO> cargoOwnerSaveVOs = Lists.newArrayList();
		IntfCargoOwnerSaveVO cargoOwnerSaveVO = new IntfCargoOwnerSaveVO();
		BeanUtils.copyProperties(cargoOwnerModel, cargoOwnerSaveVO);
		cargoOwnerSaveVOs.add(cargoOwnerSaveVO);

		log.info("货主资料接口转业务请求报文:{}", JSONObject.toJSONString(cargoOwnerSaveVOs));

		// 转业务处理
		RestMessage<List<IntfErrorLogDTO>> restMessage = maindataFeign.saveCargoOwnerInterfaceExecute(cargoOwnerSaveVOs);
		if (restMessage.isSuccess()) {
			List<IntfErrorLogDTO> intfErrorLogs = restMessage.getData();
			Assert.isTrue(ToolKits.isNotEmpty(intfErrorLogs), "处理结果异常：响应为空");

			IntfErrorLogDTO intfErrorLog = intfErrorLogs.get(0);
			if (intfErrorLog.isStatus()) {
				// 转历史
				CargoOwnerHtyModel cargoOwnerHtyModel = new CargoOwnerHtyModel();
				BeanUtils.copyProperties(cargoOwnerModel, cargoOwnerHtyModel);
				cargoOwnerHtyModel.setCargoOwnerId(ToolKits.getUUID());
				cargoOwnerHtyModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_Y);
				cargoOwnerHtyModel.setUpdatetime(DateUtil.date());
				cargoOwnerHtyMapper.save(cargoOwnerHtyModel);

				return null;
			} else {
				// 状态修改为E
				cargoOwnerModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_E);
				cargoOwnerModel.setUpdatetime(DateUtil.date());
				cargoOwnerMapper.save(cargoOwnerModel);

				ErrorLogModel errorLogModel = new ErrorLogModel();
				errorLogModel.setId(ToolKits.getUUID());
				errorLogModel.setIntfName("货主资料接口");
				errorLogModel.setErrorInfo(intfErrorLog.getErrorInfo());
				errorLogModel.setTableName("intf_base_cargo_owner");
				errorLogModel.setDataId(intfErrorLog.getDataId());
				errorLogModel.setCreatetime(DateUtil.date());
				errorLogMapper.save(errorLogModel);

				return intfErrorLog.getErrorInfo();
			}
		} else {
			throw new PrologException(restMessage.getMessage());
		}
	}

}
