init
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"exam_registration/internal/dao"
|
||||
"exam_registration/internal/model"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ExamService struct{}
|
||||
|
||||
func (s *ExamService) CreateExam(exam *model.Exam) error {
|
||||
// 验证时间逻辑
|
||||
if exam.StartTime.Before(time.Now()) {
|
||||
return errors.New("考试开始时间不能早于当前时间")
|
||||
}
|
||||
if exam.EndTime.Before(exam.StartTime) {
|
||||
return errors.New("考试结束时间不能早于开始时间")
|
||||
}
|
||||
if exam.RegistrationEnd.Before(exam.RegistrationStart) {
|
||||
return errors.New("报名截止时间不能早于开始时间")
|
||||
}
|
||||
|
||||
return dao.DB.Create(exam).Error
|
||||
}
|
||||
|
||||
func (s *ExamService) GetExamByID(id uint64) (*model.Exam, error) {
|
||||
var exam model.Exam
|
||||
if err := dao.DB.First(&exam, id).Error; err != nil {
|
||||
return nil, errors.New("考试不存在")
|
||||
}
|
||||
return &exam, nil
|
||||
}
|
||||
|
||||
func (s *ExamService) GetExamList(page, pageSize int) ([]model.Exam, int64, error) {
|
||||
var exams []model.Exam
|
||||
var total int64
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
|
||||
if err := dao.DB.Model(&model.Exam{}).Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err := dao.DB.Offset(offset).Limit(pageSize).Order("id DESC").Find(&exams).Error
|
||||
return exams, total, err
|
||||
}
|
||||
|
||||
func (s *ExamService) UpdateExam(id uint64, updates map[string]interface{}) error {
|
||||
return dao.DB.Model(&model.Exam{}).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
|
||||
func (s *ExamService) DeleteExam(id uint64) error {
|
||||
return dao.DB.Delete(&model.Exam{}, id).Error
|
||||
}
|
||||
|
||||
func (s *ExamService) UpdateExamStatus(id uint64, status int) error {
|
||||
// 更新考试状态并同步更新相关报名记录的状态
|
||||
return dao.DB.Transaction(func(tx *dao.DB) error {
|
||||
if err := tx.Model(&model.Exam{}).Where("id = ?", id).Update("status", status).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"exam_registration/internal/dao"
|
||||
"exam_registration/internal/model"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
type NoticeService struct{}
|
||||
|
||||
func (s *NoticeService) CreateNotice(notice *model.ExamNotice) error {
|
||||
notice.PublishTime = time.Now()
|
||||
return dao.DB.Create(notice).Error
|
||||
}
|
||||
|
||||
func (s *NoticeService) GetNoticeByID(id uint64) (*model.ExamNotice, error) {
|
||||
var notice model.ExamNotice
|
||||
if err := dao.DB.Preload("Exam").First(¬ice, id).Error; err != nil {
|
||||
return nil, errors.New("通知不存在")
|
||||
}
|
||||
return ¬ice, nil
|
||||
}
|
||||
|
||||
func (s *NoticeService) GetNoticeList(examID, page, pageSize int) ([]model.ExamNotice, int64, error) {
|
||||
var notices []model.ExamNotice
|
||||
var total int64
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
query := dao.DB.Model(&model.ExamNotice{}).Preload("Exam")
|
||||
|
||||
if examID > 0 {
|
||||
query = query.Where("exam_id = ?", examID)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(pageSize).Order("id DESC").Find(¬ices).Error
|
||||
return notices, total, err
|
||||
}
|
||||
|
||||
func (s *NoticeService) UpdateNotice(id uint64, updates map[string]interface{}) error {
|
||||
return dao.DB.Model(&model.ExamNotice{}).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
|
||||
func (s *NoticeService) DeleteNotice(id uint64) error {
|
||||
return dao.DB.Delete(&model.ExamNotice{}, id).Error
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"exam_registration/internal/dao"
|
||||
"exam_registration/internal/model"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type RegistrationService struct{}
|
||||
|
||||
func (s *RegistrationService) CreateRegistration(userID, examID uint64, remark string) error {
|
||||
// 检查考试是否存在
|
||||
var exam model.Exam
|
||||
if err := dao.DB.First(&exam, examID).Error; err != nil {
|
||||
return errors.New("考试不存在")
|
||||
}
|
||||
|
||||
// 检查报名时间
|
||||
now := time.Now()
|
||||
if now.Before(exam.RegistrationStart) {
|
||||
return errors.New("尚未开始报名")
|
||||
}
|
||||
if now.After(exam.RegistrationEnd) {
|
||||
return errors.New("报名已截止")
|
||||
}
|
||||
|
||||
// 检查是否已报名
|
||||
var existingReg model.ExamRegistration
|
||||
if err := dao.DB.Where("user_id = ? AND exam_id = ?", userID, examID).First(&existingReg).Error; err == nil {
|
||||
return errors.New("您已经报过名了")
|
||||
}
|
||||
|
||||
// 检查考试容量
|
||||
if exam.MaxCandidates > 0 {
|
||||
var count int64
|
||||
dao.DB.Model(&model.ExamRegistration{}).Where("exam_id = ? AND status IN (?)", []int{0, 1}).Count(&count)
|
||||
if count >= int64(exam.MaxCandidates) {
|
||||
return errors.New("报名人数已满")
|
||||
}
|
||||
}
|
||||
|
||||
registration := model.ExamRegistration{
|
||||
UserID: userID,
|
||||
ExamID: examID,
|
||||
Status: 0, // 待审核
|
||||
PaymentStatus: 0, // 未支付
|
||||
Remark: remark,
|
||||
}
|
||||
|
||||
return dao.DB.Create(®istration).Error
|
||||
}
|
||||
|
||||
func (s *RegistrationService) GetRegistrationList(userID, examID, page, pageSize int) ([]model.ExamRegistration, int64, error) {
|
||||
var registrations []model.ExamRegistration
|
||||
var total int64
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
query := dao.DB.Model(&model.ExamRegistration{}).Preload("User").Preload("Exam")
|
||||
|
||||
if userID > 0 {
|
||||
query = query.Where("user_id = ?", userID)
|
||||
}
|
||||
if examID > 0 {
|
||||
query = query.Where("exam_id = ?", examID)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(pageSize).Order("id DESC").Find(®istrations).Error
|
||||
return registrations, total, err
|
||||
}
|
||||
|
||||
func (s *RegistrationService) AuditRegistration(regID uint64, status int, comment string) error {
|
||||
now := time.Now()
|
||||
updates := map[string]interface{}{
|
||||
"status": status,
|
||||
"audit_time": now,
|
||||
"audit_comment": comment,
|
||||
}
|
||||
|
||||
if status == 1 { // 审核通过,生成准考证号
|
||||
ticketNumber := fmt.Sprintf("TKT%d%d", regID, now.Unix())
|
||||
updates["ticket_number"] = ticketNumber
|
||||
// TODO: 编排考场座位
|
||||
}
|
||||
|
||||
return dao.DB.Model(&model.ExamRegistration{}).Where("id = ?", regID).Updates(updates).Error
|
||||
}
|
||||
|
||||
func (s *RegistrationService) UpdateRegistration(regID uint64, updates map[string]interface{}) error {
|
||||
return dao.DB.Model(&model.ExamRegistration{}).Where("id = ?", regID).Updates(updates).Error
|
||||
}
|
||||
|
||||
func (s *RegistrationService) DeleteRegistration(regID uint64) error {
|
||||
return dao.DB.Delete(&model.ExamRegistration{}, regID).Error
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"exam_registration/internal/dao"
|
||||
"exam_registration/internal/model"
|
||||
"errors"
|
||||
)
|
||||
|
||||
type ScoreService struct{}
|
||||
|
||||
func (s *ScoreService) CreateScore(score *model.ExamScore) error {
|
||||
// 检查是否已存在成绩
|
||||
var existingScore model.ExamScore
|
||||
if err := dao.DB.Where("user_id = ? AND exam_id = ?", score.UserID, score.ExamID).First(&existingScore).Error; err == nil {
|
||||
return errors.New("该用户该考试的成绩已存在")
|
||||
}
|
||||
|
||||
// 判断是否及格(假设 60 分及格)
|
||||
score.Pass = score.Score >= 60
|
||||
|
||||
return dao.DB.Create(score).Error
|
||||
}
|
||||
|
||||
func (s *ScoreService) BatchCreateScores(scores []model.ExamScore) error {
|
||||
return dao.DB.Transaction(func(tx *dao.DB) error {
|
||||
for i := range scores {
|
||||
scores[i].Pass = scores[i].Score >= 60
|
||||
|
||||
// 检查是否已存在
|
||||
var existing model.ExamScore
|
||||
if err := tx.Where("user_id = ? AND exam_id = ?", scores[i].UserID, scores[i].ExamID).First(&existing).Error; err == nil {
|
||||
continue // 跳过已存在的记录
|
||||
}
|
||||
|
||||
if err := tx.Create(&scores[i]).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
func (s *ScoreService) GetScoreByUserAndExam(userID, examID uint64) (*model.ExamScore, error) {
|
||||
var score model.ExamScore
|
||||
if err := dao.DB.Preload("User").Preload("Exam").Where("user_id = ? AND exam_id = ?", userID, examID).First(&score).Error; err != nil {
|
||||
return nil, errors.New("成绩不存在")
|
||||
}
|
||||
return &score, nil
|
||||
}
|
||||
|
||||
func (s *ScoreService) GetScoreList(examID, page, pageSize int) ([]model.ExamScore, int64, error) {
|
||||
var scores []model.ExamScore
|
||||
var total int64
|
||||
|
||||
offset := (page - 1) * pageSize
|
||||
query := dao.DB.Model(&model.ExamScore{}).Preload("User").Preload("Exam")
|
||||
|
||||
if examID > 0 {
|
||||
query = query.Where("exam_id = ?", examID)
|
||||
}
|
||||
|
||||
if err := query.Count(&total).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
err := query.Offset(offset).Limit(pageSize).Order("score DESC").Find(&scores).Error
|
||||
|
||||
// 计算排名
|
||||
for i := range scores {
|
||||
scores[i].Rank = offset + i + 1
|
||||
}
|
||||
|
||||
return scores, total, err
|
||||
}
|
||||
|
||||
func (s *ScoreService) UpdateScore(id uint64, updates map[string]interface{}) error {
|
||||
return dao.DB.Model(&model.ExamScore{}).Where("id = ?", id).Updates(updates).Error
|
||||
}
|
||||
|
||||
func (s *ScoreService) PublishScore(id uint64) error {
|
||||
return dao.DB.Model(&model.ExamScore{}).Where("id = ?", id).Update("published", true).Error
|
||||
}
|
||||
|
||||
func (s *ScoreService) DeleteScore(id uint64) error {
|
||||
return dao.DB.Delete(&model.ExamScore{}, id).Error
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"exam_registration/internal/dao"
|
||||
"exam_registration/internal/model"
|
||||
"errors"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type UserService struct{}
|
||||
|
||||
type LoginRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
type RegisterRequest struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
RealName string `json:"real_name"`
|
||||
IDCard string `json:"id_card"`
|
||||
}
|
||||
|
||||
func (s *UserService) Login(req *LoginRequest) (string, error) {
|
||||
var user model.User
|
||||
if err := dao.DB.Where("username = ?", req.Username).First(&user).Error; err != nil {
|
||||
return "", errors.New("用户名或密码错误")
|
||||
}
|
||||
|
||||
if err := bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)); err != nil {
|
||||
return "", errors.New("用户名或密码错误")
|
||||
}
|
||||
|
||||
if user.Status != 1 {
|
||||
return "", errors.New("账号已被禁用")
|
||||
}
|
||||
|
||||
// 生成 JWT token
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": user.ID,
|
||||
"username": user.Username,
|
||||
"role": user.Role,
|
||||
"exp": time.Now().Add(time.Duration(viper.GetInt("jwt.expire")) * time.Second).Unix(),
|
||||
"issued_at": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
return token.SignedString([]byte(viper.GetString("jwt.secret")))
|
||||
}
|
||||
|
||||
func (s *UserService) Register(req *RegisterRequest) error {
|
||||
var existingUser model.User
|
||||
if err := dao.DB.Where("username = ?", req.Username).First(&existingUser).Error; err == nil {
|
||||
return errors.New("用户名已存在")
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return errors.New("密码加密失败")
|
||||
}
|
||||
|
||||
user := model.User{
|
||||
Username: req.Username,
|
||||
Password: string(hashedPassword),
|
||||
Email: req.Email,
|
||||
Phone: req.Phone,
|
||||
RealName: req.RealName,
|
||||
IDCard: req.IDCard,
|
||||
Role: "user",
|
||||
Status: 1,
|
||||
}
|
||||
|
||||
return dao.DB.Create(&user).Error
|
||||
}
|
||||
|
||||
func (s *UserService) GetUserByID(userID uint64) (*model.User, error) {
|
||||
var user model.User
|
||||
if err := dao.DB.First(&user, userID).Error; err != nil {
|
||||
return nil, errors.New("用户不存在")
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (s *UserService) UpdateUser(userID uint64, updates map[string]interface{}) error {
|
||||
return dao.DB.Model(&model.User{}).Where("id = ?", userID).Updates(updates).Error
|
||||
}
|
||||
Reference in New Issue
Block a user