kra/internal/biz/system/auto_code_history.go

114 lines
4.6 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package system
import (
"context"
"time"
)
// SysAutoCodeHistory 自动代码生成历史
type SysAutoCodeHistory struct {
ID uint `json:"ID" gorm:"primarykey"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
Table string `json:"tableName" gorm:"column:table_name;comment:表名"`
Package string `json:"package" gorm:"column:package;comment:模块名/插件名"`
Request string `json:"request" gorm:"type:text;column:request;comment:前端传入的结构化信息"`
StructName string `json:"structName" gorm:"column:struct_name;comment:结构体名称"`
Abbreviation string `json:"abbreviation" gorm:"column:abbreviation;comment:结构体名称缩写"`
BusinessDB string `json:"businessDb" gorm:"column:business_db;comment:业务库"`
Description string `json:"description" gorm:"column:description;comment:Struct中文名称"`
Templates map[string]string `json:"template" gorm:"serializer:json;type:text;column:templates;comment:模板信息"`
Injections map[string]string `json:"injections" gorm:"serializer:json;type:text;column:Injections;comment:注入路径"`
Flag int `json:"flag" gorm:"column:flag;comment:[0:创建,1:回滚]"`
ApiIDs []uint `json:"apiIDs" gorm:"serializer:json;column:api_ids;comment:api表注册内容"`
MenuID uint `json:"menuId" gorm:"column:menu_id;comment:菜单ID"`
ExportTemplateID uint `json:"exportTemplateID" gorm:"column:export_template_id;comment:导出模板ID"`
PackageID uint `json:"packageID" gorm:"column:package_id;comment:包ID"`
}
func (SysAutoCodeHistory) TableName() string {
return "sys_auto_code_histories"
}
// AutoCodeHistorySearchReq 历史搜索请求
type AutoCodeHistorySearchReq struct {
Page int `json:"page" form:"page"`
PageSize int `json:"pageSize" form:"pageSize"`
}
// SysAutoHistoryRollBack 回滚请求
type SysAutoHistoryRollBack struct {
ID uint `json:"id"`
DeleteApi bool `json:"deleteApi"`
DeleteMenu bool `json:"deleteMenu"`
DeleteTable bool `json:"deleteTable"`
}
// AutoCodeHistoryRepo 自动代码历史仓储接口
type AutoCodeHistoryRepo interface {
Create(ctx context.Context, history *SysAutoCodeHistory) error
First(ctx context.Context, id uint) (string, error)
Delete(ctx context.Context, id uint) error
GetList(ctx context.Context, page, pageSize int) ([]SysAutoCodeHistory, int64, error)
GetByID(ctx context.Context, id uint) (*SysAutoCodeHistory, error)
UpdateFlag(ctx context.Context, id uint, flag int) error
DropTable(ctx context.Context, businessDB, tableName string) error
Repeat(ctx context.Context, businessDB, structName, abbreviation, pkg string) bool
}
// AutoCodeHistoryUsecase 自动代码历史用例
type AutoCodeHistoryUsecase struct {
repo AutoCodeHistoryRepo
}
// NewAutoCodeHistoryUsecase 创建自动代码历史用例
func NewAutoCodeHistoryUsecase(repo AutoCodeHistoryRepo) *AutoCodeHistoryUsecase {
return &AutoCodeHistoryUsecase{repo: repo}
}
// Create 创建历史记录
func (uc *AutoCodeHistoryUsecase) Create(ctx context.Context, history *SysAutoCodeHistory) error {
return uc.repo.Create(ctx, history)
}
// First 根据id获取meta信息
func (uc *AutoCodeHistoryUsecase) First(ctx context.Context, id uint) (string, error) {
return uc.repo.First(ctx, id)
}
// Repeat 检测重复
func (uc *AutoCodeHistoryUsecase) Repeat(ctx context.Context, businessDB, structName, abbreviation, pkg string) bool {
return uc.repo.Repeat(ctx, businessDB, structName, abbreviation, pkg)
}
// Delete 删除历史记录
func (uc *AutoCodeHistoryUsecase) Delete(ctx context.Context, id uint) error {
return uc.repo.Delete(ctx, id)
}
// GetList 获取历史记录列表
func (uc *AutoCodeHistoryUsecase) GetList(ctx context.Context, page, pageSize int) ([]SysAutoCodeHistory, int64, error) {
return uc.repo.GetList(ctx, page, pageSize)
}
// RollBack 回滚自动生成代码
// 注意完整的回滚功能需要AST工具支持这里只实现基础功能
func (uc *AutoCodeHistoryUsecase) RollBack(ctx context.Context, info SysAutoHistoryRollBack) error {
// 获取历史记录
history, err := uc.repo.GetByID(ctx, info.ID)
if err != nil {
return err
}
// 删除表(如果需要)
if info.DeleteTable && history.Table != "" {
err = uc.repo.DropTable(ctx, history.BusinessDB, history.Table)
if err != nil {
return err
}
}
// 更新标记为已回滚
return uc.repo.UpdateFlag(ctx, info.ID, 1)
}