get_contact_list_logic.go 6.5 KB

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