CotentAop.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package net.mingsoft.cms.aop;
  2. import net.mingsoft.basic.aop.BaseAop;
  3. import net.mingsoft.basic.util.BasicUtil;
  4. import net.mingsoft.cms.biz.IContentBiz;
  5. import net.mingsoft.cms.biz.IHistoryLogBiz;
  6. import net.mingsoft.cms.entity.ContentEntity;
  7. import net.mingsoft.cms.entity.HistoryLogEntity;
  8. import org.apache.commons.lang3.StringUtils;
  9. import org.aspectj.lang.ProceedingJoinPoint;
  10. import org.aspectj.lang.annotation.Around;
  11. import org.aspectj.lang.annotation.Aspect;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.stereotype.Component;
  14. /**
  15. * @author 铭飞开源团队
  16. * @date 2019/12/23
  17. */
  18. @Component
  19. @Aspect
  20. public class CotentAop extends BaseAop {
  21. /**
  22. * 注入文章业务
  23. */
  24. @Autowired
  25. private IContentBiz contentBiz;
  26. /**
  27. * 注入浏览记录业务
  28. */
  29. @Autowired
  30. private IHistoryLogBiz historyLogBiz;
  31. /**
  32. * 文章浏览记录,
  33. * 如果该文章该ip已经记录过,则不在重复记录
  34. * @param pjp
  35. * @return
  36. * @throws Throwable
  37. */
  38. @Around("execution(* net.mingsoft.cms.action.web.ContentAction.get(..))")
  39. public Object get(ProceedingJoinPoint pjp) throws Throwable{
  40. // 获取方法参数
  41. ContentEntity content = getType(pjp, ContentEntity.class);
  42. // 如果id为空则直接发行
  43. if(content.getId()==null) {
  44. return pjp.proceed();
  45. }
  46. content = (ContentEntity)contentBiz.getEntity(Integer.parseInt(content.getId()));
  47. //如果文章不存在则直接发行
  48. if(content == null){
  49. return pjp.proceed();
  50. }
  51. //查询判断该ip是否已经有浏览记录了
  52. HistoryLogEntity historyLog = new HistoryLogEntity();
  53. historyLog.setContentId(content.getId());
  54. historyLog.setHlIp(BasicUtil.getIp());
  55. historyLog.setHlIsMobile(BasicUtil.isMobileDevice());
  56. HistoryLogEntity _historyLog = (HistoryLogEntity)historyLogBiz.getEntity(historyLog);
  57. //如果该ip该文章没有浏览记录则保存浏览记录
  58. //并且更新点击数
  59. if(_historyLog == null || StringUtils.isBlank(_historyLog.getId())){
  60. historyLogBiz.saveEntity(historyLog);
  61. //更新点击数
  62. ContentEntity updateContent = new ContentEntity();
  63. updateContent.setId(content.getId());
  64. if(content.getContentHit() == null){
  65. updateContent.setContentHit(1);
  66. }else{
  67. updateContent.setContentHit(content.getContentHit()+1);
  68. }
  69. contentBiz.updateEntity(updateContent);
  70. }
  71. return pjp.proceed();
  72. }
  73. }