package cn.gov.customs.wxjy.ibmmq.utils;


import jodd.util.StringUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * 包名映射工具类
 * 统一管理实体类和Mapper类的包名映射规则
 */
public class PackageMappingUtil {

	// 实体类包名映射规则
	private static final Map<String, String> ENTITY_PACKAGE_MAPPING = new HashMap<>();

	// Mapper类包名映射规则
	private static final Map<String, String> MAPPER_PACKAGE_MAPPING = new HashMap<>();

	// 新增字段名映射配置
	private static final Map<String, Map<String, String>> FIELD_MAPPING = new HashMap<>();

	static {
		// 初始化实体类包名映射
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.MaterialPo",
			"cn.gov.customs.zhsj.parts.pojo.MaterialPo");  //采购入库单
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.MaterialMo",
				"cn.gov.customs.zhsj.parts.pojo.MaterialMo");  //材料入库单
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.MaterialPp",
				"cn.gov.customs.zhsj.parts.pojo.MaterialPp");  //生产加工单
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.MaterialFgw",
				"cn.gov.customs.zhsj.parts.pojo.MaterialFgw");  //产成品入库单
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.MaterialSo",
				"cn.gov.customs.zhsj.parts.pojo.MaterialSo");  //销售出库单
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.SupervisionLog",
				"cn.gov.customs.zhsj.parts.pojo.SupervisionLog");  //监管日志
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.EntProductionLog",
				"cn.gov.customs.zhsj.parts.pojo.EntProductionLog");  //企业生产日志
		ENTITY_PACKAGE_MAPPING.put("org.springblade.bus.parts.entity.EntProductionChild",
				"cn.gov.customs.zhsj.parts.pojo.EntProductionChild");  //企业生产日志子表

		// 新增字段映射配置,若需求其他字段映射，可在mapping中添加
		Map<String, String> materialPoFieldMapping = new HashMap<>();
		materialPoFieldMapping.put("id", "pkMaterialPo"); // 采购入库
		Map<String, String> materialMoFieldMapping = new HashMap<>();
		materialMoFieldMapping.put("id", "pkMaterialMo"); // 材料入库
		Map<String, String> materialPpFieldMapping = new HashMap<>();
		materialPpFieldMapping.put("id", "pkMaterialPp"); // 生产加工
		Map<String, String> materialFgwFieldMapping = new HashMap<>();
		materialFgwFieldMapping.put("id", "pkMaterialFgw"); // 产成品入库
		Map<String, String> materialSoFieldMapping = new HashMap<>();
		materialSoFieldMapping.put("id", "pkMaterialSo"); // 销售出库
		Map<String, String> supervisionLogFieldMapping = new HashMap<>();
		supervisionLogFieldMapping.put("id", "pkSupervisionLog"); // 监管日志
		Map<String, String> entProductionLogFieldMapping = new HashMap<>();
		entProductionLogFieldMapping.put("id", "pkEntProductionLog"); // 企业生产日志
		Map<String, String> entEntProductionChildFieldMapping = new HashMap<>();
		entEntProductionChildFieldMapping.put("id", "pkEntProductionChild"); // 企业生产日志子表

		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.MaterialPo", materialPoFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.MaterialMo", materialMoFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.MaterialPp", materialPpFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.MaterialFgw", materialFgwFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.MaterialSo", materialSoFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.SupervisionLog", supervisionLogFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.EntProductionLog", entProductionLogFieldMapping);
		FIELD_MAPPING.put("cn.gov.customs.zhsj.parts.pojo.EntProductionChild", entEntProductionChildFieldMapping);


		//Mapper包名映射
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.MaterialPoMapper",
			"cn.gov.customs.zhsj.parts.dao.MaterialPoMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.MaterialMoMapper",
				"cn.gov.customs.zhsj.parts.dao.MaterialMoMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.MaterialPpMapper",
				"cn.gov.customs.zhsj.parts.dao.MaterialPpMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.MaterialFgwMapper",
				"cn.gov.customs.zhsj.parts.dao.MaterialFgwMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.MaterialSoMapper",
				"cn.gov.customs.zhsj.parts.dao.MaterialSoMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.SupervisionLogMapper",
				"cn.gov.customs.zhsj.parts.dao.SupervisionLogMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.EntProductionLogMapper",
				"cn.gov.customs.zhsj.parts.dao.EntProductionLogMapper");
		MAPPER_PACKAGE_MAPPING.put("org.springblade.bus.parts.mapper.EntProductionChildMapper",
				"cn.gov.customs.zhsj.parts.dao.EntProductionChildMapper");

		//可添加其他路径映射，若项目包路径一致，则可不用配置此映射

	}

	/**
	 * 映射实体类包名
	 */
	public static String mapEntityPackage(String originalTypeName) {
		if (StringUtil.isEmpty(originalTypeName)) {
			return originalTypeName;
		}

		for (Map.Entry<String, String> entry : ENTITY_PACKAGE_MAPPING.entrySet()) {
			if (originalTypeName.startsWith(entry.getKey())) {
				return originalTypeName.replace(entry.getKey(), entry.getValue());
			}
		}

		return originalTypeName;
	}

	/**
	 * 映射Mapper类包名
	 */
	public static String mapMapperPackage(String originalTypeName) {
		if (StringUtil.isEmpty(originalTypeName)) {
			return originalTypeName;
		}

		for (Map.Entry<String, String> entry : MAPPER_PACKAGE_MAPPING.entrySet()) {
			if (originalTypeName.startsWith(entry.getKey())) {
				return originalTypeName.replace(entry.getKey(), entry.getValue());
			}
		}

		return originalTypeName;
	}

	/**
	 * 添加实体类映射规则（运行时动态添加）
	 */
	public static void addEntityMapping(String sourcePackage, String targetPackage) {
		ENTITY_PACKAGE_MAPPING.put(sourcePackage, targetPackage);
	}

	/**
	 * 添加Mapper类映射规则（运行时动态添加）
	 */
	public static void addMapperMapping(String sourcePackage, String targetPackage) {
		MAPPER_PACKAGE_MAPPING.put(sourcePackage, targetPackage);
	}

	/**
	 * 获取字段映射配置
	 */
	public static Map<String, String> getFieldMapping(String className) {
		return FIELD_MAPPING.get(className);
	}
	/**
	 * 添加字段映射规则
	 */
	public static void addFieldMapping(String className, String sourceField, String targetField) {
		FIELD_MAPPING.computeIfAbsent(className, k -> new HashMap<>())
				.put(sourceField, targetField);
	}
}
