Archived
init
This commit is contained in:
commit
3d1d4cf506
53 files changed
+7105
No files matched your search
@@ -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
|
||||
}
|
||||
Loaded 3 of 53 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user