121 lines
3.1 KiB
Go
121 lines
3.1 KiB
Go
package service
|
||
|
||
import (
|
||
"fmt"
|
||
|
||
"dd_fiber_api/internal/question"
|
||
"dd_fiber_api/internal/question/dao"
|
||
)
|
||
|
||
// MaterialService 材料服务
|
||
type MaterialService struct {
|
||
materialDAO dao.MaterialDAOInterface
|
||
}
|
||
|
||
// NewMaterialService 创建材料服务
|
||
func NewMaterialService(materialDAO dao.MaterialDAOInterface) *MaterialService {
|
||
return &MaterialService{
|
||
materialDAO: materialDAO,
|
||
}
|
||
}
|
||
|
||
// CreateMaterialRequest 创建材料请求
|
||
type CreateMaterialRequest struct {
|
||
Type string `json:"type"` // 材料类型:objective 或 subjective
|
||
Name string `json:"name"`
|
||
Content string `json:"content"`
|
||
}
|
||
|
||
// UpdateMaterialRequest 更新材料请求
|
||
type UpdateMaterialRequest struct {
|
||
ID string `json:"id"`
|
||
Type string `json:"type"` // 材料类型:objective 或 subjective
|
||
Name string `json:"name"`
|
||
Content string `json:"content"`
|
||
}
|
||
|
||
// CreateMaterial 创建材料
|
||
func (s *MaterialService) CreateMaterial(req *CreateMaterialRequest) (*question.Material, error) {
|
||
materialID := question.GenerateID()
|
||
|
||
materialType := question.MaterialType(req.Type)
|
||
// 如果没有指定类型,默认为 objective(兼容旧数据)
|
||
if materialType == "" {
|
||
materialType = question.MaterialTypeObjective
|
||
}
|
||
|
||
materialObj := &question.Material{
|
||
ID: materialID,
|
||
Type: materialType,
|
||
Name: req.Name,
|
||
Content: req.Content,
|
||
CreatedAt: question.GetCurrentTimestamp(),
|
||
UpdatedAt: question.GetCurrentTimestamp(),
|
||
}
|
||
|
||
err := s.materialDAO.Create(materialObj)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("创建材料失败: %v", err)
|
||
}
|
||
|
||
return materialObj, nil
|
||
}
|
||
|
||
// GetMaterial 获取材料
|
||
func (s *MaterialService) GetMaterial(id string) (*question.Material, error) {
|
||
material, err := s.materialDAO.GetByID(id)
|
||
if err != nil {
|
||
return nil, fmt.Errorf("材料不存在: %v", err)
|
||
}
|
||
|
||
return material, nil
|
||
}
|
||
|
||
// SearchMaterials 搜索材料
|
||
func (s *MaterialService) SearchMaterials(query string, materialType question.MaterialType, page, pageSize int32) ([]*question.Material, int32, error) {
|
||
materials, total, err := s.materialDAO.Search(query, materialType, page, pageSize)
|
||
if err != nil {
|
||
return nil, 0, fmt.Errorf("搜索材料失败: %v", err)
|
||
}
|
||
|
||
return materials, total, nil
|
||
}
|
||
|
||
// UpdateMaterial 更新材料
|
||
func (s *MaterialService) UpdateMaterial(req *UpdateMaterialRequest) error {
|
||
materialType := question.MaterialType(req.Type)
|
||
// 如果没有指定类型,保持原有类型(需要先查询)
|
||
if materialType == "" {
|
||
existing, err := s.materialDAO.GetByID(req.ID)
|
||
if err != nil {
|
||
return fmt.Errorf("获取材料失败: %v", err)
|
||
}
|
||
materialType = existing.Type
|
||
}
|
||
|
||
material := &question.Material{
|
||
ID: req.ID,
|
||
Type: materialType,
|
||
Name: req.Name,
|
||
Content: req.Content,
|
||
UpdatedAt: question.GetCurrentTimestamp(),
|
||
}
|
||
|
||
err := s.materialDAO.Update(material)
|
||
if err != nil {
|
||
return fmt.Errorf("更新材料失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|
||
|
||
// DeleteMaterial 删除材料
|
||
func (s *MaterialService) DeleteMaterial(id string) error {
|
||
err := s.materialDAO.Delete(id)
|
||
if err != nil {
|
||
return fmt.Errorf("删除材料失败: %v", err)
|
||
}
|
||
|
||
return nil
|
||
}
|