package tests import ( "healthapi/internal/model" "gorm.io/gorm" ) // SeedMallTestData 插入商城测试数据 func SeedMallTestData(db *gorm.DB) error { // ===== 商品分类 ===== categories := []model.ProductCategory{ {Name: "养生茶饮", Sort: 1, Icon: "🍵", Description: "各类养生茶饮产品", IsActive: true}, {Name: "滋补膏方", Sort: 2, Icon: "🍯", Description: "传统膏方滋补品", IsActive: true}, {Name: "中药材", Sort: 3, Icon: "🌿", Description: "道地中药材", IsActive: true}, {Name: "保健食品", Sort: 4, Icon: "💊", Description: "各类保健食品", IsActive: true}, } for i := range categories { var count int64 db.Model(&model.ProductCategory{}).Where("name = ?", categories[i].Name).Count(&count) if count == 0 { if err := db.Create(&categories[i]).Error; err != nil { return err } } else { db.Where("name = ?", categories[i].Name).First(&categories[i]) } } // ===== 测试商品 ===== products := []model.Product{ { CategoryID: uint(categories[0].ID), Category: "养生茶饮", Name: "红枣枸杞养生茶", Description: "精选新疆大枣和宁夏枸杞,温和养生", MainImage: "https://example.com/tea1.jpg", Price: 39.9, OriginalPrice: 59.9, Stock: 100, IsActive: true, IsFeatured: true, Efficacy: "补气养血、美容养颜", Suitable: "气虚质、血虚人群", ConstitutionTypes: `["qixu","yinxu"]`, HealthTags: `["补气","养血","美容"]`, Ingredients: "红枣、枸杞、桂圆", Usage: "每日1-2袋,开水冲泡5分钟", Sort: 10, }, { CategoryID: uint(categories[0].ID), Category: "养生茶饮", Name: "菊花决明子茶", Description: "清肝明目,降火润燥", MainImage: "https://example.com/tea2.jpg", Price: 29.9, OriginalPrice: 45.0, Stock: 200, IsActive: true, IsFeatured: true, Efficacy: "清肝明目、降火润燥", Suitable: "湿热质、肝火旺盛", ConstitutionTypes: `["shire","yinxu"]`, HealthTags: `["清肝","明目","降火"]`, Ingredients: "菊花、决明子、金银花", Usage: "每日1袋,沸水冲泡", Sort: 9, }, { CategoryID: uint(categories[1].ID), Category: "滋补膏方", Name: "阿胶固元膏", Description: "传统古法熬制,滋阴润燥", MainImage: "https://example.com/gao1.jpg", Price: 168.0, OriginalPrice: 238.0, Stock: 50, IsActive: true, IsFeatured: true, Efficacy: "滋阴润燥、补血养颜", Suitable: "阴虚质、血虚质", ConstitutionTypes: `["yinxu"]`, HealthTags: `["滋阴","补血","养颜"]`, Ingredients: "阿胶、黑芝麻、核桃仁、红枣、冰糖", Usage: "每日早晚各一勺,温水冲服", Sort: 8, }, { CategoryID: uint(categories[2].ID), Category: "中药材", Name: "西洋参片", Description: "进口花旗参,补气养阴", MainImage: "https://example.com/herb1.jpg", Price: 128.0, OriginalPrice: 188.0, Stock: 80, IsActive: true, IsFeatured: false, Efficacy: "补气养阴、清热生津", Suitable: "气虚质、阴虚质", ConstitutionTypes: `["qixu","yinxu"]`, HealthTags: `["补气","养阴","生津"]`, Ingredients: "西洋参", Usage: "每日3-5片,含服或泡水", Sort: 7, }, { CategoryID: uint(categories[3].ID), Category: "保健食品", Name: "深海鱼油软胶囊", Description: "富含Omega-3,呵护心脑血管", MainImage: "https://example.com/health1.jpg", Price: 89.0, OriginalPrice: 129.0, Stock: 150, IsActive: true, IsFeatured: false, Efficacy: "调节血脂、保护心脑血管", Suitable: "中老年人、血脂偏高者", ConstitutionTypes: `["tanshi","xueyu"]`, HealthTags: `["调脂","护心","血管"]`, Ingredients: "深海鱼油、维生素E", Usage: "每日2粒,随餐服用", Sort: 6, }, } for i := range products { var count int64 db.Model(&model.Product{}).Where("name = ?", products[i].Name).Count(&count) if count == 0 { if err := db.Create(&products[i]).Error; err != nil { return err } } else { db.Where("name = ?", products[i].Name).First(&products[i]) } } // ===== 商品 SKU ===== // 为第一个商品创建 SKU var firstProduct model.Product db.Where("name = ?", "红枣枸杞养生茶").First(&firstProduct) if firstProduct.ID > 0 { skus := []model.ProductSku{ { ProductID: uint(firstProduct.ID), SkuCode: "TEA-RZ-250", Name: "250g袋装", Attributes: `{"weight":"250g","package":"袋装"}`, Price: 39.9, Stock: 60, IsActive: true, }, { ProductID: uint(firstProduct.ID), SkuCode: "TEA-RZ-500", Name: "500g礼盒装", Attributes: `{"weight":"500g","package":"礼盒"}`, Price: 69.9, Stock: 40, IsActive: true, }, } for i := range skus { var count int64 db.Model(&model.ProductSku{}).Where("sku_code = ?", skus[i].SkuCode).Count(&count) if count == 0 { db.Create(&skus[i]) } } } return nil } // CleanMallTestData 清理商城测试数据(购物车、地址、订单等用户生成数据) func CleanMallTestData(db *gorm.DB, userID uint) { db.Where("user_id = ?", userID).Delete(&model.CartItem{}) db.Where("user_id = ?", userID).Delete(&model.Address{}) // 清理订单项 var orders []model.Order db.Where("user_id = ?", userID).Find(&orders) for _, o := range orders { db.Where("order_id = ?", o.ID).Delete(&model.OrderItem{}) } db.Where("user_id = ?", userID).Delete(&model.Order{}) db.Where("user_id = ?", userID).Delete(&model.PointsRecord{}) // 恢复库存(简单做法:将被测试修改的商品库存复原) db.Model(&model.Product{}).Where("name = ?", "红枣枸杞养生茶").Update("stock", 100) db.Model(&model.Product{}).Where("name = ?", "菊花决明子茶").Update("stock", 200) }