236 lines
6.3 KiB
Go
236 lines
6.3 KiB
Go
package dao
|
|
|
|
import (
|
|
"beacon/models"
|
|
"encoding/json"
|
|
"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.ApiTestParameters").Preload("Steps.UiTestParameters").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.ApiTestParameters").Preload("Steps.UiTestParameters").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.Preload("ApiTestParameters").Preload("UiTestParameters").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
|
|
}
|
|
|
|
// CreateApiTestParameters 创建API测试参数
|
|
func CreateApiTestParameters(tx *gorm.DB, params *models.ApiTestParameters) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
return tx.Create(params).Error
|
|
}
|
|
|
|
// CreateUiTestParameters 创建UI测试参数
|
|
func CreateUiTestParameters(tx *gorm.DB, params *models.UiTestParameters) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
return tx.Create(params).Error
|
|
}
|
|
|
|
// UpdateApiTestParameters 更新API测试参数
|
|
func UpdateApiTestParameters(tx *gorm.DB, stepID uint, params *models.ApiTestParametersRequest) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
// 转换headers和query_params为JSON字符串
|
|
headersJson, _ := json.Marshal(params.Headers)
|
|
queryParamsJson, _ := json.Marshal(params.QueryParams)
|
|
|
|
updates := map[string]interface{}{
|
|
"endpoint": params.Endpoint,
|
|
"http_method": params.HttpMethod,
|
|
"headers": string(headersJson),
|
|
"query_params": string(queryParamsJson),
|
|
"body": params.Body,
|
|
"timeout": params.Timeout,
|
|
"retry_count": params.RetryCount,
|
|
}
|
|
|
|
return tx.Model(&models.ApiTestParameters{}).Where("step_id = ?", stepID).Updates(updates).Error
|
|
}
|
|
|
|
// UpdateUiTestParameters 更新UI测试参数
|
|
func UpdateUiTestParameters(tx *gorm.DB, stepID uint, params *models.UiTestParametersRequest) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
|
|
updates := map[string]interface{}{
|
|
"selector": params.Selector,
|
|
"selector_type": params.SelectorType,
|
|
"event_type": params.EventType,
|
|
"input_value": params.InputValue,
|
|
"offset_x": params.OffsetX,
|
|
"offset_y": params.OffsetY,
|
|
"wait_time_seconds": params.WaitTimeSeconds,
|
|
"screenshot_name": params.ScreenshotName,
|
|
"assertion_type": params.AssertionType,
|
|
"assertion_value": params.AssertionValue,
|
|
}
|
|
|
|
return tx.Model(&models.UiTestParameters{}).Where("step_id = ?", stepID).Updates(updates).Error
|
|
}
|
|
|
|
// DeleteApiTestParametersByStepID 根据步骤ID删除API测试参数
|
|
func DeleteApiTestParametersByStepID(tx *gorm.DB, stepID uint) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
return tx.Where("step_id = ?", stepID).Delete(&models.ApiTestParameters{}).Error
|
|
}
|
|
|
|
// DeleteUiTestParametersByStepID 根据步骤ID删除UI测试参数
|
|
func DeleteUiTestParametersByStepID(tx *gorm.DB, stepID uint) error {
|
|
if tx == nil {
|
|
tx = DB
|
|
}
|
|
return tx.Where("step_id = ?", stepID).Delete(&models.UiTestParameters{}).Error
|
|
}
|
|
|
|
// BeginTransaction 开启事务
|
|
func BeginTransaction() *gorm.DB {
|
|
return DB.Begin()
|
|
}
|