1.model定义 - 图1

1. 表定义和生成

  1. package model
  2. import (
  3. "database/sql"
  4. "time"
  5. "gorm.io/gorm"
  6. )
  7. type BaseModel struct {
  8. ID int32 `gorm:"primarykey"`
  9. CreateAt time.Time `gorm:"column:add_time"`
  10. UpdateAt sql.NullTime `gorm:"column:update_time"`
  11. DeletedAt gorm.DeletedAt
  12. IsDeleted bool
  13. }
  14. // Category 分类表
  15. type Category struct {
  16. BaseModel
  17. Name string `gorm:"type:varchar(20);not null;comment:分类名称"`
  18. ParentCategoryID int32 `gorm:"type:int(11);default:null;comment:父id 0顶级分类"`
  19. SubCategory []*Category `gorm:"foreignkey:ParentCategoryID;references:ID"`
  20. Level int32 `gorm:"type:int(11);not null;default:1;comment:顶级分类"`
  21. IsTab bool `gorm:"default:false;not null;comment:是否是选项卡"`
  22. }
  23. type Brands struct {
  24. BaseModel
  25. Name string `gorm:"type:varchar(50);not null;comment:品牌名称"`
  26. Logo string `gorm:"type:varchar(255);not null;comment:品牌logo"`
  27. }
  28. type GoodsCategoryBrand struct {
  29. BaseModel
  30. CategoryID int32 `gorm:"type:int(11);not null;comment:商品分类id"`
  31. Category Category `gorm:"foreignKey:CategoryID;references:ID"`
  32. BrandsID int32 `gorm:"type:int(11);not null;comment:品牌id"`
  33. Brand Brands `gorm:"foreignKey:BrandsID;references:ID"`
  34. }
  35. func (GoodsCategoryBrand) TableName() string {
  36. return "goodscategorybrand"
  37. }
  38. // Banner 轮播图表
  39. type Banner struct {
  40. BaseModel
  41. Image string `gorm:"type:varchar(255);not null;comment:图片地址"`
  42. Url string `gorm:"type:varchar(255);not null;comment:跳转地址"`
  43. Index int32 `gorm:"type:int(11);not null;comment:轮播顺序"`
  44. }
  45. type Goods struct {
  46. BaseModel
  47. CategoryID int32 `gorm:"type:int;not null;comment:商品分类id"`
  48. Category Category `gorm:"foreignKey:CategoryID;references:ID"`
  49. BrandsID int32 `gorm:"type:int;not null;comment:品牌id"`
  50. Brands Brands `gorm:"foreignKey:BrandsID;references:ID"`
  51. OnSale bool `gorm:"default:false;not null;comment:是否上架"`
  52. ShipFree bool `gorm:"default:false;not null;comment:是否免运费"`
  53. IsNew bool `gorm:"default:false;not null;comment:是否是新品"`
  54. IsHot bool `gorm:"default:false;not null;comment:是否是热卖"`
  55. Name string `gorm:"type:varchar(100);not null;comment:商品名称"`
  56. GoodsSn string `gorm:"type:varchar(50);not null;comment:商品货号"`
  57. ClickNum int32 `gorm:"type:int;default:0;not null;comment:点击数"`
  58. SoldNum int32 `gorm:"type:int;default:0;not null;comment:销售数"`
  59. FavNum int32 `gorm:"type:int;default:0;not null;comment:收藏数"`
  60. MarketPrice float32 `gorm:"not null;comment:市场价"`
  61. ShopPrice float32 `gorm:"not null;comment:本店价"`
  62. GoodsBrief string `gorm:"type:varchar(100);not null;comment:商品简介"`
  63. Images GormList `gorm:"type:varchar(1000);not null;comment:商品图片"`
  64. DescImages GormList `gorm:"type:varchar(4000);not null;comment:商品详情图片"` // 修改长度
  65. GoodsFrontImage string `gorm:"type:varchar(200);not null;comment:商品封面图"`
  66. }

2. 导入数据

mxshop_goods_srv.sql