12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- package chatsession
- import (
- "context"
- "github.com/suyuan32/simple-admin-common/msg/errormsg"
- "github.com/suyuan32/simple-admin-common/utils/pointy"
- "github.com/zeromicro/go-zero/core/errorx"
- "wechat-api/ent/chatsession"
- "wechat-api/ent/employee"
- "wechat-api/ent/predicate"
- "wechat-api/ent/wxcard"
- "wechat-api/internal/utils/dberrorhandler"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- "github.com/zeromicro/go-zero/core/logx"
- )
- type GetChatSessionListLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- }
- func NewGetChatSessionListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetChatSessionListLogic {
- return &GetChatSessionListLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx}
- }
- func (l *GetChatSessionListLogic) GetChatSessionList(req *types.ChatSessionListReq) (*types.ChatSessionListResp, error) {
- resp := &types.ChatSessionListResp{}
- resp.Msg = errormsg.Success
- var predicates []predicate.ChatSession
- if req.BotType == nil || *req.BotType == 0 {
- return nil, errorx.NewInvalidArgumentError("BotId cannot be null")
- }
- if req.BotName != nil && *req.BotName != "" {
- predicates = append(predicates, chatsession.BotType(*req.BotType))
- ids := make([]uint64, 0)
- if *req.BotType == 2 {
- wxcardList, err := l.svcCtx.DB.WxCard.Query().Where(wxcard.NameContains(*req.BotName)).All(l.ctx)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- for _, val := range wxcardList {
- ids = append(ids, val.ID)
- }
- } else if *req.BotType == 3 {
- employeeList, err := l.svcCtx.DB.Employee.Query().Where(employee.TitleContains(*req.BotName)).All(l.ctx)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- for _, val := range employeeList {
- ids = append(ids, val.ID)
- }
- }
- if len(ids) > 0 {
- predicates = append(predicates, chatsession.BotIDIn(ids...))
- } else {
- return resp, nil
- }
- }
- data, err := l.svcCtx.DB.ChatSession.Query().Where(predicates...).Page(l.ctx, req.Page, req.PageSize)
- if err != nil {
- return nil, dberrorhandler.DefaultEntError(l.Logger, err, req)
- }
- resp.Data.Total = data.PageDetails.Total
- for _, v := range data.List {
- resp.Data.Data = append(resp.Data.Data,
- types.ChatSessionInfo{
- BaseIDInfo: types.BaseIDInfo{
- Id: &v.ID,
- CreatedAt: pointy.GetPointer(v.CreatedAt.UnixMilli()),
- UpdatedAt: pointy.GetPointer(v.UpdatedAt.UnixMilli()),
- },
- Name: &v.Name,
- UserId: &v.UserID,
- BotId: &v.BotID,
- BotType: &v.BotType,
- })
- }
- return resp, nil
- }
|