get_contact_list_logic.go 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package contact
  2. import (
  3. "context"
  4. "time"
  5. "wechat-api/ent"
  6. "wechat-api/ent/label"
  7. "wechat-api/ent/labelrelationship"
  8. "wechat-api/ent/wx"
  9. "wechat-api/ent/contact"
  10. "wechat-api/ent/predicate"
  11. "wechat-api/internal/svc"
  12. "wechat-api/internal/types"
  13. "wechat-api/internal/utils/dberrorhandler"
  14. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  15. "github.com/suyuan32/simple-admin-common/utils/pointy"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. )
  18. type GetContactListLogic struct {
  19. ctx context.Context
  20. svcCtx *svc.ServiceContext
  21. logx.Logger
  22. }
  23. func NewGetContactListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactListLogic {
  24. return &GetContactListLogic{
  25. ctx: ctx,
  26. svcCtx: svcCtx,
  27. Logger: logx.WithContext(ctx),
  28. }
  29. }
  30. func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
  31. return types.LabelInfo{
  32. BaseIDInfo: types.BaseIDInfo{
  33. Id: &label.ID,
  34. CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
  35. UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
  36. },
  37. Status: &label.Status,
  38. Type: &label.Type,
  39. Name: &label.Name,
  40. From: &label.From,
  41. Mode: &label.Mode,
  42. Conditions: &label.Conditions,
  43. }
  44. }
  45. func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
  46. organizationId := l.ctx.Value("organizationId").(uint64)
  47. isAdmin := l.ctx.Value("isAdmin").(bool)
  48. var predicates []predicate.Contact
  49. if req.WxWxid == nil || (req.WxWxid != nil && !isAdmin) {
  50. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  51. }
  52. layout := "2006-01-02 15:04:05"
  53. var startTime, endTime *time.Time
  54. if req.SearchDate != nil && len(req.SearchDate) == 2 {
  55. startStr := req.SearchDate[0] + " 00:00:00"
  56. if t, err := time.Parse(layout, startStr); err == nil {
  57. //t = t.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  58. startTime = &t
  59. } else {
  60. l.Logger.Errorf("invalid startDate: %v", err)
  61. }
  62. endStr := req.SearchDate[1] + " 23:59:59"
  63. if t, err := time.Parse(layout, endStr); err == nil {
  64. //t = t.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  65. endTime = &t
  66. } else {
  67. l.Logger.Errorf("invalid endDate: %v", err)
  68. }
  69. }
  70. var relPreds []predicate.LabelRelationship
  71. if startTime != nil {
  72. relPreds = append(relPreds, labelrelationship.CreatedAtGTE(*startTime))
  73. }
  74. if endTime != nil {
  75. relPreds = append(relPreds, labelrelationship.CreatedAtLTE(*endTime))
  76. }
  77. if len(req.LabelIDs) > 0 {
  78. relPreds = append(relPreds,
  79. labelrelationship.HasLabelsWith(label.IDIn(req.LabelIDs...)),
  80. )
  81. }
  82. if len(relPreds) > 0 {
  83. predicates = append(predicates, contact.HasContactRelationshipsWith(relPreds...))
  84. }
  85. if req.Ctype != nil {
  86. predicates = append(predicates, contact.Ctype(*req.Ctype))
  87. } else {
  88. predicates = append(predicates, contact.Ctype(1)) // 默认 Ctype = 1
  89. }
  90. if req.WxWxid != nil {
  91. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  92. }
  93. if req.Wxid != nil {
  94. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  95. }
  96. if req.Account != nil {
  97. predicates = append(predicates, contact.AccountContains(*req.Account))
  98. }
  99. if req.Nickname != nil {
  100. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  101. }
  102. if req.Type != nil {
  103. predicates = append(predicates, contact.TypeEQ(*req.Type))
  104. } else {
  105. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  106. }
  107. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  108. query.WithLabels()
  109. }).Page(l.ctx, req.Page, req.PageSize)
  110. if err != nil {
  111. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  112. }
  113. resp := &types.ContactListResp{}
  114. resp.Msg = errormsg.Success
  115. resp.Data.Total = data.PageDetails.Total
  116. wxWxids := []string{}
  117. wxWxidsSet := make(map[string]*ent.Wx)
  118. blockListSet := make(map[string]struct{})
  119. groupBlockListSet := make(map[string]struct{})
  120. for _, v := range data.List {
  121. wxWxids = append(wxWxids, v.WxWxid)
  122. }
  123. wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
  124. for _, w := range wxs {
  125. wxWxidsSet[w.Wxid] = w
  126. for _, b := range w.BlockList {
  127. blockListSet[w.Wxid+"_"+b] = struct{}{}
  128. }
  129. for _, g := range w.GroupBlockList {
  130. groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
  131. }
  132. }
  133. for _, v := range data.List {
  134. isInBlockList := false
  135. labelRelationships := make([]types.ContactLabelList, 0)
  136. if v.Edges.ContactRelationships != nil {
  137. for _, lr := range v.Edges.ContactRelationships {
  138. if lr.Edges.Labels == nil {
  139. continue
  140. }
  141. labelRelationships = append(labelRelationships, types.ContactLabelList{
  142. Label: &lr.Edges.Labels.Name,
  143. Value: &lr.LabelID,
  144. })
  145. }
  146. }
  147. var wxNickname string
  148. if wxWxidsSet[v.WxWxid] == nil {
  149. wxNickname = v.WxWxid
  150. } else {
  151. wxNickname = wxWxidsSet[v.WxWxid].Nickname
  152. }
  153. if v.Type == 1 {
  154. if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
  155. isInBlockList = true
  156. } else {
  157. _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
  158. }
  159. } else {
  160. if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
  161. isInBlockList = true
  162. } else {
  163. _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
  164. }
  165. }
  166. resp.Data.Data = append(resp.Data.Data,
  167. types.ContactInfo{
  168. BaseIDInfo: types.BaseIDInfo{
  169. Id: &v.ID,
  170. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  171. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  172. },
  173. Status: &v.Status,
  174. WxWxid: &v.WxWxid,
  175. WxWxidNickname: &wxNickname,
  176. Type: &v.Type,
  177. Wxid: &v.Wxid,
  178. Account: &v.Account,
  179. Nickname: &v.Nickname,
  180. Markname: &v.Markname,
  181. Headimg: &v.Headimg,
  182. Sex: &v.Sex,
  183. Starrole: &v.Starrole,
  184. Dontseeit: &v.Dontseeit,
  185. Dontseeme: &v.Dontseeme,
  186. Lag: &v.Lag,
  187. Gid: &v.Gid,
  188. Gname: &v.Gname,
  189. V3: &v.V3,
  190. LabelRelationships: labelRelationships,
  191. IsInBlockList: &isInBlockList,
  192. Ctype: &v.Ctype,
  193. Cname: &v.Cname,
  194. Cage: &v.Cage,
  195. Carea: &v.Carea,
  196. Cc: &v.Cc,
  197. Phone: &v.Phone,
  198. Cbirthday: &v.Cbirthday,
  199. Cbirtharea: &v.Cbirtharea,
  200. CidcardNo: &v.CidcardNo,
  201. Ctitle: &v.Ctitle,
  202. })
  203. }
  204. return resp, nil
  205. }