userservice.go 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package service
  2. import (
  3. "sws_xcx/models"
  4. "time"
  5. "github.com/jinzhu/gorm"
  6. )
  7. type XcxUserService struct {
  8. rdb *gorm.DB
  9. wdb *gorm.DB
  10. }
  11. func NewXcxUserService() *XcxUserService {
  12. u := &models.XcxUser{}
  13. return &XcxUserService{rdb: readDb.Model(u), wdb: writeDb.Model(u)}
  14. }
  15. func (s *XcxUserService) GetUser(id uint64) (*models.XcxUser, error) {
  16. user := &models.XcxUser{}
  17. db := readDb.Where("id=?", id).First(user)
  18. return user, db.Error
  19. }
  20. func (s *XcxUserService) GetOrCreate(openId string, unionId string) (*models.XcxUser, error) {
  21. user := &models.XcxUser{OpenId: openId, UnionId: unionId}
  22. db := writeDb.Where("open_id = ?", openId)
  23. if unionId != "" {
  24. db = db.Or("union_id = ?", unionId)
  25. }
  26. db.FirstOrCreate(user)
  27. return user, db.Error
  28. }
  29. func (s *XcxUserService) UpdateUser(user *models.XcxUser) error {
  30. user.Mtime = time.Now()
  31. db := writeDb.Model(user).Update(user)
  32. return db.Error
  33. }
  34. func (s *XcxUserService) GetMobilePatientInfo(mobile, name, idCard string) (models.XcxPatients, error) {
  35. patients := models.XcxPatients{}
  36. err := ReadXtDB().Model(&patients).Where("(phone = ? or home_telephone = ? ) and name=? and id_card_no=? and status = 1", mobile, mobile, name, idCard).Find(&patients).Error
  37. if err == gorm.ErrRecordNotFound {
  38. err = nil
  39. }
  40. return patients, err
  41. }