160 lines
4.3 KiB
Go
160 lines
4.3 KiB
Go
|
|
package entities
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"encoding/json"
|
|||
|
|
"time"
|
|||
|
|
|
|||
|
|
"github.com/google/uuid"
|
|||
|
|
"gorm.io/gorm"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
// ProductApiConfig 产品API配置实体
|
|||
|
|
type ProductApiConfig struct {
|
|||
|
|
ID string `gorm:"primaryKey;type:varchar(36)" comment:"配置ID"`
|
|||
|
|
ProductID string `gorm:"type:varchar(36);not null;uniqueIndex" comment:"产品ID"`
|
|||
|
|
|
|||
|
|
// 请求参数配置
|
|||
|
|
RequestParams string `gorm:"type:json;not null" comment:"请求参数配置JSON"`
|
|||
|
|
|
|||
|
|
// 响应字段配置
|
|||
|
|
ResponseFields string `gorm:"type:json;not null" comment:"响应字段配置JSON"`
|
|||
|
|
|
|||
|
|
// 响应示例
|
|||
|
|
ResponseExample string `gorm:"type:json;not null" comment:"响应示例JSON"`
|
|||
|
|
|
|||
|
|
// 关联关系
|
|||
|
|
Product *Product `gorm:"foreignKey:ProductID" comment:"产品"`
|
|||
|
|
|
|||
|
|
CreatedAt time.Time `gorm:"autoCreateTime" comment:"创建时间"`
|
|||
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" comment:"更新时间"`
|
|||
|
|
DeletedAt gorm.DeletedAt `gorm:"index" comment:"软删除时间"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// RequestParam 请求参数结构
|
|||
|
|
type RequestParam struct {
|
|||
|
|
Name string `json:"name" comment:"参数名称"`
|
|||
|
|
Field string `json:"field" comment:"参数字段名"`
|
|||
|
|
Type string `json:"type" comment:"参数类型"`
|
|||
|
|
Required bool `json:"required" comment:"是否必填"`
|
|||
|
|
Description string `json:"description" comment:"参数描述"`
|
|||
|
|
Example string `json:"example" comment:"参数示例"`
|
|||
|
|
Validation string `json:"validation" comment:"验证规则"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ResponseField 响应字段结构
|
|||
|
|
type ResponseField struct {
|
|||
|
|
Name string `json:"name" comment:"字段名称"`
|
|||
|
|
Path string `json:"path" comment:"字段路径"`
|
|||
|
|
Type string `json:"type" comment:"字段类型"`
|
|||
|
|
Description string `json:"description" comment:"字段描述"`
|
|||
|
|
Required bool `json:"required" comment:"是否必填"`
|
|||
|
|
Example string `json:"example" comment:"字段示例"`
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// BeforeCreate GORM钩子:创建前自动生成UUID
|
|||
|
|
func (pac *ProductApiConfig) BeforeCreate(tx *gorm.DB) error {
|
|||
|
|
if pac.ID == "" {
|
|||
|
|
pac.ID = uuid.New().String()
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
|
|||
|
|
|
|||
|
|
// Validate 验证产品API配置
|
|||
|
|
func (pac *ProductApiConfig) Validate() error {
|
|||
|
|
if pac.ProductID == "" {
|
|||
|
|
return NewValidationError("产品ID不能为空")
|
|||
|
|
}
|
|||
|
|
if pac.RequestParams == "" {
|
|||
|
|
return NewValidationError("请求参数配置不能为空")
|
|||
|
|
}
|
|||
|
|
if pac.ResponseFields == "" {
|
|||
|
|
return NewValidationError("响应字段配置不能为空")
|
|||
|
|
}
|
|||
|
|
if pac.ResponseExample == "" {
|
|||
|
|
return NewValidationError("响应示例不能为空")
|
|||
|
|
}
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// NewValidationError 创建验证错误
|
|||
|
|
func NewValidationError(message string) error {
|
|||
|
|
return &ValidationError{Message: message}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// ValidationError 验证错误
|
|||
|
|
type ValidationError struct {
|
|||
|
|
Message string
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func (e *ValidationError) Error() string {
|
|||
|
|
return e.Message
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetRequestParams 获取请求参数列表
|
|||
|
|
func (pac *ProductApiConfig) GetRequestParams() ([]RequestParam, error) {
|
|||
|
|
var params []RequestParam
|
|||
|
|
if pac.RequestParams != "" {
|
|||
|
|
err := json.Unmarshal([]byte(pac.RequestParams), ¶ms)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return params, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetRequestParams 设置请求参数列表
|
|||
|
|
func (pac *ProductApiConfig) SetRequestParams(params []RequestParam) error {
|
|||
|
|
data, err := json.Marshal(params)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
pac.RequestParams = string(data)
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetResponseFields 获取响应字段列表
|
|||
|
|
func (pac *ProductApiConfig) GetResponseFields() ([]ResponseField, error) {
|
|||
|
|
var fields []ResponseField
|
|||
|
|
if pac.ResponseFields != "" {
|
|||
|
|
err := json.Unmarshal([]byte(pac.ResponseFields), &fields)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return fields, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetResponseFields 设置响应字段列表
|
|||
|
|
func (pac *ProductApiConfig) SetResponseFields(fields []ResponseField) error {
|
|||
|
|
data, err := json.Marshal(fields)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
pac.ResponseFields = string(data)
|
|||
|
|
return nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// GetResponseExample 获取响应示例
|
|||
|
|
func (pac *ProductApiConfig) GetResponseExample() (map[string]interface{}, error) {
|
|||
|
|
var example map[string]interface{}
|
|||
|
|
if pac.ResponseExample != "" {
|
|||
|
|
err := json.Unmarshal([]byte(pac.ResponseExample), &example)
|
|||
|
|
if err != nil {
|
|||
|
|
return nil, err
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return example, nil
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// SetResponseExample 设置响应示例
|
|||
|
|
func (pac *ProductApiConfig) SetResponseExample(example map[string]interface{}) error {
|
|||
|
|
data, err := json.Marshal(example)
|
|||
|
|
if err != nil {
|
|||
|
|
return err
|
|||
|
|
}
|
|||
|
|
pac.ResponseExample = string(data)
|
|||
|
|
return nil
|
|||
|
|
}
|