101 lines
2.8 KiB
Go
101 lines
2.8 KiB
Go
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
|
|
}
|