init: init refactor
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
// Code scaffolded by goctl. Safe to edit.
|
||||
// goctl 1.10.1
|
||||
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"sundynix-micro-go/app/system/api/internal/svc"
|
||||
"sundynix-micro-go/app/system/api/internal/types"
|
||||
sysModel "sundynix-micro-go/app/system/model"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/logx"
|
||||
)
|
||||
|
||||
type GetClientListLogic struct {
|
||||
logx.Logger
|
||||
ctx context.Context
|
||||
svcCtx *svc.ServiceContext
|
||||
}
|
||||
|
||||
func NewGetClientListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetClientListLogic {
|
||||
return &GetClientListLogic{
|
||||
Logger: logx.WithContext(ctx),
|
||||
ctx: ctx,
|
||||
svcCtx: svcCtx,
|
||||
}
|
||||
}
|
||||
|
||||
func (l *GetClientListLogic) GetClientList(req *types.ClientListReq) (resp interface{}, err error) {
|
||||
var clients []sysModel.SundynixClient
|
||||
var total int64
|
||||
|
||||
db := l.svcCtx.DB.Model(&sysModel.SundynixClient{})
|
||||
if req.Name != "" {
|
||||
db = db.Where("name LIKE ?", "%"+req.Name+"%")
|
||||
}
|
||||
|
||||
if err := db.Count(&total).Error; err != nil {
|
||||
l.Errorf("查询客户端总数失败: %v", err)
|
||||
return nil, fmt.Errorf("查询失败")
|
||||
}
|
||||
|
||||
current := req.Current
|
||||
pageSize := req.PageSize
|
||||
if current <= 0 {
|
||||
current = 1
|
||||
}
|
||||
if pageSize <= 0 {
|
||||
pageSize = 10
|
||||
}
|
||||
|
||||
offset := (current - 1) * pageSize
|
||||
if err := db.Offset(offset).Limit(pageSize).Find(&clients).Error; err != nil {
|
||||
l.Errorf("查询客户端列表失败: %v", err)
|
||||
return nil, fmt.Errorf("查询失败")
|
||||
}
|
||||
|
||||
return map[string]interface{}{
|
||||
"list": clients,
|
||||
"total": total,
|
||||
"current": current,
|
||||
"size": pageSize,
|
||||
}, nil
|
||||
}
|
||||
Reference in New Issue
Block a user