123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- package contact
- import (
- "context"
- "wechat-api/ent"
- "wechat-api/ent/label"
- "wechat-api/ent/labelrelationship"
- "wechat-api/ent/wx"
- "wechat-api/ent/contact"
- "wechat-api/ent/predicate"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "wechat-api/internal/utils/dberrorhandler"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/suyuan32/simple-admin-common/utils/pointy"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetContactListLogic struct {
- ctx context.Context
- svcCtx *svc.ServiceContext
- logx.Logger
- }
- func NewGetContactListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetContactListLogic {
- return &GetContactListLogic{
- ctx: ctx,
- svcCtx: svcCtx,
- Logger: logx.WithContext(ctx),
- }
- }
- func convertLabelToLabelInfo(label *ent.Label) types.LabelInfo {
- return types.LabelInfo{
- BaseIDInfo: types.BaseIDInfo{
- Id: &label.ID,
- CreatedAt: pointy.GetPointer(label.CreatedAt.UnixMilli()),
- UpdatedAt: pointy.GetPointer(label.UpdatedAt.UnixMilli()),
- },
- Status: &label.Status,
- Type: &label.Type,
- Name: &label.Name,
- From: &label.From,
- Mode: &label.Mode,
- Conditions: &label.Conditions,
- }
- }
- func (l *GetContactListLogic) GetContactList(req *types.ContactListReq) (*types.ContactListResp, error) {
- organizationId := l.ctx.Value("organizationId").(uint64)
- var predicates []predicate.Contact
- predicates = append(predicates, contact.OrganizationIDEQ(organizationId))
- predicates = append(predicates, contact.Ctype(1))
- if len(req.LabelIDs) > 0 {
- predicates = append(predicates, contact.HasContactRelationshipsWith(
- labelrelationship.HasLabelsWith(
- label.IDIn(req.LabelIDs...),
- ),
- ))
- }
- if req.WxWxid != nil {
- predicates = append(predicates, contact.WxWxidContains(*req.WxWxid))
- }
- if req.Wxid != nil {
- predicates = append(predicates, contact.WxidContains(*req.Wxid))
- }
- if req.Account != nil {
- predicates = append(predicates, contact.AccountContains(*req.Account))
- }
- if req.Nickname != nil {
- predicates = append(predicates, contact.NicknameContains(*req.Nickname))
- }
- if req.Type != nil {
- predicates = append(predicates, contact.TypeEQ(*req.Type))
- } else {
- predicates = append(predicates, contact.Or(contact.TypeEQ(1), contact.TypeEQ(2)))
- }
- data, err := l.svcCtx.DB.Contact.Query().Where(predicates...).WithContactRelationships(func(query *ent.LabelRelationshipQuery) {
- query.WithLabels()
- }).Page(l.ctx, req.Page, req.PageSize)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- resp := &types.ContactListResp{}
- resp.Msg = errormsg.Success
- resp.Data.Total = data.PageDetails.Total
- wxWxids := []string{}
- wxWxidsSet := make(map[string]*ent.Wx)
- blockListSet := make(map[string]struct{})
- groupBlockListSet := make(map[string]struct{})
- for _, v := range data.List {
- wxWxids = append(wxWxids, v.WxWxid)
- }
- wxs, err := l.svcCtx.DB.Wx.Query().Where(wx.WxidIn(wxWxids...)).All(l.ctx)
- for _, w := range wxs {
- wxWxidsSet[w.Wxid] = w
- for _, b := range w.BlockList {
- blockListSet[w.Wxid+"_"+b] = struct{}{}
- }
- for _, g := range w.GroupBlockList {
- groupBlockListSet[w.Wxid+"_"+g] = struct{}{}
- }
- }
- for _, v := range data.List {
- isInBlockList := false
- labelRelationships := make([]types.ContactLabelList, 0)
- if v.Edges.ContactRelationships != nil {
- for _, lr := range v.Edges.ContactRelationships {
- if lr.Edges.Labels == nil {
- continue
- }
- labelRelationships = append(labelRelationships, types.ContactLabelList{
- Label: &lr.Edges.Labels.Name,
- Value: &lr.LabelID,
- })
- }
- }
- var wxNickname string
- if wxWxidsSet[v.WxWxid] == nil {
- wxNickname = v.WxWxid
- } else {
- wxNickname = wxWxidsSet[v.WxWxid].Nickname
- }
- if v.Type == 1 {
- if _, exists := blockListSet[v.WxWxid+"_"+"ALL"]; exists {
- isInBlockList = true
- } else {
- _, isInBlockList = blockListSet[v.WxWxid+"_"+v.Wxid]
- }
- } else {
- if _, exists := groupBlockListSet[v.WxWxid+"_"+"ALL"]; exists {
- isInBlockList = true
- } else {
- _, isInBlockList = groupBlockListSet[v.WxWxid+"_"+v.Wxid]
- }
- }
- resp.Data.Data = append(resp.Data.Data,
- types.ContactInfo{
- BaseIDInfo: types.BaseIDInfo{
- Id: &v.ID,
- CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
- UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
- },
- Status: &v.Status,
- WxWxid: &v.WxWxid,
- WxWxidNickname: &wxNickname,
- Type: &v.Type,
- Wxid: &v.Wxid,
- Account: &v.Account,
- Nickname: &v.Nickname,
- Markname: &v.Markname,
- Headimg: &v.Headimg,
- Sex: &v.Sex,
- Starrole: &v.Starrole,
- Dontseeit: &v.Dontseeit,
- Dontseeme: &v.Dontseeme,
- Lag: &v.Lag,
- Gid: &v.Gid,
- Gname: &v.Gname,
- V3: &v.V3,
- LabelRelationships: labelRelationships,
- IsInBlockList: &isInBlockList,
- Ctype: &v.Ctype,
- Cname: &v.Cname,
- Cage: &v.Cage,
- Carea: &v.Carea,
- Cc: &v.Cc,
- Phone: &v.Phone,
- Cbirthday: &v.Cbirthday,
- Cbirtharea: &v.Cbirtharea,
- CidcardNo: &v.CidcardNo,
- Ctitle: &v.Ctitle,
- })
- }
- return resp, nil
- }
|