Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/segmentio/kafka-go v0.4.49
github.com/spf13/viper v1.20.1
github.com/upyun/go-sdk/v3 v3.0.4
github.com/west2-online/jwch v0.2.35
github.com/west2-online/jwch v0.2.36
gorm.io/driver/mysql v1.6.0
gorm.io/gorm v1.31.0
k8s.io/client-go v0.34.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -638,8 +638,8 @@ github.com/upyun/go-sdk/v3 v3.0.4 h1:2DCJa/Yi7/3ZybT9UCPATSzvU3wpPPxhXinNlb1Hi8Q
github.com/upyun/go-sdk/v3 v3.0.4/go.mod h1:P/SnuuwhrIgAVRd/ZpzDWqCsBAf/oHg7UggbAxyZa0E=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.1/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/west2-online/jwch v0.2.35 h1:vPJbwqA0FudozUWDYaWO3SHzVWBIAdgKh2FTeW+B8es=
github.com/west2-online/jwch v0.2.35/go.mod h1:DlIfTRlv5BY+4mt6PI/xzleEbEqAoUYi3xMffe4FC3A=
github.com/west2-online/jwch v0.2.36 h1:RbiERoG7gPHirTtxWGbGRFpIQ9ADXuyoUj/mzw7Qfi0=
github.com/west2-online/jwch v0.2.36/go.mod h1:DlIfTRlv5BY+4mt6PI/xzleEbEqAoUYi3xMffe4FC3A=
github.com/west2-online/yjsy v0.0.10 h1:x51yWeoPjPKQ57XTmgR1PQYLx+L6tO2UXeqy/XflSio=
github.com/west2-online/yjsy v0.0.10/go.mod h1:coXKeHqvNcFdNS2IFVZSHZ21fHnSWQXiY8AR/v4xSuE=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
Expand Down
2 changes: 1 addition & 1 deletion internal/academic/pack/credit.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func BuildCredit(data []*jwch.CreditStatistics) []*model.Credit {
return credit
}

func BuildCreditResponse(data *jwch.CreditResponse) model.CreditResponse {
func BuildCreditResponse(data *CreditResponse) model.CreditResponse {
if data == nil {
return nil
}
Expand Down
109 changes: 109 additions & 0 deletions internal/academic/pack/credit_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
Copyright 2024 The west2-online Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package pack

import (
"fmt"
"strconv"
"strings"

"github.com/west2-online/jwch"
)

// CreditDetail 用于表示学分的详细数据项
type CreditDetail struct {
Key string `json:"key"`
Value string `json:"value"`
}

// CreditCategory 用于表示学分分类
type CreditCategory struct {
Type string `json:"type"`
Data []*CreditDetail `json:"data"`
}

// CreditResponse 用于学分响应
type CreditResponse []*CreditCategory

// BuildCreditCategory 格式化处理学分统计
func BuildCreditCategory(categoryType string, credits []*jwch.CreditStatistics) *CreditCategory {
category := &CreditCategory{
Type: categoryType,
Data: make([]*CreditDetail, 0, len(credits)),
}

// 用于计算"总计"栏的还需学分总和
var totalNeed float64
specialKeys := []string{"奖励", "其它", "重修", "正在修习", "CET"}

for _, credit := range credits {
isSpecial := false
for _, key := range specialKeys {
if strings.Contains(credit.Type, key) {
isSpecial = true
break
}
}

creditType := credit.Type
switch {
case creditType == "总计":
// 处理"总计"行
gain, _ := strconv.ParseFloat(credit.Gain, 64)
total, _ := strconv.ParseFloat(credit.Total, 64)

if totalNeed == 0 {
// 如果不需要任何学分,表示已修满
category.Data = append(category.Data, &CreditDetail{
Key: credit.Type,
Value: fmt.Sprintf("%g / %g", gain, total),
})
} else {
value := fmt.Sprintf("%g / %g (还需 %g 分)", gain, total, totalNeed)

category.Data = append(category.Data, &CreditDetail{
Key: credit.Type,
Value: value,
})
}
case isSpecial:
category.Data = append(category.Data, &CreditDetail{
Key: credit.Type,
Value: credit.Gain,
})
default:
gain, _ := strconv.ParseFloat(credit.Gain, 64)
total, _ := strconv.ParseFloat(credit.Total, 64)
var value string

if gain >= total {
value = fmt.Sprintf("%g / %g", gain, total)
} else {
need := total - gain
value = fmt.Sprintf("%g / %g (还需 %g 分)", gain, total, need)
// 未修满时需要的学分计入总计
totalNeed += need
}

category.Data = append(category.Data, &CreditDetail{
Key: credit.Type,
Value: value,
})
}
}
return category
}
18 changes: 15 additions & 3 deletions internal/academic/service/get_credit_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,34 @@ package service
import (
"fmt"

"github.com/west2-online/fzuhelper-server/internal/academic/pack"
"github.com/west2-online/fzuhelper-server/pkg/base"
"github.com/west2-online/fzuhelper-server/pkg/base/context"
"github.com/west2-online/fzuhelper-server/pkg/utils"
"github.com/west2-online/jwch"
)

// GetCreditV2 获取V2版本的学分统计
func (s *AcademicService) GetCreditV2() (*jwch.CreditResponse, error) {
func (s *AcademicService) GetCreditV2() (*pack.CreditResponse, error) {
loginData, err := context.GetLoginData(s.ctx)
if err != nil {
return nil, fmt.Errorf("service.GetCreditV2: Get login data fail %w", err)
}
stu := jwch.NewStudent().WithLoginData(loginData.Id, utils.ParseCookies(loginData.Cookies))
credit, err := stu.GetCreditV2()

majorCredits, minorCredits, err := stu.GetCreditV2()
if err = base.HandleJwchError(err); err != nil {
return nil, fmt.Errorf("service.GetCreditV2: Get credit fail %w", err)
}
return &credit, nil

majorItem := pack.BuildCreditCategory("主修专业", majorCredits)
var response pack.CreditResponse
response = append(response, majorItem)

if len(minorCredits) > 0 {
minorItem := pack.BuildCreditCategory("辅修专业", minorCredits)
response = append(response, minorItem)
}

return &response, nil
}
Loading