package whatsapp import ( "context" "github.com/suyuan32/simple-admin-core/rpc/types/core" "wechat-api/ent/predicate" "wechat-api/ent/whatsapp" "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 GetWhatsappListLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewGetWhatsappListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWhatsappListLogic { return &GetWhatsappListLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } func (l *GetWhatsappListLogic) GetWhatsappList(req *types.WhatsappListReq) (*types.WhatsappListResp, error) { var predicates []predicate.Whatsapp if req.Callback != nil { predicates = append(predicates, whatsapp.CallbackContains(*req.Callback)) } data, err := l.svcCtx.DB.Whatsapp.Query().Where(predicates...).WithAgent().Page(l.ctx, req.Page, req.PageSize) if err != nil { return nil, dberrorhandler.DefaultEntError(l.Logger, err, req) } resp := &types.WhatsappListResp{} resp.Msg = errormsg.Success resp.Data.Total = data.PageDetails.Total for _, v := range data.List { departmentInfo, err := l.svcCtx.CoreRpc.GetDepartmentById(l.ctx, &core.IDReq{Id: v.OrganizationID}) if err != nil { l.Error("获取部门信息失败", err) } agent := types.AgentInfo{} //l.Logger.Infof("v.agent=%v v=%v\n", v.Edges.Agent, v) if v.Edges.Agent != nil { agent = types.AgentInfo{ BaseIDInfo: types.BaseIDInfo{ Id: &v.AgentID, }, Name: &v.Edges.Agent.Name, Role: &v.Edges.Agent.Role, Status: &v.Edges.Agent.Status, Background: &v.Edges.Agent.Background, Examples: &v.Edges.Agent.Examples, } } resp.Data.Data = append(resp.Data.Data, types.WhatsappInfo{ BaseIDInfo: types.BaseIDInfo{ Id: &v.ID, CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()), UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()), }, WaId: &v.WaID, Callback: &v.Callback, Account: &v.Account, Phone: &v.Phone, Cc: &v.Cc, PhoneName: &v.PhoneName, PhoneStatus: &v.PhoneStatus, OrganizationId: &v.OrganizationID, OrganizationName: departmentInfo.Name, AgentId: &v.AgentID, AgentInfo: &agent, ApiBase: &v.APIBase, ApiKey: &v.APIKey, }) } return resp, nil }