`
@Autowired
StringRedisTemplate stringRedisTemplate;
@ApiOperation(value = "查询操作日志" )
@GetMapping("/querySysLogList")
public R querySysLogList(
@ApiParam(name = "startTime", value = "操作时间", required = false) @RequestParam(value = "startTime", required = false) String startTime,
@ApiParam(name = "userName", value = "操作人", required = false) @RequestParam(value = "userName", required = false) String userName,
@ApiParam(name = "operate", value = "操作(添加 修改 删除)", required = false) @RequestParam(value = "operate", required = false) String operate,
@ApiParam(name = "module", value = "操作模块", required = false) @RequestParam(value = "module", required = false) String module,
@ApiParam(name = "page", value = "分页参数", required = false, defaultValue = "1") @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@ApiParam(name = "pageSize", value = "每页大小", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize)
{
JSONObject jsonObject=new JSONObject();
//此处代码用的是mybatisplus的构造器,有兴趣的自行了解
Page<SysLog> defendPage = new Page<>();
EntityWrapper<SysLog> entityWrapper = new EntityWrapper<>();
List<SysLog> dataList = new ArrayList<>();
if(StringUtils.isEmpty(startTime) && StringUtils.isEmpty(userName) && StringUtils.isEmpty(operate)
&& StringUtils.isEmpty(module) && pageSize==10){
Object o = stringRedisTemplate.opsForHash().entries(RedisConstants.USER_CACHE_LOG).get(page.toString());
//.entries 获取变量中的键值对 .get获取变量中指定的map键 存在该map键获取值 没有返回null
Object total = stringRedisTemplate.opsForHash().entries(RedisConstants.USER_CACHE_LOG).get("total");
log.info("从缓存中获取用户操作日志信息===========>>>"+o);
if(!StringUtils.isEmpty(o) && !StringUtils.isEmpty(total)){
List list = JsonUtils.fromJson(o.toString(),List.class);
Iterator iterator = list.iterator();
//迭代遍历
while (iterator.hasNext()){
SysLog sysLog = JsonUtils.fromJson(iterator.next().toString(),SysLog.class);
dataList.add(sysLog);
}
defendPage.setTotal(JsonUtils.fromJson(total.toString(),Integer.class));
jsonObject.put("dataList",dataList);
jsonObject.put("totalCount",defendPage.getTotal());
return new R().data(jsonObject);
}else{
defendPage.setCurrent(page);
defendPage.setSize(pageSize);
entityWrapper.orderBy("operate_time",false);
defendPage = sysLogService.selectPage(defendPage,entityWrapper);
dataList = defendPage.getRecords();
if (StringUtil.isNotNull(dataList)) {
stringRedisTemplate.boundHashOps(RedisConstants.USER_CACHE_LOG).put(page.toString(), JsonUtils.toJson(dataList));
stringRedisTemplate.boundHashOps(RedisConstants.USER_CACHE_LOG).put("total", JsonUtils.toJson(defendPage.getTotal()));
stringRedisTemplate.expire(RedisConstants.USER_CACHE_LOG, 30L, TimeUnit.DAYS);
}
// 第二种分页缓存方法 两种结果一样
stringRedisTemplate.opsForHash().put(RedisConstants.USER_CACHE_LOG, page.toString(), JsonUtils.toJson(dataList));
stringRedisTemplate.expire(RedisConstants.USER_CACHE_LOG, 30L, TimeUnit.DAYS);
defendPage.setTotal(defendPage.getTotal());
jsonObject.put("dataList",dataList);
jsonObject.put("totalCount",defendPage.getTotal());
return new R().data(jsonObject);
}
}else{
if(!StringUtils.isEmpty(startTime)){
Date time= DateUtil.parseDate(startTime);
Date startTime1=DateUtil.beginOfDay(time);
Date endTime1=DateUtil.endOfDay(time);
entityWrapper.between("operate_time",startTime1,endTime1);
}
if(!StringUtils.isEmpty(userName)){
entityWrapper.like("user_name",userName);
}
if(!StringUtils.isEmpty(operate)){
entityWrapper.like("operate",operate);
}
if(!StringUtils.isEmpty(module)){
entityWrapper.like("module",module);
}
entityWrapper.orderBy("operate_time",false);
Page<SysLog> list=sysLogService.selectPage(defendPage,entityWrapper);
jsonObject.put("dataList",list.getRecords());
jsonObject.put("totalCount",list.getTotal());
return new R().data(jsonObject);
}
}
`