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.ErrorLogMapper;
import com.prolog.rdc.tms.dao.mapper.ItemCategoryHtyMapper;
import com.prolog.rdc.tms.dao.mapper.ItemCategoryMapper;
import com.prolog.rdc.tms.model.dto.IntfErrorLogDTO;
import com.prolog.rdc.tms.model.entity.ErrorLogModel;
import com.prolog.rdc.tms.model.entity.ItemCategoryHtyModel;
import com.prolog.rdc.tms.model.entity.ItemCategoryModel;
import com.prolog.rdc.tms.model.vo.ItemCategorySaveVO;
import com.prolog.rdc.tms.rmi.feign.MaindataFeign;
import com.prolog.rdc.tms.service.ItemCategoryService;

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

/**
 * 
 *
 * 商品品类接口实现层
 *
 * @className :ItemCategoryServiceImpl
 * @author :fanzw
 * @creationTime :2023/04/12
 * @description :
 *
 * @modifier :fanzw
 * @modifyTime :2023/04/12
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
@Slf4j
@Service
public class ItemCategoryServiceImpl implements ItemCategoryService {

	@Autowired
	private MaindataFeign maindataFeign;

	@Autowired
	private ItemCategoryMapper itemCategoryMapper;

	@Autowired
	private ItemCategoryHtyMapper itemCategoryHtyMapper;

	@Autowired
	private ErrorLogMapper errorLogMapper;

	/**
	 * 商品品类接口保存
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public String saveItemCategoryData(ItemCategorySaveVO itemCategorySaveVO) {
		ToolKits.isParamNull(itemCategorySaveVO, "请求参数不为空");

		Date date = DateUtil.date();

		// 校验非空项
		ToolKits.isParamNull(itemCategorySaveVO.getItemCategoryCode(), "商品品类编码不为空");
		ToolKits.isParamNull(itemCategorySaveVO.getItemCategoryName(), "商品品类名称不为空");
		ToolKits.isParamNull(itemCategorySaveVO.getItemCategoryLevel(), "商品品类层级不为空");

		// 商品品类非一级时上级品类信息不为空
		if (itemCategorySaveVO.getItemCategoryLevel().intValue() > InterfaceConstant.ITEM_CATEGORY_LEVEL_ONE.intValue()) {
			ToolKits.isParamNull(itemCategorySaveVO.getFatherItemCategoryCode(), "上级商品品类编码不为空");
			ToolKits.isParamNull(itemCategorySaveVO.getFatherItemCategoryName(), "上级商品品类名称不为空");
			ToolKits.isParamNull(itemCategorySaveVO.getFatherItemCategoryLevel(), "上级商品品类层级不为空");
		}

		ToolKits.isParamNull(itemCategorySaveVO.getEnterpriseId(), "企业ID不为空");
		ToolKits.isParamNull(itemCategorySaveVO.getEnterpriseName(), "企业名称不为空");
		ToolKits.isParamNull(itemCategorySaveVO.getStatus(), "状态不为空");

		// 保存
		ItemCategoryModel itemCategoryModel = new ItemCategoryModel();
		BeanUtils.copyProperties(itemCategorySaveVO, itemCategoryModel);
		itemCategoryModel.setItemCategoryId(ToolKits.getUUID());
		itemCategoryModel.setSource(InterfaceConstant.SOURCE_INTERFACE);
		itemCategoryModel.setCreatetime(date);
		itemCategoryModel.setUpdatetime(date);

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

		ItemCategorySaveVO newItemCategorySaveVO = new ItemCategorySaveVO();
		BeanUtils.copyProperties(itemCategoryModel, newItemCategorySaveVO);
		itemCategorySaveVOs.add(newItemCategorySaveVO);

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

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

			IntfErrorLogDTO intfErrorLog = intfErrorLogs.get(0);

			if (intfErrorLog.isStatus()) {
				// 转历史
				ItemCategoryHtyModel itemCategoryHtyModel = new ItemCategoryHtyModel();
				BeanUtils.copyProperties(itemCategoryModel, itemCategoryHtyModel);
				itemCategoryHtyModel.setItemCategoryId(ToolKits.getUUID());
				itemCategoryHtyModel.setIntfStatus(InterfaceConstant.INTERFACE_DATA_Y);
				itemCategoryHtyModel.setUpdatetime(DateUtil.date());
				itemCategoryHtyMapper.save(itemCategoryHtyModel);

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

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

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

}
