StudyService.java 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. package com.ruoyi.zzb.study.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  4. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.ruoyi.common.annotation.DataSource;
  7. import com.ruoyi.common.core.domain.entity.SysUser;
  8. import com.ruoyi.common.enums.DataSourceType;
  9. import com.ruoyi.common.utils.StringUtils;
  10. import com.ruoyi.zzb.study.domain.StudyDocInfo;
  11. import com.ruoyi.zzb.study.domain.StudyViewLogInfo;
  12. import com.ruoyi.zzb.study.domain.UserSearchParam;
  13. import com.ruoyi.zzb.study.domain.req.SaveViewLogVO;
  14. import com.ruoyi.zzb.study.domain.req.UpdatePageViewNumVO;
  15. import com.ruoyi.zzb.study.mapper.StudyDocInfoMapper;
  16. import com.ruoyi.zzb.study.mapper.StudyViewLogInfoMapper;
  17. import lombok.extern.slf4j.Slf4j;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import org.springframework.transaction.annotation.Transactional;
  21. @Service
  22. @Slf4j
  23. @DataSource(value = DataSourceType.SLAVE)
  24. public class StudyService {
  25. @Autowired
  26. private StudyDocInfoMapper studyDocInfoMapper;
  27. @Autowired
  28. private StudyViewLogInfoMapper studyViewLogInfoMapper;
  29. public Page<StudyDocInfo> findAllInfo(UserSearchParam searchParam) {
  30. //分页参数
  31. Page<StudyDocInfo> rowPage = new Page(searchParam.getPageIndex(), searchParam.getPageSize());
  32. //查询条件
  33. LambdaQueryWrapper<StudyDocInfo> queryWrapper = new LambdaQueryWrapper<>();
  34. if(StringUtils.isNotEmpty(searchParam.getTitle())){
  35. queryWrapper.like(StudyDocInfo::getTitle,searchParam.getTitle());
  36. }
  37. if (StringUtils.isNotBlank(searchParam.getDatasourceCode())) {
  38. queryWrapper.eq(StudyDocInfo::getDatasourceCode, searchParam.getDatasourceCode());
  39. }
  40. //排序
  41. queryWrapper.orderByAsc(StudyDocInfo::getOrderNum);
  42. queryWrapper.orderByAsc(StudyDocInfo::getCreateTime);
  43. //分页查询
  44. rowPage = studyDocInfoMapper.selectPage(rowPage, queryWrapper);
  45. log.info("============【学习板块】查询学习文章列表成功!============");
  46. return rowPage;
  47. }
  48. public StudyDocInfo findDocInfoById(String id) {
  49. LambdaQueryWrapper<StudyDocInfo> queryWrapper = new LambdaQueryWrapper();
  50. queryWrapper.eq(StudyDocInfo::getDocId,id);
  51. StudyDocInfo studyDocInfo= studyDocInfoMapper.selectOne(queryWrapper);
  52. log.info("============【学习板块】查询文章成功!{}============",id);
  53. return studyDocInfo;
  54. }
  55. public boolean saveDocInfo(StudyDocInfo studyDocInfo) {
  56. int i = studyDocInfoMapper.insert(studyDocInfo);
  57. if( i!=1 ){
  58. throw new RuntimeException("数据写入失败,成功0条,入参:"+JSONObject.toJSONString(studyDocInfo));
  59. }
  60. log.info("============【学习板块】添加文章成功!============");
  61. return i == 1;
  62. }
  63. public boolean deleteById(String id) {
  64. int i = studyDocInfoMapper.deleteById(id);
  65. if( i!=1 ){
  66. throw new RuntimeException("数据删除失败,docId不存在:"+id);
  67. }
  68. log.info("============【学习板块】删除文章成功!{}============",id);
  69. return i == 1;
  70. }
  71. public boolean updatePageViewNumById(UpdatePageViewNumVO updatePageViewNumVO) {
  72. UpdateWrapper<StudyDocInfo> updateWrapper = new UpdateWrapper<>();
  73. updateWrapper.eq("DOC_ID",updatePageViewNumVO.getDocId()).set("PAGE_VIEW_NUM", updatePageViewNumVO.getPageViewNum());
  74. Integer i = studyDocInfoMapper.update(null, updateWrapper);
  75. if( i!=1 ){
  76. throw new RuntimeException("修改文章浏览量失败,docId不存在:"+updatePageViewNumVO.getDocId());
  77. }
  78. log.info("============【学习板块】修改文章浏览量成功!{}============",updatePageViewNumVO.getDocId());
  79. return i == 1;
  80. }
  81. @Transactional
  82. public boolean saveViewLogInfo(SaveViewLogVO saveViewLogVO,SysUser sysUser) {
  83. //记录浏览量+1
  84. UpdateWrapper<StudyDocInfo> updateDocWrapper = new UpdateWrapper<>();
  85. updateDocWrapper.eq("DOC_ID",saveViewLogVO.getDocId());
  86. updateDocWrapper.setSql("PAGE_VIEW_NUM = PAGE_VIEW_NUM + 1");
  87. Integer j = studyDocInfoMapper.update(null,updateDocWrapper);
  88. if(j != 1){
  89. throw new RuntimeException("记录浏览量更新异常,docId不存在:"+saveViewLogVO.getDocId());
  90. }
  91. //保存访问记录
  92. StudyViewLogInfo studyViewLogInfo = new StudyViewLogInfo();
  93. studyViewLogInfo.setDocId(saveViewLogVO.getDocId());
  94. studyViewLogInfo.setUserId(saveViewLogVO.getUserId());
  95. studyViewLogInfo.setUserName(sysUser.getNickName());
  96. studyViewLogInfo.setUserIdcard(sysUser.getIdCard());
  97. studyViewLogInfo.setUserPoliceNo(sysUser.getPoliceNo());
  98. studyViewLogInfo.setParentDeptCode(sysUser.getDept().getParentId()+"");
  99. studyViewLogInfo.setParentDeptName(sysUser.getDept().getParentName());
  100. studyViewLogInfo.setDeptCode(sysUser.getDept().getDeptId()+"");
  101. studyViewLogInfo.setDeptName(sysUser.getDept().getDeptName());
  102. int i = studyViewLogInfoMapper.insert(studyViewLogInfo);
  103. if(i != 1){
  104. throw new RuntimeException("保存访问记录日志保存失败:"+JSONObject.toJSONString(studyViewLogInfo));
  105. }
  106. log.info("============【学习板块】保存访问记录成功!{}============",saveViewLogVO.getDocId());
  107. return i == 1 && j ==1;
  108. }
  109. /**
  110. * 分页查询用户列表
  111. */
  112. public Page<StudyDocInfo> findInfoByTitle(UserSearchParam searchParam)
  113. {
  114. //分页参数
  115. Page<StudyDocInfo> rowPage = new Page(searchParam.getPageIndex(), searchParam.getPageSize());
  116. //查询条件
  117. LambdaQueryWrapper<StudyDocInfo> queryWrapper = new LambdaQueryWrapper<>();
  118. if(StringUtils.isNotEmpty(searchParam.getTitle()))
  119. {
  120. queryWrapper.like(StudyDocInfo::getTitle,searchParam.getTitle());
  121. }
  122. //排序
  123. queryWrapper.orderByAsc(StudyDocInfo::getOrderNum);
  124. queryWrapper.orderByAsc(StudyDocInfo::getCreateTime);
  125. //分页查询
  126. rowPage = studyDocInfoMapper.selectPage(rowPage, queryWrapper);
  127. return rowPage;
  128. }
  129. public boolean updateById(StudyDocInfo studyDocInfo) {
  130. int i = studyDocInfoMapper.updateById(studyDocInfo);
  131. if(i != 1){
  132. throw new RuntimeException("docId不存在:"+studyDocInfo.getDocId());
  133. }
  134. log.info("============【学习板块】更新文章成功!{}============",studyDocInfo.getDocId());
  135. return i == 1;
  136. }
  137. }