get_contact_list_logic.go 7.2 KB

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