get_contact_list_logic.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. package contact
  2. import (
  3. "context"
  4. "time"
  5. "wechat-api/ent"
  6. "wechat-api/ent/contactfieldtemplate"
  7. "wechat-api/ent/label"
  8. "wechat-api/ent/labelrelationship"
  9. "wechat-api/ent/wx"
  10. "wechat-api/ent/contact"
  11. "wechat-api/ent/predicate"
  12. "wechat-api/internal/svc"
  13. "wechat-api/internal/types"
  14. "wechat-api/internal/utils/dberrorhandler"
  15. "github.com/suyuan32/simple-admin-common/msg/errormsg"
  16. "github.com/suyuan32/simple-admin-common/utils/pointy"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. )
  19. type GetContactListLogic struct {
  20. ctx context.Context
  21. svcCtx *svc.ServiceContext
  22. logx.Logger
  23. }
  24. func NewGetContactListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactListLogic {
  25. return &GetContactListLogic{
  26. ctx: ctx,
  27. svcCtx: svcCtx,
  28. Logger: logx.WithContext(ctx),
  29. }
  30. }
  31. func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
  32. return types.LabelInfo{
  33. BaseIDInfo: types.BaseIDInfo{
  34. Id: &label.ID,
  35. CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
  36. UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
  37. },
  38. Status: &label.Status,
  39. Type: &label.Type,
  40. Name: &label.Name,
  41. From: &label.From,
  42. Mode: &label.Mode,
  43. Conditions: &label.Conditions,
  44. }
  45. }
  46. func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
  47. organizationId := l.ctx.Value("organizationId").(uint64)
  48. isAdmin := l.ctx.Value("isAdmin").(bool)
  49. var predicates []predicate.Contact
  50. if req.WxWxid == nil || (req.WxWxid != nil && !isAdmin) {
  51. predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
  52. }
  53. layout := "2006-01-02 15:04:05"
  54. var startTime, endTime *time.Time
  55. if req.StartDate != nil && *req.StartDate != "" {
  56. startStr := *req.StartDate + " 00:00:00"
  57. if t, err := time.Parse(layout, startStr); err == nil {
  58. //t = t.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  59. startTime = &t
  60. } else {
  61. l.Logger.Errorf("invalid startDate: %v", err)
  62. }
  63. }
  64. if req.EndDate != nil && *req.EndDate != "" {
  65. endStr := *req.EndDate + " 23:59:59"
  66. if t, err := time.Parse(layout, endStr); err == nil {
  67. //t = t.Add(23*time.Hour + 59*time.Minute + 59*time.Second)
  68. endTime = &t
  69. } else {
  70. l.Logger.Errorf("invalid endDate: %v", err)
  71. }
  72. }
  73. var relPreds []predicate.LabelRelationship
  74. if startTime != nil {
  75. relPreds = append(relPreds, labelrelationship.CreatedAtGTE(*startTime))
  76. }
  77. if endTime != nil {
  78. relPreds = append(relPreds, labelrelationship.CreatedAtLTE(*endTime))
  79. }
  80. if len(req.LabelIDs) > 0 {
  81. relPreds = append(relPreds,
  82. labelrelationship.HasLabelsWith(label.IDIn(req.LabelIDs...)),
  83. )
  84. }
  85. if len(relPreds) > 0 {
  86. predicates = append(predicates, contact.HasContactRelationshipsWith(relPreds...))
  87. }
  88. if req.Ctype != nil {
  89. predicates = append(predicates, contact.Ctype(*req.Ctype))
  90. } else {
  91. predicates = append(predicates, contact.Ctype(1)) // 默认 Ctype = 1
  92. }
  93. if req.WxWxid != nil {
  94. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  95. }
  96. if req.Wxid != nil {
  97. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  98. }
  99. if req.Account != nil {
  100. predicates = append(predicates, contact.AccountContains(*req.Account))
  101. }
  102. if req.Nickname != nil {
  103. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  104. }
  105. if req.Type != nil {
  106. predicates = append(predicates, contact.TypeEQ(*req.Type))
  107. } else {
  108. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  109. }
  110. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  111. query.WithLabels()
  112. }).WithContactFields(func(query *ent.ContactFieldQuery) {
  113. query.WithFieldContact()
  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. field_template, _ := l.svcCtx.DB.ContactFieldTemplate.Query().
  139. Where(
  140. contactfieldtemplate.OrganizationID(organizationId), // Additional filter by organizationId
  141. ).
  142. Only(l.ctx)
  143. for _, v := range data.List {
  144. // 自定义字段
  145. custom_field := make([]types.ContactFieldTemplate, 0)
  146. if field_template != nil && len(field_template.Template) > 0 {
  147. field_set := make(map[string][]string)
  148. fields := v.Edges.ContactFields
  149. for _, field := range fields {
  150. field_set[field.FormID] = field.Value
  151. }
  152. for _, template := range field_template.Template {
  153. template.Value = field_set[*template.Id]
  154. custom_field = append(custom_field, ConvertCustomToInternal(template))
  155. }
  156. }
  157. isInBlockList := false
  158. labelRelationships := make([]types.ContactLabelList, 0)
  159. if v.Edges.ContactRelationships != nil {
  160. for _, lr := range v.Edges.ContactRelationships {
  161. if lr.Edges.Labels == nil {
  162. continue
  163. }
  164. labelRelationships = append(labelRelationships, types.ContactLabelList{
  165. Label: &lr.Edges.Labels.Name,
  166. Value: &lr.LabelID,
  167. })
  168. }
  169. }
  170. var wxNickname string
  171. if wxWxidsSet[v.WxWxid] == nil {
  172. wxNickname = v.WxWxid
  173. } else {
  174. wxNickname = wxWxidsSet[v.WxWxid].Nickname
  175. }
  176. if v.Type == 1 {
  177. if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
  178. isInBlockList = true
  179. } else {
  180. _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
  181. }
  182. } else {
  183. if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
  184. isInBlockList = true
  185. } else {
  186. _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
  187. }
  188. }
  189. resp.Data.Data = append(resp.Data.Data,
  190. types.ContactInfo{
  191. BaseIDInfo: types.BaseIDInfo{
  192. Id: &v.ID,
  193. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  194. UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
  195. },
  196. Status: &v.Status,
  197. WxWxid: &v.WxWxid,
  198. WxWxidNickname: &wxNickname,
  199. Type: &v.Type,
  200. Wxid: &v.Wxid,
  201. Account: &v.Account,
  202. Nickname: &v.Nickname,
  203. Markname: &v.Markname,
  204. Headimg: &v.Headimg,
  205. Sex: &v.Sex,
  206. Starrole: &v.Starrole,
  207. Dontseeit: &v.Dontseeit,
  208. Dontseeme: &v.Dontseeme,
  209. Lag: &v.Lag,
  210. Gid: &v.Gid,
  211. Gname: &v.Gname,
  212. V3: &v.V3,
  213. LabelRelationships: labelRelationships,
  214. IsInBlockList: &isInBlockList,
  215. Ctype: &v.Ctype,
  216. Cname: &v.Cname,
  217. Cage: &v.Cage,
  218. Carea: &v.Carea,
  219. Cc: &v.Cc,
  220. Phone: &v.Phone,
  221. Cbirthday: &v.Cbirthday,
  222. Cbirtharea: &v.Cbirtharea,
  223. CidcardNo: &v.CidcardNo,
  224. Ctitle: &v.Ctitle,
  225. CustomFields: custom_field,
  226. })
  227. }
  228. return resp, nil
  229. }