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.ParameterException;
import com.prolog.rdc.tms.constant.InterfaceConstant;
import com.prolog.rdc.tms.dao.mapper.ErrorLogMapper;
import com.prolog.rdc.tms.dao.mapper.ProviderShipperHtyMapper;
import com.prolog.rdc.tms.dao.mapper.ProviderShipperMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.entity.ProviderShipperHtyModel;
import com.prolog.rdc.tms.model.entity.ProviderShipperModel;
import com.prolog.rdc.tms.model.vo.IntfProviderShipperSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.ProviderShipperService;

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

/**
 * 
 *
 * 供应商发货方接口实现层
 *
 * @className :ProviderShipperServiceImpl
 * @author :fanzw
 * @creationTime :2022/11/14
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/11/14
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class ProviderShipperServiceImpl implements ProviderShipperService {

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private ProviderShipperMapper providerShipperMapper;

	@Autowired
	private ProviderShipperHtyMapper providerShipperHtyMapper;

	@Autowired
	private ErrorLogMapper errorLogMapper;

	/**
	 * 供应商发货方接口保存
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void saveProviderShipperData(List<IntfProviderShipperSaveVO> intfProviderShipperSaveVOs) {
		ToolKits.isParamNull(intfProviderShipperSaveVOs, "请求参数不为空");
		Assert.isTrue(intfProviderShipperSaveVOs.size() <= 100, "批处理数据不大于100条");

		Date date = DateUtil.date();

		// 校验非空项
		for (IntfProviderShipperSaveVO intfProviderShipperSaveVO : intfProviderShipperSaveVOs) {
			ToolKits.isParamNull(intfProviderShipperSaveVO.getCode(), "编码不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getName(), "名称不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getProviderCode(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】供应商编码不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getProviderName(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】供应商名称不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getCargoOwnerCode(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】货主编码不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getCargoOwnerName(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】货主名称不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getEnterpriseId(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】企业ID不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getEnterpriseName(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】企业名称不为空");
			ToolKits.isParamNull(intfProviderShipperSaveVO.getStatus(), "供应商发货方【" + intfProviderShipperSaveVO.getName() + "】状态不为空");

			// 保存
			ProviderShipperModel providerShipperModel = new ProviderShipperModel();
			BeanUtils.copyProperties(intfProviderShipperSaveVO, providerShipperModel);
			providerShipperModel.setId(ToolKits.getUUID());
			providerShipperModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_N);
			providerShipperModel.setSource(InterfaceConstant.SOURCE_INTERFACE);
			providerShipperModel.setCreateperson(InterfaceConstant.INTERFACE_SYSTEM);
			providerShipperModel.setCreatetime(date);
			providerShipperModel.setUpdateperson(InterfaceConstant.INTERFACE_SYSTEM);
			providerShipperModel.setUpdatetime(date);
			providerShipperMapper.save(providerShipperModel);
		}
	}

	/**
	 * 供应商发货方接口定时任务
	 */
	@Override
	public void providerShipperTimerTask() {
		// 查询未处理供应商发货方接口
		List<ProviderShipperModel> providerShipperModels = providerShipperMapper.listProviderShipperSql();

		List<IntfProviderShipperSaveVO> intfProviderShipperSaveVOs = Lists.newArrayList();
		for (ProviderShipperModel providerShipperModel : providerShipperModels) {
			IntfProviderShipperSaveVO intfProviderShipperSaveVO = new IntfProviderShipperSaveVO();
			BeanUtils.copyProperties(providerShipperModel, intfProviderShipperSaveVO);
			intfProviderShipperSaveVOs.add(intfProviderShipperSaveVO);
		}

		if (ToolKits.isNotEmpty(intfProviderShipperSaveVOs)) {
			log.info("供应商发货方接口定时任务请求报文:{}", JSONObject.toJSONString(intfProviderShipperSaveVOs));

			Date date = DateUtil.date();

			RestMessage<List<IntfErrorLogDTO>> restMessage = maindataFeign.saveProviderShipperInterfaceExecute(intfProviderShipperSaveVOs);
			if (restMessage.isSuccess()) {
				List<IntfErrorLogDTO> intfErrorLogs = restMessage.getData();
				Assert.isTrue(ToolKits.isNotEmpty(intfErrorLogs), "处理结果异常：响应为空");
				for (IntfErrorLogDTO intfErrorLog : intfErrorLogs) {
					// 根据ID查询
					ProviderShipperModel providerShipperModel = providerShipperModels.stream().filter(item -> item.getId().equals(intfErrorLog.getDataId())).findAny().orElse(null);
					if (intfErrorLog.isStatus()) {
						// 转历史
						ProviderShipperHtyModel providerShipperHtyModel = new ProviderShipperHtyModel();
						BeanUtils.copyProperties(providerShipperModel, providerShipperHtyModel);
						providerShipperHtyModel.setId(ToolKits.getUUID());
						providerShipperHtyModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_Y);
						providerShipperHtyModel.setUpdatetime(date);
						providerShipperHtyMapper.save(providerShipperHtyModel);

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

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

}
