get_contact_list_logic.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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.StartDate != nil && *req.StartDate != "" {
  55. startStr := *req.StartDate + " 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. }
  63. if req.EndDate != nil && *req.EndDate != "" {
  64. endStr := *req.EndDate + " 23:59:59"
  65. if t, err := time.Parse(layout, endStr); err == nil {
  66. t = t.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  67. endTime = &t
  68. } else {
  69. l.Logger.Errorf("invalid endDate: %v", err)
  70. }
  71. }
  72. var relPreds []predicate.LabelRelationship
  73. if startTime != nil {
  74. relPreds = append(relPreds, labelrelationship.CreatedAtGTE(*startTime))
  75. }
  76. if endTime != nil {
  77. relPreds = append(relPreds, labelrelationship.CreatedAtLTE(*endTime))
  78. }
  79. if len(relPreds) > 0 {
  80. predicates = append(predicates, contact.HasContactRelationshipsWith(relPreds...))
  81. }
  82. if len(req.LabelIDs) > 0 {
  83. relPreds = append(relPreds,
  84. labelrelationship.HasLabelsWith(label.IDIn(req.LabelIDs...)),
  85. )
  86. }
  87. if len(relPreds) > 0 {
  88. predicates = append(predicates, contact.HasContactRelationshipsWith(relPreds...))
  89. }
  90. if req.Ctype != nil {
  91. predicates = append(predicates, contact.Ctype(*req.Ctype))
  92. } else {
  93. predicates = append(predicates, contact.Ctype(1)) // 默认 Ctype = 1
  94. }
  95. if req.WxWxid != nil {
  96. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  97. }
  98. if req.Wxid != nil {
  99. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  100. }
  101. if req.Account != nil {
  102. predicates = append(predicates, contact.AccountContains(*req.Account))
  103. }
  104. if req.Nickname != nil {
  105. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  106. }
  107. if req.Type != nil {
  108. predicates = append(predicates, contact.TypeEQ(*req.Type))
  109. } else {
  110. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  111. }
  112. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  113. query.WithLabels()
  114. }).Page(l.ctx, req.Page, req.PageSize)
  115. if err != nil {
  116. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  117. }
  118. resp := &types.ContactListResp{}
  119. resp.Msg = errormsg.Success
  120. resp.Data.Total = data.PageDetails.Total
  121. wxWxids := []string{}
  122. wxWxidsSet := make(map[string]*ent.Wx)
  123. blockListSet := make(map[string]struct{})
  124. groupBlockListSet := make(map[string]struct{})
  125. for _, v := range data.List {
  126. wxWxids = append(wxWxids, v.WxWxid)
  127. }
  128. wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
  129. for _, w := range wxs {
  130. wxWxidsSet[w.Wxid] = w
  131. for _, b := range w.BlockList {
  132. blockListSet[w.Wxid+"_"+b] = struct{}{}
  133. }
  134. for _, g := range w.GroupBlockList {
  135. groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
  136. }
  137. }
  138. for _, v := range data.List {
  139. isInBlockList := false
  140. labelRelationships := make([]types.ContactLabelList, 0)
  141. if v.Edges.ContactRelationships != nil {
  142. for _, lr := range v.Edges.ContactRelationships {
  143. if lr.Edges.Labels == nil {
  144. continue
  145. }
  146. labelRelationships = append(labelRelationships, types.ContactLabelList{
  147. Label: &lr.Edges.Labels.Name,
  148. Value: &lr.LabelID,
  149. })
  150. }
  151. }
  152. var wxNickname string
  153. if wxWxidsSet[v.WxWxid] == nil {
  154. wxNickname = v.WxWxid
  155. } else {
  156. wxNickname = wxWxidsSet[v.WxWxid].Nickname
  157. }
  158. if v.Type == 1 {
  159. if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
  160. isInBlockList = true
  161. } else {
  162. _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
  163. }
  164. } else {
  165. if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
  166. isInBlockList = true
  167. } else {
  168. _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
  169. }
  170. }
  171. resp.Data.Data = append(resp.Data.Data,
  172. types.ContactInfo{
  173. BaseIDInfo: types.BaseIDInfo{
  174. Id: &v.ID,
  175. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  176. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  177. },
  178. Status: &v.Status,
  179. WxWxid: &v.WxWxid,
  180. WxWxidNickname: &wxNickname,
  181. Type: &v.Type,
  182. Wxid: &v.Wxid,
  183. Account: &v.Account,
  184. Nickname: &v.Nickname,
  185. Markname: &v.Markname,
  186. Headimg: &v.Headimg,
  187. Sex: &v.Sex,
  188. Starrole: &v.Starrole,
  189. Dontseeit: &v.Dontseeit,
  190. Dontseeme: &v.Dontseeme,
  191. Lag: &v.Lag,
  192. Gid: &v.Gid,
  193. Gname: &v.Gname,
  194. V3: &v.V3,
  195. LabelRelationships: labelRelationships,
  196. IsInBlockList: &isInBlockList,
  197. Ctype: &v.Ctype,
  198. Cname: &v.Cname,
  199. Cage: &v.Cage,
  200. Carea: &v.Carea,
  201. Cc: &v.Cc,
  202. Phone: &v.Phone,
  203. Cbirthday: &v.Cbirthday,
  204. Cbirtharea: &v.Cbirtharea,
  205. CidcardNo: &v.CidcardNo,
  206. Ctitle: &v.Ctitle,
  207. })
  208. }
  209. return resp, nil
  210. }