Merge remote-tracking branch 'origin/master'
# Conflicts: # cash-common/cash-common-api-config/src/main/java/com/czg/exception/CzgControllerAdvice.java
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
|
||||
|
||||
package com.czg.annotation;
|
||||
|
||||
import java.lang.annotation.*;
|
||||
|
||||
/**
|
||||
* 操作日志注解
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@Documented
|
||||
public @interface LogOperation {
|
||||
String value() default "";
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
|
||||
|
||||
package com.czg.exception;
|
||||
|
||||
import com.czg.resp.CzgRespCode;
|
||||
|
||||
/**
|
||||
* 自定义异常
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class CzgException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private int code;
|
||||
private String msg;
|
||||
|
||||
public CzgException(String msg) {
|
||||
super(msg);
|
||||
this.code = CzgRespCode.FAILURE.getCode();
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public CzgException(String msg, Throwable e) {
|
||||
super(msg, e);
|
||||
this.code = CzgRespCode.FAILURE.getCode();
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
|
||||
public void setMsg(String msg) {
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public void setCode(int code) {
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.czg.group;
|
||||
|
||||
/**
|
||||
* @author GYJoker
|
||||
*/
|
||||
public interface InsertGroup {
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
package com.czg.group;
|
||||
|
||||
/**
|
||||
* @author GYJoker
|
||||
*/
|
||||
public interface UpdateGroup {
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.czg.page;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 分页工具类
|
||||
*
|
||||
* @author Tankaikai tankaikai@aliyun.com
|
||||
*/
|
||||
@Data
|
||||
public class PageData<T> implements Serializable {
|
||||
/**
|
||||
* 总记录数
|
||||
*/
|
||||
private int total;
|
||||
|
||||
/**
|
||||
* 列表数据
|
||||
*/
|
||||
private List<T> list;
|
||||
|
||||
/**
|
||||
* 分页
|
||||
*
|
||||
* @param list 列表数据
|
||||
* @param total 总记录数
|
||||
*/
|
||||
public PageData(List<T> list, long total) {
|
||||
this.list = list;
|
||||
this.total = (int) total;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
|
||||
|
||||
package com.czg.utils;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.util.ArrayUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.czg.exception.CzgException;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 校验工具类
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class AssertUtil {
|
||||
|
||||
public static void isBlank(String str, String msg) {
|
||||
if (StrUtil.isBlank(str)) {
|
||||
throw new CzgException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isBlank(String str, String errorMsgTemplate, Object... params) {
|
||||
isBlank(str, StrUtil.format(errorMsgTemplate, params));
|
||||
}
|
||||
|
||||
public static void isNull(Object object, String msg) {
|
||||
if (object == null) {
|
||||
throw new CzgException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isNull(Object object, String errorMsgTemplate, Object... params) {
|
||||
isNull(object, StrUtil.format(errorMsgTemplate, params));
|
||||
}
|
||||
|
||||
public static void isArrayEmpty(Object[] array, String msg) {
|
||||
if (ArrayUtil.isEmpty(array)) {
|
||||
throw new CzgException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isArrayEmpty(Object[] array, String errorMsgTemplate, Object... params) {
|
||||
isArrayEmpty(array, StrUtil.format(errorMsgTemplate, params));
|
||||
}
|
||||
|
||||
public static void isListEmpty(List<?> list, String msg) {
|
||||
if (CollUtil.isEmpty(list)) {
|
||||
throw new CzgException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isListEmpty(List<?> list, String errorMsgTemplate, Object... params) {
|
||||
isListEmpty(list, StrUtil.format(errorMsgTemplate, params));
|
||||
}
|
||||
|
||||
public static void isMapEmpty(Map map, String msg) {
|
||||
if (MapUtil.isEmpty(map)) {
|
||||
throw new CzgException(msg);
|
||||
}
|
||||
}
|
||||
|
||||
public static void isMapEmpty(Map map, String errorMsgTemplate, Object... params) {
|
||||
isMapEmpty(map, StrUtil.format(errorMsgTemplate, params));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.czg.validator;
|
||||
|
||||
import com.czg.exception.CzgException;
|
||||
import jakarta.validation.ConstraintViolation;
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.context.support.ResourceBundleMessageSource;
|
||||
import org.springframework.validation.beanvalidation.MessageSourceResourceBundleLocator;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* hibernate-validator校验工具类
|
||||
* 参考文档:http://docs.jboss.org/hibernate/validator/6.0/reference/en-US/html_single/
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public class ValidatorUtil {
|
||||
|
||||
private static ResourceBundleMessageSource getMessageSource() {
|
||||
ResourceBundleMessageSource bundleMessageSource = new ResourceBundleMessageSource();
|
||||
bundleMessageSource.setDefaultEncoding("UTF-8");
|
||||
//bundleMessageSource.setBasenames("i18n/validation", "i18n/validation_common");
|
||||
return bundleMessageSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验对象
|
||||
* @param object 待校验对象
|
||||
* @param groups 待校验的组
|
||||
* @throws CzgException 校验不通过,则报CzgException异常
|
||||
*/
|
||||
public static void validateEntity(Object object, Class<?>... groups)
|
||||
throws CzgException {
|
||||
Locale.setDefault(LocaleContextHolder.getLocale());
|
||||
Validator validator = Validation.byDefaultProvider().configure().messageInterpolator(
|
||||
new ResourceBundleMessageInterpolator(new MessageSourceResourceBundleLocator(getMessageSource())))
|
||||
.buildValidatorFactory().getValidator();
|
||||
Set<ConstraintViolation<Object>> constraintViolations = validator.validate(object, groups);
|
||||
if (!constraintViolations.isEmpty()) {
|
||||
ConstraintViolation<Object> constraint = constraintViolations.iterator().next();
|
||||
throw new CzgException(constraint.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
package com.czg.validator.group;
|
||||
|
||||
/**
|
||||
* 默认 Group
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface DefaultGroup {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.czg.validator.group;
|
||||
|
||||
|
||||
import jakarta.validation.GroupSequence;
|
||||
|
||||
/**
|
||||
* 定义校验顺序,如果AddGroup组失败,则UpdateGroup组不会再校验
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@GroupSequence({InsertGroup.class, UpdateGroup.class})
|
||||
public interface Group {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
package com.czg.validator.group;
|
||||
|
||||
/**
|
||||
* 新增 Group
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface InsertGroup {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
|
||||
|
||||
package com.czg.validator.group;
|
||||
|
||||
/**
|
||||
* 修改 Group
|
||||
*
|
||||
* @author admin admin@cashier.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public interface UpdateGroup {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user