公众号:架构师与哈苏
关注公众号进入it交流群! 公众号:架构师与哈苏 不定时都会推送一些实用的干货。。。

注解:EnableDimEnhance

package com.comma.teeth.enhance.dim.annotation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * <p>
 *
 * </p>
 *
 * @author: GoslingWu
 * @date: 2022-03-08
 */
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface EnableDimEnhance {

    boolean enable() default true;

}

AOP:DimEnhanceAop

package com.comma.teeth.conf;

import com.comma.teeth.common.vo.Paging;
import com.comma.teeth.enhance.dim.annotation.EnableDimEnhance;
import com.comma.teeth.enhance.dim.service.DimEnhanceService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;

/**
 * <p>
 *
 * </p>
 *
 * @author: GoslingWu
 * @date: 2022-03-08
 */
@Data
@Slf4j
@Aspect
@Component
public class DimEnhanceAop {

    /**
     * 切点 全部service方法
     */
    //private static final String POINTCUT = "execution(public * com.comma.teeth.*.service.impl..*.*(..))";


    @Autowired
    private DimEnhanceService dimEnhanceService;

    @Pointcut("@annotation(com.comma.teeth.enhance.dim.annotation.EnableDimEnhance)")
    public void dimEnhanceAspect() {
    }

    @AfterReturning(pointcut = "dimEnhanceAspect()", returning = "result")
    public void afterReturning(JoinPoint joinPoint, Object result) {
        if (result == null) {
            return;
        }

        // 方式1:service方法上开启自动注入
        {
            // 获取注解
            MethodSignature signature =
                    (MethodSignature) joinPoint.getSignature();
            // 获取当前方法被注解注释的注解对象
            EnableDimEnhance annotation =
                    signature.getMethod().getAnnotation(EnableDimEnhance.class);
            if (annotation.enable()) {
                if (result instanceof Paging) {
                    Paging iPage = (Paging) result;
                    //通过反射获取指定属性上的注解,@SysDictField(code="1",valueFieldName="typeName")去自动注入
                    dimEnhanceService.autoFillValue(iPage.getRecords());
                } else if (result instanceof Collection) {
                    dimEnhanceService.autoFillValue((Collection) result);
                } else{
                    dimEnhanceService.autoFillValue(result);
                }
            }
        }

        // 方式2:返回实体类上开启自动注入
        /*{
            if (result instanceof Paging) {
                Paging iPage = (Paging) result;
                List records = iPage.getRecords();
                if(CollectionUtil.isNotEmpty(records)){
                    Object o = records.get(0);
                    EnableDimEnhance annotation = o.getClass().getAnnotation(EnableDimEnhance.class);
                    if(annotation.enable()){
                        dimEnhanceService.autoFillValue(iPage.getRecords());
                    }
                }
            } else {
                EnableDimEnhance annotation = result.getClass().getAnnotation(EnableDimEnhance.class);
                if(annotation.enable()){
                    dimEnhanceService.autoFillValue(result);
                }
            }
        }*/
        log.info("DimEnhanceAop-------afterReturning:{}", result);
    }

}

SysDictField

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface SysDictField {

    String code();

    /**
     * 用来指定自动填充时的字段,valueField必须是字符串类型。
     * <p>
     * 默认为:注解所在字段名 + "Name"
     *
     * @return .
     */
    String valueFieldName() default "";

}
@Data
@Builder
public class CounselorFollowRecordQueryVo implements Serializable{
    private static final long serialVersionUID = 1L;

    @SysDictField(code = SysDictConstant.COMMUNICATION, valueFieldName = "typeName")
    @ApiModelProperty(value = "跟进方式id")
    private Integer typeId;

    @ApiModelProperty(value = "跟进方式")
    private String typeName;
}

使用

@Override
//使用注解标识,会进入AOP,把Vo对象中的@SysDictField属性标识指定的valueFieldName名称填充进去,通过反射(sql中typeId是要查询出来的,反射只填充name)
//我们有些表中有crrentUserId,一样可以通过自定义一个@UserField来实现UserName的填充
@EnableDimEnhance
public Paging<CounselorFollowRecordQueryVo> getCounselorFollowRecordPageList(CounselorFollowRecordQueryParam param) throws Exception {
    Page page = setPageParam(param, OrderItem.desc("id"));
    IPage<CounselorFollowRecordQueryVo> iPage = counselorFollowRecordMapper.getCounselorFollowRecordPageList(page, param, labelSearchParam);
    return new Paging<>(iPage);
}
posted on 2022-03-08 16:16  公众号/架构师与哈苏  阅读(68)  评论(0编辑  收藏  举报