1、创建注解文件:
package com.aiipc.dpm.api.annotation;import java.lang.annotation.*;@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Echarts {/*** 单位* @return*/String unit() default "";/*** 颜色* @return*/String color() default "";/*** 标题* @return*/String title() default "";/*** 第二单位* @return*/String sedUnit() default "";
}
2、创建拦截器
package com.aiipc.dpm.aspect;import com.aiipc.common.core.util.ApiResult;
import com.aiipc.common.core.util.ClassUtils;
import com.aiipc.common.core.util.StringUtils;
import com.aiipc.dpm.api.annotation.Echarts;
import com.aiipc.dpm.api.annotation.EchartsDefinition;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;@Slf4j
@Aspect
@Component
public class EchartsAspect {@Pointcut("execution(* com.aiipc.*.controller.*.*(..))")public void doPointcut() {}/*** 切点之前*/@Before("doPointcut()")public void before(JoinPoint joinPoint) throws Throwable {log.info("============ before ==========");}/*** 切点之后*/@After("doPointcut()")public void after() throws Throwable {log.info("============ after ==========");}/*** 切点返回内容后*/@AfterReturning("doPointcut()")public void afterReturning() throws Throwable {log.info("============ afterReturning ==========");}/*** 切点抛出异常后*/@AfterThrowing("doPointcut()")public void afterThrowing() throws Throwable {log.info("============ afterThrowing ==========");}@Around("doPointcut()")public Object around(ProceedingJoinPoint point) throws Throwable {log.info("============ around1 ==========");Object obj = point.proceed(point.getArgs());try {if (obj instanceof ApiResult) {ApiResult apiResult = (ApiResult) obj;Object data = apiResult.getData();if (data instanceof List) {List objectList = (List) data;if (CollectionUtils.isEmpty(objectList)) {return apiResult;}List<EchartsDefinition> dictDefinitions = getMetadata(objectList.get(0));if (CollectionUtils.isEmpty(dictDefinitions)) {return apiResult;}List updateList = new ArrayList<>();for (Object o : objectList) {JSONObject jsonObject = doConvertEcharts(o, dictDefinitions);updateList.add(jsonObject);}apiResult.setData(updateList);return apiResult;}}} catch (Exception e) {log.error("TbFlowsDAO查询结果字典转换失败", e);}log.info("============ around2 ==========");return obj;}@AfterReturning(pointcut = "doPointcut()", returning = "result")public void doAfterReturning(JoinPoint pjp, Object result) {log.info("============ doAfterReturning ==========");log.info("============end doAfterReturning ==========");}@SneakyThrowsprivate static JSONObject doConvertEcharts(Object target, List<EchartsDefinition> dictDefinitions) {JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(target));for (EchartsDefinition dictDefinition : dictDefinitions) {Echarts dict = dictDefinition.getEcharts();String fieldName = dictDefinition.getField().getName();// 颜色String color = dict.color();if (StringUtils.isNotEmpty(color)){jsonObject.put(fieldName.concat("TooltipColor"), color);}// 单位String unit = dict.unit();if (StringUtils.isNotEmpty(unit)){Object o = jsonObject.get(fieldName);if (o != null){jsonObject.put(fieldName.concat("TooltipContent"), o.toString().concat(unit));}}// 标题String title = dict.title();if (StringUtils.isNotEmpty(title)){jsonObject.put(fieldName.concat("TooltipTitle"), title);}// 第二单位String sedUnit = dict.sedUnit();if (StringUtils.isNotEmpty(sedUnit)){jsonObject.put(fieldName.concat("TooltipUnit"), sedUnit);}}return jsonObject;}/*** 仅获取一次Dict元数据,降低多次反射造成的性能消耗** @param target 目标实体类* @return Dict元数据*/private static List<EchartsDefinition> getMetadata(Object target) {List<EchartsDefinition> dictDefinitions = new ArrayList<>();if (ClassUtils.isPrimitiveOrWrapper(target.getClass())|| target instanceof Map || target instanceof String) {return dictDefinitions;}List<Field> fields = FieldUtils.getAllFieldsList(target.getClass());for (Field field : fields) {Echarts echart = AnnotationUtils.getAnnotation(field, Echarts.class);if (echart != null) {EchartsDefinition dictDefinition = new EchartsDefinition();dictDefinition.setEcharts(echart);dictDefinition.setField(field);dictDefinitions.add(dictDefinition);}}return dictDefinitions;}}
3、创建注解定义类
创建这个类是为了遍历结果的时候,防止造成多次循环反射。
package com.aiipc.dpm.api.annotation;import lombok.Data;import java.lang.reflect.Field;@Data
public class EchartsDefinition {private Echarts echarts;private Field field;}
4、在返回结果的实体类上增加注解
package com.aiipc.dpm.api.vo;import com.aiipc.dpm.api.annotation.Echarts;
import lombok.Data;@Data
public class CellsRatioVO {String pDate;/*** pack数量*/@Echarts(unit = "万", color = "rgb(39,154,217)", title= "PACK数量")private Double ratioPack;/*** 合浆投入*/@Echarts(unit = "万", color = "rgb(20,106,248)", title = "合浆投入")private Double ratioImport;/*** 出库*/@Echarts(unit = "万", color = "rgb(139,58,224)", title = "出库")private Double ratioOutput;/*** 比率*/@Echarts(unit = "%", color = "#AE57A4", title = "电芯利用率")private String effRatio;/*** 使用至批次*/private String maxBatchCode;
}
5、调用对应的查询接口返回值结果如下: