get_contact_list_logic.go 7.4 KB

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