后续会加上详细注释,或者出一个贴图详细的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 ListforecastDataDTOList; }
/** * @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 MapgetParamMap(Object param, String prefix, int index) { Map 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
private static void fillParamMapByReportFiled(Mapmap, 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
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); }