web大作业开发记录05

教师功能页后端spring接口:
AdminController:

package com.example.demo.controller;

import com.example.demo.common.Result;
import com.example.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    /**
     * 获取学生周目标活跃统计
     */
    @GetMapping("/students/goals-active-stats")
    public Result getStudentsGoalsActiveStats(@RequestParam(required = false) String className) {
        return adminService.getStudentsGoalsActiveStats(className);
    }

    /**
     * 获取学生本周博客发布统计
     */
    @GetMapping("/students/blog-stats")
    public Result getStudentsBlogStats(@RequestParam(required = false) String className) {
        return adminService.getStudentsBlogStats(className);
    }

    /**
     * 获取学生博客管理数据
     */
    @GetMapping("/students/blog-manage")
    public Result getStudentsBlogManageData(@RequestParam(required = false) String className) {
        return adminService.getStudentsBlogManageData(className);
    }

    /**
     * 重置学生密码
     */
    @PostMapping("/reset-password")
    public Result resetPassword(@RequestBody Map<String, String> params) {
        String studentId = params.get("studentId");
        if (studentId == null) {
            return Result.fail("学号不能为空");
        }
        return adminService.resetPassword(studentId);
    }

    /**
     * 学生评分
     */
    @PostMapping("/score-student")
    public Result scoreStudent(@RequestBody Map<String, Object> params) {
        String studentId = (String) params.get("studentId");
        Integer points = (Integer) params.get("points");
        String remark = (String) params.get("remark");
        
        if (studentId == null || points == null) {
            return Result.fail("参数不完整");
        }
        
        return adminService.scoreStudent(studentId, points, remark);
    }
}

AdminServiceImpl:

