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.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.ParameterException;
import com.prolog.framework.core.exception.PrologException;
import com.prolog.rdc.tms.constant.InterfaceConstant;
import com.prolog.rdc.tms.dao.mapper.CustomerConsigneeHtyMapper;
import com.prolog.rdc.tms.dao.mapper.CustomerConsigneeMapper;
import com.prolog.rdc.tms.dao.mapper.ErrorLogMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.CustomerConsigneeHtyModel;
import com.prolog.rdc.tms.model.entity.CustomerConsigneeModel;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.vo.IntfCustomerConsigneeSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.CustomerConsigneeService;

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

/**
 *
 *
 * 客户收货方接口实现层
 *
 * @className :CustomerConsigneeServiceImpl
 * @author :xiaoyf
 * @creationTime :2022/11/07
 * @description :
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class CustomerConsigneeServiceImpl implements CustomerConsigneeService {

	@Autowired
	private CustomerConsigneeMapper mapper;

	@Autowired
	private CustomerConsigneeHtyMapper htyMapper;

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private ErrorLogMapper errorLogMapper;

	@Override
	public void saveCustomerConsignee(List<IntfCustomerConsigneeSaveVO> vos) {
		ToolKits.isParamNull(vos, "请求参数不为空");
		Assert.isTrue(vos.size() <= 100, "批处理数据不大于100条");

		Date date = DateUtil.date();

		// 校验非空项
		for (IntfCustomerConsigneeSaveVO vo : vos) {
			ToolKits.isParamNull(vo.getCode(), "收货方编码不能为空");
			ToolKits.isParamNull(vo.getName(), "收货方名称不能为空");
			ToolKits.isParamNull(vo.getCustomerCode(), "客户编码不能为空");
			ToolKits.isParamNull(vo.getCustomerName(), "客户名称不能为空");
			ToolKits.isParamNull(vo.getCargoOwnerCode(), "客户收货方【" + vo.getName() + "】货主编码不能为空");
			ToolKits.isParamNull(vo.getCargoOwnerName(), "客户收货方【" + vo.getName() + "】货主名称不能为空");
			ToolKits.isParamNull(vo.getProvinceName(), "客户收货方【" + vo.getName() + "】省名称不能为空");
			ToolKits.isParamNull(vo.getCityName(), "客户收货方【" + vo.getName() + "】市名称不能为空");
			ToolKits.isParamNull(vo.getDistrictName(), "客户收货方【" + vo.getName() + "】区名称不能为空");
			ToolKits.isParamNull(vo.getAddress(), "客户收货方【" + vo.getName() + "】地址不能为空");
			ToolKits.isParamNull(vo.getStatus(), "客户收货方【" + vo.getStatus() + "】状态不能为空");
			if (ObjectUtil.notEqual(InterfaceConstant.CUSTOMER_CONSIGNEE_STATUS_TY, vo.getStatus()) && ObjectUtil.notEqual(InterfaceConstant.CUSTOMER_CONSIGNEE_STATUS_YCJ, vo.getStatus()) && ObjectUtil.notEqual(InterfaceConstant.CUSTOMER_CONSIGNEE_STATUS_SHZ, vo.getStatus()) && ObjectUtil.notEqual(InterfaceConstant.CUSTOMER_CONSIGNEE_STATUS_QY, vo.getStatus())) {
				throw new PrologException("客户收货方【" + vo.getStatus() + "】状态错误");
			}

			// 保存
			CustomerConsigneeModel model = new CustomerConsigneeModel();
			BeanUtils.copyProperties(vo, model);
			model.setId(ToolKits.getUUID());
			model.setIntfStatus(InterfaceConstant.INTERFACE_DATA_N);
			model.setSource(InterfaceConstant.SOURCE_INTERFACE);
			model.setCreateperson(InterfaceConstant.INTERFACE_SYSTEM);
			model.setCreatetime(date);
			model.setUpdateperson(InterfaceConstant.INTERFACE_SYSTEM);
			model.setUpdatetime(date);
			mapper.save(model);
		}
	}

	/**
	 * 客户收货方定时任务
	 */
	@Override
	public void customerConsigneeTimerTask() {
		// 查询未处理客户收货方接口数据
		List<CustomerConsigneeModel> customerConsigneeModels = mapper.listCustomerConsigneeSql();

		List<IntfCustomerConsigneeSaveVO> vos = Lists.newArrayList();
		for (CustomerConsigneeModel customerConsigneeModel : customerConsigneeModels) {
			IntfCustomerConsigneeSaveVO vo = new IntfCustomerConsigneeSaveVO();
			BeanUtils.copyProperties(customerConsigneeModel, vo);
			vos.add(vo);
		}

		if (ToolKits.isNotEmpty(vos)) {
			log.info("客户收货方资料接口定时任务请求报文:{}", JSONObject.toJSONString(vos));

			Date date = DateUtil.date();

			// 转业务处理
			RestMessage<List<IntfErrorLogDTO>> restMessage = maindataFeign.saveCustomerConsigneeInterfaceExecute(vos);
			if (restMessage.isSuccess()) {
				List<IntfErrorLogDTO> intfErrorLogs = restMessage.getData();
				Assert.isTrue(ToolKits.isNotEmpty(intfErrorLogs), "处理结果异常：响应为空");
				for (IntfErrorLogDTO intfErrorLog : intfErrorLogs) {
					// 根据ID查询
					CustomerConsigneeModel customerConsigneeModel = customerConsigneeModels.stream().filter(item -> item.getId().equals(intfErrorLog.getDataId())).findAny().orElse(null);
					if (intfErrorLog.isStatus()) {
						// 转历史
						CustomerConsigneeHtyModel htyModel = new CustomerConsigneeHtyModel();
						BeanUtils.copyProperties(customerConsigneeModel, htyModel);
						htyModel.setId(ToolKits.getUUID());
						htyModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_Y);
						htyModel.setUpdatetime(date);
						htyMapper.save(htyModel);

						// 删除当前数据
						mapper.deleteById(intfErrorLog.getDataId(), CustomerConsigneeModel.class);
					} else {
						// 状态修改为E
						customerConsigneeModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_E);
						customerConsigneeModel.setUpdatetime(date);
						mapper.update(customerConsigneeModel);

						ErrorLogModel errorLogModel = new ErrorLogModel();
						errorLogModel.setId(ToolKits.getUUID());
						errorLogModel.setIntfName("客户收货方接口");
						errorLogModel.setErrorInfo(intfErrorLog.getErrorInfo());
						errorLogModel.setTableName("intf_customer_consignee");
						errorLogModel.setDataId(intfErrorLog.getDataId());
						errorLogModel.setCreatetime(date);
						errorLogMapper.save(errorLogModel);
					}
				}
			} else {
				throw new ParameterException(restMessage.getMessage());
			}
		} else {
			log.info("======客户收货方资料接口定时任务没有可执行数据======");
		}
	}

}
