v0.1
This commit is contained in:
157
scripts/init_product_api_configs.go
Normal file
157
scripts/init_product_api_configs.go
Normal file
@@ -0,0 +1,157 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"log"
|
||||
"tyapi-server/internal/config"
|
||||
"tyapi-server/internal/domains/product/entities"
|
||||
"tyapi-server/internal/infrastructure/database"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// 加载配置
|
||||
cfg, err := config.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("加载配置失败: %v", err)
|
||||
}
|
||||
|
||||
// 连接数据库
|
||||
db, err := database.NewConnection(database.Config{
|
||||
Host: cfg.Database.Host,
|
||||
Port: cfg.Database.Port,
|
||||
User: cfg.Database.User,
|
||||
Password: cfg.Database.Password,
|
||||
Name: cfg.Database.Name,
|
||||
SSLMode: cfg.Database.SSLMode,
|
||||
Timezone: cfg.Database.Timezone,
|
||||
MaxOpenConns: cfg.Database.MaxOpenConns,
|
||||
MaxIdleConns: cfg.Database.MaxIdleConns,
|
||||
ConnMaxLifetime: cfg.Database.ConnMaxLifetime,
|
||||
})
|
||||
if err != nil {
|
||||
log.Fatalf("连接数据库失败: %v", err)
|
||||
}
|
||||
|
||||
// 获取所有产品
|
||||
var products []entities.Product
|
||||
if err := db.Find(&products).Error; err != nil {
|
||||
log.Fatalf("获取产品失败: %v", err)
|
||||
}
|
||||
log.Printf("找到 %d 个产品", len(products))
|
||||
|
||||
for _, product := range products {
|
||||
// 检查是否已有API配置
|
||||
var count int64
|
||||
db.Model(&entities.ProductApiConfig{}).Where("product_id = ?", product.ID).Count(&count)
|
||||
if count > 0 {
|
||||
log.Printf("产品已有API配置,跳过: %s (%s)", product.Name, product.Code)
|
||||
continue
|
||||
}
|
||||
|
||||
// 创建默认请求参数
|
||||
requestParams := []entities.RequestParam{
|
||||
{
|
||||
Name: "姓名",
|
||||
Field: "name",
|
||||
Type: "text",
|
||||
Required: true,
|
||||
Description: "用户真实姓名",
|
||||
Example: "张三",
|
||||
Validation: "^[\\u4e00-\\u9fa5]{2,4}$",
|
||||
},
|
||||
{
|
||||
Name: "身份证号",
|
||||
Field: "id_card",
|
||||
Type: "idcard",
|
||||
Required: true,
|
||||
Description: "用户身份证号码",
|
||||
Example: "110101199001011234",
|
||||
Validation: "^[1-9]\\d{5}(18|19|20)\\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\\d{3}[0-9Xx]$",
|
||||
},
|
||||
{
|
||||
Name: "手机号",
|
||||
Field: "mobile",
|
||||
Type: "phone",
|
||||
Required: true,
|
||||
Description: "用户手机号码",
|
||||
Example: "13800138000",
|
||||
Validation: "^1[3-9]\\d{9}$",
|
||||
},
|
||||
}
|
||||
|
||||
// 创建默认响应字段
|
||||
responseFields := []entities.ResponseField{
|
||||
{
|
||||
Name: "结果",
|
||||
Path: "result",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Description: "查询结果",
|
||||
Example: "success",
|
||||
},
|
||||
{
|
||||
Name: "状态码",
|
||||
Path: "code",
|
||||
Type: "number",
|
||||
Required: true,
|
||||
Description: "响应状态码",
|
||||
Example: "200",
|
||||
},
|
||||
{
|
||||
Name: "消息",
|
||||
Path: "message",
|
||||
Type: "string",
|
||||
Required: true,
|
||||
Description: "响应消息",
|
||||
Example: "查询成功",
|
||||
},
|
||||
{
|
||||
Name: "数据",
|
||||
Path: "data",
|
||||
Type: "object",
|
||||
Required: false,
|
||||
Description: "查询结果数据",
|
||||
Example: "{}",
|
||||
},
|
||||
}
|
||||
|
||||
// 创建默认响应示例
|
||||
responseExample := map[string]interface{}{
|
||||
"result": "success",
|
||||
"code": 200,
|
||||
"message": "查询成功",
|
||||
"data": map[string]interface{}{
|
||||
"name": "张三",
|
||||
"id_card": "110101199001011234",
|
||||
"mobile": "13800138000",
|
||||
"status": "正常",
|
||||
"query_time": "2024-01-01 12:00:00",
|
||||
},
|
||||
}
|
||||
|
||||
// 序列化为JSON
|
||||
requestParamsJSON, _ := json.Marshal(requestParams)
|
||||
responseFieldsJSON, _ := json.Marshal(responseFields)
|
||||
responseExampleJSON, _ := json.Marshal(responseExample)
|
||||
|
||||
// 创建API配置
|
||||
apiConfig := entities.ProductApiConfig{
|
||||
ID: uuid.New().String(),
|
||||
ProductID: product.ID,
|
||||
RequestParams: string(requestParamsJSON),
|
||||
ResponseFields: string(responseFieldsJSON),
|
||||
ResponseExample: string(responseExampleJSON),
|
||||
}
|
||||
|
||||
// 保存到数据库
|
||||
if err := db.Create(&apiConfig).Error; err != nil {
|
||||
log.Printf("创建产品API配置失败: %s, 错误: %v", product.Name, err)
|
||||
} else {
|
||||
log.Printf("成功创建产品API配置: %s (%s)", product.Name, product.Code)
|
||||
}
|
||||
}
|
||||
|
||||
log.Println("产品API配置初始化完成")
|
||||
}
|
||||
Reference in New Issue
Block a user