get_contact_list_logic.go 7.5 KB

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