| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109 |
- 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 <T> 配置
- */
- private <T extends ConfigureParameters> void conversionEntity(T t, PtlAgreementAttribution ptlAgreementAttribution) {
- JSONArray fields = ptlAgreementAttribution.getFields();
- List<PtlAttributionTemplateDto> ptlAttributionTemplateDtos = fields.toJavaList(PtlAttributionTemplateDto.class);
- Map<String, String> 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 <T> 配置
- */
- public <T extends ConfigureParameters> void toEntity(T t, String agreementId) {
- PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getById(agreementId);
- AssertionUtils.isEmpty(ptlAgreementAttribution, "此协议未查询到配置信息");
- this.conversionEntity(t, ptlAgreementAttribution);
- }
- /**
- * 通过 协议id获取配置实体
- *
- * @param t 配置
- * @param ptlAgreementAttribution 协议归属信息
- * @param <T> 配置
- */
- public <T extends ConfigureParameters> void toEntity(T t, PtlAgreementAttribution ptlAgreementAttribution) {
- this.conversionEntity(t, ptlAgreementAttribution);
- }
- /**
- * 通过 协议id获取配置实体
- *
- * @param tClass 配置类型
- * @param agreementId 协议id
- * @param <T> 配置
- */
- public <T extends ConfigureParameters> T toEntity(Class<T> tClass, String agreementId) {
- PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getById(agreementId);
- return this.toEntity(tClass, ptlAgreementAttribution);
- }
- /**
- * 通过 协议id获取配置实体
- *
- * @param tClass 配置类型
- * @param ptlAgreementAttribution 协议归属信息
- * @param <T> 配置
- */
- public <T extends ConfigureParameters> T toEntity(Class<T> tClass, PtlAgreementAttribution ptlAgreementAttribution) {
- try {
- T parameters = tClass.newInstance();
- this.conversionEntity(parameters, ptlAgreementAttribution);
- return parameters;
- } catch (InstantiationException | IllegalAccessException exception) {
- throw new SystemException("实例化异常");
- }
- }
- }
|