feat: 初始化仓库
This commit is contained in:
74
utils/auth.go
Normal file
74
utils/auth.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/fatih/color"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func CORSMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 设置允许访问的域
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
// 是否允许携带用户凭证
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
// 允许的自定义请求头
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With, X-CSRF-Token, Accept, Origin")
|
||||
// 允许的HTTP方法
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
|
||||
|
||||
// 处理预检请求
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
||||
// 继续处理非预检请求
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func CustomLogger() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// 获取开始时间
|
||||
start := time.Now()
|
||||
|
||||
// 处理请求
|
||||
c.Next()
|
||||
|
||||
// 获取状态码、耗时、客户端IP、方法、路径
|
||||
statusCode := c.Writer.Status()
|
||||
latency := time.Since(start)
|
||||
clientIP := c.ClientIP()
|
||||
method := c.Request.Method
|
||||
path := c.Request.URL.Path
|
||||
|
||||
// 颜色设置
|
||||
statusColor := color.New(color.FgGreen).SprintFunc()
|
||||
if statusCode >= 400 && statusCode < 500 {
|
||||
statusColor = color.New(color.FgYellow).SprintFunc()
|
||||
} else if statusCode >= 500 {
|
||||
statusColor = color.New(color.FgRed).SprintFunc()
|
||||
}
|
||||
|
||||
methodColor := color.New(color.FgCyan).SprintFunc()
|
||||
|
||||
// 打印彩色日志
|
||||
fmt.Printf("[GIN] %v | %s | %s | %13v | %s | %s\n",
|
||||
time.Now().Format("2006/01/02 - 15:04:05"),
|
||||
statusColor(statusCode),
|
||||
clientIP,
|
||||
latency,
|
||||
methodColor(method),
|
||||
path,
|
||||
)
|
||||
}
|
||||
}
|
||||
func NotFoundHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"code": 404, "msg": "无效路径!"})
|
||||
}
|
||||
func NotMethodHandler(c *gin.Context) {
|
||||
c.JSON(http.StatusMethodNotAllowed, gin.H{"code": 404, "msg": "请求方法错误!"})
|
||||
}
|
||||
123
utils/config.go
Normal file
123
utils/config.go
Normal file
@@ -0,0 +1,123 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"zhixiangtong/models"
|
||||
)
|
||||
|
||||
type TimeSet struct {
|
||||
Index int `json:"index"`
|
||||
Start string `json:"start"`
|
||||
End string `json:"end"`
|
||||
Time int `json:"time"`
|
||||
}
|
||||
|
||||
// Config 结构体定义服务配置
|
||||
type Config struct {
|
||||
Host string `json:"host"`
|
||||
Port string `json:"port"`
|
||||
StartDate string `json:"startDate"`
|
||||
TotalWeeks int `json:"totalWeeks"`
|
||||
EndDate string `json:"endDate"`
|
||||
TimeSetting []TimeSet `json:"timeSetting"`
|
||||
}
|
||||
|
||||
// InitConfig 初始化配置文件
|
||||
func InitConfig(dataDir string) (*Config, error) {
|
||||
configPath := filepath.Join(dataDir, "config.json")
|
||||
|
||||
// 检查配置文件是否存在
|
||||
if _, err := os.Stat(configPath); os.IsNotExist(err) {
|
||||
log.Printf("配置文件不存在,正在创建默认配置文件:%s", configPath)
|
||||
// JSON 数据
|
||||
timeSettingJSON := `[{"index": 1, "start": "08:00", "end": "08:45", "time": 0},
|
||||
{"index": 2, "start": "08:50", "end": "09:35", "time": 0},
|
||||
{"index": 3, "start": "09:55", "end": "10:40", "time": 0},
|
||||
{"index": 4, "start": "10:45", "end": "11:30", "time": 0},
|
||||
{"index": 5, "start": "11:35", "end": "12:20", "time": 0},
|
||||
{"index": 6, "start": "14:00", "end": "14:45", "time": 1},
|
||||
{"index": 7, "start": "14:50", "end": "15:35", "time": 1},
|
||||
{"index": 8, "start": "15:55", "end": "16:40", "time": 1},
|
||||
{"index": 9, "start": "16:45", "end": "17:30", "time": 1},
|
||||
{"index": 10, "start": "19:00", "end": "19:45", "time": 2},
|
||||
{"index": 11, "start": "19:50", "end": "20:35", "time": 2},
|
||||
{"index": 12, "start": "20:40", "end": "21:25", "time": 2}]`
|
||||
|
||||
// 将 JSON 数据解析为 TimeSet 切片
|
||||
var timeSettings []TimeSet
|
||||
err := json.Unmarshal([]byte(timeSettingJSON), &timeSettings)
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to parse JSON: %v", err)
|
||||
}
|
||||
// 默认配置
|
||||
defaultConfig := Config{
|
||||
Host: "localhost",
|
||||
Port: "8080",
|
||||
StartDate: "2024-09-02",
|
||||
EndDate: "2025-01-19",
|
||||
TotalWeeks: 20,
|
||||
TimeSetting: timeSettings,
|
||||
}
|
||||
|
||||
// 将默认配置写入配置文件
|
||||
configData, err := json.MarshalIndent(defaultConfig, "", " ")
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无法创建默认配置文件:%v", err)
|
||||
}
|
||||
|
||||
err = ioutil.WriteFile(configPath, configData, 0644)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无法写入默认配置文件:%v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// 读取配置文件
|
||||
configData, err := ioutil.ReadFile(configPath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无法读取配置文件:%v", err)
|
||||
}
|
||||
|
||||
// 解析配置文件
|
||||
var config Config
|
||||
err = json.Unmarshal(configData, &config)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("配置文件格式错误:%v", err)
|
||||
}
|
||||
|
||||
log.Printf("配置文件已加载:Host=%s, Port=%s", config.Host, config.Port)
|
||||
return &config, nil
|
||||
}
|
||||
|
||||
// InitDatabase 初始化数据库,如果不存在则创建并迁移
|
||||
func InitDatabase(dataDir string) (*gorm.DB, error) {
|
||||
dbPath := filepath.Join(dataDir, "database.db")
|
||||
|
||||
// 检查数据库文件是否存在
|
||||
if _, err := os.Stat(dbPath); os.IsNotExist(err) {
|
||||
log.Printf("数据库文件不存在,正在创建数据库:%s", dbPath)
|
||||
}
|
||||
|
||||
// 连接到SQLite数据库
|
||||
db, err := gorm.Open(sqlite.Open(dbPath), &gorm.Config{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("无法连接到数据库:%v", err)
|
||||
}
|
||||
|
||||
// 自动迁移数据库
|
||||
err = db.AutoMigrate(&models.Campus{}, &models.Building{}, &models.Classroom{}, &models.Course{}, &models.Classname{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("数据库迁移失败:%v", err)
|
||||
}
|
||||
|
||||
log.Printf("数据库已初始化并连接成功:%s", dbPath)
|
||||
return db, nil
|
||||
}
|
||||
402
utils/router.go
Normal file
402
utils/router.go
Normal file
@@ -0,0 +1,402 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
"zhixiangtong/models"
|
||||
)
|
||||
|
||||
type Setting struct {
|
||||
StartDate string `json:"startDate"`
|
||||
TotalWeeks int `json:"totalWeeks"`
|
||||
EndDate string `json:"endDate"`
|
||||
TimeSetting []TimeSet `json:"timeSetting"`
|
||||
}
|
||||
|
||||
// RemoveIntDuplicates 去重函数,去除整数切片中的重复元素
|
||||
func RemoveIntDuplicates(elements []int) []int {
|
||||
seen := make(map[int]bool)
|
||||
var result []int
|
||||
|
||||
for _, v := range elements {
|
||||
if _, ok := seen[v]; !ok {
|
||||
result = append(result, v)
|
||||
seen[v] = true
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
func RemoveStringDuplicates(stringSlice []string) []string {
|
||||
keys := make(map[string]bool)
|
||||
var list []string
|
||||
for _, entry := range stringSlice {
|
||||
if _, value := keys[entry]; !value {
|
||||
keys[entry] = true
|
||||
list = append(list, entry)
|
||||
}
|
||||
}
|
||||
return list
|
||||
}
|
||||
|
||||
// SetupRouter 配置Gin路由
|
||||
func SetupRouter(db *gorm.DB, config *Config) *gin.Engine {
|
||||
r := gin.New()
|
||||
r.Use(CustomLogger())
|
||||
// 处理跨域
|
||||
r.Use(CORSMiddleware())
|
||||
// 获取所有校区
|
||||
r.GET("/getCampusList", func(c *gin.Context) {
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
var campuses []models.Campus
|
||||
if err := db.Find(&campuses).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "无法获取校区列表", "data": nil})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功!", "data": campuses})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取校区内所有教学楼
|
||||
r.GET("/getBuilds/:id", func(c *gin.Context) {
|
||||
campusId := c.Param("id")
|
||||
if campusId == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,校区ID为必传项!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
var buildings []models.Building
|
||||
if err := db.Find(&buildings, "campus_id = ?", campusId).Error; err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"code": 500, "msg": "获取失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功!", "data": buildings})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取说所有教学楼
|
||||
r.GET("/getBuildList", func(c *gin.Context) {
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
var buildings []models.Building
|
||||
if err := db.Find(&buildings).Error; err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "无法获取教学楼列表", "data": nil})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功!", "data": buildings})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取教室列表
|
||||
r.GET("/getClassroomList/:id", func(c *gin.Context) {
|
||||
buildId := c.Param("id")
|
||||
var classrooms []models.Classroom
|
||||
if buildId == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,教学楼ID为必传项!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
if result := db.Find(&classrooms, "build_id = ? ", buildId); result.Error != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"code": 500, "msg": "获取失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功!", "data": classrooms})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取校区班级列表
|
||||
r.GET("/getCampusClass/:id", func(c *gin.Context) {
|
||||
var classNames []models.Classname
|
||||
campusId := c.Param("id")
|
||||
if campusId == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,校区ID为必传项!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
if result := db.Find(&classNames, "campus_id = ?", campusId); result.Error != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"code": 500, "msg": "获取失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功!", "data": classNames})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取班级课表
|
||||
r.GET("/getCourse/:id", func(c *gin.Context) {
|
||||
var courses []models.Course
|
||||
classnameId := c.Param("id")
|
||||
if classnameId == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,班级ID为必传项!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
if result := db.Find(&courses, "classname_id = ? AND status = 1", classnameId); result.Error != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"code": 422, "msg": "课程查询失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 按照课程名称、教室、星期几和上课时间去重
|
||||
uniqueCourses := make(map[string]map[string]map[int][]models.Course)
|
||||
for _, course := range courses {
|
||||
if uniqueCourses[course.Course] == nil {
|
||||
uniqueCourses[course.Course] = make(map[string]map[int][]models.Course)
|
||||
}
|
||||
if uniqueCourses[course.Course][course.Classroom] == nil {
|
||||
uniqueCourses[course.Course][course.Classroom] = make(map[int][]models.Course)
|
||||
}
|
||||
uniqueCourses[course.Course][course.Classroom][course.Day] = append(uniqueCourses[course.Course][course.Classroom][course.Day], course)
|
||||
}
|
||||
var resultList []map[string]interface{}
|
||||
for courseName, classrooms := range uniqueCourses {
|
||||
for classroom, days := range classrooms {
|
||||
for day, courses := range days {
|
||||
var weeks []int
|
||||
for _, course := range courses {
|
||||
weeks = append(weeks, course.Week)
|
||||
}
|
||||
weeks = RemoveIntDuplicates(weeks)
|
||||
sort.Ints(weeks)
|
||||
var timeRange []int
|
||||
for _, t := range courses {
|
||||
timeRange = append(timeRange, t.ClassTime)
|
||||
}
|
||||
timeRange = RemoveIntDuplicates(timeRange)
|
||||
sort.Ints(timeRange)
|
||||
resultList = append(resultList, map[string]interface{}{
|
||||
"id": uuid.New().String(),
|
||||
"course": courseName,
|
||||
"classroom": classroom,
|
||||
"teacher": courses[0].Teacher,
|
||||
"weeks": weeks,
|
||||
"day": day + 1,
|
||||
"time": timeRange,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
// 返回结果
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "获取成功!",
|
||||
"data": resultList,
|
||||
})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取空闲教室
|
||||
r.GET("/getEmptyClassroom", func(c *gin.Context) {
|
||||
// 获取请求参数
|
||||
campusID := c.Query("campus_id")
|
||||
buildID := c.Query("build_id") // 可选参数
|
||||
startStamp := c.Query("startStamp")
|
||||
endStamp := c.Query("endStamp")
|
||||
// 参数校验
|
||||
if campusID == "" || startStamp == "" || endStamp == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,校区ID和时间戳为必传项!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 解析时间戳参数
|
||||
startTimestamp, err := strconv.ParseInt(startStamp, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "开始时间戳格式错误!", "data": nil})
|
||||
return
|
||||
}
|
||||
endTimestamp, err := strconv.ParseInt(endStamp, 10, 64)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "msg": "结束时间戳格式错误!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
// 查询有课的教室 classroom1
|
||||
var classroom1 []string
|
||||
query := db.Model(&models.Course{}).
|
||||
Where("campus_id = ? AND startStamp >= ? AND endStamp <= ? AND status = 1",
|
||||
campusID, startTimestamp, endTimestamp)
|
||||
if buildID != "" {
|
||||
query = query.Where("build_id = ?", buildID)
|
||||
}
|
||||
err = query.Pluck("classroom_id", &classroom1).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "查询课程数据失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 查询所有教室 classroom2,根据 campus_id 和可选的 build_id 过滤
|
||||
var classroom2 []models.Classroom
|
||||
query = db.Where("campus_id = ?", campusID)
|
||||
if buildID != "" {
|
||||
query = query.Where("build_id = ?", buildID)
|
||||
}
|
||||
err = query.Find(&classroom2).Error
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "查询教室数据失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用一个 map 来判断 classroom1 中的教室是否有课
|
||||
classroom1Set := make(map[string]bool)
|
||||
for _, id := range classroom1 {
|
||||
classroom1Set[id] = true
|
||||
}
|
||||
// 过滤掉有课的教室,得到无课教室列表 data
|
||||
var data []models.Classroom
|
||||
for _, classroom := range classroom2 {
|
||||
if !classroom1Set[classroom.ID] {
|
||||
data = append(data, classroom)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "查询成功!", "data": data})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取教室课表
|
||||
r.GET("/getClassroomCourses", func(c *gin.Context) {
|
||||
var courses []models.Course
|
||||
classroomID := c.Query("classroom_id")
|
||||
weekStr := c.Query("week")
|
||||
|
||||
if classroomID == "" || weekStr == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,教室ID、周数为必传项!", "data": nil})
|
||||
return
|
||||
}
|
||||
|
||||
week, err := strconv.Atoi(weekStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"code": 422, "msg": "参数错误,周数格式不正确!", "data": nil})
|
||||
return
|
||||
}
|
||||
// 使用 WaitGroup 等待 Goroutine 完成
|
||||
var wg sync.WaitGroup
|
||||
wg.Add(1)
|
||||
|
||||
go func() {
|
||||
defer wg.Done() // 在 Goroutine 完成时调用 Done()
|
||||
// 根据 classroom_id 和时间范围筛选数据
|
||||
if result := db.Where("classroom_id = ? AND week = ? AND status = 1", classroomID, week).Find(&courses); result.Error != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"code": 500, "msg": "课程查询失败!", "data": nil})
|
||||
return
|
||||
}
|
||||
|
||||
// 按照 week, day 和 classTime 聚合
|
||||
type AggregatedCourse struct {
|
||||
Week int `json:"week"`
|
||||
Day int `json:"day"`
|
||||
ClassTime int `json:"classTime"`
|
||||
Classnames []string `json:"classnames"`
|
||||
Campus string `json:"campus"`
|
||||
Build string `json:"build"`
|
||||
StartTime time.Time `json:"startTime"`
|
||||
EndTime time.Time `json:"endTime"`
|
||||
StartStamp float64 `json:"startStamp"`
|
||||
EndStamp float64 `json:"endStamp"`
|
||||
ClassRoom string `json:"classroom"`
|
||||
Course string `json:"course"`
|
||||
Teacher string `json:"teacher"`
|
||||
ID string `json:"id"`
|
||||
}
|
||||
|
||||
aggregatedData := make(map[string]AggregatedCourse)
|
||||
|
||||
for _, course := range courses {
|
||||
key := fmt.Sprintf("%d-%d-%d", course.Week, course.Day, course.ClassTime)
|
||||
if _, exists := aggregatedData[key]; !exists {
|
||||
aggregatedData[key] = AggregatedCourse{
|
||||
Week: course.Week,
|
||||
Day: course.Day,
|
||||
ClassTime: course.ClassTime,
|
||||
Classnames: []string{},
|
||||
Campus: course.Campus,
|
||||
Build: course.Build,
|
||||
StartStamp: course.StartStamp,
|
||||
EndStamp: course.EndStamp,
|
||||
StartTime: course.StartTime,
|
||||
EndTime: course.EndTime,
|
||||
ClassRoom: course.Classroom,
|
||||
Course: course.Course,
|
||||
Teacher: course.Teacher,
|
||||
ID: course.ID,
|
||||
}
|
||||
}
|
||||
|
||||
aggCourse := aggregatedData[key]
|
||||
aggCourse.Classnames = append(aggCourse.Classnames, course.ClassName)
|
||||
aggregatedData[key] = aggCourse
|
||||
}
|
||||
|
||||
var resultList []AggregatedCourse
|
||||
for _, aggCourse := range aggregatedData {
|
||||
// 去重课程名称
|
||||
aggCourse.Classnames = RemoveStringDuplicates(aggCourse.Classnames)
|
||||
resultList = append(resultList, aggCourse)
|
||||
}
|
||||
|
||||
// 返回聚合结果
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"code": 200,
|
||||
"msg": "获取成功!",
|
||||
"data": resultList,
|
||||
})
|
||||
return
|
||||
}()
|
||||
wg.Wait()
|
||||
})
|
||||
// 获取学校配置
|
||||
r.GET("/getTermSetting", func(c *gin.Context) {
|
||||
data := Setting{
|
||||
TimeSetting: config.TimeSetting,
|
||||
StartDate: config.StartDate,
|
||||
EndDate: config.EndDate,
|
||||
TotalWeeks: config.TotalWeeks,
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"code": 200, "msg": "获取成功", "data": data})
|
||||
})
|
||||
// 处理请求方法错
|
||||
r.NoMethod(NotMethodHandler)
|
||||
// 处理无效路径
|
||||
r.NoRoute(NotFoundHandler)
|
||||
return r
|
||||
}
|
||||
Reference in New Issue
Block a user