get_contact_list_logic.go 7.9 KB

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