ConfigureParametersComponents.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. package com.ydtech.modules.order.components;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.ydtech.exception.SystemException;
  4. import com.ydtech.modules.order.entity.config.ConfigureParameters;
  5. import com.ydtech.modules.protocol.entity.dto.PtlAttributionTemplateDto;
  6. import com.ydtech.modules.protocol.entity.po.PtlAgreementAttribution;
  7. import com.ydtech.modules.protocol.service.PtlAgreementAttributionService;
  8. import com.ydtech.modules.protocol.utils.AssertionUtils;
  9. import lombok.extern.slf4j.Slf4j;
  10. import org.springframework.stereotype.Component;
  11. import org.springframework.util.ReflectionUtils;
  12. import org.springframework.util.StringUtils;
  13. import java.lang.reflect.Field;
  14. import java.util.LinkedHashMap;
  15. import java.util.List;
  16. import java.util.Map;
  17. @Slf4j
  18. @Component
  19. public class ConfigureParametersComponents {
  20. private final PtlAgreementAttributionService ptlAgreementAttributionService;
  21. public ConfigureParametersComponents(PtlAgreementAttributionService ptlAgreementAttributionService) {
  22. this.ptlAgreementAttributionService = ptlAgreementAttributionService;
  23. }
  24. /**
  25. * 通过反射设置对象属性
  26. *
  27. * @param t 配置
  28. * @param ptlAgreementAttribution 协议归属信息
  29. * @param <T> 配置
  30. */
  31. private <T extends ConfigureParameters> void conversionEntity(T t, PtlAgreementAttribution ptlAgreementAttribution) {
  32. JSONArray fields = ptlAgreementAttribution.getFields();
  33. List<PtlAttributionTemplateDto> ptlAttributionTemplateDtos = fields.toJavaList(PtlAttributionTemplateDto.class);
  34. Map<String, String> collect = ptlAttributionTemplateDtos.stream().collect(LinkedHashMap::new, (m, v) -> m.put(v.getField(),
  35. v.getValue()), LinkedHashMap::putAll);
  36. Field[] fieldsList = t.getClass().getDeclaredFields();
  37. for (Field field : fieldsList) {
  38. try {
  39. ReflectionUtils.makeAccessible(field);
  40. field.set(t, collect.get(field.getName()));
  41. } catch (IllegalAccessException e) {
  42. throw new SystemException("{}没有此字段的映射", collect.get(field.getName()));
  43. }
  44. }
  45. }
  46. /**
  47. * 通过 协议id获取配置实体
  48. *
  49. * @param t 配置
  50. * @param agreementId 协议id
  51. * @param <T> 配置
  52. */
  53. public <T extends ConfigureParameters> void toEntity(T t, String agreementId) {
  54. PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getById(agreementId);
  55. AssertionUtils.isEmpty(ptlAgreementAttribution, "此协议未查询到配置信息");
  56. this.conversionEntity(t, ptlAgreementAttribution);
  57. }
  58. /**
  59. * 通过 协议id获取配置实体
  60. *
  61. * @param t 配置
  62. * @param ptlAgreementAttribution 协议归属信息
  63. * @param <T> 配置
  64. */
  65. public <T extends ConfigureParameters> void toEntity(T t, PtlAgreementAttribution ptlAgreementAttribution) {
  66. this.conversionEntity(t, ptlAgreementAttribution);
  67. }
  68. /**
  69. * 通过 协议id获取配置实体
  70. *
  71. * @param tClass 配置类型
  72. * @param agreementId 协议id
  73. * @param <T> 配置
  74. */
  75. public <T extends ConfigureParameters> T toEntity(Class<T> tClass, String agreementId) {
  76. PtlAgreementAttribution ptlAgreementAttribution = ptlAgreementAttributionService.getById(agreementId);
  77. return this.toEntity(tClass, ptlAgreementAttribution);
  78. }
  79. /**
  80. * 通过 协议id获取配置实体
  81. *
  82. * @param tClass 配置类型
  83. * @param ptlAgreementAttribution 协议归属信息
  84. * @param <T> 配置
  85. */
  86. public <T extends ConfigureParameters> T toEntity(Class<T> tClass, PtlAgreementAttribution ptlAgreementAttribution) {
  87. try {
  88. T parameters = tClass.newInstance();
  89. this.conversionEntity(parameters, ptlAgreementAttribution);
  90. return parameters;
  91. } catch (InstantiationException | IllegalAccessException exception) {
  92. throw new SystemException("实例化异常");
  93. }
  94. }
  95. }