db_wechat.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package dao
  2. import (
  3. "regexp"
  4. "strings"
  5. "go-common/app/tool/saga/model"
  6. "go-common/library/log"
  7. "github.com/pkg/errors"
  8. pkgerr "github.com/pkg/errors"
  9. )
  10. var (
  11. regUserID = regexp.MustCompile(`^\d+$`)
  12. )
  13. // QueryUserByUserName query user by user name
  14. func (d *Dao) QueryUserByUserName(userName string) (contactInfo *model.ContactInfo, err error) {
  15. contactInfo = &model.ContactInfo{}
  16. err = pkgerr.WithStack(d.mysql.Where(&model.ContactInfo{UserName: userName}).First(contactInfo).Error)
  17. return
  18. }
  19. // QueryUserByID query user by user ID
  20. func (d *Dao) QueryUserByID(userID string) (contactInfo *model.ContactInfo, err error) {
  21. contactInfo = &model.ContactInfo{}
  22. err = pkgerr.WithStack(d.mysql.Where(&model.ContactInfo{UserID: userID}).First(contactInfo).Error)
  23. return
  24. }
  25. // UserIds query user ids for the user names
  26. func (d *Dao) UserIds(userNames []string) (userIds string, err error) {
  27. var (
  28. userName string
  29. ids []string
  30. contactInfo *model.ContactInfo
  31. )
  32. if len(userNames) == 0 {
  33. err = errors.Errorf("UserIds: userNames is empty!")
  34. return
  35. }
  36. for _, userName = range userNames {
  37. if contactInfo, err = d.QueryUserByUserName(userName); err != nil {
  38. log.Error("UserIds: no such user (%s) in db, err (%s)", userName, err.Error())
  39. }
  40. log.Info("UserIds: username (%s), userid (%s)", userName, contactInfo.UserID)
  41. if contactInfo.UserID != "" && regUserID.MatchString(contactInfo.UserID) {
  42. ids = append(ids, contactInfo.UserID)
  43. }
  44. }
  45. if len(ids) > 0 {
  46. userIds = strings.Join(ids, "|")
  47. err = nil
  48. } else {
  49. err = errors.Wrapf(err, "UserIds: failed to find all the users in db, what a pity!")
  50. }
  51. return
  52. }
  53. // ContactInfos query all the records in contact_infos
  54. func (d *Dao) ContactInfos() (contactInfos []*model.ContactInfo, err error) {
  55. err = pkgerr.WithStack(d.mysql.Find(&contactInfos).Error)
  56. return
  57. }
  58. // CreateContact create contact info record
  59. func (d *Dao) CreateContact(contact *model.ContactInfo) (err error) {
  60. err = pkgerr.WithStack(d.mysql.Create(contact).Error)
  61. return
  62. }
  63. // DelContact delete the contact info with the specified UserID
  64. func (d *Dao) DelContact(contact *model.ContactInfo) (err error) {
  65. err = pkgerr.WithStack(d.mysql.Delete(contact).Error)
  66. return
  67. }
  68. // UptContact update the contact information
  69. func (d *Dao) UptContact(contact *model.ContactInfo) (err error) {
  70. err = pkgerr.WithStack(d.mysql.Model(&model.ContactInfo{}).Where(&model.ContactInfo{UserID: contact.UserID}).Updates(*contact).Error)
  71. return
  72. }