144 lines
4.0 KiB
Go
144 lines
4.0 KiB
Go
package system
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
"kra/internal/biz/system"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// SysAutoCodePackage 代码包数据模型
|
|
type SysAutoCodePackage struct {
|
|
gorm.Model
|
|
Desc string `json:"desc" gorm:"comment:描述"`
|
|
Label string `json:"label" gorm:"comment:展示名"`
|
|
Template string `json:"template" gorm:"comment:模版"`
|
|
PackageName string `json:"packageName" gorm:"comment:包名"`
|
|
Module string `json:"-" gorm:"comment:模块"`
|
|
}
|
|
|
|
func (SysAutoCodePackage) TableName() string {
|
|
return "sys_auto_code_packages"
|
|
}
|
|
|
|
type autoCodePackageRepo struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
// NewAutoCodePackageRepo 创建代码包仓储
|
|
func NewAutoCodePackageRepo(db *gorm.DB) system.AutoCodePackageRepo {
|
|
return &autoCodePackageRepo{db: db}
|
|
}
|
|
|
|
// Create 创建包
|
|
func (r *autoCodePackageRepo) Create(ctx context.Context, pkg *system.SysAutoCodePackage) error {
|
|
entity := toDataAutoCodePackage(pkg)
|
|
if err := r.db.WithContext(ctx).Create(entity).Error; err != nil {
|
|
return err
|
|
}
|
|
pkg.ID = entity.ID
|
|
return nil
|
|
}
|
|
|
|
// Delete 删除包
|
|
func (r *autoCodePackageRepo) Delete(ctx context.Context, id uint) error {
|
|
return r.db.WithContext(ctx).Delete(&SysAutoCodePackage{}, id).Error
|
|
}
|
|
|
|
// DeleteByNames 根据名称删除包
|
|
func (r *autoCodePackageRepo) DeleteByNames(ctx context.Context, names []string) error {
|
|
if len(names) == 0 {
|
|
return nil
|
|
}
|
|
return r.db.WithContext(ctx).Where("package_name IN ?", names).Delete(&SysAutoCodePackage{}).Error
|
|
}
|
|
|
|
// All 获取所有包
|
|
func (r *autoCodePackageRepo) All(ctx context.Context) ([]system.SysAutoCodePackage, error) {
|
|
var entities []SysAutoCodePackage
|
|
err := r.db.WithContext(ctx).Find(&entities).Error
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toBizAutoCodePackages(entities), nil
|
|
}
|
|
|
|
// First 查询包
|
|
func (r *autoCodePackageRepo) First(ctx context.Context, packageName, template string) (*system.SysAutoCodePackage, error) {
|
|
var entity SysAutoCodePackage
|
|
err := r.db.WithContext(ctx).Where("package_name = ? AND template = ?", packageName, template).First(&entity).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return toBizAutoCodePackage(&entity), nil
|
|
}
|
|
|
|
// FindByPackageName 根据包名查询
|
|
func (r *autoCodePackageRepo) FindByPackageName(ctx context.Context, packageName string) (*system.SysAutoCodePackage, error) {
|
|
var entity SysAutoCodePackage
|
|
err := r.db.WithContext(ctx).Where("package_name = ?", packageName).First(&entity).Error
|
|
if err != nil {
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
return toBizAutoCodePackage(&entity), nil
|
|
}
|
|
|
|
// BatchCreate 批量创建
|
|
func (r *autoCodePackageRepo) BatchCreate(ctx context.Context, pkgs []system.SysAutoCodePackage) error {
|
|
if len(pkgs) == 0 {
|
|
return nil
|
|
}
|
|
entities := make([]SysAutoCodePackage, len(pkgs))
|
|
for i, pkg := range pkgs {
|
|
entities[i] = *toDataAutoCodePackage(&pkg)
|
|
}
|
|
return r.db.WithContext(ctx).Create(&entities).Error
|
|
}
|
|
|
|
// DeleteByIDs 根据ID批量删除
|
|
func (r *autoCodePackageRepo) DeleteByIDs(ctx context.Context, ids []uint) error {
|
|
if len(ids) == 0 {
|
|
return nil
|
|
}
|
|
return r.db.WithContext(ctx).Delete(&SysAutoCodePackage{}, ids).Error
|
|
}
|
|
|
|
// 转换函数
|
|
func toBizAutoCodePackage(m *SysAutoCodePackage) *system.SysAutoCodePackage {
|
|
return &system.SysAutoCodePackage{
|
|
ID: m.ID,
|
|
Desc: m.Desc,
|
|
Label: m.Label,
|
|
Template: m.Template,
|
|
PackageName: m.PackageName,
|
|
Module: m.Module,
|
|
}
|
|
}
|
|
|
|
func toBizAutoCodePackages(models []SysAutoCodePackage) []system.SysAutoCodePackage {
|
|
result := make([]system.SysAutoCodePackage, len(models))
|
|
for i, m := range models {
|
|
result[i] = *toBizAutoCodePackage(&m)
|
|
}
|
|
return result
|
|
}
|
|
|
|
func toDataAutoCodePackage(pkg *system.SysAutoCodePackage) *SysAutoCodePackage {
|
|
return &SysAutoCodePackage{
|
|
Model: gorm.Model{ID: pkg.ID},
|
|
Desc: pkg.Desc,
|
|
Label: pkg.Label,
|
|
Template: pkg.Template,
|
|
PackageName: pkg.PackageName,
|
|
Module: pkg.Module,
|
|
}
|
|
}
|