后端重构
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
{{- if .IsAdd }}
|
||||
// 请在 {{.Package}} 包的 enter.go 文件中的 ProviderSet 添加以下内容:
|
||||
// New{{.StructName}}Service,
|
||||
{{- else }}
|
||||
package {{ .Package }}
|
||||
|
||||
import "github.com/google/wire"
|
||||
|
||||
// ProviderSet is {{.Package}} service providers.
|
||||
// 包含 {{.Description}} 相关的服务层实现
|
||||
// Service 层负责:
|
||||
// 1. 请求类型转换(Request -> Model)
|
||||
// 2. 事务管理
|
||||
// 3. 调用 Biz 层 Usecase 完成业务逻辑
|
||||
var ProviderSet = wire.NewSet(New{{.StructName}}Service)
|
||||
{{- end }}
|
||||
@@ -0,0 +1,245 @@
|
||||
{{- if .IsAdd}}
|
||||
|
||||
// Get{{.StructName}}InfoList 新增搜索语句
|
||||
{{ GenerateSearchConditions .Fields }}
|
||||
// Get{{.StructName}}InfoList 新增排序语句 请自行在搜索语句中添加orderMap内容
|
||||
{{- range .Fields}}
|
||||
{{- if .Sort}}
|
||||
orderMap["{{.ColumnName}}"] = true
|
||||
{{- end}}
|
||||
{{- end}}
|
||||
|
||||
|
||||
{{- if .HasDataSource }}
|
||||
// Get{{.StructName}}DataSource()方法新增关联语句
|
||||
{{range $key, $value := .DataSourceMap}}
|
||||
{{$key}} := make([]map[string]any, 0)
|
||||
s.data.DB().Table("{{$value.Table}}"){{- if $value.HasDeletedAt}}.Where("deleted_at IS NULL"){{ end }}.Select("{{$value.Label}} as label,{{$value.Value}} as value").Scan(&{{$key}})
|
||||
res["{{$key}}"] = {{$key}}
|
||||
{{- end }}
|
||||
{{- end }}
|
||||
{{- else}}
|
||||
package {{.Package}}
|
||||
|
||||
import (
|
||||
{{- if not .OnlyTemplate }}
|
||||
"context"
|
||||
"{{.Module}}/internal/biz/{{.Package}}"
|
||||
model "{{.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}}Service {{.Description}}服务层
|
||||
// 负责处理请求类型转换、事务管理,并调用 Biz 层 Usecase 完成业务逻辑
|
||||
type {{.StructName}}Service struct {
|
||||
uc *{{.Package}}.{{.StructName}}Usecase
|
||||
db *gorm.DB
|
||||
log *log.Helper
|
||||
}
|
||||
|
||||
// New{{.StructName}}Service 创建{{.Description}}服务
|
||||
// @param uc Biz 层 Usecase,包含业务逻辑
|
||||
// @param db 数据库连接,用于事务管理
|
||||
// @param logger 日志记录器
|
||||
func New{{.StructName}}Service(uc *{{.Package}}.{{.StructName}}Usecase, db *gorm.DB, logger log.Logger) *{{.StructName}}Service {
|
||||
return &{{.StructName}}Service{
|
||||
uc: uc,
|
||||
db: db,
|
||||
log: log.NewHelper(logger),
|
||||
}
|
||||
}
|
||||
|
||||
{{- if not .OnlyTemplate }}
|
||||
|
||||
// Create{{.StructName}} 创建{{.Description}}记录
|
||||
// @param ctx 上下文
|
||||
// @param req 创建请求,包含{{.Description}}信息
|
||||
// @return error 创建失败时返回错误
|
||||
func (s *{{.StructName}}Service) Create{{.StructName}}(ctx context.Context, req *{{.Package}}Req.{{.StructName}}Request) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 创建{{.Description}}")
|
||||
// 请求类型转换:将 Request 转换为 Model
|
||||
{{.Abbreviation}} := req.To{{.StructName}}()
|
||||
return s.uc.Create(ctx, {{.Abbreviation}})
|
||||
}
|
||||
|
||||
// Create{{.StructName}}WithModel 使用 Model 创建{{.Description}}记录(兼容 Handler 直接调用)
|
||||
// @param ctx 上下文
|
||||
// @param {{.Abbreviation}} {{.Description}}模型
|
||||
// @return error 创建失败时返回错误
|
||||
func (s *{{.StructName}}Service) Create{{.StructName}}WithModel(ctx context.Context, {{.Abbreviation}} *model.{{.StructName}}) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 创建{{.Description}}(Model)")
|
||||
return s.uc.Create(ctx, {{.Abbreviation}})
|
||||
}
|
||||
|
||||
|
||||
// Delete{{.StructName}} 删除{{.Description}}记录
|
||||
// @param ctx 上下文
|
||||
// @param {{.PrimaryField.FieldJson}} 主键ID
|
||||
{{- if .AutoCreateResource }}
|
||||
// @param userID 操作用户ID
|
||||
{{- end }}
|
||||
// @return error 删除失败时返回错误
|
||||
{{- if .AutoCreateResource }}
|
||||
// 注意:当启用 AutoCreateResource 时,删除操作使用事务确保 deleted_by 字段更新和删除操作的原子性
|
||||
{{- end }}
|
||||
func (s *{{.StructName}}Service) Delete{{.StructName}}(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}{{- if .AutoCreateResource -}}, userID uint{{- end -}}) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 删除{{.Description}}, {{.PrimaryField.FieldJson}}: %v", {{.PrimaryField.FieldJson}})
|
||||
{{- if .AutoCreateResource }}
|
||||
// 使用事务确保 deleted_by 更新和删除操作的原子性
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 先更新 deleted_by 字段
|
||||
if err := tx.Model(&model.{{.StructName}}{}).Where("{{.PrimaryField.ColumnName}} = ?", {{.PrimaryField.FieldJson}}).Update("deleted_by", userID).Error; err != nil {
|
||||
s.log.WithContext(ctx).Errorf("更新 deleted_by 失败: %v", err)
|
||||
return err
|
||||
}
|
||||
// 再执行删除
|
||||
return s.uc.DeleteWithTx(ctx, tx, {{.PrimaryField.FieldJson}})
|
||||
})
|
||||
{{- else }}
|
||||
return s.uc.Delete(ctx, {{.PrimaryField.FieldJson}})
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
// Delete{{.StructName}}ByIds 批量删除{{.Description}}记录
|
||||
// @param ctx 上下文
|
||||
// @param {{.PrimaryField.FieldJson}}s 主键ID列表
|
||||
{{- if .AutoCreateResource }}
|
||||
// @param deletedBy 操作用户ID
|
||||
{{- end }}
|
||||
// @return error 删除失败时返回错误
|
||||
{{- if .AutoCreateResource }}
|
||||
// 注意:当启用 AutoCreateResource 时,批量删除操作使用事务确保 deleted_by 字段更新和删除操作的原子性
|
||||
{{- end }}
|
||||
func (s *{{.StructName}}Service) Delete{{.StructName}}ByIds(ctx context.Context, {{.PrimaryField.FieldJson}}s []{{.PrimaryField.FieldType}}{{- if .AutoCreateResource }}, deletedBy uint{{- end}}) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 批量删除{{.Description}}, 数量: %d", len({{.PrimaryField.FieldJson}}s))
|
||||
{{- if .AutoCreateResource }}
|
||||
// 使用事务确保 deleted_by 更新和删除操作的原子性
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
// 先批量更新 deleted_by 字段
|
||||
if err := tx.Model(&model.{{.StructName}}{}).Where("{{.PrimaryField.ColumnName}} in ?", {{.PrimaryField.FieldJson}}s).Update("deleted_by", deletedBy).Error; err != nil {
|
||||
s.log.WithContext(ctx).Errorf("批量更新 deleted_by 失败: %v", err)
|
||||
return err
|
||||
}
|
||||
// 再执行批量删除
|
||||
return s.uc.DeleteByIdsWithTx(ctx, tx, {{.PrimaryField.FieldJson}}s)
|
||||
})
|
||||
{{- else }}
|
||||
return s.uc.DeleteByIds(ctx, {{.PrimaryField.FieldJson}}s)
|
||||
{{- end }}
|
||||
}
|
||||
|
||||
|
||||
// Update{{.StructName}} 更新{{.Description}}记录
|
||||
// @param ctx 上下文
|
||||
// @param req 更新请求,包含{{.Description}}信息
|
||||
// @return error 更新失败时返回错误
|
||||
func (s *{{.StructName}}Service) Update{{.StructName}}(ctx context.Context, req *{{.Package}}Req.{{.StructName}}Request) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 更新{{.Description}}")
|
||||
// 请求类型转换:将 Request 转换为 Model
|
||||
{{.Abbreviation}} := req.To{{.StructName}}()
|
||||
return s.uc.Update(ctx, {{.Abbreviation}})
|
||||
}
|
||||
|
||||
// Update{{.StructName}}WithModel 使用 Model 更新{{.Description}}记录(兼容 Handler 直接调用)
|
||||
// @param ctx 上下文
|
||||
// @param {{.Abbreviation}} {{.Description}}模型
|
||||
// @return error 更新失败时返回错误
|
||||
func (s *{{.StructName}}Service) Update{{.StructName}}WithModel(ctx context.Context, {{.Abbreviation}} *model.{{.StructName}}) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 更新{{.Description}}(Model)")
|
||||
return s.uc.Update(ctx, {{.Abbreviation}})
|
||||
}
|
||||
|
||||
// Get{{.StructName}} 根据{{.PrimaryField.FieldJson}}获取{{.Description}}记录
|
||||
// @param ctx 上下文
|
||||
// @param {{.PrimaryField.FieldJson}} 主键ID
|
||||
// @return *model.{{.StructName}} {{.Description}}记录
|
||||
// @return error 查询失败时返回错误
|
||||
func (s *{{.StructName}}Service) Get{{.StructName}}(ctx context.Context, {{.PrimaryField.FieldJson}} {{.PrimaryField.FieldType}}) (*model.{{.StructName}}, error) {
|
||||
s.log.WithContext(ctx).Debugf("Service: 查询{{.Description}}, {{.PrimaryField.FieldJson}}: %v", {{.PrimaryField.FieldJson}})
|
||||
return s.uc.FindByID(ctx, {{.PrimaryField.FieldJson}})
|
||||
}
|
||||
|
||||
|
||||
{{- if .IsTree }}
|
||||
// Get{{.StructName}}InfoList 获取{{.Description}}列表(树形结构)
|
||||
// @param ctx 上下文
|
||||
// @return []*model.{{.StructName}} {{.Description}}列表
|
||||
// @return error 查询失败时返回错误
|
||||
func (s *{{.StructName}}Service) Get{{.StructName}}InfoList(ctx context.Context) ([]*model.{{.StructName}}, error) {
|
||||
s.log.WithContext(ctx).Debug("Service: 获取{{.Description}}树形列表")
|
||||
return s.uc.List(ctx)
|
||||
}
|
||||
{{- else }}
|
||||
// Get{{.StructName}}InfoList 分页获取{{.Description}}列表
|
||||
// @param ctx 上下文
|
||||
// @param info 搜索条件,包含分页和筛选参数
|
||||
// @return []*model.{{.StructName}} {{.Description}}列表
|
||||
// @return int64 总记录数
|
||||
// @return error 查询失败时返回错误
|
||||
func (s *{{.StructName}}Service) Get{{.StructName}}InfoList(ctx context.Context, info *{{.Package}}Req.{{.StructName}}Search) ([]*model.{{.StructName}}, int64, error) {
|
||||
s.log.WithContext(ctx).Debugf("Service: 获取{{.Description}}列表, 页码: %d, 每页: %d", info.Page, info.PageSize)
|
||||
return s.uc.List(ctx, info)
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
{{- if .HasDataSource }}
|
||||
// Get{{.StructName}}DataSource 获取{{.StructName}}的数据源
|
||||
// @param ctx 上下文
|
||||
// @return map[string][]map[string]any 数据源映射
|
||||
// @return error 查询失败时返回错误
|
||||
func (s *{{.StructName}}Service) Get{{.StructName}}DataSource(ctx context.Context) (map[string][]map[string]any, error) {
|
||||
s.log.WithContext(ctx).Debug("Service: 获取{{.Description}}数据源")
|
||||
return s.uc.GetDataSource(ctx)
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
|
||||
// BatchCreate{{.StructName}} 批量创建{{.Description}}记录(带事务)
|
||||
// @param ctx 上下文
|
||||
// @param reqs 创建请求列表
|
||||
// @return error 创建失败时返回错误,事务会自动回滚
|
||||
func (s *{{.StructName}}Service) BatchCreate{{.StructName}}(ctx context.Context, reqs []*{{.Package}}Req.{{.StructName}}Request) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 批量创建{{.Description}}, 数量: %d", len(reqs))
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, req := range reqs {
|
||||
{{.Abbreviation}} := req.To{{.StructName}}()
|
||||
if err := s.uc.Create(ctx, {{.Abbreviation}}); err != nil {
|
||||
s.log.WithContext(ctx).Errorf("批量创建{{.Description}}失败: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
// BatchUpdate{{.StructName}} 批量更新{{.Description}}记录(带事务)
|
||||
// @param ctx 上下文
|
||||
// @param reqs 更新请求列表
|
||||
// @return error 更新失败时返回错误,事务会自动回滚
|
||||
func (s *{{.StructName}}Service) BatchUpdate{{.StructName}}(ctx context.Context, reqs []*{{.Package}}Req.{{.StructName}}Request) error {
|
||||
s.log.WithContext(ctx).Infof("Service: 批量更新{{.Description}}, 数量: %d", len(reqs))
|
||||
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
for _, req := range reqs {
|
||||
{{.Abbreviation}} := req.To{{.StructName}}()
|
||||
if err := s.uc.Update(ctx, {{.Abbreviation}}); err != nil {
|
||||
s.log.WithContext(ctx).Errorf("批量更新{{.Description}}失败: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
{{- end }}
|
||||
|
||||
// Get{{.StructName}}Public 获取公开数据
|
||||
// @param ctx 上下文
|
||||
func (s *{{.StructName}}Service) Get{{.StructName}}Public(ctx context.Context) {
|
||||
s.log.WithContext(ctx).Debug("Service: 获取{{.Description}}公开数据")
|
||||
s.uc.GetPublic(ctx)
|
||||
}
|
||||
{{- end }}
|
||||
Reference in New Issue
Block a user