package service import ( "context" "errors" "gitea.youtukeji.com.cn/xiabin/youtu_ecpm/dao/model" "gitea.youtukeji.com.cn/xiabin/youtu_ecpm/dao/query" "github.com/gin-gonic/gin" "gorm.io/gorm" ) type AppUserInfo struct { q *query.Query } func NewAppUserInfo(q *query.Query) *AppUserInfo { return &AppUserInfo{q: q} } func (a *AppUserInfo) SaveUserInfoByRes(c context.Context, res *model.AppUserInfo) (err error) { m, err := a.q.AppUserInfo.WithContext(c).Where(a.q.AppUserInfo.Openid.Eq(res.Openid), a.q.AppAccount.AppID.Eq(res.Openid)).Join(a.q.AppAccount, a.q.AppAccount.ID.EqCol(a.q.AppUserInfo.AppAccountID)).Take() if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) { return } if m == nil || m.ID == 0 { err = a.q.AppUserInfo.WithContext(c).Create(res) } return } func (a *AppUserInfo) UpdateUserNickNameAndImageUrl(ctx context.Context, id uint64, nickname string, url string) (err error) { _, err = a.q.AppUserInfo.WithContext(ctx).Where(a.q.AppUserInfo.ID.Eq(id)).Updates(&model.AppUserInfo{ID: id, Nickname: nickname, Avatar: url}) return } func (a *AppUserInfo) GetUserById(c *gin.Context, id uint64) (*model.AppUserInfo, error) { return a.q.AppUserInfo.WithContext(c).Where(a.q.AppUserInfo.ID.Eq(id)).Take() }