LogServiceImpl.java 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package jnpf.service.impl;
  2. import cn.hutool.core.util.ObjectUtil;
  3. import cn.hutool.http.useragent.UserAgent;
  4. import cn.hutool.http.useragent.UserAgentUtil;
  5. import com.baomidou.dynamic.datasource.annotation.DSTransactional;
  6. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  9. import com.google.common.collect.Lists;
  10. import jnpf.base.UserInfo;
  11. import jnpf.base.service.SuperServiceImpl;
  12. import jnpf.config.ConfigValueUtil;
  13. import jnpf.database.util.TenantDataSourceUtil;
  14. import jnpf.entity.LogEntity;
  15. import jnpf.enums.LogSortEnum;
  16. import jnpf.mapper.LogMapper;
  17. import jnpf.model.PaginationLogModel;
  18. import jnpf.service.LogService;
  19. import jnpf.util.*;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.stereotype.Service;
  22. import java.util.Date;
  23. import java.util.HashSet;
  24. import java.util.List;
  25. import java.util.Set;
  26. import java.util.stream.Collectors;
  27. /**
  28. * 系统日志
  29. *
  30. * @author JNPF开发平台组
  31. * @version V3.1.0
  32. * @copyright 引迈信息技术有限公司
  33. * @date 2019年9月27日 上午9:18
  34. */
  35. @Service
  36. public class LogServiceImpl extends SuperServiceImpl<LogMapper, LogEntity> implements LogService {
  37. @Autowired
  38. private ConfigValueUtil configValueUtil;
  39. @Override
  40. public List<LogEntity> getList(int category, PaginationLogModel paginationTime, Boolean filterUser) {
  41. UserInfo userInfo = UserProvider.getUser();
  42. QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
  43. queryWrapper.lambda().eq(LogEntity::getType, category);
  44. if (filterUser) {
  45. //用户Id
  46. String userId = userInfo.getUserId();
  47. String userAccount = userInfo.getUserAccount();
  48. queryWrapper.lambda().and(
  49. t -> t.eq(LogEntity::getUserId, userId)
  50. .or().eq(LogEntity::getUserId, userAccount)
  51. );
  52. }
  53. //日期范围(近7天、近1月、近3月、自定义)
  54. if (!ObjectUtil.isEmpty(paginationTime.getStartTime()) && !ObjectUtil.isEmpty(paginationTime.getEndTime())) {
  55. queryWrapper.lambda().between(LogEntity::getCreatorTime, new Date(paginationTime.getStartTime()), new Date(paginationTime.getEndTime()));
  56. }
  57. //关键字(用户、IP地址、功能名称)
  58. String keyWord = paginationTime.getKeyword();
  59. if (!StringUtil.isEmpty(keyWord)) {
  60. if (category == 1) {
  61. queryWrapper.lambda().and(
  62. t -> t.like(LogEntity::getUserName, keyWord)
  63. .or().like(LogEntity::getIpAddress, keyWord)
  64. );
  65. } else if (category == 5 || category == 4) {
  66. queryWrapper.lambda().and(
  67. t -> t.like(LogEntity::getUserName, keyWord)
  68. .or().like(LogEntity::getIpAddress, keyWord)
  69. .or().like(LogEntity::getRequestUrl, keyWord)
  70. );
  71. } else if (category == 3) {
  72. queryWrapper.lambda().and(
  73. t -> t.like(LogEntity::getUserName, keyWord)
  74. .or().like(LogEntity::getIpAddress, keyWord)
  75. .or().like(LogEntity::getRequestUrl, keyWord)
  76. .or().like(LogEntity::getModuleName, keyWord)
  77. );
  78. }
  79. }
  80. // 请求方式
  81. if (StringUtil.isNotEmpty(paginationTime.getRequestMethod())) {
  82. queryWrapper.lambda().eq(LogEntity::getRequestMethod, paginationTime.getRequestMethod());
  83. }
  84. // 类型
  85. if (paginationTime.getLoginType() != null) {
  86. queryWrapper.lambda().eq(LogEntity::getLoginType, paginationTime.getLoginType());
  87. }
  88. // 状态
  89. if (paginationTime.getLoginMark() != null) {
  90. queryWrapper.lambda().eq(LogEntity::getLoginMark, paginationTime.getLoginMark());
  91. }
  92. if (StringUtil.isNotEmpty(paginationTime.getDataInterFaceId())) {
  93. String s =
  94. paginationTime.getDataInterFaceId() +
  95. "/Actions/Preview";
  96. String ss =
  97. paginationTime.getDataInterFaceId() +
  98. "/Actions/Response";
  99. queryWrapper.lambda().and(query ->
  100. query.like(LogEntity::getRequestUrl, s).or().like(LogEntity::getRequestUrl, ss)
  101. );
  102. }
  103. //排序
  104. queryWrapper.lambda().orderByDesc(LogEntity::getCreatorTime);
  105. Page<LogEntity> page = new Page<>(paginationTime.getCurrentPage(), paginationTime.getPageSize());
  106. IPage<LogEntity> userPage = this.page(page, queryWrapper);
  107. return paginationTime.setData(userPage.getRecords(), page.getTotal());
  108. }
  109. @Override
  110. public LogEntity getInfo(String id) {
  111. QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
  112. queryWrapper.lambda().eq(LogEntity::getId, id);
  113. return this.getOne(queryWrapper);
  114. }
  115. @Override
  116. @DSTransactional
  117. public boolean delete(String[] ids) {
  118. if (ids.length > 0) {
  119. QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
  120. queryWrapper.lambda().in(LogEntity::getId, ids);
  121. return this.remove(queryWrapper);
  122. }
  123. return false;
  124. }
  125. @Override
  126. public void writeLogAsync(String userId, String userName, String abstracts, long requestDuration) {
  127. writeLogAsync(userId, userName, abstracts, null, 1, null, requestDuration);
  128. }
  129. @Override
  130. public void writeLogAsync(String userId, String userName, String abstracts, UserInfo userInfo, int loginMark, Integer loginType, long requestDuration) {
  131. LogEntity entity = new LogEntity();
  132. if (configValueUtil.isMultiTenancy() && userInfo != null) {
  133. try {
  134. TenantDataSourceUtil.switchTenant(userInfo.getTenantId());
  135. } catch (Exception e) {
  136. return;
  137. }
  138. }
  139. String ipAddr = IpUtil.getIpAddr();
  140. entity.setIpAddress(ipAddr);
  141. entity.setIpAddressName(IpUtil.getIpCity(ipAddr));
  142. // 请求设备
  143. UserAgent userAgent = UserAgentUtil.parse(ServletUtil.getUserAgent());
  144. if (userAgent != null) {
  145. entity.setPlatForm(userAgent.getPlatform().getName() + " " + userAgent.getOsVersion());
  146. entity.setBrowser(userAgent.getBrowser().getName() + " " + userAgent.getVersion());
  147. }
  148. if (loginType != null) {
  149. entity.setLoginType(1);
  150. } else {
  151. entity.setLoginType(0);
  152. }
  153. entity.setLoginMark(loginMark);
  154. entity.setRequestDuration(Integer.parseInt(String.valueOf(requestDuration)));
  155. entity.setId(RandomUtil.uuId());
  156. entity.setUserId(userId);
  157. entity.setUserName(userName);
  158. entity.setDescription(abstracts);
  159. entity.setRequestUrl(ServletUtil.getServletPath());
  160. entity.setRequestMethod(ServletUtil.getRequest().getMethod());
  161. entity.setType(LogSortEnum.Login.getCode());
  162. this.save(entity);
  163. }
  164. @Override
  165. public void writeLogAsync(LogEntity entity) {
  166. entity.setId(RandomUtil.uuId());
  167. this.save(entity);
  168. }
  169. @Override
  170. public void deleteHandleLog(String type, Integer userOnline, String dataInterfaceId) {
  171. QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
  172. queryWrapper.lambda().eq(LogEntity::getType, Integer.valueOf(type));
  173. if (ObjectUtil.equals(userOnline, 1)) {
  174. queryWrapper.lambda().eq(LogEntity::getCreatorUserId, UserProvider.getLoginUserId());
  175. }
  176. if (StringUtil.isNotEmpty(dataInterfaceId)) {
  177. String s = dataInterfaceId +
  178. "/Actions/Preview";
  179. String ss = dataInterfaceId +
  180. "/Actions/Response";
  181. queryWrapper
  182. .lambda()
  183. .and(query -> query
  184. .like(LogEntity::getRequestUrl, s).or().like(LogEntity::getRequestUrl, ss));
  185. }
  186. queryWrapper.lambda().select(LogEntity::getId);
  187. List<String> ids = this.list(queryWrapper).stream().map(LogEntity::getId).collect(Collectors.toList());
  188. List<List<String>> lists = Lists.partition(ids, 1000);
  189. for (List<String> list : lists) {
  190. QueryWrapper<LogEntity> deleteWrapper = new QueryWrapper<>();
  191. deleteWrapper.lambda().in(LogEntity::getId, list);
  192. this.remove(deleteWrapper);
  193. }
  194. }
  195. @Override
  196. public Set<String> queryList() {
  197. QueryWrapper<LogEntity> queryWrapper = new QueryWrapper<>();
  198. queryWrapper.lambda().eq(LogEntity::getType, 3);
  199. return this.list(queryWrapper).size() > 0 ? this.list(queryWrapper).stream().map(t -> t.getModuleName()).collect(Collectors.toSet()) : new HashSet<>(16);
  200. }
  201. }