get_contact_list_logic.go 8.3 KB

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