Skip to content

Commit

Permalink
用户访问量
Browse files Browse the repository at this point in the history
  • Loading branch information
tangllty committed Jan 10, 2024
1 parent 54c69ae commit be3a322
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.tang.admin.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.tang.commons.utils.AjaxResult;
import com.tang.system.service.log.SysLogLoginService;

/**
* 首页逻辑控制层
*
* @author Tang
*/
@RestController
@RequestMapping("/index")
public class IndexController {

private final SysLogLoginService logLoginService;

public IndexController(SysLogLoginService logLoginService) {
this.logLoginService = logLoginService;
}

/**
* 用户访问量
*
* @return 用户访问量
*/
@GetMapping("/user-visit")
public AjaxResult userVisit() {
var userVisit = logLoginService.selectUserVisit();
return AjaxResult.success(userVisit);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.tang.system.mapper.log;

import java.time.LocalDate;
import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.tang.system.entity.log.SysLogLogin;

/**
Expand Down Expand Up @@ -35,6 +38,14 @@ public interface SysLogLoginMapper {
*/
SysLogLogin selectSysLogLoginByLoginId(Long loginId);

/*
* 查询用户访问量
*
* @startTime 开始时间
* @return 用户访问量
*/
List<SysLogLogin> selectUserVisit(@Param("userId") Long userId, @Param("startDate") LocalDate startDate);

/**
* 新增登陆日志信息
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package com.tang.system.service.impl.log;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.stereotype.Service;

Expand Down Expand Up @@ -58,6 +62,37 @@ public SysLogLogin selectSysLogLoginByLoginId(Long loginId) {
return sysLogLoginMapper.selectSysLogLoginByLoginId(loginId);
}

/*
* 查询用户访问量
*
* @return 用户访问量
*/
@Override
public List<Long> selectUserVisit() {
var userId = SecurityUtils.getUser().getUserId();
var now = LocalDate.now();
var monday = now.with(DayOfWeek.MONDAY);
var sunday = monday.plusDays(6);
var startDate = monday;
var list = sysLogLoginMapper.selectUserVisit(userId, startDate);
var listGrouped = list.stream()
.collect(Collectors.groupingBy(logLogin -> {
var loginTime = logLogin.getLoginTime();
return LocalDate.of(loginTime.getYear(), loginTime.getMonth(), loginTime.getDayOfMonth());
}));
var userVisitList = new ArrayList<Long>();
while (startDate.isBefore(sunday)) {
var count = listGrouped.get(startDate);
if (count != null) {
userVisitList.add(count.stream().count());
} else {
userVisitList.add(0L);
}
startDate = startDate.plusDays(1);
}
return userVisitList;
}

/**
* 新增登陆日志信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ public interface SysLogLoginService {
*/
SysLogLogin selectSysLogLoginByLoginId(Long loginId);

/*
* 查询用户访问量
*
* @return 用户访问量
*/
List<Long> selectUserVisit();

/**
* 新增登陆日志信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@
where ll.login_id = #{loginId}
</select>

<select id="selectUserVisit" resultMap="SysLogLoginMap">
<include refid="SysLogLoginVo" />
where ll.user_id = #{userId}
and ll.login_time &gt;= #{startDate}
</select>

<insert id="insertSysLogLogin" useGeneratedKeys="true" keyProperty="loginId">
insert into sys_log_login (
<trim suffixOverrides=",">
Expand Down

0 comments on commit be3a322

Please sign in to comment.