6 changed files with 173 additions and 10 deletions
@ -0,0 +1,13 @@ |
|||
package com.epmet.commons.tools.annotation; |
|||
|
|||
import java.lang.annotation.*; |
|||
|
|||
/** |
|||
* 标记一个接口,它会被报表服务所调用 |
|||
*/ |
|||
@Target(ElementType.METHOD) |
|||
@Retention(RetentionPolicy.RUNTIME) |
|||
@Documented |
|||
public @interface ReportRequest { |
|||
|
|||
} |
@ -0,0 +1,83 @@ |
|||
package com.epmet.commons.tools.aspect; |
|||
|
|||
import cn.hutool.core.bean.BeanUtil; |
|||
import com.epmet.commons.tools.exception.EpmetErrorCode; |
|||
import com.epmet.commons.tools.exception.EpmetException; |
|||
import com.epmet.commons.tools.redis.RedisUtils; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.apache.commons.lang3.StringUtils; |
|||
import org.aspectj.lang.JoinPoint; |
|||
import org.aspectj.lang.annotation.Aspect; |
|||
import org.aspectj.lang.annotation.Before; |
|||
import org.aspectj.lang.reflect.MethodSignature; |
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.core.annotation.Order; |
|||
import org.springframework.stereotype.Component; |
|||
import org.springframework.web.bind.annotation.RequestBody; |
|||
import org.springframework.web.context.request.RequestAttributes; |
|||
import org.springframework.web.context.request.RequestContextHolder; |
|||
import org.springframework.web.context.request.ServletRequestAttributes; |
|||
|
|||
import java.lang.reflect.Parameter; |
|||
import java.util.Map; |
|||
|
|||
/** |
|||
* 需要被报表服务请求的api,需要加上这个注解 |
|||
* 1.该注解会取url中固定的key,去redis获取参数,给入参dto复制 |
|||
*/ |
|||
@Aspect |
|||
@Component |
|||
@Order(20) |
|||
@Slf4j |
|||
public class ReportRequestAspect { |
|||
|
|||
/** |
|||
* 从redis中取参数用 |
|||
*/ |
|||
public static final String REPORT_REDIS_KEY = "paramKey"; |
|||
|
|||
@Autowired |
|||
private RedisUtils redisUtils; |
|||
|
|||
@Before("@annotation(com.epmet.commons.tools.annotation.ReportRequest)") |
|||
public void before(JoinPoint point) { |
|||
RequestAttributes ra = RequestContextHolder.getRequestAttributes(); |
|||
ServletRequestAttributes sra = (ServletRequestAttributes) ra; |
|||
String paramKey = sra.getRequest().getParameter(REPORT_REDIS_KEY); |
|||
if (StringUtils.isBlank(paramKey)) { |
|||
// 没有携带key参数,直接跳过
|
|||
return; |
|||
} |
|||
|
|||
Map<String, Object> cachedParams = redisUtils.hGetAll(paramKey); |
|||
if (cachedParams == null || cachedParams.size() == 0) { |
|||
log.warn("【报表服务】根据paramKey:{}未获取到有效的参数缓存。", paramKey); |
|||
throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "参数失效", "参数失效"); |
|||
// redis中没有此参数
|
|||
} |
|||
|
|||
// 使用方法签名获取出参数class列表
|
|||
Object[] args = point.getArgs(); |
|||
MethodSignature signature = (MethodSignature) point.getSignature(); |
|||
Parameter[] parameters = signature.getMethod().getParameters(); |
|||
fillArgsToRequestBody(args, cachedParams, parameters); |
|||
} |
|||
|
|||
/** |
|||
* 将redis中取出的参数,赋值到指定的入参dto上 |
|||
* @param args |
|||
* @param storedParams |
|||
*/ |
|||
private void fillArgsToRequestBody(Object[] args, Map<String, Object> storedParams, Parameter[] parameters) { |
|||
for (int i = 0; i < args.length; i++) { |
|||
Object arg = args[i]; |
|||
Class<?> argClazz = arg.getClass(); |
|||
RequestBody requestBodyAnno = parameters[i].getAnnotation(RequestBody.class); |
|||
if (arg != null && requestBodyAnno != null) { |
|||
Object argBean = BeanUtil.mapToBean(storedParams, arg.getClass(), true); |
|||
BeanUtil.copyProperties(argBean, arg); |
|||
return; |
|||
} |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,33 @@ |
|||
package com.epmet.dto.form; |
|||
|
|||
import lombok.Data; |
|||
|
|||
import javax.validation.constraints.NotBlank; |
|||
import java.io.Serializable; |
|||
import java.util.List; |
|||
|
|||
/** |
|||
* @Author zxc |
|||
* @DateTime 2022/8/8 14:04 |
|||
* @DESC |
|||
*/ |
|||
@Data |
|||
public class ReportEditFormDTO implements Serializable { |
|||
|
|||
private static final long serialVersionUID = -2157859106926125470L; |
|||
|
|||
public interface ReportEditForm{} |
|||
|
|||
/** |
|||
* 操作类型,批量操作:add;单个编辑:edit |
|||
*/ |
|||
@NotBlank(message = "type不能为空", groups = ReportEditForm.class) |
|||
private String type; |
|||
|
|||
@NotBlank(message = "reportId不能为空", groups = ReportEditForm.class) |
|||
private String reportId; |
|||
|
|||
private List<String> customerIds; |
|||
|
|||
private List<String> categoryKeys; |
|||
} |
Loading…
Reference in new issue