search_app_page_logic.go 1.8 KB
package page

import (
	"context"
	"github.com/samber/lo"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/contextdata"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/pkg/xerr"

	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-bchart/cmd/chart-server/api/internal/types"

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

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

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

func (l *SearchAppPageLogic) SearchAppPage(req *types.AppPageSearchRequest) (resp *types.AppPageSearchResponse, err error) {
	var (
		conn     = l.svcCtx.DefaultDBConn()
		appPages []*domain.AppPage
		charts   []*domain.Chart
		tenantId = contextdata.GetTenantFromCtx(l.ctx)
		total    int64
		chartIds []int64
	)
	if total, appPages, err = l.svcCtx.AppPageRepository.Find(l.ctx, conn, domain.IndexTenantId(tenantId)().WithOffsetLimit(req.Page, req.Size)); err != nil {
		return nil, xerr.NewErr(err)
	}
	lo.ForEach(appPages, func(item *domain.AppPage, index int) {
		chartIds = append(chartIds, item.Charts...)
	})

	queryOptions := domain.IndexTenantId(tenantId)().WithKV("ids", chartIds)
	if _, charts, err = l.svcCtx.ChartRepository.Find(l.ctx, conn, queryOptions); err != nil {
		return nil, xerr.NewErr(err)
	}
	resp = &types.AppPageSearchResponse{
		Total: total,
	}
	for _, page := range appPages {
		resp.List = append(resp.List, types.NewAppPageItem(page, charts))
	}
	return
}