Commit 42fd6d20 authored by liming's avatar liming

用户主页数据统计

parent 97c0ab9e
package com.antai.sport.http.server.server.api.business.traininglog.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
@ApiModel("用户运动数据汇总")
@Data
public class SportUserTrainingLogSummaryVO {
@ApiModelProperty("时长(秒)")
private Integer totalDuration;
@ApiModelProperty("累计距离")
private BigDecimal totalDistance;
@ApiModelProperty("累计爬坡高度")
private BigDecimal totalClimbDistance;
@ApiModelProperty("单次最长记录")
private BigDecimal longestDistance;
}
package com.antai.sport.http.server.server.api.business.traininglog.mapper;
import com.antai.sport.http.server.repository.sport.entity.SportUserTrainingLog;
import com.antai.sport.http.server.server.api.business.traininglog.dto.SportUserTrainingLogSummaryVO;
import org.apache.ibatis.annotations.Param;
import java.time.LocalDate;
public interface SportUserTrainingLogBusinessMapper {
SportUserTrainingLog getLongestTrainingLog(@Param("sportType") Integer sportType, @Param("userId") Long userId);
SportUserTrainingLogSummaryVO getTrainingLogSummary(@Param("sportType") Integer sportType,
@Param("userId") Long userId,
@Param("beginDate") LocalDate beginDate,
@Param("endDate") LocalDate endDate);
}
......@@ -7,6 +7,8 @@ import com.antai.sport.http.server.repository.sport.entity.SportUserTrainingLog;
import com.antai.sport.http.server.repository.sport.mapper.SportUserSummaryMapper;
import com.antai.sport.http.server.repository.sport.mapper.SportUserTrainingLogMapper;
import com.antai.sport.http.server.server.api.business.traininglog.converter.SportUserTrainingLogConverter;
import com.antai.sport.http.server.server.api.business.traininglog.dto.SportUserTrainingLogSummaryVO;
import com.antai.sport.http.server.server.api.business.traininglog.mapper.SportUserTrainingLogBusinessMapper;
import com.antai.sport.http.server.server.api.common.dto.CyclingDataDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
......@@ -15,7 +17,6 @@ import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Service
public class SportUserTrainingLogService {
......@@ -24,6 +25,9 @@ public class SportUserTrainingLogService {
@Resource
private SportUserSummaryMapper sportUserSummaryMapper;
@Resource
private SportUserTrainingLogBusinessMapper sportUserTrainingLogBusinessMapper;
@Resource
private SportUserTrainingLogConverter sportUserTrainingLogConverter;
......@@ -50,10 +54,11 @@ public class SportUserTrainingLogService {
if (summary == null) {
summary = new SportUserSummary();
summary.setUserId(playerId);
summary.setType( SportType.BICYCLE);
summary.setType(SportType.BICYCLE);
summary.setTotalDistance(BigDecimal.ZERO);
summary.setTotalClimbDistance(BigDecimal.ZERO);
summary.setTotalDuration(0);
summary.setTotalPraiseNum(0);
}
summary.setTotalDuration(summary.getTotalDuration() + log.getDuration());
summary.setTotalDistance(summary.getTotalDistance().add(log.getDistance()));
......@@ -65,4 +70,37 @@ public class SportUserTrainingLogService {
}
}
/**
* 获取用户某个运动下的最长记录
*
* @param sportType
* @param userId
* @return
*/
public SportUserTrainingLog getLongestTrainingLog(Integer sportType, Long userId) {
return sportUserTrainingLogBusinessMapper.getLongestTrainingLog(sportType, userId);
}
/**
* 获取时间范围内运动数据汇总
*
* @param sportType 运动类型
* @param userId 用户Id
* @param daysCondition 统计天数
* @return
*/
public SportUserTrainingLogSummaryVO getTrainingLogSummary(Integer sportType, Long userId, Integer daysCondition) {
LocalDate now = LocalDate.now();
LocalDate beginDate = now.minusDays(daysCondition - 1);
SportUserTrainingLogSummaryVO summaryVO = sportUserTrainingLogBusinessMapper.getTrainingLogSummary(sportType, userId, beginDate, now);
if (summaryVO == null) {
summaryVO = new SportUserTrainingLogSummaryVO();
summaryVO.setLongestDistance(BigDecimal.ZERO);
summaryVO.setTotalDistance(BigDecimal.ZERO);
summaryVO.setTotalClimbDistance(BigDecimal.ZERO);
summaryVO.setTotalDuration(0);
}
return summaryVO;
}
}
package com.antai.sport.http.server.server.api.business.user.controller;
import com.antai.sport.http.server.common.base.Result;
import com.antai.sport.http.server.server.api.business.user.dto.RespUserHomeInfo;
import com.antai.sport.http.server.server.api.business.user.service.SportUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import static com.antai.sport.http.server.common.util.ResultUtil.success;
@Api(tags = {"用户相关接口"})
@RestController
@RequestMapping("user")
public class SportUserController {
@Resource
private SportUserService sportUserService;
@ApiOperation("查询用户主页数据")
@GetMapping("{userId}")
public ResponseEntity<Result<RespUserHomeInfo>> getUserHomeInfo(@PathVariable("userId") Long userId) {
return success(sportUserService.getUserHomeInfo(userId));
}
}
package com.antai.sport.http.server.server.api.business.user.dto;
import com.antai.sport.http.server.server.api.business.traininglog.dto.SportUserTrainingLogSummaryVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.math.BigDecimal;
@Data
@ApiModel("用户主页汇总统计数据")
public class RespUserHomeInfo {
@ApiModelProperty("用户id")
private Long id ;
@ApiModelProperty("用户昵称")
private String nickname;
@ApiModelProperty("头像")
private String avatar;
@ApiModelProperty("ftp")
private Integer ftp;
@ApiModelProperty("功体比")
private BigDecimal wkg;
@ApiModelProperty("观看次数")
private Integer views;
@ApiModelProperty("7天数据汇总")
private SportUserTrainingLogSummaryVO weekSummary;
@ApiModelProperty("30天数据汇总")
private SportUserTrainingLogSummaryVO monthSummary;
@ApiModelProperty("全部数据汇总")
private SportUserTrainingLogSummaryVO totalSummary;
}
package com.antai.sport.http.server.server.api.business.user.mapper;
import com.antai.sport.http.server.repository.sport.entity.SportUserSummary;
import org.apache.ibatis.annotations.Param;
public interface SportUserSummaryBusinessMapper {
int updateUserPraise(@Param("userId") Long userId, @Param("sportType") Integer sportType);
SportUserSummary selectBySportTypeAndUserId(@Param("userId") Long userId, @Param("sportType") Integer sportType);
}
package com.antai.sport.http.server.server.api.business.user.service;
import com.antai.sport.http.server.constants.SportType;
import com.antai.sport.http.server.repository.sport.entity.SportUser;
import com.antai.sport.http.server.repository.sport.entity.SportUserSummary;
import com.antai.sport.http.server.repository.sport.entity.SportUserTrainingLog;
import com.antai.sport.http.server.repository.sport.mapper.SportUserMapper;
import com.antai.sport.http.server.server.api.business.traininglog.service.SportUserTrainingLogService;
import com.antai.sport.http.server.server.api.business.user.dto.RespUserHomeInfo;
import com.antai.sport.http.server.server.api.business.traininglog.dto.SportUserTrainingLogSummaryVO;
import com.antai.sport.http.server.server.api.business.user.mapper.SportUserSummaryBusinessMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
@Service
public class SportUserService {
@Resource
private SportUserMapper sportUserMapper;
@Resource
private SportUserSummaryBusinessMapper sportUserSummaryBusinessMapper;
@Resource
private SportUserTrainingLogService sportUserTrainingLogService;
public RespUserHomeInfo getUserHomeInfo(Long userId) {
RespUserHomeInfo resp = new RespUserHomeInfo();
SportUser sportUser = sportUserMapper.selectById(userId);
SportUserSummary sportUserSummary = sportUserSummaryBusinessMapper.selectBySportTypeAndUserId(userId, SportType.BICYCLE);
resp.setId(sportUser.getId());
resp.setAvatar(sportUser.getAvatar());
resp.setNickname(sportUser.getNickname());
resp.setViews(sportUserSummary.getViews() == null ? 0 : sportUserSummary.getViews());
resp.setFtp(sportUser.getFtp() == null ? 0 : sportUser.getFtp());
if (sportUser.getFtp() == null || sportUser.getWeight() == null) {
resp.setWkg(BigDecimal.ZERO);
} else {
resp.setWkg(BigDecimal.valueOf(sportUser.getFtp()).divide(sportUser.getWeight(), 1, RoundingMode.HALF_UP));
}
//组装全部汇总数据
SportUserTrainingLogSummaryVO totalSummary = new SportUserTrainingLogSummaryVO();
totalSummary.setTotalDistance(sportUserSummary.getTotalDistance());
totalSummary.setTotalClimbDistance(sportUserSummary.getTotalClimbDistance());
totalSummary.setTotalDuration(sportUserSummary.getTotalDuration());
SportUserTrainingLog longestTrainingLog = sportUserTrainingLogService.getLongestTrainingLog(SportType.BICYCLE, userId);
if(longestTrainingLog == null){
totalSummary.setLongestDistance(BigDecimal.ZERO);
} else {
totalSummary.setLongestDistance(longestTrainingLog.getDistance());
}
resp.setTotalSummary(totalSummary);
//查询7天数据汇总
resp.setWeekSummary(sportUserTrainingLogService.getTrainingLogSummary(SportType.BICYCLE,userId,7));
//查询30天数据汇总
resp.setMonthSummary(sportUserTrainingLogService.getTrainingLogSummary(SportType.BICYCLE,userId,30));
return resp;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.antai.sport.http.server.server.api.business.traininglog.mapper.SportUserTrainingLogBusinessMapper">
<select id="getLongestTrainingLog" resultType="com.antai.sport.http.server.repository.sport.entity.SportUserTrainingLog">
select * from sport_user_training_log
where player_id = #{userId} and sport_type = #{sportType}
order by distance desc limit 1
</select>
<select id="getTrainingLogSummary" resultType="com.antai.sport.http.server.server.api.business.traininglog.dto.SportUserTrainingLogSummaryVO">
select ifnull(sum(duration),0) as totalDuration, ifnull(sum(distance),0) as totalDistance,
ifnull(sum(climb_distance),0) as totalClimbDistance,ifnull(max(distance),0) as longestDistance
from sport_user_training_log
where player_id = #{userId} and sport_type = #{sportType}
and create_date &gt;= #{beginDate} and create_date &lt;= #{endDate}
</select>
</mapper>
\ No newline at end of file
......@@ -6,4 +6,11 @@
set total_praise_num = ifnull(total_praise_num, 0) + 1
where user_id = #{userId} and type = #{sportType}
</update>
<select id="selectBySportTypeAndUserId"
resultType="com.antai.sport.http.server.repository.sport.entity.SportUserSummary">
select *
from sport_user_summary
where user_id = #{userId} and type = #{sportType}
</select>
</mapper>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment