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

import java.math.BigDecimal;
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.ErrorLogMapper;
import com.prolog.rdc.tms.dao.mapper.ItemHtyMapper;
import com.prolog.rdc.tms.dao.mapper.ItemMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.entity.ItemHtyModel;
import com.prolog.rdc.tms.model.entity.ItemModel;
import com.prolog.rdc.tms.model.vo.IntfItemSaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.ItemService;

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

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

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private ItemMapper itemMapper;

	@Autowired
	private ItemHtyMapper itemHtyMapper;

	@Autowired
	private ErrorLogMapper errorLogMapper;

	/**
	 * 商品资料接口保存
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveItemData(IntfItemSaveVO intfItemSaveVO) {
		ToolKits.isParamNull(intfItemSaveVO, "请求参数不为空");

		Date date = DateUtil.date();

		// 校验非空项
		ToolKits.isParamNull(intfItemSaveVO.getItemCode(), "商品编码不为空");
		ToolKits.isParamNull(intfItemSaveVO.getItemName(), "商品名称不为空");
		Assert.isTrue(ToolKits.isNotEmpty(intfItemSaveVO.getItemCategoryCode()) || ToolKits.isNotEmpty(intfItemSaveVO.getItemCategoryName()), "商品【" + intfItemSaveVO.getItemName() + "】商品品类编码/名称不同时为空");
		ToolKits.isParamNull(intfItemSaveVO.getCargoOwnerCode(), "商品【" + intfItemSaveVO.getItemName() + "】货主编码不为空");
		ToolKits.isParamNull(intfItemSaveVO.getCargoOwnerName(), "商品【" + intfItemSaveVO.getItemName() + "】货主名称不为空");
		ToolKits.isParamNull(intfItemSaveVO.getEnterpriseId(), "商品【" + intfItemSaveVO.getItemName() + "】企业ID不为空");
		ToolKits.isParamNull(intfItemSaveVO.getEnterpriseName(), "商品【" + intfItemSaveVO.getItemName() + "】企业名称不为空");
		ToolKits.isParamNull(intfItemSaveVO.getStatus(), "商品【" + intfItemSaveVO.getItemName() + "】状态不为空");

		// 保存
		ItemModel itemModel = new ItemModel();
		BeanUtils.copyProperties(intfItemSaveVO, itemModel);
		itemModel.setItemId(ToolKits.getUUID());
		itemModel.setLength(ToolKits.isEmpty(intfItemSaveVO.getLength()) ? BigDecimal.ZERO : intfItemSaveVO.getLength());
		itemModel.setWidth(ToolKits.isEmpty(intfItemSaveVO.getWidth()) ? BigDecimal.ZERO : intfItemSaveVO.getWidth());
		itemModel.setHeigth(ToolKits.isEmpty(intfItemSaveVO.getHeigth()) ? BigDecimal.ZERO : intfItemSaveVO.getHeigth());
		itemModel.setWeight(ToolKits.isEmpty(intfItemSaveVO.getWeight()) ? BigDecimal.ZERO : intfItemSaveVO.getWeight());
		itemModel.setVolume(ToolKits.isEmpty(intfItemSaveVO.getVolume()) ? BigDecimal.ZERO : intfItemSaveVO.getVolume());
		itemModel.setSource(InterfaceConstant.SOURCE_INTERFACE);
		itemModel.setCreatetime(date);
		itemModel.setUpdatetime(date);


		// 转业务请求集合
		List<IntfItemSaveVO> itemSaveVOs = Lists.newArrayList();

		IntfItemSaveVO itemSaveVO = new IntfItemSaveVO();
		BeanUtils.copyProperties(itemModel, itemSaveVO);
		itemSaveVOs.add(itemSaveVO);

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

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

			IntfErrorLogDTO intfErrorLog = intfErrorLogs.get(0);
			if (intfErrorLog.isStatus()) {
				// 转历史
				ItemHtyModel itemHtyModel = new ItemHtyModel();
				BeanUtils.copyProperties(itemModel, itemHtyModel);
				itemHtyModel.setItemId(ToolKits.getUUID());
				itemHtyModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_Y);
				itemHtyModel.setUpdatetime(DateUtil.date());
				itemHtyMapper.save(itemHtyModel);

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

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

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

}
