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.ProviderHtyMapper;
import com.prolog.rdc.tms.dao.mapper.ProviderMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.entity.ProviderHtyModel;
import com.prolog.rdc.tms.model.entity.ProviderModel;
import com.prolog.rdc.tms.model.vo.IntfProviderSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.ProviderService;

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

/**
 * 
 *
 * 供应商资料接口实现层
 *
 * @className :ProviderServiceImpl
 * @author :fanzw
 * @creationTime :2022/11/07
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2022/11/07
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class ProviderServiceImpl implements ProviderService {

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private ProviderMapper providerMapper;

	@Autowired
	private ProviderHtyMapper providerHtyMapper;

	@Autowired
	private ErrorLogMapper errorLogMapper;

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

		Date date = DateUtil.date();

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

			// 保存
			ProviderModel providerModel = new ProviderModel();
			BeanUtils.copyProperties(intfProviderSaveVO, providerModel);
			providerModel.setId(ToolKits.getUUID());
			providerModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_N);
			providerModel.setSource(InterfaceConstant.SOURCE_INTERFACE);
			providerModel.setCreateperson(InterfaceConstant.INTERFACE_SYSTEM);
			providerModel.setCreatetime(date);
			providerModel.setUpdateperson(InterfaceConstant.INTERFACE_SYSTEM);
			providerModel.setUpdatetime(date);
			providerMapper.save(providerModel);
		}
	}

	/**
	 * 供应商接口定时任务
	 */
	@Override
	public void providerTimerTask() {
		// 查询未处理供应商接口资料
		List<ProviderModel> providerModels = providerMapper.listProviderSql();

		List<IntfProviderSaveVO> intfProviderSaveVOs = Lists.newArrayList();
		for (ProviderModel providerModel : providerModels) {
			IntfProviderSaveVO intfProviderSaveVO = new IntfProviderSaveVO();
			BeanUtils.copyProperties(providerModel, intfProviderSaveVO);
			intfProviderSaveVOs.add(intfProviderSaveVO);
		}

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

			Date date = DateUtil.date();

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

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

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

}
