158 lines
3.8 KiB
Go
158 lines
3.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"beacon/models"
|
|
"errors"
|
|
"fmt"
|
|
"gorm.io/gorm"
|
|
"strconv"
|
|
)
|
|
|
|
// CreateCompositeCase 创建复合案例
|
|
func CreateCompositeCase(tx *gorm.DB, compositeCase *models.CompositeCase) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
if compositeCase.Status == "" {
|
|
compositeCase.Status = "active"
|
|
}
|
|
|
|
return tx.Create(compositeCase).Error
|
|
}
|
|
|
|
// GetCompositeCaseByID 根据ID获取复合案例
|
|
func GetCompositeCaseByID(id uint) (*models.CompositeCase, error) {
|
|
var compositeCase models.CompositeCase
|
|
|
|
err := DB.Preload("Steps", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("step_order ASC")
|
|
}).First(&compositeCase, id).Error
|
|
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fmt.Errorf("复合案例不存在")
|
|
}
|
|
return nil, fmt.Errorf("获取复合案例失败: %w", err)
|
|
}
|
|
|
|
return &compositeCase, nil
|
|
}
|
|
|
|
// UpdateCompositeCase 更新复合案例基本信息
|
|
func UpdateCompositeCase(tx *gorm.DB, id uint, updates map[string]interface{}) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
if len(updates) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return tx.Model(&models.CompositeCase{}).Where("id = ?", id).Updates(updates).Error
|
|
}
|
|
|
|
// DeleteCompositeCase 删除复合案例
|
|
func DeleteCompositeCase(tx *gorm.DB, id uint) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
return tx.Delete(&models.CompositeCase{}, id).Error
|
|
}
|
|
|
|
// ListCompositeCases 获取复合案例列表
|
|
func ListCompositeCases(page, pageSize int, status string) ([]models.CompositeCase, int64, error) {
|
|
var compositeCases []models.CompositeCase
|
|
var total int64
|
|
|
|
query := DB.Model(&models.CompositeCase{})
|
|
|
|
if status != "" {
|
|
query = query.Where("status = ?", status)
|
|
}
|
|
|
|
// 获取总数
|
|
if err := query.Count(&total).Error; err != nil {
|
|
return nil, 0, fmt.Errorf("获取复合案例总数失败: %w", err)
|
|
}
|
|
|
|
// 分页查询
|
|
offset := (page - 1) * pageSize
|
|
err := query.Preload("Steps", func(db *gorm.DB) *gorm.DB {
|
|
return db.Order("step_order ASC")
|
|
}).Offset(offset).Limit(pageSize).Order("created_at DESC").Find(&compositeCases).Error
|
|
|
|
if err != nil {
|
|
return nil, 0, fmt.Errorf("获取复合案例列表失败: %w", err)
|
|
}
|
|
|
|
return compositeCases, total, nil
|
|
}
|
|
|
|
// ExistsCompositeCase 检查复合案例是否存在
|
|
func ExistsCompositeCase(tx *gorm.DB, id uint) (*models.CompositeCase, error) {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
var compositeCase models.CompositeCase
|
|
err := tx.First(&compositeCase, id).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, fmt.Errorf("复合案例不存在")
|
|
}
|
|
return nil, fmt.Errorf("查询复合案例失败: %w", err)
|
|
}
|
|
|
|
return &compositeCase, nil
|
|
}
|
|
|
|
// CreateCompositeCaseSteps 创建复合案例步骤
|
|
func CreateCompositeCaseSteps(tx *gorm.DB, steps []models.CompositeCaseStep) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
if len(steps) == 0 {
|
|
return nil
|
|
}
|
|
|
|
return tx.Create(&steps).Error
|
|
}
|
|
|
|
// GetCompositeCaseSteps 根据复合案例ID获取步骤列表
|
|
func GetCompositeCaseSteps(compositeCaseId string) ([]models.CompositeCaseStep, error) {
|
|
var steps []models.CompositeCaseStep
|
|
|
|
// 将字符串ID转换为uint
|
|
id, err := strconv.ParseUint(compositeCaseId, 10, 32)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid composite case id: %w", err)
|
|
}
|
|
|
|
err = DB.Where("composite_case_id = ?", uint(id)).
|
|
Order("step_order ASC").
|
|
Find(&steps).Error
|
|
|
|
if err != nil {
|
|
return nil, fmt.Errorf("获取复合案例步骤失败: %w", err)
|
|
}
|
|
|
|
return steps, nil
|
|
}
|
|
|
|
// DeleteCompositeCaseStepsByCompositeCaseID 根据复合案例ID删除所有步骤
|
|
func DeleteCompositeCaseStepsByCompositeCaseID(tx *gorm.DB, compositeCaseId uint) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
return tx.Where("composite_case_id = ?", compositeCaseId).Delete(&models.CompositeCaseStep{}).Error
|
|
}
|
|
|
|
// BeginTransaction 开启事务
|
|
func BeginTransaction() *gorm.DB {
|
|
return DB.Begin()
|
|
}
|