package cn.com.goldenwater.dcproj.aop; import cn.com.goldenwater.dcproj.dao.SupervisionPlanDao; import cn.com.goldenwater.dcproj.dto.LgtdAndLttd; import cn.com.goldenwater.dcproj.model.BisInspAllObj; import cn.com.goldenwater.dcproj.service.BisInspAllObjService; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @Aspect @Component public class BisInspAllObjAop { private Logger logger = LoggerFactory.getLogger(getClass()); @Autowired private BisInspAllObjService bisInspAllObjService; @Autowired private SupervisionPlanDao supervisionPlanDao; /** * 这里我们使用注解的形式 * 当然,我们也可以通过切点表达式直接指定需要拦截的package,需要拦截的class 以及 method * 切点表达式: execution(...) */ @Pointcut("execution(* *..BisInspAllObjDao.insert(..))") public void pointCut() { } /** * 环绕通知 @Around , 当然也可以使用 @Before (前置通知) @After (后置通知) * * @param point * @return * @throws Throwable */ @Around("pointCut()") public Object around(ProceedingJoinPoint point) throws Throwable { long beginTime = System.currentTimeMillis(); Object result = point.proceed(); long time = System.currentTimeMillis() - beginTime; try { updateObjAdCode(point); } catch (Exception e) { logger.error(e.getMessage()); } return result; } /** * 保存日志 * * @param joinPoint */ private void updateObjAdCode(ProceedingJoinPoint joinPoint) { //请求的参数 Object[] args = joinPoint.getArgs(); BisInspAllObj bisInspAllObj = (BisInspAllObj) args[0]; if (StringUtils.isNotBlank(bisInspAllObj.getPtype()) && StringUtils.isNotBlank(bisInspAllObj.getCode())) { String ptype = bisInspAllObj.getPtype(); if (bisInspAllObj.getPtype().length() == 1) { ptype = "00" + ptype; } if (bisInspAllObj.getPtype().length() == 2) { ptype = "0" + ptype; } LgtdAndLttd ld = supervisionPlanDao.getLgtdAndLttd(bisInspAllObj.getCode(), ptype); bisInspAllObj.setObjAdCode(ld.getAdCode()); bisInspAllObjService.update(bisInspAllObj); } } }