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.CustomerHtyMapper;
import com.prolog.rdc.tms.dao.mapper.CustomerMapper;
import com.prolog.rdc.tms.dao.mapper.ErrorLogMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.CustomerHtyModel;
import com.prolog.rdc.tms.model.entity.CustomerModel;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.vo.IntfCustomerSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.CustomerService;

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

/**
 * 
 *
 * 客户资料接口实现层
 *
 * @className :CustomerServiceImpl
 * @author :fanzw
 * @creationTime :2022/11/07
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/11/07
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private CustomerMapper customerMapper;

	@Autowired
	private CustomerHtyMapper customerHtyMapper;

	@Autowired
	private ErrorLogMapper errorLogMapper;

	/**
	 * 客户资料接口保存
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveCustomerData(IntfCustomerSaveVO intfCustomerSaveVO) {
		ToolKits.isParamNull(intfCustomerSaveVO, "请求参数不为空");

		Date date = DateUtil.date();

		// 校验非空项
		ToolKits.isParamNull(intfCustomerSaveVO.getCode(), "编码不为空");
		ToolKits.isParamNull(intfCustomerSaveVO.getName(), "名称不为空");
		ToolKits.isParamNull(intfCustomerSaveVO.getCargoOwnerCode(), "客户【" + intfCustomerSaveVO.getName() + "】货主编码不为空");
		ToolKits.isParamNull(intfCustomerSaveVO.getCargoOwnerName(), "客户【" + intfCustomerSaveVO.getName() + "】货主名称不为空");
		ToolKits.isParamNull(intfCustomerSaveVO.getEnterpriseId(), "客户【" + intfCustomerSaveVO.getName() + "】企业ID不为空");
		ToolKits.isParamNull(intfCustomerSaveVO.getEnterpriseName(), "客户【" + intfCustomerSaveVO.getName() + "】企业名称不为空");
		ToolKits.isParamNull(intfCustomerSaveVO.getStatus(), "客户【" + intfCustomerSaveVO.getName() + "】状态不为空");

		// 保存
		CustomerModel customerModel = new CustomerModel();
		BeanUtils.copyProperties(intfCustomerSaveVO, customerModel);
		customerModel.setId(ToolKits.getUUID());
		customerModel.setSource(InterfaceConstant.SOURCE_INTERFACE);
		customerModel.setCreatetime(date);
		customerModel.setUpdatetime(date);

		// 转业务请求集合
		List<IntfCustomerSaveVO> customerSaveVOs = Lists.newArrayList();
		IntfCustomerSaveVO customerSaveVO = new IntfCustomerSaveVO();
		BeanUtils.copyProperties(customerModel, customerSaveVO);
		customerSaveVOs.add(customerSaveVO);

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

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

			IntfErrorLogDTO intfErrorLog = intfErrorLogs.get(0);
				if (intfErrorLog.isStatus()) {
					// 转历史
					CustomerHtyModel customerHtyModel = new CustomerHtyModel();
					BeanUtils.copyProperties(customerModel, customerHtyModel);
					customerHtyModel.setId(ToolKits.getUUID());
					customerHtyModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_Y);
					customerHtyModel.setUpdatetime(DateUtil.date());
					customerHtyMapper.save(customerHtyModel);

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

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

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

}
