自定义注解使用案例,

发布时间:2024年01月24日

?后续会加上详细注释,或者出一个贴图详细的2.0版本,大家不要错过!我会详细教大家如何使用自定义注解开发!让你的代码瞬间高大上起来

@Data
public class ReportDateDTO {

    /**
     * 年
     */
    @ApiModelProperty(value = "年", notes = "")
    @ReportFiled(value = "year", description = "date", font = FONT_MS_YH, fontsize = 13.5f)
    private String year;

    /**
     * 月
     */
    @ApiModelProperty(value = "月", notes = "")
    @ReportFiled(value = "month", description = "date", font = FONT_MS_YH, fontsize = 13.5f)
    private String month;

    /**
     * 日
     */
    @ApiModelProperty(value = "日", notes = "")
    @ReportFiled(value = "day", description = "date", font = FONT_MS_YH, fontsize = 13.5f)
    private String day;

    @ReportList("report_")
    private List<ForecastDataDTO> forecastDataDTOList;
}

?

/**
 * @author:Ttc
 * @date 2024/1/22  14:36
 * @description: 导出大屏预报数据VO
 */
@Data
public class ForecastDataDTO {
    /**
     * 日期
     */
    @ApiModelProperty(value = "日期", notes = "")
    @ReportFiled(value = "date")
    private String formattedDate;
    /**
     * 天气
     */
    @ApiModelProperty(value = "天气", notes = "")
    @ReportFiled(value = "weather")
    private String weaDay;
    /**
     * 大屏显示风力等级
     */
    @ApiModelProperty(value = "大屏显示风力等级", notes = "")
    @ReportFiled(value = "wind")
    private String largeScreenWind;
    /**
     * 最高温度
     */
    @ApiModelProperty(value = "最高温度", notes = "")
    @ReportFiled(value = "tem_max")
    private String tem1;

    /**
     * 最低温度
     */
    @ApiModelProperty(value = "最低温度", notes = "")
    @ReportFiled(value = "tem_min")
    private String tem2;
}

?

?

/**
 * @author:Ttc
 * @date 2024/1/18  17:54
 * @description: 报告字段
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ReportFiled {
    String value() default "";

    float fontsize() default 12.0f;

    /**
     * 字体路径
     * 如果为空,使用默认字体
     * @return
     */
    String font() default "";
    String prefix() default "";
    String description() default "";
}

?

/**
 * @author:Ttc
 * @date 2024/1/23  14:48
 * @description: pdf
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface ReportList {
    /**
     * 报表集合名称
     * 作为其中元素的前缀
     * @return
     */
    String value() default "";
}
public Map<String, PdfFiledDto> getParamMap(Object param, String prefix, int index) {
    Map<String, PdfFiledDto> map = new HashMap<>(16);
    if (param == null) {
        return map;
    }
    Field[] fields = param.getClass().getDeclaredFields();
    for (Field field : fields) {
        Object formVal = ReflectUtil.getFieldValue(param, field.getName());
        ReportFiled reportFiled = field.getAnnotation(ReportFiled.class);
        if (reportFiled != null) {
            fillParamMapByReportFiled(map, formVal, reportFiled, prefix, index);
        }
        ReportList reportList = field.getAnnotation(ReportList.class);
        if (reportList != null) {
            String formKeyPrefix = reportList.value();
            if (formVal instanceof List) {
                List<Object> formObjList = (List<Object>) formVal;
                for (int i = 0; i < formObjList.size(); i++) {
                    Object subObj = formObjList.get(i);
                    Map<String, PdfFiledDto> subMap = getParamMap(subObj, formKeyPrefix, i + 1);
                    map.putAll(subMap);
                }
            }
        }
    }
    return map;
}

?

private static void fillParamMapByReportFiled(Map<String, PdfFiledDto> map,
                                              Object formVal, ReportFiled reportFiled, String prefix, int index) {
    String formKey = reportFiled.value();
    if (StrUtil.isNotBlank(prefix)) {
        // 获取实际key
        formKey = handleFormKey(prefix, index, formKey);
    }
    if (formVal == null) {
        map.put(formKey, getPdfFiledDto("", -1.0f));
        return;
    }
    fillParamMapByObject(map, reportFiled, formKey, formVal, index);
}

?

??private static void fillParamMapByReportFiled(Map<String, PdfFiledDto> map,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Object formVal, ReportFiled reportFiled, String prefix, int index) {
? ? ? ? String formKey = reportFiled.value();
? ? ? ? if (StrUtil.isNotBlank(prefix)) {
? ? ? ? ? ? // 获取实际key
? ? ? ? ? ? formKey = handleFormKey(prefix, index, formKey);
? ? ? ? }
? ? ? ? if (formVal == null) {
? ? ? ? ? ? map.put(formKey, getPdfFiledDto("", -1.0f));
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? if (formVal.getClass().isArray()) {
? ? ? ? ? ? Object[] arr = (Object[]) formVal;
? ? ? ? ? ? for (int i = 0; i < arr.length; i++) {
? ? ? ? ? ? ? ? Object item = arr[i];
? ? ? ? ? ? ? ? fillParamMapByObject(map, reportFiled, handleFormKey(formKey, i, ""), item);
? ? ? ? ? ? }
? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? fillParamMapByObject(map, reportFiled, formKey, formVal);
? ? }

这里list集合,单独处理,加上文本域对应序号。?

private static String handleFormKey(String prefix, int index, String formKey) {
    prefix = prefix.endsWith(StrPool.UNDERLINE) ? prefix : prefix + StrPool.UNDERLINE;
    return StrUtil.isBlank(formKey) ?
            prefix + index
            : prefix + formKey + StrPool.UNDERLINE + index;
}

?

?

private static PdfFiledDto getPdfFiledDto(String val, float fontsize) {
    return new PdfFiledDto(val, fontsize, null);
}

private static PdfFiledDto getPdfFiledDto(String val, float fontsize, String fontPath) {
    return new PdfFiledDto(val, fontsize, fontPath);
}

?

?

文章来源:https://blog.csdn.net/weixin_53618357/article/details/135828242
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。