userservice.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. }
  42. func (s *XcxUserService) GetPatientInfo(id int64) (models.XcxPatients, error) {
  43. patients := models.XcxPatients{}
  44. err := ReadXtDB().Model(&patients).Find(&patients, id).Error
  45. if err == gorm.ErrRecordNotFound {
  46. err = nil
  47. }
  48. return patients, err
  49. }