原来的案例是 商城系统的收藏夹的关系,收藏夹里面需要有收藏用户,产品,产品用有人的信息,因此下面的结构如此体现,这样子设计非常巧妙,使用了belong to

1
2
3
4
5
6
7
8
9
type Favorite struct {
gorm.Model
User User `gorm:"ForeignKey:UserID"`
UserID uint `gorm:"not null"`
Product Product `gorm:"ForeignKey:ProductID"`
ProductID uint `gorm:"not null"`
Boss User `gorm:"ForeignKey:BossID"`
BossID uint `gorm:"not null"`
}

下面的分别是用户和产品的

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
type Product struct {
gorm.Model
Name string `gorm:"size:255;index"`
CategoryID uint `gorm:"not null"`
Title string
Info string `gorm:"size:1000"`
ImgPath string
Price string
DiscountPrice string
OnSale bool `gorm:"default:false"`
Num int
BossID uint
BossName string
BossAvatar string
}
1
2
3
4
5
6
7
8
9
10
11
type User struct {
gorm.Model
UserName string `gorm:"unique"`
Email string
PasswordDigest string
NickName string
Status string
Avatar string `gorm:"size:1000"`
Money string
Relations []User `gorm:"many2many:relation;"`
}

创建一个收藏夹

只显示了service层的代码,但是可以看出,创建需要把所有信息填入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
func (s *FavoriteSrv) FavoriteCreate(ctx context.Context, req *types.FavoriteCreateReq) (resp interface{}, err error) {
u, err := ctl.GetUserInfo(ctx)
if err != nil {
util.LogrusObj.Error(err)
return nil, err
}
fDao := dao.NewFavoritesDao(ctx)
exist, _ := fDao.FavoriteExistOrNot(req.ProductId, u.Id)
if exist {
err = errors.New("已经存在了")
util.LogrusObj.Error(err)
return
}

userDao := dao.NewUserDao(ctx)
user, err := userDao.GetUserById(u.Id)
if err != nil {
util.LogrusObj.Error(err)
return
}

bossDao := dao.NewUserDaoByDB(userDao.DB)
boss, err := bossDao.GetUserById(req.BossId)
if err != nil {
util.LogrusObj.Error(err)
return
}

product, err := dao.NewProductDao(ctx).GetProductById(req.ProductId)
if err != nil {
util.LogrusObj.Error(err)
return
}

favorite := &model.Favorite{
UserID: u.Id,
User: *user,
ProductID: req.ProductId,
Product: *product,
BossID: req.BossId,
Boss: *boss,
}
err = fDao.CreateFavorite(favorite)
if err != nil {
util.LogrusObj.Error(err)
return
}

return
}

展示收藏夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
func (dao *FavoritesDao) ListFavoriteByUserId(uId uint, pageSize, pageNum int) (r []*types.FavoriteListResp, total int64, err error) {
// 总数
err = dao.DB.Model(&model.Favorite{}).Preload("User").
Where("user_id=?", uId).Count(&total).Error
if err != nil {
return
}
err = dao.DB.Model(&model.Favorite{}).
Joins("AS f LEFT JOIN user AS u on u.id = f.boss_id").
Joins("LEFT JOIN product AS p ON p.id = f.product_id").
Joins("LEFT JOIN category AS c ON c.id = p.category_id").
Where("f.user_id = ?", uId).
Offset((pageNum - 1) * pageSize).Limit(pageSize).
Select("f.user_id AS user_id," +
"f.product_id AS product_id," +
"UNIX_TIMESTAMP(f.created_at) AS created_at," +
"p.title AS title," +
"p.info AS info," +
"p.name AS name," +
"c.id AS category_id," +
"c.category_name AS category_name," +
"u.id AS boss_id," +
"u.user_name AS boss_name," +
"u.avatar AS boss_avatar," +
"p.price AS price," +
"p.img_path AS img_path," +
"p.discount_price AS discount_price," +
"p.num AS num," +
"p.on_sale AS on_sale").
Find(&r).Error

return
}

这边可以自定义结构体,将想要的数据进行选择,使用Joins来联结表查询

==As==表示 看作的意思 ,就是相当于取了一个别名

就这么写,我也是第一次见,所以特此记录