218 lines
8.6 KiB
Smarty
218 lines
8.6 KiB
Smarty
{{- if .IsAdd}}
|
||
// 在 {{.StructName}}Repo 接口中新增如下方法
|
||
// 请根据实际需求添加
|
||
|
||
{{- else}}
|
||
package {{.Package}}
|
||
|
||
import (
|
||
{{- if not .OnlyTemplate }}
|
||
"context"
|
||
{{- if .IsTree }}
|
||
"errors"
|
||
{{- end }}
|
||
"{{.Module}}/internal/data/model/{{.Package}}"
|
||
{{- if not .IsTree}}
|
||
{{.Package}}Req "{{.Module}}/internal/service/types/{{.Package}}/request"
|
||
{{- end }}
|
||
"github.com/go-kratos/kratos/v2/log"
|
||
"gorm.io/gorm"
|
||
{{- end }}
|
||
)
|
||
|
||
// {{.StructName}}Repo {{.Description}}仓储接口
|
||
type {{.StructName}}Repo interface {
|
||
{{- if not .OnlyTemplate }}
|
||
Create(ctx context.Context, {{.Abbreviation}} *{{.Package}}.{{.StructName}}) error
|
||
Delete(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}{{- if .AutoCreateResource -}}, userID uint{{- end -}}) error
|
||
DeleteByIds(ctx context.Context, {{.PrimaryField.FieldJson}}s []{{.PrimaryField.FieldType}}{{- if .AutoCreateResource -}}, deletedBy uint{{- end -}}) error
|
||
{{- if .AutoCreateResource }}
|
||
// DeleteWithTx 在事务中删除记录(用于 AutoCreateResource 场景)
|
||
DeleteWithTx(ctx context.Context, tx *gorm.DB, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}) error
|
||
// DeleteByIdsWithTx 在事务中批量删除记录(用于 AutoCreateResource 场景)
|
||
DeleteByIdsWithTx(ctx context.Context, tx *gorm.DB, {{.PrimaryField.FieldJson}}s []{{.PrimaryField.FieldType}}) error
|
||
{{- end }}
|
||
Update(ctx context.Context, {{.Abbreviation}} *{{.Package}}.{{.StructName}}) error
|
||
FindByID(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}) (*{{.Package}}.{{.StructName}}, error)
|
||
{{- if .IsTree }}
|
||
List(ctx context.Context) ([]*{{.Package}}.{{.StructName}}, error)
|
||
HasChildren(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}) (bool, error)
|
||
{{- else }}
|
||
List(ctx context.Context, req *{{.Package}}Req.{{.StructName}}Search) ([]*{{.Package}}.{{.StructName}}, int64, error)
|
||
{{- end }}
|
||
{{- if .HasDataSource }}
|
||
GetDataSource(ctx context.Context) (map[string][]map[string]any, error)
|
||
{{- end }}
|
||
{{- end }}
|
||
{{- if .HasCustomMethods }}
|
||
{{ GenerateCustomMethodInterface .CustomMethods .StructName .Package }}
|
||
{{- end }}
|
||
}
|
||
|
||
// {{.StructName}}Usecase {{.Description}}用例
|
||
type {{.StructName}}Usecase struct {
|
||
repo {{.StructName}}Repo
|
||
log *log.Helper
|
||
}
|
||
|
||
// New{{.StructName}}Usecase 创建{{.Description}}用例
|
||
func New{{.StructName}}Usecase(repo {{.StructName}}Repo, logger log.Logger) *{{.StructName}}Usecase {
|
||
return &{{.StructName}}Usecase{
|
||
repo: repo,
|
||
log: log.NewHelper(logger),
|
||
}
|
||
}
|
||
|
||
{{- if not .OnlyTemplate }}
|
||
|
||
// Create 创建{{.Description}}
|
||
func (uc *{{.StructName}}Usecase) Create(ctx context.Context, {{.Abbreviation}} *{{.Package}}.{{.StructName}}) error {
|
||
uc.log.WithContext(ctx).Infof("创建{{.Description}}")
|
||
if err := uc.repo.Create(ctx, {{.Abbreviation}}); err != nil {
|
||
uc.log.WithContext(ctx).Errorf("创建{{.Description}}失败: %v", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// Delete 删除{{.Description}}
|
||
func (uc *{{.StructName}}Usecase) Delete(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}{{- if .AutoCreateResource -}}, userID uint{{- end -}}) error {
|
||
uc.log.WithContext(ctx).Infof("删除{{.Description}}, {{.PrimaryField.FieldJson}}: %v", {{.PrimaryField.FieldJson}})
|
||
{{- if .IsTree }}
|
||
// 检查是否有子节点
|
||
hasChildren, err := uc.repo.HasChildren(ctx, {{.PrimaryField.FieldJson}})
|
||
if err != nil {
|
||
uc.log.WithContext(ctx).Errorf("检查子节点失败: %v", err)
|
||
return err
|
||
}
|
||
if hasChildren {
|
||
uc.log.WithContext(ctx).Warn("此节点存在子节点不允许删除")
|
||
return errors.New("此节点存在子节点不允许删除")
|
||
}
|
||
{{- end }}
|
||
if err := uc.repo.Delete(ctx, {{.PrimaryField.FieldJson}}{{- if .AutoCreateResource -}}, userID{{- end -}}); err != nil {
|
||
uc.log.WithContext(ctx).Errorf("删除{{.Description}}失败: %v", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// DeleteByIds 批量删除{{.Description}}
|
||
func (uc *{{.StructName}}Usecase) DeleteByIds(ctx context.Context, {{.PrimaryField.FieldJson}}s []{{.PrimaryField.FieldType}}{{- if .AutoCreateResource -}}, deletedBy uint{{- end -}}) error {
|
||
uc.log.WithContext(ctx).Infof("批量删除{{.Description}}, 数量: %d", len({{.PrimaryField.FieldJson}}s))
|
||
if err := uc.repo.DeleteByIds(ctx, {{.PrimaryField.FieldJson}}s{{- if .AutoCreateResource -}}, deletedBy{{- end -}}); err != nil {
|
||
uc.log.WithContext(ctx).Errorf("批量删除{{.Description}}失败: %v", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
{{- if .AutoCreateResource }}
|
||
// DeleteWithTx 在事务中删除{{.Description}}(用于 AutoCreateResource 场景)
|
||
// @param ctx 上下文
|
||
// @param tx 事务对象
|
||
// @param {{.PrimaryField.FieldJson}} 主键ID
|
||
// @return error 删除失败时返回错误
|
||
func (uc *{{.StructName}}Usecase) DeleteWithTx(ctx context.Context, tx *gorm.DB, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}) error {
|
||
uc.log.WithContext(ctx).Infof("事务删除{{.Description}}, {{.PrimaryField.FieldJson}}: %v", {{.PrimaryField.FieldJson}})
|
||
{{- if .IsTree }}
|
||
// 检查是否有子节点
|
||
hasChildren, err := uc.repo.HasChildren(ctx, {{.PrimaryField.FieldJson}})
|
||
if err != nil {
|
||
uc.log.WithContext(ctx).Errorf("检查子节点失败: %v", err)
|
||
return err
|
||
}
|
||
if hasChildren {
|
||
uc.log.WithContext(ctx).Warn("此节点存在子节点不允许删除")
|
||
return errors.New("此节点存在子节点不允许删除")
|
||
}
|
||
{{- end }}
|
||
if err := uc.repo.DeleteWithTx(ctx, tx, {{.PrimaryField.FieldJson}}); err != nil {
|
||
uc.log.WithContext(ctx).Errorf("事务删除{{.Description}}失败: %v", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// DeleteByIdsWithTx 在事务中批量删除{{.Description}}(用于 AutoCreateResource 场景)
|
||
// @param ctx 上下文
|
||
// @param tx 事务对象
|
||
// @param {{.PrimaryField.FieldJson}}s 主键ID列表
|
||
// @return error 删除失败时返回错误
|
||
func (uc *{{.StructName}}Usecase) DeleteByIdsWithTx(ctx context.Context, tx *gorm.DB, {{.PrimaryField.FieldJson}}s []{{.PrimaryField.FieldType}}) error {
|
||
uc.log.WithContext(ctx).Infof("事务批量删除{{.Description}}, 数量: %d", len({{.PrimaryField.FieldJson}}s))
|
||
if err := uc.repo.DeleteByIdsWithTx(ctx, tx, {{.PrimaryField.FieldJson}}s); err != nil {
|
||
uc.log.WithContext(ctx).Errorf("事务批量删除{{.Description}}失败: %v", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
{{- end }}
|
||
|
||
// Update 更新{{.Description}}
|
||
func (uc *{{.StructName}}Usecase) Update(ctx context.Context, {{.Abbreviation}} *{{.Package}}.{{.StructName}}) error {
|
||
uc.log.WithContext(ctx).Infof("更新{{.Description}}")
|
||
if err := uc.repo.Update(ctx, {{.Abbreviation}}); err != nil {
|
||
uc.log.WithContext(ctx).Errorf("更新{{.Description}}失败: %v", err)
|
||
return err
|
||
}
|
||
return nil
|
||
}
|
||
|
||
// FindByID 根据ID获取{{.Description}}
|
||
func (uc *{{.StructName}}Usecase) FindByID(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}) (*{{.Package}}.{{.StructName}}, error) {
|
||
uc.log.WithContext(ctx).Debugf("查询{{.Description}}, {{.PrimaryField.FieldJson}}: %v", {{.PrimaryField.FieldJson}})
|
||
result, err := uc.repo.FindByID(ctx, {{.PrimaryField.FieldJson}})
|
||
if err != nil {
|
||
uc.log.WithContext(ctx).Errorf("查询{{.Description}}失败: %v", err)
|
||
return nil, err
|
||
}
|
||
return result, nil
|
||
}
|
||
|
||
{{- if .IsTree }}
|
||
// List 获取{{.Description}}列表(树形)
|
||
func (uc *{{.StructName}}Usecase) List(ctx context.Context) ([]*{{.Package}}.{{.StructName}}, error) {
|
||
uc.log.WithContext(ctx).Debug("获取{{.Description}}树形列表")
|
||
result, err := uc.repo.List(ctx)
|
||
if err != nil {
|
||
uc.log.WithContext(ctx).Errorf("获取{{.Description}}列表失败: %v", err)
|
||
return nil, err
|
||
}
|
||
return result, nil
|
||
}
|
||
{{- else }}
|
||
// List 分页获取{{.Description}}列表
|
||
func (uc *{{.StructName}}Usecase) List(ctx context.Context, req *{{.Package}}Req.{{.StructName}}Search) ([]*{{.Package}}.{{.StructName}}, int64, error) {
|
||
uc.log.WithContext(ctx).Debugf("获取{{.Description}}列表, 页码: %d, 每页: %d", req.Page, req.PageSize)
|
||
list, total, err := uc.repo.List(ctx, req)
|
||
if err != nil {
|
||
uc.log.WithContext(ctx).Errorf("获取{{.Description}}列表失败: %v", err)
|
||
return nil, 0, err
|
||
}
|
||
return list, total, nil
|
||
}
|
||
{{- end }}
|
||
|
||
{{- if .HasDataSource }}
|
||
// GetDataSource 获取{{.Description}}数据源
|
||
func (uc *{{.StructName}}Usecase) GetDataSource(ctx context.Context) (map[string][]map[string]any, error) {
|
||
uc.log.WithContext(ctx).Debug("获取{{.Description}}数据源")
|
||
result, err := uc.repo.GetDataSource(ctx)
|
||
if err != nil {
|
||
uc.log.WithContext(ctx).Errorf("获取{{.Description}}数据源失败: %v", err)
|
||
return nil, err
|
||
}
|
||
return result, nil
|
||
}
|
||
{{- end }}
|
||
|
||
{{- end }}
|
||
|
||
// GetPublic 获取{{.Description}}公开数据
|
||
func (uc *{{.StructName}}Usecase) GetPublic(ctx context.Context) {
|
||
// 此方法为获取公开数据
|
||
// 请自行实现
|
||
}
|
||
{{- end }}
|