userservice.go 860B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package service
  2. import (
  3. "sws_xcx/models"
  4. "github.com/jinzhu/gorm"
  5. )
  6. type XcxUserService struct {
  7. rdb *gorm.DB
  8. wdb *gorm.DB
  9. }
  10. func NewXcxUserService() *XcxUserService {
  11. u := &models.XcxUser{}
  12. return &XcxUserService{rdb: readDb.Model(u), wdb: writeDb.Model(u)}
  13. }
  14. func (s *XcxUserService) GetUser(id uint64) (*models.XcxUser, error) {
  15. user := &models.XcxUser{}
  16. db := readDb.Where("id=?", id).First(user)
  17. return user, db.Error
  18. }
  19. func (s *XcxUserService) GetOrCreate(openId string, unionId string) (*models.XcxUser, error) {
  20. user := &models.XcxUser{OpenId: openId, UnionId: unionId}
  21. db := writeDb.Where("open_id = ?", openId).Or("union_id = ?", unionId).FirstOrCreate(user)
  22. return user, db.Error
  23. }
  24. func (s *XcxUserService) UpdateUser(user *models.XcxUser) error {
  25. db := writeDb.Where("id = ?", user.Id).Update(user)
  26. return db.Error
  27. }