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