mini_company_search_logic.go
1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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)
}
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),
}
}