get_contact_list_logic.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 && *req.WxWxid != "" {
  116. predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
  117. }
  118. if req.Wxid != nil && *req.Wxid != "" {
  119. predicates = append(predicates, contact.WxidContains(*req.Wxid))
  120. }
  121. if req.Account != nil && *req.Account != "" {
  122. predicates = append(predicates, contact.AccountContains(*req.Account))
  123. }
  124. if req.Nickname != nil && *req.Nickname != "" {
  125. predicates = append(predicates, contact.NicknameContains(*req.Nickname))
  126. }
  127. if req.Markname != nil && *req.Markname != "" {
  128. predicates = append(predicates, contact.MarknameContains(*req.Markname))
  129. }
  130. if req.Type != nil {
  131. predicates = append(predicates, contact.TypeEQ(*req.Type))
  132. } else {
  133. predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
  134. }
  135. data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
  136. query.WithLabels()
  137. }).WithContactFields(func(query *ent.ContactFieldQuery) {
  138. query.WithFieldContact()
  139. }).Page(l.ctx, req.Page, req.PageSize)
  140. if err != nil {
  141. return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
  142. }
  143. resp := &types.ContactListResp{}
  144. resp.Msg = errormsg.Success
  145. resp.Data.Total = data.PageDetails.Total
  146. wxWxids := []string{}
  147. wxWxidsSet := make(map[string]*ent.Wx)
  148. blockListSet := make(map[string]struct{})
  149. groupBlockListSet := make(map[string]struct{})
  150. for _, v := range data.List {
  151. wxWxids = append(wxWxids, v.WxWxid)
  152. }
  153. wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
  154. for _, w := range wxs {
  155. wxWxidsSet[w.Wxid] = w
  156. for _, b := range w.BlockList {
  157. blockListSet[w.Wxid+"_"+b] = struct{}{}
  158. }
  159. for _, g := range w.GroupBlockList {
  160. groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
  161. }
  162. }
  163. field_template, _ := l.svcCtx.DB.ContactFieldTemplate.Query().
  164. Where(
  165. contactfieldtemplate.OrganizationID(organizationId), // Additional filter by organizationId
  166. ).
  167. Only(l.ctx)
  168. for _, v := range data.List {
  169. // 自定义字段
  170. custom_field := make([]types.ContactFieldTemplate, 0)
  171. if field_template != nil && len(field_template.Template) > 0 {
  172. field_set := make(map[string][]string)
  173. fields := v.Edges.ContactFields
  174. for _, field := range fields {
  175. field_set[field.FormID] = field.Value
  176. }
  177. for _, template := range field_template.Template {
  178. template.Value = field_set[*template.Id]
  179. custom_field = append(custom_field, ConvertCustomToInternal(template))
  180. }
  181. }
  182. isInBlockList := false
  183. labelRelationships := make([]types.ContactLabelList, 0)
  184. if v.Edges.ContactRelationships != nil {
  185. for _, lr := range v.Edges.ContactRelationships {
  186. if lr.Edges.Labels == nil {
  187. continue
  188. }
  189. labelRelationships = append(labelRelationships, types.ContactLabelList{
  190. Label: &lr.Edges.Labels.Name,
  191. Value: &lr.LabelID,
  192. })
  193. }
  194. }
  195. var wxNickname string
  196. if wxWxidsSet[v.WxWxid] == nil {
  197. wxNickname = v.WxWxid
  198. } else {
  199. wxNickname = wxWxidsSet[v.WxWxid].Nickname
  200. }
  201. if v.Type == 1 {
  202. if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
  203. isInBlockList = true
  204. } else {
  205. _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
  206. }
  207. } else {
  208. if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
  209. isInBlockList = true
  210. } else {
  211. _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
  212. }
  213. }
  214. // 这里要判断联系人与自定义字段的修改时间,使用最大的那个
  215. var updateTime, cfUpdateTime, cUpdateTime int64 = 0, 0, v.UpdatedAt.UnixMilli()
  216. if len(v.Edges.ContactFields) > 0 {
  217. cfUpdateTime = v.Edges.ContactFields[0].UpdatedAt.UnixMilli()
  218. }
  219. if cfUpdateTime > cUpdateTime {
  220. updateTime = cfUpdateTime
  221. } else {
  222. updateTime = cUpdateTime
  223. }
  224. resp.Data.Data = append(resp.Data.Data,
  225. types.ContactInfo{
  226. BaseIDInfo: types.BaseIDInfo{
  227. Id: &v.ID,
  228. CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
  229. UpdatedAt: pointy.GetPointer(updateTime),
  230. },
  231. Status: &v.Status,
  232. WxWxid: &v.WxWxid,
  233. WxWxidNickname: &wxNickname,
  234. Type: &v.Type,
  235. Wxid: &v.Wxid,
  236. Account: &v.Account,
  237. Nickname: &v.Nickname,
  238. Markname: &v.Markname,
  239. Headimg: &v.Headimg,
  240. Sex: &v.Sex,
  241. Starrole: &v.Starrole,
  242. Dontseeit: &v.Dontseeit,
  243. Dontseeme: &v.Dontseeme,
  244. Lag: &v.Lag,
  245. Gid: &v.Gid,
  246. Gname: &v.Gname,
  247. V3: &v.V3,
  248. LabelRelationships: labelRelationships,
  249. IsInBlockList: &isInBlockList,
  250. Ctype: &v.Ctype,
  251. Cname: &v.Cname,
  252. Cage: &v.Cage,
  253. Carea: &v.Carea,
  254. Cc: &v.Cc,
  255. Phone: &v.Phone,
  256. Cbirthday: &v.Cbirthday,
  257. Cbirtharea: &v.Cbirtharea,
  258. CidcardNo: &v.CidcardNo,
  259. Ctitle: &v.Ctitle,
  260. CustomFields: custom_field,
  261. })
  262. }
  263. return resp, nil
  264. }