package service import ( "fmt" "dd_fiber_api/internal/camp" "dd_fiber_api/internal/camp/dao" "dd_fiber_api/pkg/snowflake" ) // SectionService 小节服务 type SectionService struct { sectionDAO *dao.SectionDAO campDAO *dao.CampDAO } // NewSectionService 创建小节服务 func NewSectionService(sectionDAO *dao.SectionDAO, campDAO *dao.CampDAO) *SectionService { return &SectionService{ sectionDAO: sectionDAO, campDAO: campDAO, } } // CreateSection 创建小节 func (s *SectionService) CreateSection(req *camp.CreateSectionRequest) (*camp.CreateSectionResponse, error) { sectionID := snowflake.GetInstance().NextIDString() section := &camp.Section{ ID: sectionID, CampID: req.CampID, Title: req.Title, SectionNumber: req.SectionNumber, PriceFen: req.PriceFen, RequirePreviousSection: req.RequirePreviousSection, TimeIntervalType: req.TimeIntervalType, TimeIntervalValue: req.TimeIntervalValue, } err := s.sectionDAO.Create(section) if err != nil { return &camp.CreateSectionResponse{ Success: false, Message: fmt.Sprintf("创建小节失败: %v", err), }, nil } // 更新打卡营的小节数量 if err := s.campDAO.UpdateSectionCount(req.CampID); err != nil { return &camp.CreateSectionResponse{ Success: false, Message: fmt.Sprintf("创建小节成功,但更新打卡营小节数量失败: %v", err), }, nil } return &camp.CreateSectionResponse{ ID: sectionID, Success: true, Message: "创建小节成功", }, nil } // GetSection 获取小节 func (s *SectionService) GetSection(id string) (*camp.GetSectionResponse, error) { section, err := s.sectionDAO.GetByID(id) if err != nil { return &camp.GetSectionResponse{ Success: false, Message: fmt.Sprintf("获取小节失败: %v", err), }, nil } return &camp.GetSectionResponse{ Section: section, Success: true, Message: "获取小节成功", }, nil } // UpdateSection 更新小节 func (s *SectionService) UpdateSection(req *camp.UpdateSectionRequest) (*camp.UpdateSectionResponse, error) { // 先获取原有小节信息,检查 camp_id 是否变更,并保留 require_previous_section 原值(该字段已从管理端移除) var oldCampID string var existingRequirePreviousSection bool if existingSection, err := s.sectionDAO.GetByID(req.ID); err == nil { oldCampID = existingSection.CampID existingRequirePreviousSection = existingSection.RequirePreviousSection } section := &camp.Section{ ID: req.ID, CampID: req.CampID, Title: req.Title, SectionNumber: req.SectionNumber, PriceFen: req.PriceFen, RequirePreviousSection: existingRequirePreviousSection, // 保留原值,不再接受请求体 TimeIntervalType: req.TimeIntervalType, TimeIntervalValue: req.TimeIntervalValue, } err := s.sectionDAO.Update(section) if err != nil { return &camp.UpdateSectionResponse{ Success: false, Message: fmt.Sprintf("更新小节失败: %v", err), }, nil } // 更新打卡营的小节数量 // 如果 camp_id 变更,需要更新两个打卡营的数量 if oldCampID != "" && oldCampID != req.CampID { // 更新原打卡营的小节数量 if err := s.campDAO.UpdateSectionCount(oldCampID); err != nil { return &camp.UpdateSectionResponse{ Success: false, Message: fmt.Sprintf("更新小节成功,但更新原打卡营小节数量失败: %v", err), }, nil } } // 更新新打卡营的小节数量 if err := s.campDAO.UpdateSectionCount(req.CampID); err != nil { return &camp.UpdateSectionResponse{ Success: false, Message: fmt.Sprintf("更新小节成功,但更新打卡营小节数量失败: %v", err), }, nil } return &camp.UpdateSectionResponse{ Success: true, Message: "更新小节成功", }, nil } // DeleteSection 删除小节 func (s *SectionService) DeleteSection(id string) (*camp.DeleteSectionResponse, error) { // 先获取小节信息,以便知道属于哪个打卡营 section, err := s.sectionDAO.GetByID(id) if err != nil { return &camp.DeleteSectionResponse{ Success: false, Message: fmt.Sprintf("获取小节信息失败: %v", err), }, nil } campID := section.CampID // TODO: 检查是否存在未删除的任务和用户进度 // 暂时先允许删除,后续可以添加检查逻辑 // 删除小节 err = s.sectionDAO.Delete(id) if err != nil { return &camp.DeleteSectionResponse{ Success: false, Message: fmt.Sprintf("删除小节失败: %v", err), }, nil } // 更新打卡营的小节数量 if err := s.campDAO.UpdateSectionCount(campID); err != nil { return &camp.DeleteSectionResponse{ Success: false, Message: fmt.Sprintf("删除小节成功,但更新打卡营小节数量失败: %v", err), }, nil } return &camp.DeleteSectionResponse{ Success: true, Message: "删除小节成功", }, nil } // ListSections 列出小节(支持关键词搜索和筛选) func (s *SectionService) ListSections(req *camp.ListSectionsRequest) (*camp.ListSectionsResponse, error) { // 设置默认值 if req.Page < 1 { req.Page = 1 } if req.PageSize < 1 { req.PageSize = 10 } if req.PageSize > 100 { req.PageSize = 100 } sections, total, err := s.sectionDAO.List(req.Keyword, req.CampID, req.Page, req.PageSize) if err != nil { return &camp.ListSectionsResponse{ Success: false, Message: fmt.Sprintf("获取小节列表失败: %v", err), }, nil } return &camp.ListSectionsResponse{ Sections: sections, Total: total, Success: true, Message: "获取小节列表成功", }, nil }