75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package repository
|
|
|
|
import (
|
|
"Lee-WineList/client"
|
|
"Lee-WineList/model/entity"
|
|
"Lee-WineList/model/param"
|
|
"errors"
|
|
"git.echol.cn/loser/logger/log"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type user struct {
|
|
}
|
|
|
|
// User ...
|
|
func User() *user {
|
|
return &user{}
|
|
}
|
|
|
|
// GetUser 查询单个用户信息
|
|
func (user) GetUser(user *entity.User) (err error) {
|
|
return client.MySQL.Take(&user, user).Error
|
|
}
|
|
|
|
// GetOrCreate 查询或创建用户
|
|
func (user) GetOrCreate(user *entity.User) (err error) {
|
|
//err = client.MySQL.Take(&user, user).Error
|
|
err = client.MySQL.Model(&user).Where("phone = ?", user.Phone).First(&user).Error
|
|
if err == nil {
|
|
return
|
|
}
|
|
// 如果是没查询到记录,则创建用户
|
|
if err == gorm.ErrRecordNotFound {
|
|
// 用户不存在,创建用户
|
|
if err = client.MySQL.Create(&user).Error; err != nil {
|
|
log.Errorf("账号创建失败: %v", err)
|
|
err = errors.New("登录失败")
|
|
return
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// CheckUnionIdIsExist 检查UnionId和OpenId是否存在
|
|
func (user) CheckUnionIdIsExist(unionId, openId string) bool {
|
|
var count int64
|
|
err := client.MySQL.Model(&entity.User{}).Where("wechat_union_id = ? and wechat_open_id = ?", unionId, openId).Count(&count).Error
|
|
if err != nil {
|
|
return false
|
|
}
|
|
return count > 0
|
|
}
|
|
|
|
// UpdateUserInfo 更新普通用户信息
|
|
func (user) UpdateUserInfo(e *entity.User) (err error) {
|
|
return client.MySQL.Updates(&e).Error
|
|
}
|
|
|
|
// GetLikeWineList 获取用户喜欢的酒单列表
|
|
func (u user) GetLikeWineList(e *param.GetLikeWineList) (wines []entity.Wine, err error) {
|
|
var ids []int64
|
|
client.MySQL.Model(&entity.LikeWine{}).Where("user_id = ?", e.UserId).Pluck("wine_id", &ids)
|
|
|
|
err = client.MySQL.Model(&entity.Wine{}).
|
|
Where("wine_id in ?", ids).
|
|
Preload("Materials").
|
|
Scopes(page(e.Current, e.Size)).
|
|
Find(&wines).Error
|
|
return
|
|
}
|
|
|
|
func (u user) DeleteLikeWine(p *param.DeleteLikeWine) (err error) {
|
|
return client.MySQL.Delete(&entity.LikeWine{}, "wine_id = ?", p.LikeWineId).Error
|
|
}
|