package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.common.Result;
import com.example.demo.entity.BlogLink;
import com.example.demo.entity.Goals;
import com.example.demo.entity.Users;
import com.example.demo.service.AdminService;
import com.example.demo.service.BlogLinkService;
import com.example.demo.service.GoalsService;
import com.example.demo.service.UsersService;
import com.example.demo.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    private UsersService usersService;

    @Autowired
    private GoalsService goalsService;

    @Autowired
    private BlogLinkService blogLinkService;

    @Override
    public Result getStudentsGoalsActiveStats(String className) {
        try {
            // 获取所有学生(排除管理员)
            QueryWrapper<Users> userWrapper = new QueryWrapper<>();
            userWrapper.ne("student_id", "root");
            
            // 如果指定了班级,添加班级过滤条件
            if (className != null && !className.trim().isEmpty()) {
                userWrapper.eq("class_name", className);
            }
            
            List<Users> students = usersService.list(userWrapper);

            int activeCount = 0;
            int inactiveCount = 0;

            for (Users student : students) {
                // 查询该学生是否有活跃的周目标
                QueryWrapper<Goals> goalsWrapper = new QueryWrapper<>();
                goalsWrapper.eq("student_id", student.getStudentId())
                           .eq("status", 1); // 活跃状态
                long activeGoalsCount = goalsService.count(goalsWrapper);

                if (activeGoalsCount > 0) {
                    activeCount++;
                } else {
                    inactiveCount++;
                }
            }

            Map<String, Object> result = new HashMap<>();
            result.put("activeCount", activeCount);
            result.put("inactiveCount", inactiveCount);
            result.put("totalStudents", students.size());

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("获取学生周目标活跃统计失败");
        }
    }

    @Override
    public Result getStudentsBlogStats(String className) {
        try {
            // 获取所有学生(排除管理员)
            QueryWrapper<Users> userWrapper = new QueryWrapper<>();
            userWrapper.ne("student_id", "root");
            
            // 如果指定了班级,添加班级过滤条件
            if (className != null && !className.trim().isEmpty()) {
                userWrapper.eq("class_name", className);
            }
            
            List<Users> students = usersService.list(userWrapper);

            int noBlog = 0;      // 未发布博客的学生数
            int lessThan5 = 0;   // 发布1-4篇博客的学生数
            int moreThan5 = 0;   // 发布5篇及以上博客的学生数
            int totalBlogs = 0;  // 本周博客总数

            for (Users student : students) {
                // 查询该学生本周发布的博客数量
                QueryWrapper<BlogLink> blogWrapper = new QueryWrapper<>();
                blogWrapper.eq("student_id", student.getStudentId())
                          .between("publish_time", DateUtils.getWeekStart(), DateUtils.getWeekEnd());
                long blogCount = blogLinkService.count(blogWrapper);
                totalBlogs += blogCount;

                if (blogCount == 0) {
                    noBlog++;
                } else if (blogCount < 5) {
                    lessThan5++;
                } else {
                    moreThan5++;
                }
            }

            Map<String, Object> result = new HashMap<>();
            result.put("noBlog", noBlog);
            result.put("lessThan5", lessThan5);
            result.put("moreThan5", moreThan5);
            result.put("totalBlogs", totalBlogs);
            result.put("totalStudents", students.size());

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("获取学生博客发布统计失败");
        }
    }

    @Override
    public Result resetPassword(String studentId) {
        try {
            Users user = usersService.getById(studentId);
            if (user == null) {
                return Result.fail("学生不存在");
            }
            
            // 重置为默认密码
            user.setPassword("123456");
            if (usersService.updateById(user)) {
                return Result.success("密码重置成功");
            }
            return Result.fail("密码重置失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("密码重置失败");
        }
    }

    @Override
    public Result getStudentsBlogManageData(String className) {
        try {
            // 获取所有学生(排除管理员)
            QueryWrapper<Users> userWrapper = new QueryWrapper<>();
            userWrapper.ne("student_id", "root");
            
            // 如果指定了班级,添加班级过滤条件
            if (className != null && !className.trim().isEmpty()) {
                userWrapper.eq("class_name", className);
            }
            
            List<Users> students = usersService.list(userWrapper);
            List<Map<String, Object>> noBlogStudents = new ArrayList<>();
            List<Map<String, Object>> hasBlogStudents = new ArrayList<>();

            for (Users student : students) {
                Map<String, Object> studentData = new HashMap<>();
                studentData.put("studentId", student.getStudentId());
                studentData.put("name", student.getName());
                studentData.put("className", student.getClassName());
                studentData.put("points", student.getPoints()); // 添加积分字段
                
                // 查询该学生本周发布的博客数量
                QueryWrapper<BlogLink> weekBlogWrapper = new QueryWrapper<>();
                weekBlogWrapper.eq("student_id", student.getStudentId())
                              .between("publish_time", DateUtils.getWeekStart(), DateUtils.getWeekEnd());
                long weekBlogCount = blogLinkService.count(weekBlogWrapper);
                
                // 查询该学生所有博客数量
                QueryWrapper<BlogLink> allBlogWrapper = new QueryWrapper<>();
                allBlogWrapper.eq("student_id", student.getStudentId());
                long allBlogCount = blogLinkService.count(allBlogWrapper);
                
                studentData.put("weekBlogCount", weekBlogCount);
                studentData.put("allBlogCount", allBlogCount);
                
                if (weekBlogCount == 0) {
                    noBlogStudents.add(studentData);
                } else {
                    hasBlogStudents.add(studentData);
                }
            }

            Map<String, Object> result = new HashMap<>();
            result.put("noBlogStudents", noBlogStudents);
            result.put("hasBlogStudents", hasBlogStudents);

            return Result.success(result);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("获取学生博客管理数据失败");
        }
    }

    @Override
    public Result scoreStudent(String studentId, Integer points, String remark) {
        try {
            Users user = usersService.getById(studentId);
            if (user == null) {
                return Result.fail("学生不存在");
            }
            
            // 更新学生积分
            user.setPoints(user.getPoints() + points);
            if (usersService.updateById(user)) {
                return Result.success("评分成功");
            }
            return Result.fail("评分失败");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("评分失败");
        }
    }
}
posted @ 2025-06-13 16:06  vivi_vimi  阅读(49)  评论(0)    收藏  举报