• 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();
        }
    
    
    }
    
    
  • 相关阅读:
    EasyNVR平台使用Onvif探测设备时的操作注意事项
    视频平台EasyNVR更新版本后视频播放不了,识别不了js是什么原因?
    EasyNVR配置连接EasyNVS无法连接,报错timeout是什么原因?
    wNAF不相邻表示形式
    密码的前情知识
    64位编译器下基本数据类型所占字节数目
    gitlab禁止用户自己删除帐号
    Python中类创建和实例化过程 勿忘
    Qt5msvc2012编译安装
    kubuntu中ibus选词窗口不能正常弹出问题人解决办法
  • 原文地址:https://www.cnblogs.com/kltsee/p/14504479.html
Copyright © 2020-2023  润新知