获取每个年龄段的人数

    <select id="AgeGroupStatistics" resultType="com.lpk.common.model.user.User">
        SELECT birthday
        FROM eb_user
        WHERE birthday IS NOT NULL AND birthday != '';
    </select>
List<User> AgeGroupStatistics();
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="AgeGroupStatisticsResponse", description="年龄区间人数-的数据对象")
public class AgeGroupStatisticsResponse implements Serializable {

    @ApiModelProperty(value = "哪个年龄阶段")
    private Object ageGroup ;

    @ApiModelProperty(value = "所在阶段的人数")
    private Object memberCount;

    @ApiModelProperty(value = "所在阶段的人数的百分比")
    private Object percentage;

    public void setPercentage(double percentage) {
        this.percentage = percentage;
    }
}

 

@Override
public List<AgeGroupStatisticsResponse> calculateAgeGroupStatistics() {
    // 获取具有生日信息的用户列表
    List<User> usersWithBirthday = statisticsMembershipDao.AgeGroupStatistics();

    // 获取当前日期
    LocalDate currentDate = LocalDate.now();

    // 创建日期格式化器,用于解析生日字符串
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

    // 创建用于存储年龄组统计信息的列表
    List<AgeGroupStatisticsResponse> ageGroupStatisticsList = new ArrayList<>();

    // 创建长度为 5 的整数数组,用于存储不同年龄组的计数器
    int[] ageGroupCounters = new int[5];

    // 遍历具有生日信息的用户列表
    for (User user : usersWithBirthday) {
        // 将用户的生日字符串解析为 LocalDate 对象
        LocalDate birthdayDate = LocalDate.parse(user.getBirthday(), formatter);

        // 计算用户的年龄
        int age = currentDate.getYear() - birthdayDate.getYear();

        // 根据年龄将用户分配到不同的年龄组计数器
        if (age <= 18) {
            ageGroupCounters[0]++;
        } else if (age <= 25) {
            ageGroupCounters[1]++;
        } else if (age <= 33) {
            ageGroupCounters[2]++;
        } else if (age <= 42) {
            ageGroupCounters[3]++;
        } else {
            ageGroupCounters[4]++;
        }
    }

    // 计算用户总数
    long totalCount = usersWithBirthday.size();

    // 遍历年龄组计数器,为每个年龄组创建 AgeGroupStatisticsResponse 对象
    for (int i = 0; i < ageGroupCounters.length; i++) {
        AgeGroupStatisticsResponse ageGroupStatistics = new AgeGroupStatisticsResponse();

        // 获取年龄组标签
        ageGroupStatistics.setAgeGroup(getAgeGroupLabel(i));

        // 设置年龄组成员计数
        ageGroupStatistics.setMemberCount(ageGroupCounters[i]);

        // 计算并设置百分比信息
        ageGroupStatistics.setPercentage((int) ageGroupCounters[i] / totalCount * 100);

        // 将 AgeGroupStatisticsResponse 对象添加到列表中
        ageGroupStatisticsList.add(ageGroupStatistics);
    }

    // 返回填充有年龄组统计数据的列表
    return ageGroupStatisticsList;
}

// 根据索引获取年龄组标签的私有方法
private String getAgeGroupLabel(int index) {
    switch (index) {
        case 0:
            return "18以下";
        case 1:
            return "18岁-25岁";
        case 2:
            return "26岁-33岁";
        case 3:
            return "34岁-42岁";
        default:
            return "42岁以上";
    }
}
    // 获取每个年龄段的人数及对应百分比
    //@PreAuthorize("hasAuthority('admin:statistics:home:chart:user')")
    @ApiOperation(value = "获取每个年龄段的人数")
    @RequestMapping(value = "/ageGroupStatistics", method = RequestMethod.GET)
    public CommonResult<List<AgeGroupStatisticsResponse>> getAgeGroupStatistics() {
        return CommonResult.success(statisticsMembershipService.calculateAgeGroupStatistics());
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值