183 lines
5.7 KiB
Go
183 lines
5.7 KiB
Go
// Package service 业务逻辑层
|
|
package service
|
|
|
|
import (
|
|
"fmt"
|
|
"server/common"
|
|
"server/modules/yx/dto"
|
|
"server/modules/yx/entity"
|
|
"server/modules/yx/mapper"
|
|
"time"
|
|
)
|
|
|
|
// IScoreService 定义成绩服务的接口,用于解耦
|
|
type IScoreService interface {
|
|
GetByID(id string) (interface{}, error)
|
|
GetActiveScoreByID(userID string) (entity.YxUserScore, error)
|
|
GetActiveScoreID(userID string) (interface{}, error)
|
|
UpdateFields(id string, fields map[string]interface{}) error
|
|
Delete(id string) error
|
|
}
|
|
|
|
type YxVolunteerService struct {
|
|
mapper *mapper.YxVolunteerMapper
|
|
volunteerRecordMapper *mapper.YxVolunteerRecordMapper
|
|
}
|
|
|
|
func NewYxVolunteerService() *YxVolunteerService {
|
|
return &YxVolunteerService{mapper: mapper.NewYxVolunteerMapper(), volunteerRecordMapper: mapper.NewYxVolunteerRecordMapper()}
|
|
}
|
|
|
|
func (s *YxVolunteerService) List(page, size int) ([]entity.YxVolunteer, int64, error) {
|
|
return s.mapper.FindAll(page, size)
|
|
}
|
|
|
|
func (s *YxVolunteerService) GetByID(id string) (*entity.YxVolunteer, error) {
|
|
return s.mapper.FindByID(id)
|
|
}
|
|
|
|
func (s *YxVolunteerService) Create(item *entity.YxVolunteer) error {
|
|
item.ID = common.GenerateStringID()
|
|
return s.mapper.Create(item)
|
|
}
|
|
|
|
func (s *YxVolunteerService) FindActiveByScoreId(scoreId string) (*entity.YxVolunteer, error) {
|
|
return s.mapper.FindActiveByScoreId(scoreId)
|
|
}
|
|
|
|
func (s *YxVolunteerService) Update(item *entity.YxVolunteer) error {
|
|
return s.mapper.Update(item)
|
|
}
|
|
|
|
func (s *YxVolunteerService) UpdateFields(id string, fields map[string]interface{}) error {
|
|
return s.mapper.UpdateFields(id, fields)
|
|
}
|
|
|
|
func (s *YxVolunteerService) Delete(id string) error {
|
|
return s.mapper.Delete(id)
|
|
}
|
|
|
|
func (s *YxVolunteerService) BatchCreate(items []entity.YxVolunteer) error {
|
|
for i := range items {
|
|
items[i].ID = common.GenerateStringID()
|
|
}
|
|
return s.mapper.BatchCreate(items, 100)
|
|
}
|
|
|
|
func (s *YxVolunteerService) BatchUpdate(items []entity.YxVolunteer) error {
|
|
return s.mapper.BatchUpdate(items)
|
|
}
|
|
|
|
func (s *YxVolunteerService) BatchUpsert(items []entity.YxVolunteer, updateColumns []string) error {
|
|
for i := range items {
|
|
if items[i].ID == "" {
|
|
items[i].ID = common.GenerateStringID()
|
|
}
|
|
}
|
|
return s.mapper.BatchUpsert(items, updateColumns)
|
|
}
|
|
|
|
func (s *YxVolunteerService) BatchDelete(ids []string) error {
|
|
return s.mapper.BatchDelete(ids)
|
|
}
|
|
|
|
// 根据ScoreId创建新志愿表
|
|
func (s *YxVolunteerService) CreateByScoreId(scoreId string, userId string) error {
|
|
volunteer := entity.YxVolunteer{}
|
|
volunteer.ID = common.GenerateStringID()
|
|
// 志愿表名称格式 时间戳 20260101134501志愿表
|
|
volunteer.VolunteerName = time.Now().Format("20060102150405") + "志愿表"
|
|
|
|
volunteer.ScoreId = scoreId
|
|
volunteer.CreateType = "1"
|
|
volunteer.State = "1"
|
|
volunteer.CreateBy = userId
|
|
volunteer.CreateTime = time.Now()
|
|
volunteer.UpdateTime = time.Now()
|
|
|
|
// 先关闭当前用户其他志愿单 - ✅ 检查错误
|
|
if err := s.mapper.CloseOtherVolunteer(userId); err != nil {
|
|
return fmt.Errorf("关闭其他志愿表失败: %w", err)
|
|
}
|
|
// 创建志愿表
|
|
return s.mapper.Create(&volunteer)
|
|
}
|
|
func (s *YxVolunteerService) UpdateName(id, name, userID string) error {
|
|
volunteer, err := s.mapper.FindByID(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if volunteer.CreateBy != userID {
|
|
return fmt.Errorf("无权修改该志愿单")
|
|
}
|
|
return s.mapper.UpdateFields(id, map[string]interface{}{"volunteer_name": name, "update_by": userID, "update_time": time.Now()})
|
|
}
|
|
|
|
func (s *YxVolunteerService) ListByUser(userID string, page, size int) ([]dto.UserVolunteerVO, int64, error) {
|
|
return s.mapper.ListByUser(userID, page, size)
|
|
}
|
|
|
|
func (s *YxVolunteerService) DeleteVolunteer(id, userID string) error {
|
|
volunteer, err := s.mapper.FindByID(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if volunteer.CreateBy != userID {
|
|
return fmt.Errorf("无权删除该志愿单")
|
|
}
|
|
if volunteer.State == "1" {
|
|
return fmt.Errorf("激活状态的志愿单不可删除")
|
|
}
|
|
s.volunteerRecordMapper.BatchDeleteByVolunteerId(id)
|
|
s.mapper.Delete(id)
|
|
// 1. 获取成绩单信息
|
|
//scoreObj, err := scoreService.GetByID(volunteer.ScoreId)
|
|
// if err != nil {
|
|
// return fmt.Errorf("获取成绩单失败: %w", err)
|
|
// }
|
|
// _ = scoreObj // temporarily ignore until cascade logic is refined
|
|
|
|
// 这里需要从 scoreObj 中获取 CalculationTableName 和 ID
|
|
// 由于是 interface{},可以通过反射或简单断言(如果能在 yx 定义共用结构最好)
|
|
// 或者直接在 scoreService 中增加一个获取这些字段的方法
|
|
|
|
// 简化:这里我们假设 scoreObj 其实包含了这些信息。
|
|
// 为了真正的解耦,我们可以在 scoreService 中增加专门的方法。
|
|
return nil // 暂时回退复杂逻辑
|
|
}
|
|
|
|
func (s *YxVolunteerService) SwitchVolunteer(id, userID string, scoreService IScoreService) error {
|
|
if err := s.mapper.CloseOtherVolunteer(userID); err != nil {
|
|
return err
|
|
}
|
|
if err := s.mapper.UpdateFields(id, map[string]interface{}{"state": "1", "update_by": userID, "update_time": time.Now()}); err != nil {
|
|
return err
|
|
}
|
|
|
|
volunteer, err := s.mapper.FindByID(id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// 获取之前的激活成绩并关闭
|
|
scoreObj, err := scoreService.GetActiveScoreID(userID)
|
|
if err != nil {
|
|
return fmt.Errorf("获取激活成绩失败: %w", err)
|
|
}
|
|
var activeScoreID string
|
|
if scoreObj != nil {
|
|
// 尝试从 VO 对象中提取 ID
|
|
if vo, ok := scoreObj.(map[string]interface{}); ok {
|
|
if id, ok := vo["id"].(string); ok {
|
|
activeScoreID = id
|
|
}
|
|
} else if vo, ok := scoreObj.(struct{ ID string }); ok {
|
|
activeScoreID = vo.ID
|
|
}
|
|
}
|
|
if activeScoreID != "" && activeScoreID != volunteer.ScoreId {
|
|
scoreService.UpdateFields(activeScoreID, map[string]interface{}{"state": "0"})
|
|
}
|
|
return scoreService.UpdateFields(volunteer.ScoreId, map[string]interface{}{"state": "1"})
|
|
}
|