package com.ydtech.modules.order.components; import com.alibaba.fastjson.JSONArray; import com.ydtech.exception.SystemException; import com.ydtech.modules.order.entity.config.ConfigureParameters; import com.ydtech.modules.protocol.entity.dto.PtlAttributionTemplateDto; import com.ydtech.modules.protocol.entity.po.PtlAgreementAttribution; import com.ydtech.modules.protocol.service.PtlAgreementAttributionService; import com.ydtech.modules.protocol.utils.AssertionUtils; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import java.lang.reflect.Field; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; @Slf4j @Component public class ConfigureParametersComponents { private final PtlAgreementAttributionService ptlAgreementAttributionService; public ConfigureParametersComponents(PtlAgreementAttributionService ptlAgreementAttributionService) { this.ptlAgreementAttributionService = ptlAgreementAttributionService; } /** * 通过反射设置对象属性 * * @param t 配置 * @param ptlAgreementAttribution 协议归属信息 * @param 配置 */ private void conversionEntity(T t, PtlAgreementAttribution ptlAgreementAttribution) { JSONArray fields = ptlAgreementAttribution.getFields(); List ptlAttributionTemplateDtos = fields.toJavaList(PtlAttributionTemplateDto.class); Map collect = ptlAttributionTemplateDtos.stream().collect(LinkedHashMap::new, (m, v) -> m.put(v.getField(), v.getValue()), LinkedHashMap::putAll); Field[] fieldsList = t.getClass().getDeclaredFields(); for (Field field : fieldsList) { try { ReflectionUtils.makeAccessible(field); field.set(t, collect.get(field.getName())); } catch (IllegalAccessException e) { throw new SystemException("{}没有此字段的映射", collect.get(field.getName())); } } } /** * 通过 协议id获取配置实体 * * @param t 配置 * @param agreementId 协议id * @param 配置 */ public void toEntity(T t, String agreementId) { PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getById(agreementId); AssertionUtils.isEmpty(ptlAgreementAttribution, "此协议未查询到配置信息"); this.conversionEntity(t, ptlAgreementAttribution); } /** * 通过 协议id获取配置实体 * * @param t 配置 * @param ptlAgreementAttribution 协议归属信息 * @param 配置 */ public void toEntity(T t, PtlAgreementAttribution ptlAgreementAttribution) { this.conversionEntity(t, ptlAgreementAttribution); } /** * 通过 协议id获取配置实体 * * @param tClass 配置类型 * @param agreementId 协议id * @param 配置 */ public T toEntity(Class tClass, String agreementId) { PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getById(agreementId); return this.toEntity(tClass, ptlAgreementAttribution); } /** * 通过 协议id获取配置实体 * * @param tClass 配置类型 * @param ptlAgreementAttribution 协议归属信息 * @param 配置 */ public T toEntity(Class tClass, PtlAgreementAttribution ptlAgreementAttribution) { try { T parameters = tClass.newInstance(); this.conversionEntity(parameters, ptlAgreementAttribution); return parameters; } catch (InstantiationException | IllegalAccessException exception) { throw new SystemException("实例化异常"); } } }