contact.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. package wechat
  2. import (
  3. "context"
  4. "go-common/app/tool/saga/model"
  5. "go-common/library/log"
  6. )
  7. // Changes changes structure
  8. type Changes struct {
  9. Adds []*model.ContactInfo
  10. Upts []*model.ContactInfo
  11. Dels []*model.ContactInfo
  12. }
  13. // SyncContacts sync the contacts from wechat work
  14. func (w *Wechat) SyncContacts(c context.Context) (err error) {
  15. if err = w.AnalysisContacts(c); err != nil {
  16. return
  17. }
  18. /*if err = w.UpdateVisible(c); err != nil {
  19. return
  20. }*/
  21. return
  22. }
  23. // AnalysisContacts analysis the contact difference and save them
  24. func (w *Wechat) AnalysisContacts(c context.Context) (err error) {
  25. var (
  26. contactsInDB []*model.ContactInfo
  27. wechatContacts []*model.ContactInfo
  28. changes = &Changes{}
  29. )
  30. if contactsInDB, err = w.dao.ContactInfos(); err != nil {
  31. return
  32. }
  33. if wechatContacts, err = w.QueryWechatContacts(c); err != nil {
  34. return
  35. }
  36. if changes, err = w.diffChanges(wechatContacts, contactsInDB); err != nil {
  37. return
  38. }
  39. if err = w.saveChanges(changes); err != nil {
  40. return
  41. }
  42. return
  43. }
  44. // QueryWechatContacts query wechat contacts with access token
  45. func (w *Wechat) QueryWechatContacts(c context.Context) (contacts []*model.ContactInfo, err error) {
  46. var (
  47. token string
  48. )
  49. if token, err = w.AccessToken(c, w.contact); err != nil {
  50. return
  51. }
  52. if contacts, err = w.dao.WechatContacts(c, token); err != nil {
  53. return
  54. }
  55. return
  56. }
  57. func (w *Wechat) saveChanges(changes *Changes) (err error) {
  58. var (
  59. contact *model.ContactInfo
  60. )
  61. log.Info("saveChanges add(%d), upt(%d), del(%d)", len(changes.Adds), len(changes.Upts), len(changes.Dels))
  62. for _, contact = range changes.Adds {
  63. if err = w.dao.CreateContact(contact); err != nil {
  64. return
  65. }
  66. log.Info("saveChanges add: %v", contact)
  67. }
  68. for _, contact = range changes.Upts {
  69. if err = w.dao.UptContact(contact); err != nil {
  70. return
  71. }
  72. log.Info("saveChanges upt: %v", contact)
  73. }
  74. for _, contact = range changes.Dels {
  75. if err = w.dao.DelContact(contact); err != nil {
  76. return
  77. }
  78. log.Info("saveChanges del: %v", contact)
  79. }
  80. return
  81. }
  82. func (w *Wechat) diffChanges(wechatContacts, contactsInDB []*model.ContactInfo) (changes *Changes, err error) {
  83. var (
  84. contact *model.ContactInfo
  85. wechatContactsMap = make(map[string]*model.ContactInfo)
  86. contactsInDBMap = make(map[string]*model.ContactInfo)
  87. wechatContactIDs []string
  88. dbContactsIDs []string
  89. userID string
  90. )
  91. changes = new(Changes)
  92. for _, contact = range wechatContacts {
  93. wechatContactsMap[contact.UserID] = contact
  94. wechatContactIDs = append(wechatContactIDs, contact.UserID)
  95. }
  96. for _, contact = range contactsInDB {
  97. contactsInDBMap[contact.UserID] = contact
  98. dbContactsIDs = append(dbContactsIDs, contact.UserID)
  99. }
  100. // 分析变化
  101. for _, userID = range wechatContactIDs {
  102. contact = wechatContactsMap[userID]
  103. if w.inSlice(dbContactsIDs, userID) { // 企业微信联系人ID,在数据库中能找到
  104. if !contact.AlmostEqual(contactsInDBMap[userID]) { // 但是域不同
  105. contact.ID = contactsInDBMap[userID].ID
  106. changes.Upts = append(changes.Upts, contact)
  107. }
  108. } else {
  109. changes.Adds = append(changes.Adds, contact) // 这个联系人是新增的
  110. }
  111. }
  112. for _, userID = range dbContactsIDs {
  113. if !w.inSlice(wechatContactIDs, userID) {
  114. changes.Dels = append(changes.Dels, contactsInDBMap[userID])
  115. }
  116. }
  117. return
  118. }
  119. func (w *Wechat) inSlice(slice []string, target string) bool {
  120. for _, v := range slice {
  121. if v == target {
  122. return true
  123. }
  124. }
  125. return false
  126. }
  127. // UpdateVisible update the visible property
  128. func (w *Wechat) UpdateVisible(c context.Context) (err error) {
  129. var (
  130. user *model.UserInfo
  131. users []*model.UserInfo
  132. contact *model.ContactInfo
  133. )
  134. if users, err = w.querySagaVisible(c); err != nil {
  135. return
  136. }
  137. for _, user = range users {
  138. contact = &model.ContactInfo{UserID: user.UserID, VisibleSaga: true}
  139. if err = w.dao.UptContact(contact); err != nil {
  140. return
  141. }
  142. }
  143. return
  144. }
  145. func (w *Wechat) querySagaVisible(c context.Context) (users []*model.UserInfo, err error) {
  146. var (
  147. token string
  148. )
  149. if token, err = w.AccessToken(c, w.saga); err != nil {
  150. return
  151. }
  152. if users, err = w.dao.WechatSagaVisible(c, token, w.saga.AppID); err != nil {
  153. return
  154. }
  155. return
  156. }