mini_company_search_logic.go 1.8 KB
package company

import (
	"context"
	"github.com/samber/lo"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/svc"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/api/internal/types"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/cmd/discuss/interanl/pkg/domain"
	"gitlab.fjmaimaimai.com/allied-creation/sumifcc-discuss/pkg/xerr"

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

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

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

func (l *MiniCompanySearchLogic) MiniCompanySearch(req *types.CompanySearchRequest) (resp *types.CompanySearchResponse, err error) {
	var (
		conn        = l.svcCtx.DefaultDBConn()
		companyList []*domain.Company
		total       int64
	)
	queryOptions := domain.NewQueryOptions()
	if req.Page != 0 {
		queryOptions.WithOffsetLimit(req.Page, req.Size)
	}
	if req.Code != "" {
		queryOptions.WithKV("code", req.Code)
	}
	queryOptions.MustWithKV("visible", 1)
	total, companyList, err = l.svcCtx.CompanyRepository.Find(l.ctx, conn, queryOptions)
	if err != nil {
		return nil, xerr.NewErrMsgErr("公司列表获取失败", err)
	}
	resp = &types.CompanySearchResponse{
		Total: total,
		List:  make([]types.Company, 0),
	}
	lo.ForEach(companyList, func(item *domain.Company, index int) {
		resp.List = append(resp.List, NewCompany(item))
	})
	return
}

func NewCompany(item *domain.Company) types.Company {
	return types.Company{
		Id:   item.Id,
		Name: item.Name,
		Code: item.Code,
		Logo: lo.Ternary(item.Logo != "", item.Logo, domain.DefaultCompanyLog),
	}
}