个人作业前端
package org.example.blog.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class CorsConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedOrigins("http://192.168.20.105:8080") // 前端实际地址
.allowedMethods("GET", "POST", "PUT", "DELETE")
.allowedHeaders("*")
.allowCredentials(true);
}
};
}
}
package org.example.blog.controller;
import org.example.blog.entity.CodingRecord;
import org.example.blog.service.CodingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.time.LocalDate;
import java.util.List;
@RestController
@RequestMapping("/api/coding_records")
public class CodingRecordController {
@Autowired
private CodingRecordService codingRecordService;
@GetMapping("/")
public ResponseEntity<List<CodingRecord>> getCodingRecords(@RequestParam String userId) {
List<CodingRecord> records = codingRecordService.getAllCodingRecordsByUserId(Long.valueOf(userId));
return ResponseEntity.ok(records);
}
@PostMapping("/")
public ResponseEntity<Void> addCodingRecord(@RequestBody CodingRecord codingRecord) {
codingRecord.setDate(LocalDate.now());
codingRecordService.addCodingRecord(codingRecord);
return ResponseEntity.ok().build();
}
}
package org.example.blog.controller;
import org.example.blog.entity.WeeklyGoal;
import org.example.blog.model.ApiResponse;
import org.example.blog.service.GoalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/goals")
public class GoalController {
@Autowired
private GoalService goalService;
@PostMapping("/weekly")
public ResponseEntity<ApiResponse<Void>> setWeeklyGoal(
@RequestParam String content,
@RequestParam Long userId
) {
goalService.setWeeklyGoal(userId, content);
return ResponseEntity.ok(new ApiResponse<>(200, "目标设置成功", null));
}
@GetMapping("/current")
public ResponseEntity<ApiResponse<WeeklyGoal>> getCurrentGoal(
@RequestParam Long userId
) {
WeeklyGoal goal = goalService.getCurrentWeeklyGoal(userId);
return ResponseEntity.ok(new ApiResponse<>(200, "查询成功", goal));
}
}
package org.example.blog.controller;
import jakarta.servlet.http.HttpSession;
import org.example.blog.entity.DailySummary;
import org.example.blog.entity.User;
import org.example.blog.entity.WeeklyGoal;
import org.example.blog.service.GoalService;
import org.example.blog.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.Collections;
import java.util.List;
@Controller
public class HomeController {
@Autowired
private GoalService goalService;
@Autowired
private SummaryService summaryService;
@GetMapping(value = {"/", "/home"})
public String home(HttpSession session, Model model) {
User currentUser = (User) session.getAttribute("currentUser");
if (currentUser == null) {
return "redirect:/user/login";
}
WeeklyGoal currentGoal = goalService.getCurrentWeeklyGoal(currentUser.getId());
List<DailySummary> summaries = summaryService.getUserSummaries(currentUser.getId());
model.addAttribute("currentWeeklyGoal", currentGoal);
model.addAttribute("dailySummaries", summaries != null ? summaries : Collections.emptyList());
return "home";
}
}
package org.example.blog.controller;
import jakarta.servlet.http.HttpSession;
import org.example.blog.entity.DailySummary;
import org.example.blog.entity.User;
import org.example.blog.model.ApiResponse;
import org.example.blog.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/api")
public class SummaryController {
@Autowired
private SummaryService summaryService;
@GetMapping("/summaries")
public ResponseEntity<ApiResponse<List<DailySummary>>> getSummaries(@RequestParam("userId") Long userId) {
List<DailySummary> summaries = summaryService.getSummariesByUserId(userId);
return ResponseEntity.ok(new ApiResponse<>(200, "成功", summaries));
}
@PostMapping("/summary")
public ResponseEntity<ApiResponse<Void>> addSummary(@RequestBody DailySummary dailySummary) {
summaryService.addSummary(dailySummary);
return ResponseEntity.ok(new ApiResponse<>(200, "成功", null));
}
}
package org.example.blog.controller;
import jakarta.servlet.http.HttpSession;
import org.example.blog.entity.CodingRecord;
import org.example.blog.entity.User;
import org.example.blog.model.ApiResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.example.blog.service.CodingRecordService;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/teacher")
public class TeacherController {
private final CodingRecordService codingRecordService;
@Autowired
public TeacherController(CodingRecordService codingRecordService) {
this.codingRecordService = codingRecordService;
}
@GetMapping("/dashboard")
public ResponseEntity<ApiResponse<User>> dashboard(HttpSession session) {
User user = (User) session.getAttribute("currentUser");
if (user == null || !"teacher".equals(user.getRole())) {
return ResponseEntity.status(401).body(new ApiResponse<>(401, "未登录", null));
}
return ResponseEntity.ok(new ApiResponse<>(200, "成功", user));
}
@GetMapping("/coding-records")
public ResponseEntity<ApiResponse<List<CodingRecord>>> getStudentCodingRecords(
@RequestParam(required = false) Long studentId) {
List<CodingRecord> records = studentId != null ?
codingRecordService.getAllCodingRecordsByUserId(studentId) :
codingRecordService.getAllRecordsForTeachers();
return ResponseEntity.ok(new ApiResponse<>(200, "成功", records));
}
}package org.example.blog.controller;
import jakarta.servlet.http.HttpSession;
import org.example.blog.entity.DailySummary;
import org.example.blog.entity.User;
import org.example.blog.model.ApiResponse;
import org.example.blog.service.SummaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
@Controller
@RequestMapping("/teacher")
public class TeacherSummaryController {
private final SummaryService summaryService;
@Autowired
public TeacherSummaryController(SummaryService summaryService) {
this.summaryService = summaryService;
}
@GetMapping("/summaries/search")
public ResponseEntity<ApiResponse<List<DailySummary>>> showSearchPage() {
LocalDate endDate = LocalDate.now();
LocalDate startDate = endDate.minusWeeks(1);
List<DailySummary> results = summaryService.searchSummariesForTeacher(startDate, endDate, null);
return ResponseEntity.ok(new ApiResponse<>(200, "成功", results));
}
@GetMapping("/summaries/search/results")
public ResponseEntity<ApiResponse<List<DailySummary>>> searchSummaries(
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
@RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate,
@RequestParam(required = false) String keyword) {
// 移除了session检查
if (startDate == null && endDate == null) {
endDate = LocalDate.now();
startDate = endDate.minusWeeks(1);
}
List<DailySummary> results = summaryService.searchSummariesForTeacher(startDate, endDate, keyword);
return ResponseEntity.ok(new ApiResponse<>(200, "成功", results));
}
@GetMapping("/generate-all-summary-stats")
public ResponseEntity<ApiResponse<List<Map<String, Object>>>> generateAllUserSummaryStats() {
List<Map<String, Object>> summaryStats = summaryService.generateAllUserSummaryStats();
return ResponseEntity.ok(new ApiResponse<>(200, "成功", summaryStats));
}
}package org.example.blog.controller;
import jakarta.servlet.http.HttpSession;
import org.example.blog.entity.User;
import org.example.blog.model.ApiResponse;
import org.example.blog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/register")
public String showRegisterForm(Model model) {
model.addAttribute("user", new User());
return "register";
}
@PostMapping("/register")
public @ResponseBody ApiResponse register(@RequestBody User user) {
// 添加角色验证
if(!"student".equals(user.getRole()) && !"teacher".equals(user.getRole())) {
return new ApiResponse(400, "角色不合法", null);
}
if (userService.isUserIdExist(user.getUserId())) {
return new ApiResponse(400, "学号/工号已存在", null);
}
if (userService.isPhoneExist(user.getPhone())) {
return new ApiResponse(400, "手机号已存在", null);
}
System.out.println("Registering user: " + user.toString());
userService.register(user);
return new ApiResponse(200, "注册成功", null);
}
@PostMapping("/login")
public @ResponseBody ApiResponse login(
@RequestParam String userId,
@RequestParam String password,
@RequestParam String role,
HttpSession session
) {
System.out.println("Received login request: userId=" + userId + ", password=" + password + ", role=" + role);
User user = userService.login(userId, password, role);
if (user == null) {
return new ApiResponse(400, "账号或密码错误", null);
}
session.setAttribute("currentUser", user);
return new ApiResponse(200, "登录成功", user);
}
@GetMapping("/logout")
public String logout(HttpSession session) {
session.removeAttribute("currentUser");
return "redirect:/user/login";
}
@GetMapping("/current-user")
public @ResponseBody ApiResponse<User> getCurrentUser(
@RequestParam String userId,
@RequestParam String role
) {
User user = userService.getUserById(Long.parseLong(userId));
if (user == null) {
return new ApiResponse<>(400, "用户不存在", null);
}
return new ApiResponse<>(200, "查询成功", user);
}
}package org.example.blog.entity;
import jakarta.persistence.*;
import java.time.LocalDate;
@Entity
@Table(name = "coding_records")
public class CodingRecord {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Integer userId;
@Column(name = "date", nullable = false)
private LocalDate date;
@Column(name = "planning_content")
private String planningContent;
@Column(name = "planning_time")
private Integer planningTime;
@Column(name = "analysis_content")
private String analysisContent;
@Column(name = "analysis_time")
private Integer analysisTime;
@Column(name = "design_spec_content")
private String designSpecContent;
@Column(name = "design_spec_time")
private Integer designSpecTime;
@Column(name = "coding_standard_content")
private String codingStandardContent;
@Column(name = "coding_standard_time")
private Integer codingStandardTime;
@Column(name = "design_content")
private String designContent;
@Column(name = "design_time")
private Integer designTime;
@Column(name = "coding_content")
private String codingContent;
@Column(name = "coding_time")
private Integer codingTime;
@Column(name = "test_content")
private String testContent;
@Column(name = "test_time")
private Integer testTime;
@Column(name = "size_measurement_content")
private String sizeMeasurementContent;
@Column(name = "size_measurement_time")
private Integer sizeMeasurementTime;
@Column(name = "postmortem_content")
private String postmortemContent;
@Column(name = "postmortem_time")
private Integer postmortemTime;
@Column(name = "total_time")
private Integer totalTime;
// 默认构造函数
public CodingRecord() {
}
// 全参构造函数
public CodingRecord(Integer userId, LocalDate date, String planningContent, Integer planningTime,
String analysisContent, Integer analysisTime, String designSpecContent,
Integer designSpecTime, String codingStandardContent, Integer codingStandardTime,
String designContent, Integer designTime, String codingContent, Integer codingTime,
String testContent, Integer testTime, String sizeMeasurementContent,
Integer sizeMeasurementTime, String postmortemContent, Integer postmortemTime,
Integer totalTime) {
this.userId = userId;
this.date = date;
this.planningContent = planningContent;
this.planningTime = planningTime;
this.analysisContent = analysisContent;
this.analysisTime = analysisTime;
this.designSpecContent = designSpecContent;
this.designSpecTime = designSpecTime;
this.codingStandardContent = codingStandardContent;
this.codingStandardTime = codingStandardTime;
this.designContent = designContent;
this.designTime = designTime;
this.codingContent = codingContent;
this.codingTime = codingTime;
this.testContent = testContent;
this.testTime = testTime;
this.sizeMeasurementContent = sizeMeasurementContent;
this.sizeMeasurementTime = sizeMeasurementTime;
this.postmortemContent = postmortemContent;
this.postmortemTime = postmortemTime;
this.totalTime = totalTime;
}
// Getter 和 Setter 方法
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = Math.toIntExact(userId);
}
public LocalDate getDate() {
return date;
}
public void setDate(LocalDate date) {
this.date = date;
}
public String getPlanningContent() {
return planningContent;
}
public void setPlanningContent(String planningContent) {
this.planningContent = planningContent;
}
public Integer getPlanningTime() {
return planningTime;
}
public void setPlanningTime(Integer planningTime) {
this.planningTime = planningTime;
}
public String getAnalysisContent() {
return analysisContent;
}
public void setAnalysisContent(String analysisContent) {
this.analysisContent = analysisContent;
}
public Integer getAnalysisTime() {
return analysisTime;
}
public void setAnalysisTime(Integer analysisTime) {
this.analysisTime = analysisTime;
}
public String getDesignSpecContent() {
return designSpecContent;
}
public void setDesignSpecContent(String designSpecContent) {
this.designSpecContent = designSpecContent;
}
public Integer getDesignSpecTime() {
return designSpecTime;
}
public void setDesignSpecTime(Integer designSpecTime) {
this.designSpecTime = designSpecTime;
}
public String getCodingStandardContent() {
return codingStandardContent;
}
public void setCodingStandardContent(String codingStandardContent) {
this.codingStandardContent = codingStandardContent;
}
public Integer getCodingStandardTime() {
return codingStandardTime;
}
public void setCodingStandardTime(Integer codingStandardTime) {
this.codingStandardTime = codingStandardTime;
}
public String getDesignContent() {
return designContent;
}
public void setDesignContent(String designContent) {
this.designContent = designContent;
}
public Integer getDesignTime() {
return designTime;
}
public void setDesignTime(Integer designTime) {
this.designTime = designTime;
}
public String getCodingContent() {
return codingContent;
}
public void setCodingContent(String codingContent) {
this.codingContent = codingContent;
}
public Integer getCodingTime() {
return codingTime;
}
public void setCodingTime(Integer codingTime) {
this.codingTime = codingTime;
}
public String getTestContent() {
return testContent;
}
public void setTestContent(String testContent) {
this.testContent = testContent;
}
public Integer getTestTime() {
return testTime;
}
public void setTestTime(Integer testTime) {
this.testTime = testTime;
}
public String getSizeMeasurementContent() {
return sizeMeasurementContent;
}
public void setSizeMeasurementContent(String sizeMeasurementContent) {
this.sizeMeasurementContent = sizeMeasurementContent;
}
public Integer getSizeMeasurementTime() {
return sizeMeasurementTime;
}
public void setSizeMeasurementTime(Integer sizeMeasurementTime) {
this.sizeMeasurementTime = sizeMeasurementTime;
}
public String getPostmortemContent() {
return postmortemContent;
}
public void setPostmortemContent(String postmortemContent) {
this.postmortemContent = postmortemContent;
}
public Integer getPostmortemTime() {
return postmortemTime;
}
public void setPostmortemTime(Integer postmortemTime) {
this.postmortemTime = postmortemTime;
}
public Integer getTotalTime() {
return totalTime;
}
public void setTotalTime(Integer totalTime) {
this.totalTime = totalTime;
}
@PrePersist
protected void onCreate() {
if (this.date == null) {
this.date = LocalDate.now(); // 设置当前日期
}
}
@Override
public String toString() {
return "CodingRecord{" +
"id=" + id +
", userId=" + userId +
", date=" + date +
", planningContent='" + planningContent + '\'' +
", planningTime=" + planningTime +
", analysisContent='" + analysisContent + '\'' +
", analysisTime=" + analysisTime +
", designSpecContent='" + designSpecContent + '\'' +
", designSpecTime=" + designSpecTime +
", codingStandardContent='" + codingStandardContent + '\'' +
", codingStandardTime=" + codingStandardTime +
", designContent='" + designContent + '\'' +
", designTime=" + designTime +
", codingContent='" + codingContent + '\'' +
", codingTime=" + codingTime +
", testContent='" + testContent + '\'' +
", testTime=" + testTime +
", sizeMeasurementContent='" + sizeMeasurementContent + '\'' +
", sizeMeasurementTime=" + sizeMeasurementTime +
", postmortemContent='" + postmortemContent + '\'' +
", postmortemTime=" + postmortemTime +
", totalTime=" + totalTime +
'}';
}
}
package org.example.blog.entity;
import jakarta.persistence.*;
import java.time.LocalDate;
import java.time.LocalDateTime;
@Entity
@Table(name = "daily_summaries")
public class DailySummary {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "blog_url", nullable = false, length = 500)
private String blogUrl;
@Column(name = "summary_date", nullable = false)
private LocalDate summaryDate;
@Column(name = "create_time", nullable = false)
private LocalDateTime createTime;
// getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getBlogUrl() {
return blogUrl;
}
public void setBlogUrl(String blogUrl) {
this.blogUrl = blogUrl;
}
public LocalDate getSummaryDate() {
return summaryDate;
}
public void setSummaryDate(LocalDate summaryDate) {
this.summaryDate = summaryDate;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
}package org.example.blog.entity;
import jakarta.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false, unique = true)
private String userId; // 学号
@Column(nullable = false)
private String username; // 姓名
@Column(nullable = false)
private String password;
@Column(nullable = false, unique = true)
private String phone; // 手机号码
@Column(name = "user_class", nullable = false)
private String userClass; // 用户班级
@Column(name = "created_at")
private LocalDateTime createdAt;
@Column(nullable = false)
private String role;
private String token;
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
// 构造方法、getter和setter
public User() {}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userId='" + userId + '\'' +
", username='" + username + '\'' +
", password='" + password + '\'' +
", phone='" + phone + '\'' +
", userClass='" + userClass + '\'' +
", createdAt=" + createdAt +
", role='" + role + '\'' +
'}';
}
// User.java
public User(String userId, String username, String password, String phone,
String userClass, String role) {
this.userId = userId;
this.username = username;
this.password = password;
this.phone = phone;
this.userClass = userClass;
this.role = role;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getUserClass() {
return userClass;
}
public void setUserClass(String userClass) {
this.userClass = userClass;
}
public LocalDateTime getCreatedAt() {
return createdAt;
}
public void setCreatedAt(LocalDateTime createdAt) {
this.createdAt = createdAt;
}
}package org.example.blog.entity;
import jakarta.persistence.*;
import java.time.LocalDateTime;
@Entity
@Table(name = "weekly_goals")
public class WeeklyGoal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(nullable = false, length = 500)
private String content;
@Column(name = "create_time", nullable = false)
private LocalDateTime createTime;
// getters and setters
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public LocalDateTime getCreateTime() {
return createTime;
}
public void setCreateTime(LocalDateTime createTime) {
this.createTime = createTime;
}
}package org.example.blog.model;
public class ApiResponse
private int status;
private String message;
private T data;
// 构造函数
public ApiResponse(int status, String message, T data) {
this.status = status;
this.message = message;
this.data = data;
}
// Getters and Setters
public int getStatus() { return status; }
public void setStatus(int status) { this.status = status; }
public String getMessage() { return message; }
public void setMessage(String message) { this.message = message; }
public T getData() { return data; }
public void setData(T data) { this.data = data; }
}package org.example.blog.repository;
import org.example.blog.entity.CodingRecord;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import java.util.List;
public interface CodingRecordRepository extends JpaRepository<CodingRecord, Integer> {
// 修正:参数类型改为Long以匹配实体类
List
@Query("SELECT cr FROM CodingRecord cr ORDER BY cr.date DESC")
List<CodingRecord> findAllOrderByDateDesc();
@Query("SELECT cr.userId, SUM(cr.totalTime) FROM CodingRecord cr GROUP BY cr.userId")
List<Object[]> sumTotalTimeByUser();
}
package org.example.blog.repository;
import org.example.blog.entity.DailySummary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.time.LocalDate;
import java.util.List;
public interface DailySummaryRepository extends JpaRepository<DailySummary, Long> {
List<DailySummary> findByUserIdOrderBySummaryDateDesc(Long userId);
List<DailySummary> findAllByOrderBySummaryDateDesc();
@Query("SELECT COUNT(d) > 0 FROM DailySummary d WHERE d.userId = :userId AND d.summaryDate = :date")
boolean existsByUserIdAndSummaryDate(@Param("userId") Long userId, @Param("date") LocalDate date);
List<DailySummary> findByUserIdAndSummaryDateBetween(Long userId, LocalDate startDate, LocalDate endDate);
List<DailySummary> findBySummaryDateBetween(LocalDate startDate, LocalDate endDate);
@Query("SELECT ds FROM DailySummary ds WHERE LOWER(ds.blogUrl) LIKE LOWER(CONCAT('%', :keyword, '%'))")
List<DailySummary> findByBlogUrlContainingIgnoreCase(@Param("keyword") String keyword);
@Query("SELECT ds FROM DailySummary ds WHERE ds.summaryDate BETWEEN :startDate AND :endDate AND LOWER(ds.blogUrl) LIKE LOWER(CONCAT('%', :keyword, '%'))")
List<DailySummary> findBySummaryDateBetweenAndBlogUrlContainingIgnoreCase(
@Param("startDate") LocalDate startDate,
@Param("endDate") LocalDate endDate,
@Param("keyword") String keyword);
}package org.example.blog.repository;
import org.example.blog.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import java.util.List;
public interface UserRepository extends JpaRepository<User, Long> {
User findByUserId(String userId);
User findByPhone(String phone);
boolean existsByUserId(String userId);
boolean existsByPhone(String phone);
List
}package org.example.blog.repository;
import org.example.blog.entity.WeeklyGoal;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
public interface WeeklyGoalRepository extends JpaRepository<WeeklyGoal, Long> {
/**
* 查找用户指定日期后设置的最新周目标
* @param userId 用户ID
* @param afterDate 查询起始日期(通常是本周一)
* @return 最新的周目标
*/
@Query("SELECT w FROM WeeklyGoal w WHERE w.userId = :userId AND w.createTime >= :afterDate ORDER BY w.createTime DESC")
List<WeeklyGoal> findLatestByUserAndDate(@Param("userId") Long userId,
@Param("afterDate") LocalDateTime afterDate);
}package org.example.blog.service;
import org.example.blog.entity.CodingRecord;
import org.example.blog.repository.CodingRecordRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CodingRecordService {
private final CodingRecordRepository codingRecordRepository;
@Autowired
public CodingRecordService(CodingRecordRepository codingRecordRepository) {
this.codingRecordRepository = codingRecordRepository;
}
public CodingRecord addCodingRecord(CodingRecord codingRecord) {
CodingRecord saved = codingRecordRepository.save(codingRecord);
System.out.println("保存后的记录: " + saved); // 打印日志
return saved;
}
public List<CodingRecord> getAllCodingRecordsByUserId(Long userId) {
// 修改为使用新的Repository方法
return codingRecordRepository.findByUserIdOrderByDateDesc(userId);
}
// 新增:获取所有学生记录
public List<CodingRecord> getAllRecordsForTeachers() {
return codingRecordRepository.findAllOrderByDateDesc();
}
// 新增:按用户统计总时间
public List<Object[]> getTotalTimeStatistics() {
return codingRecordRepository.sumTotalTimeByUser();
}
}
package org.example.blog.service;
import org.example.blog.entity.WeeklyGoal;
public interface GoalService {
/**
* 设置每周学习目标
* @param userId 用户ID
* @param content 目标内容
*/
void setWeeklyGoal(Long userId, String content);
/**
* 获取当前周的学习目标
* @param userId 用户ID
* @return 本周的学习目标(如果存在)
*/
WeeklyGoal getCurrentWeeklyGoal(Long userId);
}package org.example.blog.service;
import org.example.blog.entity.DailySummary;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
public interface SummaryService {
/**
* 添加每日总结
* @param userId 用户ID
* @param blogUrl 博客链接
* @param summaryDate 总结日期
*/
void addSummary(Long userId, String blogUrl, LocalDate summaryDate);
/**
* 获取用户的所有总结
* @param userId 用户ID
* @return 总结列表(按日期降序)
*/
List<DailySummary> getUserSummaries(Long userId);
/**
* 检查某天是否已有总结
* @param userId 用户ID
* @param date 查询日期
* @return 是否已存在
*/
boolean existsSummaryForDate(Long userId, LocalDate date);
/**
* 获取日期范围内的总结
* @param userId 用户ID
* @param startDate 开始日期
* @param endDate 结束日期
* @return 符合条件的总结列表
*/
List<DailySummary> getSummariesByDateRange(Long userId, LocalDate startDate, LocalDate endDate);
List<DailySummary> searchSummariesForTeacher(LocalDate startDate, LocalDate endDate, String keyword);
List<Map<String, Object>> generateAllUserSummaryStats();
List<DailySummary> getAllSummaries();
void addSummary(DailySummary dailySummary);
List<DailySummary> getSummariesByUserId(Long userId);
}
// 其他方法...
package org.example.blog.service;
import org.example.blog.entity.DailySummary;
import org.example.blog.entity.User;
import java.util.List;
public interface UserService {
User register(User user);
User login(String userId, String password, String role);
boolean isUserIdExist(String userId);
boolean isPhoneExist(String phone);
User getUserById(Long id);
}

浙公网安备 33010602011771号