fix():格式规范修改

boot3.0
dongzeliang 2025-02-26 22:57:49 +08:00
parent fdd5cd030f
commit 3f32325c9b
9 changed files with 184 additions and 109 deletions

View File

@ -152,7 +152,12 @@ public @interface Excel {
*
*/
enum Type {
ALL(0), EXPORT(1), IMPORT(2);
// 所有
ALL(0),
// 仅导出
EXPORT(1),
// 仅导入
IMPORT(2);
private final int value;
Type (int value) {
@ -168,7 +173,18 @@ public @interface Excel {
*
*/
enum ColumnType {
NUMERIC(0), STRING(1), IMAGE(2);
/**
*
*/
NUMERIC(0),
/**
*
*/
STRING(1),
/**
*
*/
IMAGE(2);
private final int value;
ColumnType (int value) {

View File

@ -0,0 +1,44 @@
package com.muyu.common.core.constant;
/**
* @author dongzeliang
* @version 1.0
* @description:
* @date 2025/2/26 22:56
*/
public class FileConstants {
// GIF
public static final int PHOTO_BYTE_GIF_ZERO = 71;
// GIF
public static final int PHOTO_BYTE_GIF_ONE = 73;
// GIF
public static final int PHOTO_BYTE_GIF_TWO = 70;
// GIF
public static final int PHOTO_BYTE_GIF_THREE = 56;
// GIF
public static final int PHOTO_BYTE_GIF_FOUR = 55;
// GIF
public static final int PHOTO_BYTE_GIF_FOUR_OR = 57;
// GIF
public static final int PHOTO_BYTE_GIF_FIVE = 97;
// JPG
public static final int PHOTO_BYTE_JPG_SIX = 74;
// JPG
public static final int PHOTO_BYTE_JPG_SEVEN = 70;
// JPG
public static final int PHOTO_BYTE_JPG_EIGHT = 73;
// JPG
public static final int PHOTO_BYTE_JPG_NINE = 70;
// BMP
public static final int PHOTO_BYTE_BMP_ZERO = 66;
// BMP
public static final int PHOTO_BYTE_BMP_ONE = 77;
// PNG
public static final int PHOTO_BYTE_PNG_ONE = 80;
// PNG
public static final int PHOTO_BYTE_PNG_TWO = 78;
// PNG
public static final int PHOTO_BYTE_PNG_THREE = 71;
}

View File

@ -0,0 +1,28 @@
package com.muyu.common.core.constant;
/**
* @author dongzeliang
* @version 1.0
* @description: HTTP
* @date 2025/2/26 22:37
*/
public class HttpConstants {
// json请求
public static final String APPLICATION_JSON = "application/json";
// ACCEPT
public static final String ACCEPT = "accept";
// 请求
public static final String X_REQUESTED_WITH = "X-Requested-With";
// XMLHttpRequest
public static final String XML_HTTP_REQUEST = "XMLHttpRequest";
// 内容忽略
public static final String[] XML_HTTP_REQUEST_IGNORE = new String[]{".json", ".xml"};
// ajax
public static final String AJAX = "__ajax";
// ajax内容忽略
public static final String[] AJAX_IGNORE = new String[]{"json", "xml"};
}

View File

@ -12,7 +12,13 @@ import lombok.Getter;
@Getter
public enum SysWhetherEnum {
/**
*
*/
YES("Y", "是"),
/**
*
*/
NO("N", "否");
private final String code;
private final String info;

View File

@ -1,12 +1,26 @@
package com.muyu.common.core.enums;
import lombok.Getter;
/**
*
*
* @author muyu
*/
@Getter
public enum UserStatusEnum {
OK("0", "正常"), DISABLE("1", "停用"), DELETED("2", "删除");
/**
*
*/
OK("0", "正常"),
/**
*
*/
DISABLE("1", "停用"),
/**
*
*/
DELETED("2", "删除");
private final String code;
private final String info;
@ -16,11 +30,4 @@ public enum UserStatusEnum {
this.info = info;
}
public String getCode () {
return code;
}
public String getInfo () {
return info;
}
}

View File

@ -1,7 +1,6 @@
package com.muyu.common.core.utils;
import com.alibaba.fastjson2.JSON;
import com.muyu.common.core.constant.Constants;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.text.Convert;
import jakarta.servlet.ServletRequest;
@ -21,12 +20,14 @@ import reactor.core.publisher.Mono;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.*;
import static com.muyu.common.core.constant.HttpConstants.*;
import static org.springframework.http.HttpHeaders.ACCEPT;
/**
*
*
@ -37,42 +38,42 @@ public class ServletUtils {
* String
*/
public static String getParameter (String name) {
return getRequest().getParameter(name);
return Objects.requireNonNull(getRequest()).getParameter(name);
}
/**
* String
*/
public static String getParameter (String name, String defaultValue) {
return Convert.toStr(getRequest().getParameter(name), defaultValue);
return Convert.toStr(Objects.requireNonNull(getRequest()).getParameter(name), defaultValue);
}
/**
* Integer
*/
public static Integer getParameterToInt (String name) {
return Convert.toInt(getRequest().getParameter(name));
return Convert.toInt(Objects.requireNonNull(getRequest()).getParameter(name));
}
/**
* Integer
*/
public static Integer getParameterToInt (String name, Integer defaultValue) {
return Convert.toInt(getRequest().getParameter(name), defaultValue);
return Convert.toInt(Objects.requireNonNull(getRequest()).getParameter(name), defaultValue);
}
/**
* Boolean
*/
public static Boolean getParameterToBool (String name) {
return Convert.toBool(getRequest().getParameter(name));
return Convert.toBool(Objects.requireNonNull(getRequest()).getParameter(name));
}
/**
* Boolean
*/
public static Boolean getParameterToBool (String name, Boolean defaultValue) {
return Convert.toBool(getRequest().getParameter(name), defaultValue);
return Convert.toBool(Objects.requireNonNull(getRequest()).getParameter(name), defaultValue);
}
/**
@ -170,8 +171,8 @@ public class ServletUtils {
public static void renderString (HttpServletResponse response, String string) {
try {
response.setStatus(200);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.setContentType(APPLICATION_JSON);
response.setCharacterEncoding(StandardCharsets.UTF_8);
response.getWriter().print(string);
} catch (IOException e) {
e.printStackTrace();
@ -181,26 +182,26 @@ public class ServletUtils {
/**
* Ajax
*
* @param request
* @param request
*/
public static boolean isAjaxRequest (HttpServletRequest request) {
String accept = request.getHeader("accept");
if (accept != null && accept.contains("application/json")) {
String accept = request.getHeader(ACCEPT);
if (accept != null && accept.contains(APPLICATION_JSON)) {
return true;
}
String xRequestedWith = request.getHeader("X-Requested-With");
if (xRequestedWith != null && xRequestedWith.contains("XMLHttpRequest")) {
String xRequestedWith = request.getHeader(X_REQUESTED_WITH);
if (xRequestedWith != null && xRequestedWith.contains(XML_HTTP_REQUEST)) {
return true;
}
String uri = request.getRequestURI();
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml")) {
if (StringUtils.inStringIgnoreCase(uri, XML_HTTP_REQUEST_IGNORE)) {
return true;
}
String ajax = request.getParameter("__ajax");
return StringUtils.inStringIgnoreCase(ajax, "json", "xml");
String ajax = request.getParameter(AJAX);
return StringUtils.inStringIgnoreCase(ajax, AJAX_IGNORE);
}
/**
@ -211,11 +212,7 @@ public class ServletUtils {
* @return
*/
public static String urlEncode (String str) {
try {
return URLEncoder.encode(str, Constants.UTF8);
} catch (UnsupportedEncodingException e) {
return StringUtils.EMPTY;
}
return URLEncoder.encode(str, StandardCharsets.UTF_8);
}
/**

View File

@ -311,9 +311,9 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
}
StringBuilder sb = new StringBuilder();
// 前置字符是否大写
boolean preCharIsUpperCase = true;
boolean preCharIsUpperCase;
// 当前字符是否大写
boolean curreCharIsUpperCase = true;
boolean curreCharIsUpperCase;
// 下一字符是否大写
boolean nexteCharIsUpperCase = true;
for (int i = 0 ; i < str.length() ; i++) {
@ -345,13 +345,13 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*
*
* @param str
* @param strs
* @param strArr
*
* @return true
*/
public static boolean inStringIgnoreCase (String str, String... strs) {
if (str != null && strs != null) {
for (String s : strs) {
public static boolean inStringIgnoreCase (String str, String... strArr) {
if (str != null && strArr != null) {
for (String s : strArr) {
if (str.equalsIgnoreCase(trim(s))) {
return true;
}
@ -448,8 +448,6 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*
* @param pattern
* @param url url
*
* @return
*/
public static boolean isMatch (String pattern, String url) {
AntPathMatcher matcher = new AntPathMatcher();
@ -469,7 +467,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*
* @return
*/
public static final String padl (final Number num, final int size) {
public static String padl(final Number num, final int size) {
return padl(num.toString(), size, '0');
}
@ -482,7 +480,7 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*
* @return
*/
public static final String padl (final String s, final int size, final char c) {
public static String padl(final String s, final int size, final char c) {
final StringBuilder sb = new StringBuilder(size);
if (s != null) {
final int len = s.length();

View File

@ -7,6 +7,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.util.Objects;
import static com.muyu.common.core.constant.FileConstants.*;
/**
*
*
@ -53,7 +55,7 @@ public class FileTypeUtils {
*
* @return
*/
public static final String getExtension (MultipartFile file) {
public static String getExtension(MultipartFile file) {
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
if (StringUtils.isEmpty(extension)) {
extension = MimeTypeUtils.getExtension(Objects.requireNonNull(file.getContentType()));
@ -70,14 +72,14 @@ public class FileTypeUtils {
*/
public static String getFileExtendName (byte[] photoByte) {
String strFileExtendName = "JPG";
if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
&& ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
if ((photoByte[0] == PHOTO_BYTE_GIF_ZERO) && (photoByte[1] == PHOTO_BYTE_GIF_ONE) && (photoByte[2] == PHOTO_BYTE_GIF_TWO) && (photoByte[3] == PHOTO_BYTE_GIF_THREE)
&& ((photoByte[4] == PHOTO_BYTE_GIF_FOUR) || (photoByte[4] == PHOTO_BYTE_GIF_FOUR_OR)) && (photoByte[5] == PHOTO_BYTE_GIF_FIVE)) {
strFileExtendName = "GIF";
} else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
} else if ((photoByte[6] == PHOTO_BYTE_JPG_SIX) && (photoByte[7] == PHOTO_BYTE_JPG_SEVEN) && (photoByte[8] == PHOTO_BYTE_JPG_EIGHT) && (photoByte[9] == PHOTO_BYTE_JPG_NINE)) {
strFileExtendName = "JPG";
} else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
} else if ((photoByte[0] == PHOTO_BYTE_BMP_ZERO) && (photoByte[1] == PHOTO_BYTE_BMP_ONE)) {
strFileExtendName = "BMP";
} else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
} else if ((photoByte[1] == PHOTO_BYTE_PNG_ONE) && (photoByte[2] == PHOTO_BYTE_PNG_TWO) && (photoByte[3] == PHOTO_BYTE_PNG_THREE)) {
strFileExtendName = "PNG";
}
return strFileExtendName;

View File

@ -154,7 +154,7 @@ public class ExcelUtil<T> {
if (StringUtils.containsAny(propertyValue, separator)) {
for (String value : propertyValue.split(separator)) {
if (itemArray[0].equals(value)) {
propertyString.append(itemArray[1] + separator);
propertyString.append(itemArray[1]).append(separator);
break;
}
}
@ -184,7 +184,7 @@ public class ExcelUtil<T> {
if (StringUtils.containsAny(propertyValue, separator)) {
for (String value : propertyValue.split(separator)) {
if (itemArray[1].equals(value)) {
propertyString.append(itemArray[0] + separator);
propertyString.append(itemArray[0]).append(separator);
break;
}
}
@ -201,8 +201,6 @@ public class ExcelUtil<T> {
* Excel
*
* @param fields ["name"/"id","name"]
*
* @throws Exception
*/
public void hideColumn (String... fields) {
this.excludeFields = fields;
@ -275,7 +273,7 @@ public class ExcelUtil<T> {
* @return
*/
public List<T> importExcel (InputStream is) {
List<T> list = null;
List<T> list;
try {
list = importExcel(is, 0);
} catch (Exception e) {
@ -393,7 +391,6 @@ public class ExcelUtil<T> {
} else if (Boolean.TYPE == fieldType || Boolean.class == fieldType) {
val = Convert.toBool(val, false);
}
if (StringUtils.isNotNull(fieldType)) {
String propertyName = field.getName();
if (StringUtils.isNotEmpty(attr.targetAttr())) {
propertyName = field.getName() + "." + attr.targetAttr();
@ -405,7 +402,6 @@ public class ExcelUtil<T> {
}
ReflectUtils.invokeSetter(entity, propertyName, val);
}
}
list.add(entity);
}
}
@ -418,8 +414,6 @@ public class ExcelUtil<T> {
* @param response
* @param list
* @param sheetName
*
* @return
*/
public void exportExcel (HttpServletResponse response, List<T> list, String sheetName) {
exportExcel(response, list, sheetName, StringUtils.EMPTY);
@ -433,7 +427,6 @@ public class ExcelUtil<T> {
* @param sheetName
* @param title
*
* @return
*/
public void exportExcel (HttpServletResponse response, List<T> list, String sheetName, String title) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
@ -446,8 +439,6 @@ public class ExcelUtil<T> {
* listexcel
*
* @param sheetName
*
* @return
*/
public void importTemplateExcel (HttpServletResponse response, String sheetName) {
importTemplateExcel(response, sheetName, StringUtils.EMPTY);
@ -458,8 +449,6 @@ public class ExcelUtil<T> {
*
* @param sheetName
* @param title
*
* @return
*/
public void importTemplateExcel (HttpServletResponse response, String sheetName, String title) {
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
@ -470,8 +459,6 @@ public class ExcelUtil<T> {
/**
* listexcel
*
* @return
*/
public void exportExcel (HttpServletResponse response) {
try {
@ -510,7 +497,7 @@ public class ExcelUtil<T> {
}
}
if (Type.EXPORT.equals(type)) {
fillExcelData(index, row);
fillExcelData(index);
addStatisticsRow();
}
}
@ -520,18 +507,17 @@ public class ExcelUtil<T> {
* excel
*
* @param index
* @param row
*/
@SuppressWarnings("unchecked")
public void fillExcelData (int index, Row row) {
public void fillExcelData (int index) {
int startNo = index * SHEET_SIZE;
int endNo = Math.min(startNo + SHEET_SIZE, list.size());
int rowNo = (1 + rownum) - startNo;
for (int i = startNo ; i < endNo ; i++) {
rowNo = isSubList() ? (i > 1 ? rowNo + 1 : rowNo + i) : i + 1 + rownum - startNo;
row = sheet.createRow(rowNo);
Row row = sheet.createRow(rowNo);
// 得到导出对象.
T vo = (T) list.get(i);
T vo = list.get(i);
Collection<?> subList = null;
if (isSubList()) {
if (isSubListValue(vo)) {
@ -702,7 +688,7 @@ public class ExcelUtil<T> {
Cell cell = row.createCell(column);
// 写入列信息
cell.setCellValue(attr.name());
setDataValidation(attr, row, column);
setDataValidation(attr, column);
cell.setCellStyle(styles.get(StringUtils.format("header_{}_{}", attr.headerColor(), attr.headerBackgroundColor())));
if (isSubList()) {
// 填充默认样式,防止合并单元格样式失效
@ -762,7 +748,7 @@ public class ExcelUtil<T> {
/**
*
*/
public void setDataValidation (Excel attr, Row row, int column) {
public void setDataValidation (Excel attr, int column) {
if (attr.name().contains("注:")) {
sheet.setColumnWidth(column, 6000);
} else {
@ -772,7 +758,7 @@ public class ExcelUtil<T> {
if (StringUtils.isNotEmpty(attr.prompt()) || attr.combo().length > 0) {
if (attr.combo().length > 15 || StringUtils.join(attr.combo()).length() > 255) {
// 如果下拉数大于15或字符串长度大于255则使用一个新sheet存储避免生成的模板下拉值获取不到
setXSSFValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
setXssfValidationWithHidden(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
} else {
// 提示信息或只能选择不能输入的列内容.
setPromptOrValidation(sheet, attr.combo(), attr.prompt(), 1, 100, column, column);
@ -784,7 +770,7 @@ public class ExcelUtil<T> {
*
*/
public void addCell (Excel attr, Row row, T vo, Field field, int column) {
Cell cell = null;
Cell cell;
try {
// 设置行高
row.setHeight(maxHeight);
@ -858,24 +844,24 @@ public class ExcelUtil<T> {
* ,.
*
* @param sheet sheet.
* @param textlist
* @param textList
* @param promptContent
* @param firstRow
* @param endRow
* @param firstCol
* @param endCol
*/
public void setXSSFValidationWithHidden (Sheet sheet, String[] textlist, String promptContent, int firstRow, int endRow, int firstCol, int endCol) {
public void setXssfValidationWithHidden(Sheet sheet, String[] textList, String promptContent, int firstRow, int endRow, int firstCol, int endCol) {
String hideSheetName = "combo_" + firstCol + "_" + endCol;
// 用于存储 下拉菜单数据
Sheet hideSheet = wb.createSheet(hideSheetName);
for (int i = 0 ; i < textlist.length ; i++) {
hideSheet.createRow(i).createCell(0).setCellValue(textlist[i]);
for (int i = 0 ; i < textList.length ; i++) {
hideSheet.createRow(i).createCell(0).setCellValue(textList[i]);
}
// 创建名称,可被其他单元格引用
Name name = wb.createName();
name.setNameName(hideSheetName + "_data");
name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textlist.length);
name.setRefersToFormula(hideSheetName + "!$A$1:$A$" + textList.length);
DataValidationHelper helper = sheet.getDataValidationHelper();
// 加载下拉列表内容
DataValidationConstraint constraint = helper.createFormulaListConstraint(hideSheetName + "_data");
@ -906,13 +892,11 @@ public class ExcelUtil<T> {
*
* @param value
* @param excel
*
* @return
*/
public String dataFormatHandlerAdapter (Object value, Excel excel, Cell cell) {
try {
Object instance = excel.handler().getDeclaredConstructor().newInstance();
Method formatMethod = excel.handler().getMethod("format", new Class[]{Object.class, String[].class, Cell.class, Workbook.class});
Method formatMethod = excel.handler().getMethod("format", Object.class, String[].class, Cell.class, Workbook.class);
value = formatMethod.invoke(instance, value, excel.args(), cell, this.wb);
} catch (Exception e) {
log.error("不能格式化数据 「{}-{}」", excel.handler() , e.getMessage());
@ -931,7 +915,7 @@ public class ExcelUtil<T> {
}
try {
temp = Double.valueOf(text);
} catch (NumberFormatException e) {
} catch (NumberFormatException ignored) {
}
statistics.put(index, statistics.get(index) + temp);
}
@ -941,7 +925,7 @@ public class ExcelUtil<T> {
*
*/
public void addStatisticsRow () {
if (statistics.size() > 0) {
if (!statistics.isEmpty()) {
Row row = sheet.createRow(sheet.getLastRowNum() + 1);
Set<Integer> keys = statistics.keySet();
Cell cell = row.createCell(0);
@ -965,8 +949,6 @@ public class ExcelUtil<T> {
* @param excel
*
* @return
*
* @throws Exception
*/
private Object getTargetValue (T vo, Field field, Excel excel) throws Exception {
Object o = field.get(vo);
@ -987,12 +969,10 @@ public class ExcelUtil<T> {
/**
* get
*
* @param o
* @param name
* @param o
* @param name
*
* @return value
*
* @throws Exception
* @return value
*/
private Object getValue (Object o, String name) throws Exception {
if (StringUtils.isNotNull(o) && StringUtils.isNotEmpty(name)) {
@ -1043,8 +1023,7 @@ public class ExcelUtil<T> {
Excels attrs = field.getAnnotation(Excels.class);
Excel[] excels = attrs.value();
for (Excel attr : excels) {
if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr())
&& (attr != null && (attr.type() == Type.ALL || attr.type() == type))) {
if (!ArrayUtils.contains(this.excludeFields, field.getName() + "." + attr.targetAttr()) && (attr.type() == Type.ALL || attr.type() == type)) {
field.setAccessible(true);
fields.add(new Object[]{field, attr});
}
@ -1102,7 +1081,7 @@ public class ExcelUtil<T> {
*/
public Object getCellValue (Row row, int column) {
if (row == null) {
return row;
return null;
}
Object val = "";
try {
@ -1139,8 +1118,6 @@ public class ExcelUtil<T> {
*
*
* @param row
*
* @return
*/
private boolean isRowEmpty (Row row) {
if (row == null) {
@ -1184,14 +1161,14 @@ public class ExcelUtil<T> {
*
*/
public boolean isSubList () {
return StringUtils.isNotNull(subFields) && subFields.size() > 0;
return StringUtils.isNotNull(subFields) && !subFields.isEmpty();
}
/**
*
*/
public boolean isSubListValue (T vo) {
return StringUtils.isNotNull(subFields) && subFields.size() > 0 && StringUtils.isNotNull(getListCellValue(vo)) && getListCellValue(vo).size() > 0;
return StringUtils.isNotNull(subFields) && !subFields.isEmpty() && StringUtils.isNotNull(getListCellValue(vo)) && !getListCellValue(vo).isEmpty();
}
/**
@ -1200,7 +1177,7 @@ public class ExcelUtil<T> {
public Collection<?> getListCellValue (Object obj) {
Object value;
try {
value = subMethod.invoke(obj, new Object[]{});
value = subMethod.invoke(obj);
} catch (Exception e) {
return new ArrayList<>();
}
@ -1221,7 +1198,7 @@ public class ExcelUtil<T> {
getMethodName.append(name.substring(1));
Method method = null;
try {
method = pojoClass.getMethod(getMethodName.toString(), new Class[]{});
method = pojoClass.getMethod(getMethodName.toString());
} catch (Exception e) {
log.error("获取对象异常{}", e.getMessage());
}