一、获取预设时间范围内的活跃会员数
<select id="getPresetTimeRangeActivity" parameterType="java.util.Map" resultType="java.util.Map">
SELECT date, COUNT(DISTINCT uid) AS active_members
FROM eb_user_visit_record
WHERE visit_type = 1
AND date >=
<choose>
<when test="timeRangeType == 'today'">
CURDATE()
</when>
<when test="timeRangeType == 'thisWeek'">
DATE_SUB(CURDATE(), INTERVAL DAYOFWEEK(CURDATE()) - 1 DAY)
</when>
<when test="timeRangeType == 'thisMonth'">
DATE_FORMAT(CURDATE(), '%Y-%m-01')
</when>
</choose>
GROUP BY date
</select>
List<Map<String, Object>> getPresetTimeRangeActivity(Map<String, Object> params);
@Override
public List<Map<String, Object>> getPresetTimeRangeActivity(String timeRangeType) {
Map<String, Object> params = new HashMap<>();
params.put("timeRangeType", timeRangeType);
return statisticsMembershipDao.getPresetTimeRangeActivity(params);
}
//@PreAuthorize("hasAuthority('admin:statistics:home:chart:user')")
@ApiOperation(value = "获取指定日期范围内的活跃会员数,参数:today,thisWeek,thisMonth")
@RequestMapping(value = "/activity/range", method = RequestMethod.GET)
public CommonResult<List<Map<String, Object>>> getPresetTimeRangeActivity(
@RequestParam("timeRangeType") String timeRangeType) {
return CommonResult.success(statisticsMembershipService.getPresetTimeRangeActivity(timeRangeType));
}
二、计算平均年龄
public int calculateAverageAge() {
// 获取生日列表
List<String> birthdayList = statisticsMembershipDao.AverageAge();
// 获取当前日期
LocalDate currentDate = LocalDate.now();
// 初始化总年龄和有效生日数量
int totalAge = 0;
int validCount = 0; // 记录有效生日的数量
// 遍历生日列表
for (String birthday : birthdayList) {
// 检查生日是否有效
if (birthday != null && !birthday.isEmpty()) {
// 将生日字符串解析为 LocalDate 对象
LocalDate birthdate = LocalDate.parse(birthday);
// 计算年龄差并获取年龄
int age = Period.between(birthdate, currentDate).getYears();
// 累加年龄并增加有效生日计数
totalAge += age;
validCount++;
}
}
// 检查是否存在有效生日
if (validCount > 0) {
// 计算平均年龄并返回
return totalAge / validCount;
} else {
// 如果没有有效生日,则返回 0
return 0;
}
}