• aop编程,自定义注解参数和方法范围


    
    import cn.zk.czgj.common.annotation.KwMessageAnno;
    import cn.zk.czgj.common.annotation.KwMessageParam;
    import cn.zk.czgj.common.dto.homework.KwHomeworkSaveDto;
    import cn.zk.czgj.common.dto.homework.UserClassIdDto;
    import cn.zk.czgj.common.model.message.KwMessage;
    import cn.zk.czgj.common.model.message.KwMessageUser;
    import cn.zk.czgj.common.model.notice.KwNotice;
    import cn.zk.czgj.common.util.RequestUtils;
    import cn.zk.czgj.common.util.StringUtil;
    import cn.zk.czgj.common.vo.student.StudentViewVo;
    import cn.zk.czgj.common.vo.system.SysUserVo;
    import cn.zk.czgj.dao.mapper.message.KwMessageMapper;
    import cn.zk.czgj.dao.mapper.message.KwMessageUserMapper;
    import cn.zk.czgj.miniprogram.controller.util.UserUtil;
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
    import org.aspectj.lang.JoinPoint;
    import org.aspectj.lang.annotation.*;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.transaction.annotation.Transactional;
    
    import java.lang.annotation.Annotation;
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.stream.Collectors;
    
    
    @Aspect
    @Component
    public class KwMessageAspect {
    
        @Autowired
        private KwMessageMapper kwMessageMapper;
        @Autowired
        private KwMessageUserMapper kwMessageUserMapper;
    
        @Pointcut("@annotation(cn.zk.czgj.common.annotation.KwMessageAnno)")
        public void kwMessageAspect(){}
    
        @Before("kwMessageAspect()")
        public void before(JoinPoint joinPoint){
        }
    
        @After("kwMessageAspect()")
        public void after(JoinPoint joinPoint) throws ClassNotFoundException {
        }
    
        @AfterReturning(returning = "res", pointcut = "kwMessageAspect()")
        @Transactional(rollbackFor = Exception.class)
        public void afterReturning(JoinPoint joinPoint,Object res) throws ClassNotFoundException {
            MethodSignature methodSignature = (MethodSignature)joinPoint.getSignature();
            Method method = methodSignature.getMethod();
            KwMessageAnno kwMessageAnno = (KwMessageAnno)method.getAnnotation(KwMessageAnno.class);
            int msgType = kwMessageAnno.msgType();
            String operaVal = kwMessageAnno.value();
    
            Annotation[][] an = method.getParameterAnnotations();
            Class<?>[] paramClazzs = method.getParameterTypes();
            Object[] arguments = joinPoint.getArgs();
            int paramIndex = 0;
            int outerId = 0;
            Integer userId = null;
            int userType = 0;
            SysUserVo sysUserVo = null;
            StudentViewVo studentViewVo = null;
            KwHomeworkSaveDto kwHomeworkSaveDto = null;
            KwNotice kwNoticeUsers = null;
            for (Class<?> paramClazz : paramClazzs) {
                Annotation[] paramAns = an[paramIndex];
                for (Annotation paramAn : paramAns) {
                    if (KwMessageParam.class.isAssignableFrom(paramAn.getClass())){
                        KwMessageParam mp = (KwMessageParam) paramAn;
                        String desc = mp.description();
                        if (desc.equals("outerId")){
                            outerId = (int)arguments[paramIndex];
                        }else if (desc.equals("userId")){
                            userId = (int)arguments[paramIndex];
                        } else if(desc.equals("userType")) {
                            userType = (int)arguments[paramIndex];
                        }else if(desc.equals("sysUserVo")) {
                            sysUserVo = (SysUserVo)arguments[paramIndex];
                        }else if(desc.equals("studentViewVo")) {
                            studentViewVo = (StudentViewVo)arguments[paramIndex];
                        } else {
                            if (desc.equals("homeworkUserList")){
                                kwHomeworkSaveDto = (KwHomeworkSaveDto)arguments[paramIndex];
                            }else if (desc.equals("noticeUserList")){
                                kwNoticeUsers = (KwNotice)arguments[paramIndex];
                            }
                        }
    
                    }
                }
                paramIndex++;
            }
    
            if (msgType==9){
                if (userType==1){
                    userId = sysUserVo.getId();
                }else if (userType==2){
                    userId = studentViewVo.getUserid();
                }
            }
    
            if ("insert".equals(operaVal)){
                List<Integer> userIdList = new ArrayList<>();
    
                if (msgType==1){ // 作业类型
                    userIdList =
                            kwHomeworkSaveDto.getUserList().stream().map(UserClassIdDto::getUserId).collect(Collectors.toList());
                }else if (msgType==9){ // 通知类型
                    userIdList = kwNoticeUsers.getUserIds();
                }
                String token = RequestUtils.getParams("token");
                if(!StringUtil.isEmpty(token)) {
                    SysUserVo user = UserUtil.newInstance().getCurrentUser(token);
                    userIdList.add(user.getId());
                }
    
                Integer retMsgId = (Integer)res;
                KwMessage dbKwMessage = KwMessage.builder()
                        .outerId(retMsgId)
                        .type(msgType).build();
                kwMessageMapper.insert(dbKwMessage);
                if (userIdList!=null){
                    for (Integer uId : userIdList) {
                        KwMessageUser kwMessageUser = KwMessageUser.builder().userId(uId).messageId(dbKwMessage.getId()).readStatus(0).top(0).build();
                        kwMessageUserMapper.insert(kwMessageUser);
                    }
                }
            }else if ("update".equals(operaVal)){ // 更新消息阅读状态
                KwMessage kwMessage = kwMessageMapper.selectOne(new QueryWrapper<KwMessage>()
                        .eq("outer_id", outerId)
                        .eq("type", msgType));
                Integer msgId = kwMessage.getId();
                KwMessageUser kwMessageUser = KwMessageUser.builder().readStatus(1).build();
                UpdateWrapper<KwMessageUser> msgUserUpdateWrapper =
                        new UpdateWrapper<KwMessageUser>()
                                .eq("message_id", msgId).eq("user_id",userId);
                kwMessageUserMapper.update(kwMessageUser,msgUserUpdateWrapper);
            }
    
    
        }
    
        @AfterThrowing(pointcut = "kwMessageAspect()", throwing = "e")
        public void doAfterThrowing(JoinPoint joinPoint, Throwable e) {
            e.printStackTrace();
        }
    
    
    }
    
    
  • 相关阅读:
    (计算几何 线段判交) 51nod1264 线段相交
    (线段判交的一些注意。。。)nyoj 1016-德莱联盟
    Spring的事务管理
    Spring JDBC模版以及三种数据库连接池的使用
    Springmvc架构
    AspectJ用注解替换xml配置
    在eclipse中spring的xml配置文件标签中class路径全限定名自动提示设置
    给属性字符串添加下划线
    检测程序是否打开
    系统目录
  • 原文地址:https://www.cnblogs.com/kltsee/p/14504479.html
Copyright © 2020-2023  润新知