mini_business_logic.go 5.5 KB
package message

import (
	"context"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type MiniBusinessLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMiniBusinessLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MiniBusinessLogic {
	return &MiniBusinessLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MiniBusinessLogic) MiniBusiness(req *types.MessageBusinessRequest) (resp *types.MessageBusinessResponse, err error) {
	var msgType = req.Type

	total, list, err := l.svcCtx.MessageBusinessRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(), domain.NewQueryOptions().
		WithOffsetLimit(req.Page, req.Size).
		WithKV("type", msgType))
	if err != nil {
		return nil, err
	}
	// 输出
	resp = &types.MessageBusinessResponse{}
	resp.Total = total
	resp.List = make([]types.MessageBusinessItem, 0)
	if total == 0 {
		return resp, nil
	}

	var companyIdMap = map[int64]*domain.Company{}
	var userIdMap = map[int64]*domain.User{}
	var articleIdMap = map[int64]*domain.Article{}
	var commentIdMap = map[int64]*domain.Comment{}
	//var discussionIdMap = map[int64]int{}
	//var discussionOpinionIdMap = map[int64]int{}
	for _, item := range list {
		if item.CompanyId != 0 {
			companyIdMap[item.CompanyId] = nil
		}
		if item.UserId != 0 {
			userIdMap[item.UserId] = nil
		}
		if item.ArticleId != 0 {
			articleIdMap[item.ArticleId] = nil
		}
		if item.CommentId != 0 {
			commentIdMap[item.CommentId] = nil
		}
		//if item.DiscussionId != 0 {
		//	discussionIdMap[item.DiscussionId] = 0
		//}
		//if item.DiscussionOpinionId != 0 {
		//	discussionOpinionIdMap[item.DiscussionOpinionId] = 0
		//}
	}
	var companyIds = make([]int64, 0) // 公司ID
	var userIds = make([]int64, 0)    // 用户ID
	var articleIds = make([]int64, 0) // 文章ID
	var commentIds = make([]int64, 0) // 评论ID
	//var discussionIds = make([]int64, 0)        // 讨论ID 暂时搁置
	//var discussionOpinionIds = make([]int64, 0) // 观点ID
	for k, _ := range companyIdMap {
		companyIds = append(companyIds, k)
	}
	for k, _ := range userIdMap {
		userIds = append(userIds, k)
	}
	for k, _ := range articleIdMap {
		articleIds = append(articleIds, k)
	}
	for k, _ := range commentIdMap {
		commentIds = append(commentIds, k)
	}
	//for k, _ := range discussionIdMap {
	//	discussionIds = append(discussionIds, k)
	//}
	//for k, _ := range discussionOpinionIdMap {
	//	discussionOpinionIds = append(discussionOpinionIds, k)
	//}

	// 获取公司
	if len(companyIds) > 0 {
		_, companyList, err := l.svcCtx.CompanyRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(),
			domain.NewQueryOptions().
				WithFindOnly().
				WithKV("ids", userIds).
				WithKV("limit", len(userIds)))
		if err != nil {
			return nil, err
		}
		for i := range companyList {
			companyIdMap[companyList[i].Id] = companyList[i]
		}
	}

	// 获取用户
	if len(userIds) > 0 {
		_, userList, err := l.svcCtx.UserRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(),
			domain.NewQueryOptions().
				WithFindOnly().
				WithKV("ids", userIds).
				WithKV("limit", len(userIds)))
		if err != nil {
			return nil, err
		}
		for i := range userList {
			userIdMap[userList[i].Id] = userList[i]
		}
	}

	// 获取评论
	if len(commentIds) > 0 {
		_, commentList, err := l.svcCtx.CommentRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(),
			domain.NewQueryOptions().
				WithFindOnly().
				WithKV("ids", commentIds).
				WithKV("limit", len(commentIds)))
		if err != nil {
			return nil, err
		}
		for i := range commentList {
			commentIdMap[commentList[i].Id] = commentList[i]
		}
	}

	// 只有这个才需要
	if msgType == int(domain.MsgTypeReply) || msgType == int(domain.MsgTypeLike) {
		// 获取文章数据
		if len(articleIds) > 0 {
			_, articleList, err := l.svcCtx.ArticleRepository.Find(l.ctx, l.svcCtx.DefaultDBConn(),
				domain.NewQueryOptions().
					WithFindOnly().
					WithKV("ids", articleIds).
					WithKV("limit", len(articleIds)))
			if err != nil {
				return nil, err
			}
			for i := range articleList {
				articleIdMap[articleList[i].Id] = articleList[i]
			}
		}
	}

	for _, item := range list {
		to := types.MessageBusinessItem{
			Id:                  item.Id,
			Type:                item.Type,
			OptType:             item.OptType,
			CompanyId:           item.CompanyId,
			UserId:              item.UserId,
			RecipientId:         item.RecipientId,
			ArticleId:           item.ArticleId,
			CommentId:           item.CommentId,
			DiscussionId:        item.DiscussionId,
			DiscussionOpinionId: item.DiscussionOpinionId,
			Content:             item.Content,
			CreatedAt:           item.CreatedAt,
		}

		if v, ok := userIdMap[item.UserId]; ok {
			to.User = &types.User{
				Id:        v.Id,
				CompanyId: v.CompanyId,
				Position:  v.Position,
				Name:      v.Name,
			}

			if v, ok := companyIdMap[item.CompanyId]; ok {
				to.User.CompanyName = v.Name
			}
		}

		if v, ok := articleIdMap[item.ArticleId]; ok {
			to.Article = &types.SimpleArticle{
				Id:           v.Id,
				Title:        v.Title,
				CountLove:    v.CountLove,
				CountComment: v.CountComment,
			}
		}

		if _, ok := commentIdMap[item.CommentId]; ok {
			to.Comment = &types.Comment{
				//Id:           v.Id,
				//Title:        v.Title,
				//CountLove:    v.CountLove,
				//CountComment: v.CountComment,
			}
		}

		resp.List = append(resp.List, to)
	}
	return resp, nil
}