mirror of
https://gitee.com/dromara/mayfly-go
synced 2025-11-02 15:30:25 +08:00
refactor: base.repo与app重构优化
This commit is contained in:
@@ -33,7 +33,7 @@
|
||||
"sql-formatter": "^15.0.2",
|
||||
"trzsz": "^1.1.5",
|
||||
"uuid": "^9.0.1",
|
||||
"vue": "^3.4.25",
|
||||
"vue": "^3.4.26",
|
||||
"vue-router": "^4.3.2",
|
||||
"xterm": "^5.3.0",
|
||||
"xterm-addon-fit": "^0.8.0",
|
||||
@@ -56,7 +56,7 @@
|
||||
"prettier": "^3.2.5",
|
||||
"sass": "^1.75.0",
|
||||
"typescript": "^5.4.5",
|
||||
"vite": "^5.2.10",
|
||||
"vite": "^5.2.11",
|
||||
"vue-eslint-parser": "^9.4.2"
|
||||
},
|
||||
"browserslist": [
|
||||
|
||||
@@ -171,7 +171,7 @@ func (a *Oauth2Login) doLoginAction(rc *req.Ctx, userId string, oauth *config.Oa
|
||||
}
|
||||
|
||||
// 进行登录
|
||||
account, err := a.AccountApp.GetById(new(sysentity.Account), accountId, "Id", "Name", "Username", "Password", "Status", "LastLoginTime", "LastLoginIp", "OtpSecret")
|
||||
account, err := a.AccountApp.GetById(accountId, "Id", "Name", "Username", "Password", "Status", "LastLoginTime", "LastLoginIp", "OtpSecret")
|
||||
biz.ErrIsNilAppendErr(err, "获取用户信息失败: %s")
|
||||
|
||||
clientIp := getIpAndRegion(rc)
|
||||
|
||||
@@ -84,8 +84,6 @@ func (d *Db) DeleteDb(rc *req.Ctx) {
|
||||
dbId := cast.ToUint64(v)
|
||||
biz.NotBlank(dbId, "存在错误dbId")
|
||||
biz.ErrIsNil(d.DbApp.Delete(ctx, dbId))
|
||||
// 删除该库的sql执行记录
|
||||
d.DbSqlExecApp.DeleteBy(ctx, &entity.DbSqlExec{DbId: dbId})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,7 +280,7 @@ func (d *Db) DumpSql(rc *req.Ctx) {
|
||||
needData := dumpType == "2" || dumpType == "3"
|
||||
|
||||
la := rc.GetLoginAccount()
|
||||
db, err := d.DbApp.GetById(new(entity.Db), dbId)
|
||||
db, err := d.DbApp.GetById(dbId)
|
||||
biz.ErrIsNil(err, "该数据库不存在")
|
||||
biz.ErrIsNilAppendErr(d.TagApp.CanAccess(la.Id, d.TagApp.ListTagPathByTypeAndCode(consts.ResourceTypeDb, db.Code)...), "%s")
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ type DbBackup struct {
|
||||
func (d *DbBackup) GetPageList(rc *req.Ctx) {
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
|
||||
db, err := d.dbApp.GetById(dbId, "db_instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
queryCond, page := req.BindQueryAndPage[*entity.DbBackupQuery](rc, new(entity.DbBackupQuery))
|
||||
@@ -49,7 +49,7 @@ func (d *DbBackup) Create(rc *req.Ctx) {
|
||||
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instanceId")
|
||||
db, err := d.dbApp.GetById(dbId, "instanceId")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
jobs := make([]*entity.DbBackup, 0, len(dbNames))
|
||||
for _, dbName := range dbNames {
|
||||
@@ -134,7 +134,7 @@ func (d *DbBackup) Start(rc *req.Ctx) {
|
||||
// @router /api/dbs/:dbId/db-names-without-backup [GET]
|
||||
func (d *DbBackup) GetDbNamesWithoutBackup(rc *req.Ctx) {
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instance_id", "database")
|
||||
db, err := d.dbApp.GetById(dbId, "instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
dbNames := strings.Fields(db.Database)
|
||||
dbNamesWithoutBackup, err := d.backupApp.GetDbNamesWithoutBackup(db.InstanceId, dbNames)
|
||||
@@ -147,7 +147,7 @@ func (d *DbBackup) GetDbNamesWithoutBackup(rc *req.Ctx) {
|
||||
func (d *DbBackup) GetHistoryPageList(rc *req.Ctx) {
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
|
||||
db, err := d.dbApp.GetById(dbId, "db_instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
backupHistoryCond, page := req.BindQueryAndPage[*entity.DbBackupHistoryQuery](rc, new(entity.DbBackupHistoryQuery))
|
||||
|
||||
@@ -64,7 +64,7 @@ func (d *DataSyncTask) ChangeStatus(rc *req.Ctx) {
|
||||
_ = d.DataSyncTaskApp.UpdateById(rc.MetaCtx, task)
|
||||
|
||||
if task.Status == entity.DataSyncTaskStatusEnable {
|
||||
task, err := d.DataSyncTaskApp.GetById(new(entity.DataSyncTask), task.Id)
|
||||
task, err := d.DataSyncTaskApp.GetById(task.Id)
|
||||
biz.ErrIsNil(err, "该任务不存在")
|
||||
d.DataSyncTaskApp.AddCronJob(rc.MetaCtx, task)
|
||||
} else {
|
||||
@@ -92,7 +92,7 @@ func (d *DataSyncTask) Stop(rc *req.Ctx) {
|
||||
|
||||
func (d *DataSyncTask) GetTask(rc *req.Ctx) {
|
||||
taskId := d.getTaskId(rc)
|
||||
dbEntity, _ := d.DataSyncTaskApp.GetById(new(entity.DataSyncTask), taskId)
|
||||
dbEntity, _ := d.DataSyncTaskApp.GetById(taskId)
|
||||
rc.ResData = dbEntity
|
||||
}
|
||||
|
||||
|
||||
@@ -83,7 +83,7 @@ func (d *Instance) SaveInstance(rc *req.Ctx) {
|
||||
// @router /api/instances/:instance [GET]
|
||||
func (d *Instance) GetInstance(rc *req.Ctx) {
|
||||
dbId := getInstanceId(rc)
|
||||
dbEntity, err := d.InstanceApp.GetById(new(entity.DbInstance), dbId)
|
||||
dbEntity, err := d.InstanceApp.GetById(dbId)
|
||||
biz.ErrIsNil(err, "获取数据库实例错误")
|
||||
rc.ResData = dbEntity
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ type DbRestore struct {
|
||||
func (d *DbRestore) GetPageList(rc *req.Ctx) {
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "db_instance_id", "database")
|
||||
db, err := d.dbApp.GetById(dbId, "db_instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
var restores []vo.DbRestore
|
||||
@@ -43,7 +43,7 @@ func (d *DbRestore) Create(rc *req.Ctx) {
|
||||
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
biz.IsTrue(dbId > 0, "无效的 dbId: %v", dbId)
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instanceId")
|
||||
db, err := d.dbApp.GetById(dbId, "instanceId")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
|
||||
job := &entity.DbRestore{
|
||||
@@ -123,7 +123,7 @@ func (d *DbRestore) Disable(rc *req.Ctx) {
|
||||
// @router /api/dbs/:dbId/db-names-without-backup [GET]
|
||||
func (d *DbRestore) GetDbNamesWithoutRestore(rc *req.Ctx) {
|
||||
dbId := uint64(rc.PathParamInt("dbId"))
|
||||
db, err := d.dbApp.GetById(new(entity.Db), dbId, "instance_id", "database")
|
||||
db, err := d.dbApp.GetById(dbId, "instance_id", "database")
|
||||
biz.ErrIsNilAppendErr(err, "获取数据库信息失败: %v")
|
||||
dbNames := strings.Fields(db.Database)
|
||||
dbNamesWithoutRestore, err := d.restoreApp.GetDbNamesWithoutRestore(db.InstanceId, dbNames)
|
||||
|
||||
@@ -43,8 +43,7 @@ func (d *DbSql) GetSqlNames(rc *req.Ctx) {
|
||||
// 获取用于是否有该dbsql的保存记录,有则更改,否则新增
|
||||
dbSql := &entity.DbSql{Type: 1, DbId: dbId, Db: dbName}
|
||||
dbSql.CreatorId = rc.GetLoginAccount().Id
|
||||
var sqls []entity.DbSql
|
||||
d.DbSqlApp.ListByCond(model.NewModelCond(dbSql).Columns("id", "name"), &sqls)
|
||||
sqls, _ := d.DbSqlApp.ListByCond(model.NewModelCond(dbSql).Columns("id", "name"))
|
||||
|
||||
rc.ResData = sqls
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ type dbAppImpl struct {
|
||||
|
||||
dbSqlRepo repository.DbSql `inject:"DbSqlRepo"`
|
||||
dbInstanceApp Instance `inject:"DbInstanceApp"`
|
||||
dbSqlExecApp DbSqlExec `inject:"DbSqlExecApp"`
|
||||
tagApp tagapp.TagTree `inject:"TagTreeApp"`
|
||||
resourceAuthCertApp tagapp.ResourceAuthCert `inject:"ResourceAuthCertApp"`
|
||||
}
|
||||
@@ -103,7 +104,7 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db) error {
|
||||
}
|
||||
|
||||
dbId := dbEntity.Id
|
||||
old, err := d.GetById(new(entity.Db), dbId)
|
||||
old, err := d.GetById(dbId)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该数据库不存在")
|
||||
}
|
||||
@@ -142,7 +143,7 @@ func (d *dbAppImpl) SaveDb(ctx context.Context, dbEntity *entity.Db) error {
|
||||
}
|
||||
|
||||
func (d *dbAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
db, err := d.GetById(new(entity.Db), id)
|
||||
db, err := d.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该数据库不存在")
|
||||
}
|
||||
@@ -159,6 +160,8 @@ func (d *dbAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
func(ctx context.Context) error {
|
||||
// 删除该库下用户保存的所有sql信息
|
||||
return d.dbSqlRepo.DeleteByCond(ctx, &entity.DbSql{DbId: id})
|
||||
}, func(ctx context.Context) error {
|
||||
return d.dbSqlExecApp.DeleteBy(ctx, &entity.DbSqlExec{DbId: id})
|
||||
}, func(ctx context.Context) error {
|
||||
return d.tagApp.DeleteTagByParam(ctx, &tagapp.DelResourceTagParam{
|
||||
ResourceCode: db.Code,
|
||||
@@ -169,12 +172,12 @@ func (d *dbAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
|
||||
func (d *dbAppImpl) GetDbConn(dbId uint64, dbName string) (*dbi.DbConn, error) {
|
||||
return dbm.GetDbConn(dbId, dbName, func() (*dbi.DbInfo, error) {
|
||||
db, err := d.GetById(new(entity.Db), dbId)
|
||||
db, err := d.GetById(dbId)
|
||||
if err != nil {
|
||||
return nil, errorx.NewBiz("数据库信息不存在")
|
||||
}
|
||||
|
||||
instance, err := d.dbInstanceApp.GetById(new(entity.DbInstance), db.InstanceId)
|
||||
instance, err := d.dbInstanceApp.GetById(db.InstanceId)
|
||||
if err != nil {
|
||||
return nil, errorx.NewBiz("数据库实例不存在")
|
||||
}
|
||||
@@ -205,9 +208,8 @@ func (d *dbAppImpl) GetDbConnByInstanceId(instanceId uint64) (*dbi.DbConn, error
|
||||
return conn, nil
|
||||
}
|
||||
|
||||
var dbs []*entity.Db
|
||||
|
||||
if err := d.ListByCond(model.NewModelCond(&entity.Db{InstanceId: instanceId}).Columns("id", "database"), &dbs); err != nil {
|
||||
dbs, err := d.ListByCond(&entity.Db{InstanceId: instanceId}, "id", "database")
|
||||
if err != nil {
|
||||
return nil, errorx.NewBiz("获取数据库列表失败")
|
||||
}
|
||||
if len(dbs) == 0 {
|
||||
|
||||
@@ -63,21 +63,18 @@ func (app *DbBackupApp) Init() error {
|
||||
}
|
||||
|
||||
func (app *DbBackupApp) prune(ctx context.Context) error {
|
||||
var jobs []*entity.DbBackup
|
||||
if err := app.backupRepo.ListByCond(map[string]any{}, &jobs); err != nil {
|
||||
jobs, err := app.backupRepo.SelectByCond(map[string]any{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, job := range jobs {
|
||||
if ctx.Err() != nil {
|
||||
return nil
|
||||
}
|
||||
var histories []*entity.DbBackupHistory
|
||||
historyCond := map[string]any{
|
||||
"db_backup_id": job.Id,
|
||||
}
|
||||
if err := app.backupHistoryRepo.SelectByCond(historyCond, &histories); err != nil {
|
||||
return err
|
||||
}
|
||||
histories, _ := app.backupHistoryRepo.SelectByCond(historyCond)
|
||||
expiringTime := time.Now().Add(-math.MaxInt64)
|
||||
if job.MaxSaveDays > 0 {
|
||||
expiringTime = time.Now().Add(-time.Hour * 24 * time.Duration(job.MaxSaveDays+1))
|
||||
@@ -160,8 +157,8 @@ func (app *DbBackupApp) Enable(ctx context.Context, jobId uint64) error {
|
||||
defer app.mutex.Unlock()
|
||||
|
||||
repo := app.backupRepo
|
||||
job := &entity.DbBackup{}
|
||||
if err := repo.GetById(job, jobId); err != nil {
|
||||
job, err := repo.GetById(jobId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if job.IsEnabled() {
|
||||
@@ -183,8 +180,8 @@ func (app *DbBackupApp) Disable(ctx context.Context, jobId uint64) error {
|
||||
defer app.mutex.Unlock()
|
||||
|
||||
repo := app.backupRepo
|
||||
job := &entity.DbBackup{}
|
||||
if err := repo.GetById(job, jobId); err != nil {
|
||||
job, err := repo.GetById(jobId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !job.IsEnabled() {
|
||||
@@ -202,8 +199,8 @@ func (app *DbBackupApp) StartNow(ctx context.Context, jobId uint64) error {
|
||||
app.mutex.Lock()
|
||||
defer app.mutex.Unlock()
|
||||
|
||||
job := &entity.DbBackup{}
|
||||
if err := app.backupRepo.GetById(job, jobId); err != nil {
|
||||
job, err := app.backupRepo.GetById(jobId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !job.IsEnabled() {
|
||||
@@ -267,8 +264,8 @@ func (app *DbBackupApp) DeleteHistory(ctx context.Context, historyId uint64) (re
|
||||
if !ok {
|
||||
return errRestoringBackupHistory
|
||||
}
|
||||
job := &entity.DbBackupHistory{}
|
||||
if err := app.backupHistoryRepo.GetById(job, historyId); err != nil {
|
||||
job, err := app.backupHistoryRepo.GetById(historyId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
conn, err := app.dbApp.GetDbConnByInstanceId(job.DbInstanceId)
|
||||
|
||||
@@ -75,8 +75,8 @@ func (app *DbBinlogApp) fetchBinlog(ctx context.Context) error {
|
||||
}
|
||||
|
||||
func (app *DbBinlogApp) pruneBinlog(ctx context.Context) error {
|
||||
var jobs []*entity.DbBinlog
|
||||
if err := app.binlogRepo.SelectByCond(map[string]any{}, &jobs); err != nil {
|
||||
jobs, err := app.binlogRepo.SelectByCond(map[string]any{})
|
||||
if err != nil {
|
||||
logx.Error("DbBinlogApp: 获取 BINLOG 同步任务失败: ", err.Error())
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func (app *dataSyncAppImpl) Save(ctx context.Context, taskEntity *entity.DataSyn
|
||||
return err
|
||||
}
|
||||
|
||||
task, err := app.GetById(new(entity.DataSyncTask), taskEntity.Id)
|
||||
task, err := app.GetById(taskEntity.Id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -112,7 +112,7 @@ func (app *dataSyncAppImpl) AddCronJob(ctx context.Context, taskEntity *entity.D
|
||||
}
|
||||
|
||||
func (app *dataSyncAppImpl) RemoveCronJobById(taskId uint64) {
|
||||
task, err := app.GetById(new(entity.DataSyncTask), taskId)
|
||||
task, err := app.GetById(taskId)
|
||||
if err == nil {
|
||||
scheduler.RemoveByKey(task.TaskKey)
|
||||
}
|
||||
@@ -127,7 +127,7 @@ func (app *dataSyncAppImpl) changeRunningState(id uint64, state int8) {
|
||||
|
||||
func (app *dataSyncAppImpl) RunCronJob(ctx context.Context, id uint64) error {
|
||||
// 查询最新的任务信息
|
||||
task, err := app.GetById(new(entity.DataSyncTask), id)
|
||||
task, err := app.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("任务不存在")
|
||||
}
|
||||
@@ -369,7 +369,7 @@ func (app *dataSyncAppImpl) srcData2TargetDb(srcRes []map[string]any, fieldMap [
|
||||
}
|
||||
|
||||
// 运行过程中,判断状态是否为已关闭,是则结束运行,否则继续运行
|
||||
taskParam, _ := app.GetById(new(entity.DataSyncTask), task.Id)
|
||||
taskParam, _ := app.GetById(task.Id)
|
||||
if taskParam.RunningState == entity.DataSyncTaskRunStateStop {
|
||||
return errorx.NewBiz("该任务已被手动终止")
|
||||
}
|
||||
|
||||
@@ -142,7 +142,7 @@ func (app *instanceAppImpl) SaveDbInstance(ctx context.Context, instance *SaveDb
|
||||
}
|
||||
} else {
|
||||
// 根据host等未查到旧数据,则需要根据id重新获取,因为后续需要使用到code
|
||||
oldInstance, err = app.GetById(new(entity.DbInstance), instanceEntity.Id)
|
||||
oldInstance, err = app.GetById(instanceEntity.Id)
|
||||
if err != nil {
|
||||
return 0, errorx.NewBiz("该数据库实例不存在")
|
||||
}
|
||||
@@ -170,7 +170,7 @@ func (app *instanceAppImpl) SaveDbInstance(ctx context.Context, instance *SaveDb
|
||||
}
|
||||
|
||||
func (app *instanceAppImpl) Delete(ctx context.Context, instanceId uint64) error {
|
||||
instance, err := app.GetById(new(entity.DbInstance), instanceId, "name")
|
||||
instance, err := app.GetById(instanceId, "name")
|
||||
if err != nil {
|
||||
return errorx.NewBiz("获取数据库实例错误,数据库实例ID为: %d", instance.Id)
|
||||
}
|
||||
@@ -201,18 +201,9 @@ func (app *instanceAppImpl) Delete(ctx context.Context, instanceId uint64) error
|
||||
biz.ErrIsNil(err, "删除数据库实例失败: %v", err)
|
||||
}
|
||||
|
||||
db := &entity.Db{
|
||||
dbs, _ := app.dbApp.ListByCond(&entity.Db{
|
||||
InstanceId: instanceId,
|
||||
}
|
||||
err = app.dbApp.GetByCond(db)
|
||||
switch {
|
||||
case err == nil:
|
||||
biz.ErrNotNil(err, "不能删除数据库实例【%s】,请先删除关联的数据库资源。", instance.Name)
|
||||
case errors.Is(err, gorm.ErrRecordNotFound):
|
||||
break
|
||||
default:
|
||||
biz.ErrIsNil(err, "删除数据库实例失败: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
return app.Tx(ctx, func(ctx context.Context) error {
|
||||
return app.DeleteById(ctx, instanceId)
|
||||
@@ -227,6 +218,14 @@ func (app *instanceAppImpl) Delete(ctx context.Context, instanceId uint64) error
|
||||
ResourceCode: instance.Code,
|
||||
ResourceType: tagentity.TagType(consts.ResourceTypeDb),
|
||||
})
|
||||
}, func(ctx context.Context) error {
|
||||
// 删除所有库配置
|
||||
for _, db := range dbs {
|
||||
if err := app.dbApp.Delete(ctx, db.Id); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
@@ -285,10 +284,10 @@ func (m *instanceAppImpl) genDbInstanceResourceTag(me *entity.DbInstance, authCe
|
||||
}
|
||||
})
|
||||
|
||||
var dbs []*entity.Db
|
||||
if err := m.dbApp.ListByCond(&entity.Db{
|
||||
dbs, err := m.dbApp.ListByCond(&entity.Db{
|
||||
InstanceId: me.Id,
|
||||
}, &dbs); err != nil {
|
||||
})
|
||||
if err != nil {
|
||||
logx.Errorf("获取实例关联的数据库失败: %v", err)
|
||||
}
|
||||
|
||||
|
||||
@@ -78,8 +78,8 @@ func (app *DbRestoreApp) Enable(ctx context.Context, jobId uint64) error {
|
||||
defer app.mutex.Unlock()
|
||||
|
||||
repo := app.restoreRepo
|
||||
job := &entity.DbRestore{}
|
||||
if err := repo.GetById(job, jobId); err != nil {
|
||||
job, err := repo.GetById(jobId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if job.IsEnabled() {
|
||||
@@ -101,8 +101,8 @@ func (app *DbRestoreApp) Disable(ctx context.Context, jobId uint64) error {
|
||||
defer app.mutex.Unlock()
|
||||
|
||||
repo := app.restoreRepo
|
||||
job := &entity.DbRestore{}
|
||||
if err := repo.GetById(job, jobId); err != nil {
|
||||
job, err := repo.GetById(jobId)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !job.IsEnabled() {
|
||||
|
||||
@@ -4,8 +4,6 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"golang.org/x/sync/singleflight"
|
||||
"gorm.io/gorm"
|
||||
"mayfly-go/internal/db/dbm/dbi"
|
||||
"mayfly-go/internal/db/domain/entity"
|
||||
"mayfly-go/internal/db/domain/repository"
|
||||
@@ -14,6 +12,9 @@ import (
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/singleflight"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -173,8 +174,8 @@ func (s *dbScheduler) restore(ctx context.Context, dbProgram dbi.DbProgram, rest
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
backupHistory := &entity.DbBackupHistory{}
|
||||
if err := s.backupHistoryRepo.GetById(backupHistory, restore.DbBackupHistoryId); err != nil {
|
||||
backupHistory, err := s.backupHistoryRepo.GetById(restore.DbBackupHistoryId)
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
err = errors.New("备份历史已删除")
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ type DbSqlExec interface {
|
||||
Exec(ctx context.Context, execSqlReq *DbSqlExecReq) (*DbSqlExecRes, error)
|
||||
|
||||
// 根据条件删除sql执行记录
|
||||
DeleteBy(ctx context.Context, condition *entity.DbSqlExec)
|
||||
DeleteBy(ctx context.Context, condition *entity.DbSqlExec) error
|
||||
|
||||
// 分页获取
|
||||
GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
@@ -196,8 +196,8 @@ func (d *dbSqlExecAppImpl) FlowBizHandle(ctx context.Context, bizHandleParam *fl
|
||||
return d.dbSqlExecRepo.UpdateById(ctx, dbSqlExec)
|
||||
}
|
||||
|
||||
func (d *dbSqlExecAppImpl) DeleteBy(ctx context.Context, condition *entity.DbSqlExec) {
|
||||
d.dbSqlExecRepo.DeleteByCond(ctx, condition)
|
||||
func (d *dbSqlExecAppImpl) DeleteBy(ctx context.Context, condition *entity.DbSqlExec) error {
|
||||
return d.dbSqlExecRepo.DeleteByCond(ctx, condition)
|
||||
}
|
||||
|
||||
func (d *dbSqlExecAppImpl) GetPageList(condition *entity.DbSqlExecQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
|
||||
@@ -87,7 +87,7 @@ func (app *dbTransferAppImpl) CreateLog(ctx context.Context, taskId uint64) (uin
|
||||
}
|
||||
|
||||
func (app *dbTransferAppImpl) Run(ctx context.Context, taskId uint64, logId uint64) {
|
||||
task, err := app.GetById(new(entity.DbTransferTask), taskId)
|
||||
task, err := app.GetById(taskId)
|
||||
if err != nil {
|
||||
logx.Errorf("创建DBMS-执行数据迁移日志失败:%v", err)
|
||||
return
|
||||
@@ -150,7 +150,7 @@ func (app *dbTransferAppImpl) Run(ctx context.Context, taskId uint64, logId uint
|
||||
}
|
||||
|
||||
func (app *dbTransferAppImpl) Stop(ctx context.Context, taskId uint64) error {
|
||||
task, err := app.GetById(new(entity.DbTransferTask), taskId)
|
||||
task, err := app.GetById(taskId)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("任务不存在")
|
||||
}
|
||||
|
||||
@@ -14,6 +14,4 @@ type DbBackup interface {
|
||||
|
||||
// GetPageList 分页获取数据库任务列表
|
||||
GetPageList(condition *entity.DbBackupQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error)
|
||||
|
||||
ListByCond(cond any, listModels any, cols ...string) error
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ func NewDbBackupRepo() repository.DbBackup {
|
||||
|
||||
func (d *dbBackupRepoImpl) GetDbNamesWithoutBackup(instanceId uint64, dbNames []string) ([]string, error) {
|
||||
var dbNamesWithBackup []string
|
||||
err := global.Db.Model(d.GetModel()).
|
||||
err := global.Db.Model(d.NewModel()).
|
||||
Where("db_instance_id = ?", instanceId).
|
||||
Where("repeated = ?", true).
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -41,7 +41,7 @@ func (d *dbBackupRepoImpl) GetDbNamesWithoutBackup(instanceId uint64, dbNames []
|
||||
}
|
||||
|
||||
func (d *dbBackupRepoImpl) ListDbInstances(enabled bool, repeated bool, instanceIds *[]uint64) error {
|
||||
return global.Db.Model(d.GetModel()).
|
||||
return global.Db.Model(d.NewModel()).
|
||||
Where("enabled = ?", enabled).
|
||||
Where("repeated = ?", repeated).
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -51,7 +51,7 @@ func (d *dbBackupRepoImpl) ListDbInstances(enabled bool, repeated bool, instance
|
||||
}
|
||||
|
||||
func (d *dbBackupRepoImpl) ListToDo(jobs any) error {
|
||||
db := global.Db.Model(d.GetModel())
|
||||
db := global.Db.Model(d.NewModel())
|
||||
err := db.Where("enabled = ?", true).
|
||||
Where(db.Where("repeated = ?", true).Or("last_status <> ?", entity.DbJobSuccess)).
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -70,7 +70,7 @@ func (d *dbBackupRepoImpl) GetPageList(condition *entity.DbBackupQuery, pagePara
|
||||
Eq0("repeated", condition.Repeated).
|
||||
In0("db_name", condition.InDbNames).
|
||||
Like("db_name", condition.DbName)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
// AddJob 添加数据库任务
|
||||
@@ -91,7 +91,3 @@ func (d *dbBackupRepoImpl) UpdateEnabled(ctx context.Context, jobId uint64, enab
|
||||
"enabled_desc": desc,
|
||||
}, cond)
|
||||
}
|
||||
|
||||
func (d *dbBackupRepoImpl) ListByCond(cond any, listModels any, cols ...string) error {
|
||||
return d.dbJobBaseImpl.SelectByCond(model.NewModelCond(cond).Columns(cols...), listModels)
|
||||
}
|
||||
|
||||
@@ -29,11 +29,11 @@ func (repo *dbBackupHistoryRepoImpl) GetPageList(condition *entity.DbBackupHisto
|
||||
In0("db_name", condition.InDbNames).
|
||||
Eq("db_backup_id", condition.DbBackupId).
|
||||
Eq("db_name", condition.DbName)
|
||||
return repo.PageByCond(qd, pageParam, toEntity)
|
||||
return repo.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (repo *dbBackupHistoryRepoImpl) GetHistories(backupHistoryIds []uint64, toEntity any) error {
|
||||
return global.Db.Model(repo.GetModel()).
|
||||
return global.Db.Model(repo.NewModel()).
|
||||
Where("id in ?", backupHistoryIds).
|
||||
Where("deleting = false").
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -44,7 +44,7 @@ func (repo *dbBackupHistoryRepoImpl) GetHistories(backupHistoryIds []uint64, toE
|
||||
func (repo *dbBackupHistoryRepoImpl) GetLatestHistoryForBinlog(instanceId uint64, dbName string, bi *entity.BinlogInfo) (*entity.DbBackupHistory, error) {
|
||||
history := &entity.DbBackupHistory{}
|
||||
db := global.Db
|
||||
err := db.Model(repo.GetModel()).
|
||||
err := db.Model(repo.NewModel()).
|
||||
Where("db_instance_id = ?", instanceId).
|
||||
Where("db_name = ?", dbName).
|
||||
Where(db.Where("binlog_sequence < ?", bi.Sequence).
|
||||
@@ -63,7 +63,7 @@ func (repo *dbBackupHistoryRepoImpl) GetLatestHistoryForBinlog(instanceId uint64
|
||||
|
||||
func (repo *dbBackupHistoryRepoImpl) GetEarliestHistoryForBinlog(instanceId uint64) (*entity.DbBackupHistory, bool, error) {
|
||||
history := &entity.DbBackupHistory{}
|
||||
db := global.Db.Model(repo.GetModel())
|
||||
db := global.Db.Model(repo.NewModel())
|
||||
err := db.Where("db_instance_id = ?", instanceId).
|
||||
Where("binlog_sequence > 0").
|
||||
Where("deleting = false").
|
||||
@@ -81,7 +81,7 @@ func (repo *dbBackupHistoryRepoImpl) GetEarliestHistoryForBinlog(instanceId uint
|
||||
}
|
||||
|
||||
func (repo *dbBackupHistoryRepoImpl) UpdateDeleting(deleting bool, backupHistoryId ...uint64) (bool, error) {
|
||||
db := global.Db.Model(repo.GetModel()).
|
||||
db := global.Db.Model(repo.NewModel()).
|
||||
Where("id in ?", backupHistoryId).
|
||||
Where("restoring = false").
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -96,7 +96,7 @@ func (repo *dbBackupHistoryRepoImpl) UpdateDeleting(deleting bool, backupHistory
|
||||
}
|
||||
|
||||
func (repo *dbBackupHistoryRepoImpl) UpdateRestoring(restoring bool, backupHistoryId ...uint64) (bool, error) {
|
||||
db := global.Db.Model(repo.GetModel()).
|
||||
db := global.Db.Model(repo.NewModel()).
|
||||
Where("id in ?", backupHistoryId).
|
||||
Where("deleting = false").
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -111,7 +111,7 @@ func (repo *dbBackupHistoryRepoImpl) UpdateRestoring(restoring bool, backupHisto
|
||||
}
|
||||
|
||||
func (repo *dbBackupHistoryRepoImpl) ZeroBinlogInfo(backupHistoryId uint64) error {
|
||||
return global.Db.Model(repo.GetModel()).
|
||||
return global.Db.Model(repo.NewModel()).
|
||||
Where("id = ?", backupHistoryId).
|
||||
Where("restoring = false").
|
||||
Scopes(gormx.UndeleteScope).
|
||||
|
||||
@@ -42,8 +42,8 @@ func (repo *dbBinlogHistoryRepoImpl) GetHistories(instanceId uint64, start, targ
|
||||
Ge("sequence", start.Sequence).
|
||||
Le("sequence", target.Sequence).
|
||||
OrderByAsc("sequence")
|
||||
var histories []*entity.DbBinlogHistory
|
||||
if err := repo.SelectByCond(qc, &histories); err != nil {
|
||||
histories, err := repo.SelectByCond(qc)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(histories) == 0 {
|
||||
@@ -121,7 +121,7 @@ func (repo *dbBinlogHistoryRepoImpl) InsertWithBinlogFiles(ctx context.Context,
|
||||
}
|
||||
|
||||
func (repo *dbBinlogHistoryRepoImpl) GetHistoriesBeforeSequence(ctx context.Context, instanceId uint64, binlogSeq int64, histories *[]*entity.DbBinlogHistory) error {
|
||||
return global.Db.Model(repo.GetModel()).
|
||||
return global.Db.Model(repo.NewModel()).
|
||||
Where("db_instance_id = ?", instanceId).
|
||||
Where("sequence < ?", binlogSeq).
|
||||
Scopes(gormx.UndeleteScope).
|
||||
|
||||
@@ -20,7 +20,7 @@ func (d *dataSyncTaskRepoImpl) GetTaskList(condition *entity.DataSyncTaskQuery,
|
||||
qd := model.NewCond().
|
||||
Like("task_name", condition.Name).
|
||||
Eq("status", condition.Status)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
type dataSyncLogRepoImpl struct {
|
||||
@@ -31,7 +31,7 @@ type dataSyncLogRepoImpl struct {
|
||||
func (d *dataSyncLogRepoImpl) GetTaskLogList(condition *entity.DataSyncLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewCond().
|
||||
Eq("task_id", condition.TaskId)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func newDataSyncLogRepo() repository.DataSyncLog {
|
||||
|
||||
@@ -58,7 +58,7 @@ func addJob[T entity.DbJob](ctx context.Context, repo dbJobBaseImpl[T], jobs any
|
||||
}
|
||||
|
||||
var res []string
|
||||
err := db.Model(repo.GetModel()).Select("db_name").
|
||||
err := db.Model(repo.NewModel()).Select("db_name").
|
||||
Where("db_instance_id = ?", instanceId).
|
||||
Where("db_name in ?", dbNames).
|
||||
Where("repeated = true").
|
||||
|
||||
@@ -22,7 +22,7 @@ func NewDbRestoreRepo() repository.DbRestore {
|
||||
|
||||
func (d *dbRestoreRepoImpl) GetDbNamesWithoutRestore(instanceId uint64, dbNames []string) ([]string, error) {
|
||||
var dbNamesWithRestore []string
|
||||
err := global.Db.Model(d.GetModel()).
|
||||
err := global.Db.Model(d.NewModel()).
|
||||
Where("db_instance_id = ?", instanceId).
|
||||
Where("repeated = ?", true).
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -42,7 +42,7 @@ func (d *dbRestoreRepoImpl) GetDbNamesWithoutRestore(instanceId uint64, dbNames
|
||||
}
|
||||
|
||||
func (d *dbRestoreRepoImpl) ListToDo(jobs any) error {
|
||||
db := global.Db.Model(d.GetModel())
|
||||
db := global.Db.Model(d.NewModel())
|
||||
err := db.Where("enabled = ?", true).
|
||||
Where(db.Where("repeated = ?", true).Or("last_status <> ?", entity.DbJobSuccess)).
|
||||
Scopes(gormx.UndeleteScope).
|
||||
@@ -61,11 +61,11 @@ func (d *dbRestoreRepoImpl) GetPageList(condition *entity.DbRestoreQuery, pagePa
|
||||
Eq0("repeated", condition.Repeated).
|
||||
In0("db_name", condition.InDbNames).
|
||||
Like("db_name", condition.DbName)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (d *dbRestoreRepoImpl) GetEnabledRestores(toEntity any, backupHistoryId ...uint64) error {
|
||||
return global.Db.Model(d.GetModel()).
|
||||
return global.Db.Model(d.NewModel()).
|
||||
Select("id", "db_backup_history_id", "last_status", "last_result", "last_time").
|
||||
Where("db_backup_history_id in ?", backupHistoryId).
|
||||
Where("enabled = true").
|
||||
|
||||
@@ -21,6 +21,6 @@ func (d *dbRestoreHistoryRepoImpl) GetDbRestoreHistories(condition *entity.DbRes
|
||||
qd := model.NewCond().
|
||||
Eq("id", condition.Id).
|
||||
Eq("db_backup_id", condition.DbRestoreId)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
|
||||
}
|
||||
|
||||
@@ -25,5 +25,5 @@ func (d *dbSqlExecRepoImpl) GetPageList(condition *entity.DbSqlExecQuery, pagePa
|
||||
Eq("flow_biz_key", condition.FlowBizKey).
|
||||
In("status", condition.Status).
|
||||
RLike("db", condition.Db).OrderBy(orderBy...)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -20,5 +20,5 @@ func (d *dbTransferTaskRepoImpl) GetTaskList(condition *entity.DbTransferTaskQue
|
||||
qd := model.NewCond()
|
||||
//Like("task_name", condition.Name).
|
||||
//Eq("status", condition.Status)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -23,5 +23,5 @@ func (d *instanceRepoImpl) GetInstanceList(condition *entity.InstanceQuery, page
|
||||
Like("name", condition.Name).
|
||||
Like("code", condition.Code).
|
||||
In("code", condition.Codes)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -40,19 +40,19 @@ func (p *Procinst) ProcinstCancel(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (p *Procinst) GetProcinstDetail(rc *req.Ctx) {
|
||||
pi, err := p.ProcinstApp.GetById(new(entity.Procinst), uint64(rc.PathParamInt("id")))
|
||||
pi, err := p.ProcinstApp.GetById(uint64(rc.PathParamInt("id")))
|
||||
biz.ErrIsNil(err, "流程实例不存在")
|
||||
pivo := new(vo.ProcinstVO)
|
||||
structx.Copy(pivo, pi)
|
||||
|
||||
// 流程定义信息
|
||||
procdef, _ := p.ProcdefApp.GetById(new(entity.Procdef), pi.ProcdefId)
|
||||
procdef, _ := p.ProcdefApp.GetById(pi.ProcdefId)
|
||||
pivo.Procdef = procdef
|
||||
|
||||
// 流程实例任务信息
|
||||
instTasks := new([]*entity.ProcinstTask)
|
||||
biz.ErrIsNil(p.ProcinstTaskRepo.SelectByCond(&entity.ProcinstTask{ProcinstId: pi.Id}, instTasks))
|
||||
pivo.ProcinstTasks = *instTasks
|
||||
instTasks, err := p.ProcinstTaskRepo.SelectByCond(&entity.ProcinstTask{ProcinstId: pi.Id})
|
||||
biz.ErrIsNil(err)
|
||||
pivo.ProcinstTasks = instTasks
|
||||
|
||||
rc.ResData = pivo
|
||||
}
|
||||
@@ -69,9 +69,8 @@ func (p *Procinst) GetTasks(rc *req.Ctx) {
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
instIds := collx.ArrayMap[*vo.ProcinstTask, uint64](*taskVos, func(val *vo.ProcinstTask) uint64 { return val.ProcinstId })
|
||||
insts := new([]*entity.Procinst)
|
||||
p.ProcinstApp.GetByIds(insts, instIds)
|
||||
instId2Inst := collx.ArrayToMap[*entity.Procinst, uint64](*insts, func(val *entity.Procinst) uint64 { return val.Id })
|
||||
insts, _ := p.ProcinstApp.GetByIds(instIds)
|
||||
instId2Inst := collx.ArrayToMap[*entity.Procinst, uint64](insts, func(val *entity.Procinst) uint64 { return val.Id })
|
||||
|
||||
// 赋值任务对应的流程实例
|
||||
for _, task := range *taskVos {
|
||||
|
||||
@@ -86,7 +86,7 @@ func (p *procinstAppImpl) StartProc(ctx context.Context, procdefKey string, reqP
|
||||
}
|
||||
|
||||
func (p *procinstAppImpl) CancelProc(ctx context.Context, procinstId uint64) error {
|
||||
procinst, err := p.GetById(new(entity.Procinst), procinstId)
|
||||
procinst, err := p.GetById(procinstId)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("流程不存在")
|
||||
}
|
||||
@@ -122,11 +122,8 @@ func (p *procinstAppImpl) CompleteTask(ctx context.Context, instTaskId uint64, r
|
||||
instTask.Remark = remark
|
||||
instTask.SetEnd()
|
||||
|
||||
procinst := new(entity.Procinst)
|
||||
p.GetById(procinst, instTask.ProcinstId)
|
||||
|
||||
procdef := new(entity.Procdef)
|
||||
p.procdefApp.GetById(procdef, procinst.ProcdefId)
|
||||
procinst, _ := p.GetById(instTask.ProcinstId)
|
||||
procdef, _ := p.procdefApp.GetById(procinst.ProcdefId)
|
||||
|
||||
// 获取下一实例审批任务
|
||||
task := p.getNextTask(procdef, instTask.TaskKey)
|
||||
@@ -163,8 +160,7 @@ func (p *procinstAppImpl) RejectTask(ctx context.Context, instTaskId uint64, rem
|
||||
instTask.Remark = remark
|
||||
instTask.SetEnd()
|
||||
|
||||
procinst := new(entity.Procinst)
|
||||
p.GetById(procinst, instTask.ProcinstId)
|
||||
procinst, _ := p.GetById(instTask.ProcinstId)
|
||||
// 更新流程实例为终止状态,无法重新提交
|
||||
procinst.Status = entity.ProcinstStatusTerminated
|
||||
procinst.BizStatus = entity.ProcinstBizStatusNo
|
||||
@@ -189,8 +185,7 @@ func (p *procinstAppImpl) BackTask(ctx context.Context, instTaskId uint64, remar
|
||||
instTask.Status = entity.ProcinstTaskStatusBack
|
||||
instTask.Remark = remark
|
||||
|
||||
procinst := new(entity.Procinst)
|
||||
p.GetById(procinst, instTask.ProcinstId)
|
||||
procinst, _ := p.GetById(instTask.ProcinstId)
|
||||
|
||||
// 更新流程实例为挂起状态,等待重新提交
|
||||
procinst.Status = entity.ProcinstStatusSuspended
|
||||
@@ -207,9 +202,8 @@ func (p *procinstAppImpl) BackTask(ctx context.Context, instTaskId uint64, remar
|
||||
// 取消处理中的流程实例任务
|
||||
func (p *procinstAppImpl) cancelInstTasks(ctx context.Context, procinstId uint64, cancelReason string) error {
|
||||
// 流程实例任务信息
|
||||
instTasks := new([]*entity.ProcinstTask)
|
||||
p.procinstTaskRepo.SelectByCond(&entity.ProcinstTask{ProcinstId: procinstId, Status: entity.ProcinstTaskStatusProcess}, instTasks)
|
||||
for _, instTask := range *instTasks {
|
||||
instTasks, _ := p.procinstTaskRepo.SelectByCond(&entity.ProcinstTask{ProcinstId: procinstId, Status: entity.ProcinstTaskStatusProcess})
|
||||
for _, instTask := range instTasks {
|
||||
instTask.Status = entity.ProcinstTaskStatusCanceled
|
||||
instTask.Remark = cancelReason
|
||||
instTask.SetEnd()
|
||||
@@ -250,8 +244,8 @@ func (p *procinstAppImpl) triggerProcinstStatusChangeEvent(ctx context.Context,
|
||||
|
||||
// 获取并校验实例任务
|
||||
func (p *procinstAppImpl) getAndValidInstTask(ctx context.Context, instTaskId uint64) (*entity.ProcinstTask, error) {
|
||||
instTask := new(entity.ProcinstTask)
|
||||
if err := p.procinstTaskRepo.GetById(instTask, instTaskId); err != nil {
|
||||
instTask, err := p.procinstTaskRepo.GetById(instTaskId)
|
||||
if err != nil {
|
||||
return nil, errorx.NewBiz("流程实例任务不存在")
|
||||
}
|
||||
|
||||
|
||||
@@ -19,5 +19,5 @@ func (p *procdefImpl) GetPageList(condition *entity.Procdef, pageParam *model.Pa
|
||||
qd := model.NewCond().
|
||||
Like("name", condition.Name).
|
||||
Like("def_key", condition.DefKey)
|
||||
return p.PageByCond(qd, pageParam, toEntity)
|
||||
return p.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func newProcinstRepo() repository.Procinst {
|
||||
|
||||
func (p *procinstImpl) GetPageList(condition *entity.ProcinstQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewModelCond(condition)
|
||||
return p.PageByCond(qd, pageParam, toEntity)
|
||||
return p.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
//-----------procinst task--------------
|
||||
@@ -32,5 +32,5 @@ func newProcinstTaskRepo() repository.ProcinstTask {
|
||||
|
||||
func (p *procinstTaskImpl) GetPageList(condition *entity.ProcinstTaskQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewModelCond(condition)
|
||||
return p.PageByCond(qd, pageParam, toEntity)
|
||||
return p.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -218,7 +218,7 @@ func (m *Machine) MachineTermOpRecords(rc *req.Ctx) {
|
||||
}
|
||||
|
||||
func (m *Machine) MachineTermOpRecord(rc *req.Ctx) {
|
||||
termOp, err := m.MachineTermOpApp.GetById(new(entity.MachineTermOp), uint64(rc.PathParamInt("recId")))
|
||||
termOp, err := m.MachineTermOpApp.GetById(uint64(rc.PathParamInt("recId")))
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
bytes, err := os.ReadFile(path.Join(config.GetMachine().TerminalRecPath, termOp.RecordFilePath))
|
||||
|
||||
@@ -22,7 +22,7 @@ func (m *MachineCmdConf) MachineCmdConfs(rc *req.Ctx) {
|
||||
cond := req.BindQuery(rc, new(entity.MachineCmdConf))
|
||||
|
||||
var vos []*vo.MachineCmdConfVO
|
||||
err := m.MachineCmdConfApp.ListByCond(cond, &vos)
|
||||
err := m.MachineCmdConfApp.ListByCondToAny(cond, &vos)
|
||||
biz.ErrIsNil(err)
|
||||
|
||||
m.TagTreeRelateApp.FillTagInfo(tagentity.TagRelateTypeMachineCmd, collx.ArrayMap(vos, func(mvo *vo.MachineCmdConfVO) tagentity.IRelateTag {
|
||||
|
||||
@@ -51,7 +51,7 @@ func (m *MachineScript) DeleteMachineScript(rc *req.Ctx) {
|
||||
func (m *MachineScript) RunMachineScript(rc *req.Ctx) {
|
||||
scriptId := GetMachineScriptId(rc)
|
||||
ac := GetMachineAc(rc)
|
||||
ms, err := m.MachineScriptApp.GetById(new(entity.MachineScript), scriptId, "MachineId", "Name", "Script")
|
||||
ms, err := m.MachineScriptApp.GetById(scriptId, "MachineId", "Name", "Script")
|
||||
biz.ErrIsNil(err, "该脚本不存在")
|
||||
|
||||
script := ms.Script
|
||||
|
||||
@@ -132,7 +132,7 @@ func (m *machineAppImpl) SaveMachine(ctx context.Context, param *SaveMachinePara
|
||||
}
|
||||
// 如果调整了ssh username等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
||||
if oldMachine.Code == "" {
|
||||
oldMachine, _ = m.GetById(new(entity.Machine), me.Id)
|
||||
oldMachine, _ = m.GetById(me.Id)
|
||||
}
|
||||
|
||||
// 关闭连接
|
||||
@@ -201,7 +201,7 @@ func (m *machineAppImpl) ChangeStatus(ctx context.Context, id uint64, status int
|
||||
|
||||
// 根据条件获取机器信息
|
||||
func (m *machineAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
machine, err := m.GetById(new(entity.Machine), id)
|
||||
machine, err := m.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("机器信息不存在")
|
||||
}
|
||||
@@ -266,9 +266,8 @@ func (m *machineAppImpl) GetSshTunnelMachine(machineId int) (*mcm.SshTunnelMachi
|
||||
func (m *machineAppImpl) TimerUpdateStats() {
|
||||
logx.Debug("开始定时收集并缓存服务器状态信息...")
|
||||
scheduler.AddFun("@every 2m", func() {
|
||||
machineIds := new([]entity.Machine)
|
||||
m.ListByCond(model.NewModelCond(&entity.Machine{Status: entity.MachineStatusEnable, Protocol: entity.MachineProtocolSsh}).Columns("id"), machineIds)
|
||||
for _, ma := range *machineIds {
|
||||
machineIds, _ := m.ListByCond(model.NewModelCond(&entity.Machine{Status: entity.MachineStatusEnable, Protocol: entity.MachineProtocolSsh}).Columns("id"))
|
||||
for _, ma := range machineIds {
|
||||
go func(mid uint64) {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
@@ -320,7 +319,7 @@ func (m *machineAppImpl) ToMachineInfoById(machineId uint64) (*mcm.MachineInfo,
|
||||
}
|
||||
|
||||
func (m *machineAppImpl) getMachineAndAuthCert(machineId uint64) (*entity.Machine, *tagentity.ResourceAuthCert, error) {
|
||||
me, err := m.GetById(new(entity.Machine), machineId)
|
||||
me, err := m.GetById(machineId)
|
||||
if err != nil {
|
||||
return nil, nil, errorx.NewBiz("[%d]机器信息不存在", machineId)
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func (m *machineCmdConfAppImpl) SaveCmdConf(ctx context.Context, cmdConfParam *S
|
||||
}
|
||||
|
||||
func (m *machineCmdConfAppImpl) DeleteCmdConf(ctx context.Context, id uint64) error {
|
||||
_, err := m.GetById(new(entity.MachineCmdConf), id)
|
||||
_, err := m.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该命令配置不存在")
|
||||
}
|
||||
@@ -82,9 +82,7 @@ func (m *machineCmdConfAppImpl) GetCmdConfsByMachineTags(tagPaths ...string) []*
|
||||
return cmds
|
||||
}
|
||||
|
||||
var cmdConfs []*entity.MachineCmdConf
|
||||
m.GetByIds(&cmdConfs, cmdConfIds)
|
||||
|
||||
cmdConfs, _ := m.GetByIds(cmdConfIds)
|
||||
for _, cmdConf := range cmdConfs {
|
||||
for _, cmd := range cmdConf.Cmds {
|
||||
if p, err := regexp.Compile(cmd); err != nil {
|
||||
|
||||
@@ -78,7 +78,7 @@ func (m *machineCronJobAppImpl) SaveMachineCronJob(ctx context.Context, mcj *ent
|
||||
// 更新操作
|
||||
if mcj.Id != 0 {
|
||||
m.UpdateById(ctx, mcj)
|
||||
cj, err := m.GetById(new(entity.MachineCronJob), mcj.Id)
|
||||
cj, err := m.GetById(mcj.Id)
|
||||
if err != nil {
|
||||
return 0, errorx.NewBiz("该任务不存在")
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ func (m *machineFileAppImpl) GetMachineFile(condition *entity.MachineFile, cols
|
||||
|
||||
// 保存机器文件配置
|
||||
func (m *machineFileAppImpl) Save(ctx context.Context, mf *entity.MachineFile) error {
|
||||
_, err := m.machineApp.GetById(new(entity.Machine), mf.MachineId, "Name")
|
||||
_, err := m.machineApp.GetById(mf.MachineId, "Name")
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该机器不存在")
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ func (m *machineScriptAppImpl) GetPageList(condition *entity.MachineScript, page
|
||||
func (m *machineScriptAppImpl) Save(ctx context.Context, ms *entity.MachineScript) error {
|
||||
// 如果机器id不为公共脚本id,则校验机器是否存在
|
||||
if machineId := ms.MachineId; machineId != Common_Script_Machine_Id {
|
||||
_, err := m.machineApp.GetById(new(entity.Machine), machineId, "Name")
|
||||
_, err := m.machineApp.GetById(machineId, "Name")
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该机器不存在")
|
||||
}
|
||||
|
||||
@@ -15,8 +15,6 @@ type MachineCronJob interface {
|
||||
type MachineCronJobRelate interface {
|
||||
base.Repo[*entity.MachineCronJobRelate]
|
||||
|
||||
GetList(condition *entity.MachineCronJobRelate) []entity.MachineCronJobRelate
|
||||
|
||||
GetMachineIds(cronJobId uint64) []uint64
|
||||
|
||||
GetCronJobIds(machineId uint64) []uint64
|
||||
|
||||
@@ -35,5 +35,5 @@ func (m *machineRepoImpl) GetMachineList(condition *entity.MachineQuery, pagePar
|
||||
}))
|
||||
}
|
||||
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ func newMachineCronJobRepo() repository.MachineCronJob {
|
||||
// 分页获取机器信息列表
|
||||
func (m *machineCronJobRepoImpl) GetPageList(condition *entity.MachineCronJob, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewCond().Like("name", condition.Name).Eq("status", condition.Status).OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ func newMachineCronJobExecRepo() repository.MachineCronJobExec {
|
||||
// 分页获取机器信息列表
|
||||
func (m *machineCropJobExecRepoImpl) GetPageList(condition *entity.MachineCronJobExec, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewModelCond(condition).OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -15,20 +15,14 @@ func newMachineCronJobRelateRepo() repository.MachineCronJobRelate {
|
||||
return &machineCronJobRelateRepoImpl{base.RepoImpl[*entity.MachineCronJobRelate]{M: new(entity.MachineCronJobRelate)}}
|
||||
}
|
||||
|
||||
func (m *machineCronJobRelateRepoImpl) GetList(condition *entity.MachineCronJobRelate) []entity.MachineCronJobRelate {
|
||||
list := new([]entity.MachineCronJobRelate)
|
||||
m.SelectByCond(condition, list)
|
||||
return *list
|
||||
}
|
||||
|
||||
func (m *machineCronJobRelateRepoImpl) GetMachineIds(cronJobId uint64) []uint64 {
|
||||
var machineIds []uint64
|
||||
m.SelectByCond(model.NewModelCond(&entity.MachineCronJobRelate{CronJobId: cronJobId}).Columns("machine_id"), &machineIds)
|
||||
m.SelectByCondToAny(model.NewModelCond(&entity.MachineCronJobRelate{CronJobId: cronJobId}).Columns("machine_id"), &machineIds)
|
||||
return machineIds
|
||||
}
|
||||
|
||||
func (m *machineCronJobRelateRepoImpl) GetCronJobIds(machineId uint64) []uint64 {
|
||||
var cronJobIds []uint64
|
||||
m.SelectByCond(model.NewModelCond(&entity.MachineCronJobRelate{MachineId: machineId}).Columns("cron_job_id"), &cronJobIds)
|
||||
m.SelectByCondToAny(model.NewModelCond(&entity.MachineCronJobRelate{MachineId: machineId}).Columns("cron_job_id"), &cronJobIds)
|
||||
return cronJobIds
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ func newMachineFileRepo() repository.MachineFile {
|
||||
// 分页获取机器文件信息列表
|
||||
func (m *machineFileRepoImpl) GetPageList(condition *entity.MachineFile, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewModelCond(condition).OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ func newMachineScriptRepo() repository.MachineScript {
|
||||
// 分页获取机器信息列表
|
||||
func (m *machineScriptRepoImpl) GetPageList(condition *entity.MachineScript, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewModelCond(condition).OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -17,13 +17,11 @@ func newMachineTermOpRepoImpl() repository.MachineTermOp {
|
||||
|
||||
func (m *machineTermOpRepoImpl) GetPageList(condition *entity.MachineTermOp, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
pd := model.NewModelCond(condition).OrderBy(orderBy...)
|
||||
return m.PageByCond(pd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(pd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
// 根据条件获取记录列表
|
||||
func (m *machineTermOpRepoImpl) SelectByQuery(cond *entity.MachineTermOpQuery) ([]*entity.MachineTermOp, error) {
|
||||
qd := model.NewCond().Le("create_time", cond.StartCreateTime)
|
||||
var res []*entity.MachineTermOp
|
||||
err := m.SelectByCond(qd, &res)
|
||||
return res, err
|
||||
return m.SelectByCond(qd)
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ func (d *mongoAppImpl) GetPageList(condition *entity.MongoQuery, pageParam *mode
|
||||
}
|
||||
|
||||
func (d *mongoAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
mongoEntity, err := d.GetById(new(entity.Mongo), id)
|
||||
mongoEntity, err := d.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("mongo信息不存在")
|
||||
}
|
||||
@@ -106,7 +106,7 @@ func (d *mongoAppImpl) SaveMongo(ctx context.Context, m *entity.Mongo, tagCodePa
|
||||
}
|
||||
// 如果调整了ssh等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
||||
if oldMongo.Code == "" {
|
||||
oldMongo, _ = d.GetById(new(entity.Mongo), m.Id)
|
||||
oldMongo, _ = d.GetById(m.Id)
|
||||
}
|
||||
|
||||
// 先关闭连接
|
||||
@@ -133,7 +133,7 @@ func (d *mongoAppImpl) SaveMongo(ctx context.Context, m *entity.Mongo, tagCodePa
|
||||
|
||||
func (d *mongoAppImpl) GetMongoConn(id uint64) (*mgm.MongoConn, error) {
|
||||
return mgm.GetMongoConn(id, func() (*mgm.MongoInfo, error) {
|
||||
me, err := d.GetById(new(entity.Mongo), id)
|
||||
me, err := d.GetById(id)
|
||||
if err != nil {
|
||||
return nil, errorx.NewBiz("mongo信息不存在")
|
||||
}
|
||||
|
||||
@@ -21,5 +21,5 @@ func (d *mongoRepoImpl) GetList(condition *entity.MongoQuery, pageParam *model.P
|
||||
Like("name", condition.Name).
|
||||
Eq("code", condition.Code).
|
||||
In("code", condition.Codes)
|
||||
return d.PageByCond(qd, pageParam, toEntity)
|
||||
return d.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -17,5 +17,5 @@ func newMsgRepo() repository.Msg {
|
||||
|
||||
func (m *msgRepoImpl) GetPageList(condition *entity.Msg, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
pd := model.NewModelCond(condition).OrderBy(orderBy...)
|
||||
return m.PageByCond(pd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(pd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ func (r *redisAppImpl) SaveRedis(ctx context.Context, param *SaveRedisParam) err
|
||||
}
|
||||
// 如果调整了ssh等会查不到旧数据,故需要根据id获取旧信息将code赋值给标签进行关联
|
||||
if oldRedis.Code == "" {
|
||||
oldRedis, _ = r.GetById(new(entity.Redis), re.Id)
|
||||
oldRedis, _ = r.GetById(re.Id)
|
||||
}
|
||||
|
||||
re.Code = ""
|
||||
@@ -189,7 +189,7 @@ func (r *redisAppImpl) SaveRedis(ctx context.Context, param *SaveRedisParam) err
|
||||
|
||||
// 删除Redis信息
|
||||
func (r *redisAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
re, err := r.GetById(new(entity.Redis), id)
|
||||
re, err := r.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该redis信息不存在")
|
||||
}
|
||||
@@ -220,7 +220,7 @@ func (r *redisAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
func (r *redisAppImpl) GetRedisConn(id uint64, db int) (*rdm.RedisConn, error) {
|
||||
return rdm.GetRedisConn(id, db, func() (*rdm.RedisInfo, error) {
|
||||
// 缓存不存在,则回调获取redis信息
|
||||
re, err := r.GetById(new(entity.Redis), id)
|
||||
re, err := r.GetById(id)
|
||||
if err != nil {
|
||||
return nil, errorx.NewBiz("redis信息不存在")
|
||||
}
|
||||
|
||||
@@ -22,5 +22,5 @@ func (r *redisRepoImpl) GetRedisList(condition *entity.RedisQuery, pageParam *mo
|
||||
Like("host", condition.Host).
|
||||
Eq("code", condition.Code).
|
||||
In("code", condition.Codes)
|
||||
return r.PageByCond(qd, pageParam, toEntity)
|
||||
return r.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -108,7 +108,7 @@ func (a *Account) UpdateAccount(rc *req.Ctx) {
|
||||
updateAccount.Password = cryptox.PwdHash(updateAccount.Password)
|
||||
}
|
||||
|
||||
oldAcc, err := a.AccountApp.GetById(new(entity.Account), updateAccount.Id)
|
||||
oldAcc, err := a.AccountApp.GetById(updateAccount.Id)
|
||||
biz.ErrIsNil(err, "账号信息不存在")
|
||||
// 账号创建十分钟内允许修改用户名(兼容oauth2首次登录修改用户名),否则不允许修改
|
||||
if oldAcc.CreateTime.Add(10 * time.Minute).Before(time.Now()) {
|
||||
@@ -148,7 +148,7 @@ func (a *Account) SimpleAccounts(rc *req.Ctx) {
|
||||
// 获取账号详情
|
||||
func (a *Account) AccountDetail(rc *req.Ctx) {
|
||||
accountId := uint64(rc.PathParamInt("id"))
|
||||
account, err := a.AccountApp.GetById(new(entity.Account), accountId)
|
||||
account, err := a.AccountApp.GetById(accountId)
|
||||
biz.ErrIsNil(err, "账号不存在")
|
||||
accountvo := new(vo.SimpleAccountVO)
|
||||
structx.Copy(accountvo, account)
|
||||
|
||||
@@ -18,12 +18,12 @@ type Resource struct {
|
||||
|
||||
func (r *Resource) GetAllResourceTree(rc *req.Ctx) {
|
||||
var resources vo.ResourceManageVOList
|
||||
r.ResourceApp.ListByCond(model.NewCond().OrderByAsc("weight"), &resources)
|
||||
r.ResourceApp.ListByCondToAny(model.NewCond().OrderByAsc("weight"), &resources)
|
||||
rc.ResData = resources.ToTrees(0)
|
||||
}
|
||||
|
||||
func (r *Resource) GetById(rc *req.Ctx) {
|
||||
res, err := r.ResourceApp.GetById(new(entity.Resource), uint64(rc.PathParamInt("id")))
|
||||
res, err := r.ResourceApp.GetById(uint64(rc.PathParamInt("id")))
|
||||
biz.ErrIsNil(err, "该资源不存在")
|
||||
rc.ResData = res
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ func (r *resourceAppImpl) Save(ctx context.Context, resource *entity.Resource) e
|
||||
// 更新操作
|
||||
if resource.Id != 0 {
|
||||
if resource.Code != "" {
|
||||
oldRes, err := r.GetById(new(entity.Resource), resource.Id, "Code")
|
||||
oldRes, err := r.GetById(resource.Id, "Code")
|
||||
if err != nil {
|
||||
return errorx.NewBiz("更新失败, 该资源不存在")
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func (r *resourceAppImpl) Save(ctx context.Context, resource *entity.Resource) e
|
||||
// 生成随机八位唯一标识符
|
||||
ui := stringx.Rand(8)
|
||||
if pid := resource.Pid; pid != 0 {
|
||||
pResource, err := r.GetById(new(entity.Resource), uint64(pid))
|
||||
pResource, err := r.GetById(uint64(pid))
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该父资源不存在")
|
||||
}
|
||||
@@ -80,7 +80,7 @@ func (r *resourceAppImpl) Save(ctx context.Context, resource *entity.Resource) e
|
||||
}
|
||||
|
||||
func (r *resourceAppImpl) ChangeStatus(ctx context.Context, resourceId uint64, status int8) error {
|
||||
resource, err := r.GetById(new(entity.Resource), resourceId)
|
||||
resource, err := r.GetById(resourceId)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("资源不存在")
|
||||
}
|
||||
@@ -89,7 +89,7 @@ func (r *resourceAppImpl) ChangeStatus(ctx context.Context, resourceId uint64, s
|
||||
}
|
||||
|
||||
func (r *resourceAppImpl) Sort(ctx context.Context, sortResource *entity.Resource) error {
|
||||
resource, err := r.GetById(new(entity.Resource), sortResource.Id)
|
||||
resource, err := r.GetById(sortResource.Id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("资源不存在")
|
||||
}
|
||||
@@ -115,7 +115,7 @@ func (r *resourceAppImpl) Sort(ctx context.Context, sortResource *entity.Resourc
|
||||
|
||||
newParentResourceUiPath := ""
|
||||
if sortResource.Pid != 0 {
|
||||
newParentResource, err := r.GetById(new(entity.Resource), uint64(sortResource.Pid))
|
||||
newParentResource, err := r.GetById(uint64(sortResource.Pid))
|
||||
if err != nil {
|
||||
return errorx.NewBiz("父资源不存在")
|
||||
}
|
||||
@@ -159,7 +159,7 @@ func (r *resourceAppImpl) checkCode(code string) error {
|
||||
}
|
||||
|
||||
func (r *resourceAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
resource, err := r.GetById(new(entity.Resource), id)
|
||||
resource, err := r.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("资源不存在")
|
||||
}
|
||||
@@ -180,7 +180,7 @@ func (r *resourceAppImpl) GetAccountResources(accountId uint64, toEntity any) er
|
||||
cond := &entity.Resource{
|
||||
Status: entity.ResourceStatusEnable,
|
||||
}
|
||||
return r.ListByCond(model.NewModelCond(cond).OrderByAsc("pid").OrderByAsc("weight"), toEntity)
|
||||
return r.ListByCondToAny(model.NewModelCond(cond).OrderByAsc("pid").OrderByAsc("weight"), toEntity)
|
||||
}
|
||||
|
||||
return r.GetRepo().GetAccountResources(accountId, toEntity)
|
||||
|
||||
@@ -141,9 +141,7 @@ func (m *roleAppImpl) RelateAccountRole(ctx context.Context, accountId, roleId u
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetAccountRoles(accountId uint64) ([]*entity.AccountRole, error) {
|
||||
var res []*entity.AccountRole
|
||||
err := m.accountRoleRepo.SelectByCond(&entity.AccountRole{AccountId: accountId}, &res)
|
||||
return res, err
|
||||
return m.accountRoleRepo.SelectByCond(&entity.AccountRole{AccountId: accountId})
|
||||
}
|
||||
|
||||
func (m *roleAppImpl) GetRoleAccountPage(condition *entity.RoleAccountQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
|
||||
@@ -114,8 +114,8 @@ func (m *syslogAppImpl) GetLogDetail(logId uint64) *entity.SysLog {
|
||||
if syslog != nil {
|
||||
return syslog
|
||||
}
|
||||
syslog = new(entity.SysLog)
|
||||
if err := m.SyslogRepo.GetById(syslog, logId); err != nil {
|
||||
syslog, err := m.SyslogRepo.GetById(logId)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return syslog
|
||||
@@ -137,11 +137,12 @@ func (m *syslogAppImpl) CreateLog(ctx context.Context, log *CreateLogReq) (uint6
|
||||
func (m *syslogAppImpl) AppendLog(logId uint64, appendLog *AppendLogReq) {
|
||||
syslog := m.GetCacheLog(logId)
|
||||
if syslog == nil {
|
||||
syslog = new(entity.SysLog)
|
||||
if err := m.SyslogRepo.GetById(syslog, logId); err != nil {
|
||||
sl, err := m.SyslogRepo.GetById(logId)
|
||||
if err != nil {
|
||||
logx.Warnf("追加日志不存在: %d", logId)
|
||||
return
|
||||
}
|
||||
syslog = sl
|
||||
}
|
||||
|
||||
appendLogMsg := fmt.Sprintf("%s %s", timex.DefaultFormat(time.Now()), appendLog.AppendResp)
|
||||
@@ -158,11 +159,12 @@ func (m *syslogAppImpl) AppendLog(logId uint64, appendLog *AppendLogReq) {
|
||||
func (m *syslogAppImpl) SetExtra(logId uint64, key string, val any) {
|
||||
syslog := m.GetCacheLog(logId)
|
||||
if syslog == nil {
|
||||
syslog = new(entity.SysLog)
|
||||
if err := m.SyslogRepo.GetById(syslog, logId); err != nil {
|
||||
sl, err := m.SyslogRepo.GetById(logId)
|
||||
if err != nil {
|
||||
logx.Warnf("追加日志不存在: %d", logId)
|
||||
return
|
||||
}
|
||||
syslog = sl
|
||||
}
|
||||
|
||||
extraMap := jsonx.ToMap(syslog.Extra)
|
||||
|
||||
@@ -20,5 +20,5 @@ func (m *AccountRepoImpl) GetPageList(condition *entity.AccountQuery, pageParam
|
||||
Like("name", condition.Name).
|
||||
Like("username", condition.Username).
|
||||
In("id", condition.Ids)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -20,5 +20,5 @@ func (m *configRepoImpl) GetPageList(condition *entity.Config, pageParam *model.
|
||||
Like("`key`", condition.Key).
|
||||
And("permission = 'all' OR permission LIKE ?", "%"+condition.Permission+",%").
|
||||
OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -22,17 +22,15 @@ func (m *roleRepoImpl) GetPageList(condition *entity.RoleQuery, pageParam *model
|
||||
In("id", condition.Ids).
|
||||
NotIn("id", condition.NotIds).
|
||||
OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
func (m *roleRepoImpl) ListByQuery(condition *entity.RoleQuery) ([]*entity.Role, error) {
|
||||
var res []*entity.Role
|
||||
qd := model.NewCond().
|
||||
Like("name", condition.Name).
|
||||
Like("code", condition.Code).
|
||||
In("id", condition.Ids).
|
||||
NotIn("id", condition.NotIds).
|
||||
OrderByDesc("id")
|
||||
err := m.SelectByCond(qd, &res)
|
||||
return res, err
|
||||
return m.SelectByCond(qd)
|
||||
}
|
||||
|
||||
@@ -4,7 +4,6 @@ import (
|
||||
"mayfly-go/internal/sys/domain/entity"
|
||||
"mayfly-go/internal/sys/domain/repository"
|
||||
"mayfly-go/pkg/base"
|
||||
"mayfly-go/pkg/model"
|
||||
)
|
||||
|
||||
type roleResourceRepoImpl struct {
|
||||
@@ -17,10 +16,7 @@ func newRoleResourceRepo() repository.RoleResource {
|
||||
|
||||
// 获取角色拥有的资源id数组
|
||||
func (m *roleResourceRepoImpl) GetRoleResourceIds(roleId uint64) []uint64 {
|
||||
var rrs []entity.RoleResource
|
||||
|
||||
condtion := &entity.RoleResource{RoleId: roleId}
|
||||
m.SelectByCond(model.NewModelCond(condtion).Columns("ResourceId"), &rrs)
|
||||
rrs, _ := m.SelectByCond(&entity.RoleResource{RoleId: roleId}, "ResourceId")
|
||||
|
||||
var rids []uint64
|
||||
for _, v := range rrs {
|
||||
|
||||
@@ -18,5 +18,5 @@ func newSyslogRepo() repository.Syslog {
|
||||
func (m *syslogRepoImpl) GetPageList(condition *entity.SysLogQuery, pageParam *model.PageParam, toEntity any, orderBy ...string) (*model.PageResult[any], error) {
|
||||
qd := model.NewCond().Like("description", condition.Description).
|
||||
Eq("creator_id", condition.CreatorId).Eq("type", condition.Type).OrderBy(orderBy...)
|
||||
return m.PageByCond(qd, pageParam, toEntity)
|
||||
return m.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -22,10 +22,9 @@ func (r *ResourceAuthCert) ListByQuery(rc *req.Ctx) {
|
||||
cond.CiphertextType = entity.AuthCertCiphertextType(rc.QueryInt("ciphertextType"))
|
||||
cond.Name = rc.Query("name")
|
||||
|
||||
var racs []*entity.ResourceAuthCert
|
||||
res, err := r.ResourceAuthCertApp.PageByCond(cond, rc.GetPageParam(), &racs)
|
||||
res, err := r.ResourceAuthCertApp.PageByCond(cond, rc.GetPageParam())
|
||||
biz.ErrIsNil(err)
|
||||
for _, rac := range racs {
|
||||
for _, rac := range res.List {
|
||||
rac.CiphertextClear()
|
||||
}
|
||||
rc.ResData = res
|
||||
|
||||
@@ -17,8 +17,7 @@ func (r *ResourceOpLog) PageAccountOpLog(rc *req.Ctx) {
|
||||
cond.ResourceType = int8(rc.QueryInt("resourceType"))
|
||||
cond.CreatorId = rc.GetLoginAccount().Id
|
||||
|
||||
var rols []*entity.ResourceOpLog
|
||||
res, err := r.ResourceOpLogApp.PageByCond(cond, rc.GetPageParam(), &rols)
|
||||
rols, err := r.ResourceOpLogApp.PageByCond(cond, rc.GetPageParam())
|
||||
biz.ErrIsNil(err)
|
||||
rc.ResData = res
|
||||
rc.ResData = rols
|
||||
}
|
||||
|
||||
@@ -107,8 +107,7 @@ func (r *resourceAuthCertAppImpl) RelateAuthCert(ctx context.Context, params *Re
|
||||
}
|
||||
}
|
||||
|
||||
var oldAuthCert []*entity.ResourceAuthCert
|
||||
r.ListByCond(&entity.ResourceAuthCert{ResourceCode: resourceCode, ResourceType: resourceType}, &oldAuthCert)
|
||||
oldAuthCert, _ := r.ListByCond(&entity.ResourceAuthCert{ResourceCode: resourceCode, ResourceType: resourceType})
|
||||
|
||||
// 新增、删除以及不变的授权凭证名
|
||||
var adds, dels, unmodifys []string
|
||||
@@ -185,7 +184,7 @@ func (r *resourceAuthCertAppImpl) SaveAuthCert(ctx context.Context, rac *entity.
|
||||
}
|
||||
|
||||
func (r *resourceAuthCertAppImpl) DeleteAuthCert(ctx context.Context, id uint64) error {
|
||||
rac, err := r.GetById(new(entity.ResourceAuthCert), id)
|
||||
rac, err := r.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("授权凭证不存在")
|
||||
}
|
||||
@@ -224,11 +223,11 @@ func (r *resourceAuthCertAppImpl) GetAuthCert(authCertName string) (*entity.Reso
|
||||
}
|
||||
|
||||
func (r *resourceAuthCertAppImpl) GetResourceAuthCert(resourceType entity.TagType, resourceCode string) (*entity.ResourceAuthCert, error) {
|
||||
var resourceAuthCerts []*entity.ResourceAuthCert
|
||||
if err := r.ListByCond(&entity.ResourceAuthCert{
|
||||
resourceAuthCerts, err := r.ListByCond(&entity.ResourceAuthCert{
|
||||
ResourceType: int8(resourceType),
|
||||
ResourceCode: resourceCode,
|
||||
}, &resourceAuthCerts); err != nil {
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -272,8 +271,7 @@ func (r *resourceAuthCertAppImpl) FillAuthCertByAcs(authCerts []*entity.Resource
|
||||
}
|
||||
|
||||
func (r *resourceAuthCertAppImpl) FillAuthCertByAcNames(authCertNames []string, resources ...entity.IAuthCert) {
|
||||
var acs []*entity.ResourceAuthCert
|
||||
r.ListByCond(model.NewCond().In("name", authCertNames), &acs)
|
||||
acs, _ := r.ListByCond(model.NewCond().In("name", authCertNames))
|
||||
r.FillAuthCertByAcs(acs, resources...)
|
||||
}
|
||||
|
||||
@@ -285,8 +283,7 @@ func (r *resourceAuthCertAppImpl) FillAuthCert(resourceType int8, resources ...e
|
||||
resourceCodes := collx.ArrayMap(resources, func(ac entity.IAuthCert) string {
|
||||
return ac.GetCode()
|
||||
})
|
||||
var acs []*entity.ResourceAuthCert
|
||||
r.ListByCond(model.NewCond().In("ResoourceCode", resourceCodes).Eq0("ResourceType", resourceType), &acs)
|
||||
acs, _ := r.ListByCond(model.NewCond().In("resource_code", resourceCodes).Eq("resource_type", resourceType))
|
||||
r.FillAuthCertByAcs(acs, resources...)
|
||||
}
|
||||
|
||||
@@ -312,8 +309,7 @@ func (r *resourceAuthCertAppImpl) addAuthCert(ctx context.Context, rac *entity.R
|
||||
// 如果该资源存在对应的授权凭证标签类型,则说明需要关联至tagTree,否则直接从授权凭证库中验证资源编号是否正确即可(一个资源最少有一个授权凭证)
|
||||
if authCertTagType != 0 {
|
||||
// 获取资源编号对应的资源标签信息
|
||||
var resourceTags []*entity.TagTree
|
||||
r.tagTreeApp.ListByCond(&entity.TagTree{Type: entity.TagType(resourceType), Code: resourceCode}, &resourceTags)
|
||||
resourceTags, _ := r.tagTreeApp.ListByCond(&entity.TagTree{Type: entity.TagType(resourceType), Code: resourceCode})
|
||||
// 资源标签tagPath(相当于父tag)
|
||||
resourceTagCodePaths = collx.ArrayMap(resourceTags, func(tag *entity.TagTree) string {
|
||||
return tag.CodePath
|
||||
@@ -353,7 +349,7 @@ func (r *resourceAuthCertAppImpl) addAuthCert(ctx context.Context, rac *entity.R
|
||||
|
||||
// updateAuthCert 更新授权凭证
|
||||
func (r *resourceAuthCertAppImpl) updateAuthCert(ctx context.Context, rac *entity.ResourceAuthCert) error {
|
||||
oldRac, err := r.GetById(new(entity.ResourceAuthCert), rac.Id)
|
||||
oldRac, err := r.GetById(rac.Id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该授权凭证不存在")
|
||||
}
|
||||
|
||||
@@ -39,7 +39,8 @@ func (rol *resourceOpLogAppImpl) AddResourceOpLog(ctx context.Context, codePath
|
||||
|
||||
var logs []*entity.ResourceOpLog
|
||||
qc := model.NewCond().Ge("create_time", time.Now().Add(-5*time.Minute)).Eq("creator_id", loginAccount.Id).Eq("code_path", codePath)
|
||||
if err := rol.ListByCond(qc, &logs); err != nil {
|
||||
logs, err := rol.ListByCond(qc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 指定时间内多次操作则不记录
|
||||
|
||||
@@ -53,19 +53,6 @@ type TagTree interface {
|
||||
|
||||
SaveTag(ctx context.Context, pid uint64, tt *entity.TagTree) error
|
||||
|
||||
Delete(ctx context.Context, id uint64) error
|
||||
|
||||
// GetAccountTags 获取指定账号有权限操作的标签列表
|
||||
// @param accountId 账号id
|
||||
// @param query 查询条件
|
||||
GetAccountTags(accountId uint64, query *entity.TagTreeQuery) []*entity.TagTree
|
||||
|
||||
// GetAccountTagCodes 获取指定账号有权限操作的标签codes
|
||||
GetAccountTagCodes(accountId uint64, resourceType int8, tagPath string) []string
|
||||
|
||||
// GetAccountTagCodePaths 获取指定账号有权限操作的codePaths
|
||||
GetAccountTagCodePaths(accountId uint64, tagType entity.TagType, tagPath string) []string
|
||||
|
||||
// SaveResourceTag 保存资源类型标签
|
||||
SaveResourceTag(ctx context.Context, param *SaveResourceTagParam) error
|
||||
|
||||
@@ -84,6 +71,19 @@ type TagTree interface {
|
||||
// DeleteTagByParam 删除标签,会删除该标签下所有子标签信息以及团队关联的标签信息
|
||||
DeleteTagByParam(ctx context.Context, param *DelResourceTagParam) error
|
||||
|
||||
Delete(ctx context.Context, id uint64) error
|
||||
|
||||
// GetAccountTags 获取指定账号有权限操作的标签列表
|
||||
// @param accountId 账号id
|
||||
// @param query 查询条件
|
||||
GetAccountTags(accountId uint64, query *entity.TagTreeQuery) []*entity.TagTree
|
||||
|
||||
// GetAccountTagCodes 获取指定账号有权限操作的标签codes
|
||||
GetAccountTagCodes(accountId uint64, resourceType int8, tagPath string) []string
|
||||
|
||||
// GetAccountTagCodePaths 获取指定账号有权限操作的codePaths
|
||||
GetAccountTagCodePaths(accountId uint64, tagType entity.TagType, tagPath string) []string
|
||||
|
||||
// 根据标签类型和标签code获取对应的标签路径列表
|
||||
ListTagPathByTypeAndCode(resourceType int8, resourceCode string) []string
|
||||
|
||||
@@ -116,7 +116,7 @@ func (p *tagTreeAppImpl) SaveTag(ctx context.Context, pid uint64, tag *entity.Ta
|
||||
return errorx.NewBiz("标识符不能包含'/'")
|
||||
}
|
||||
if pid != 0 {
|
||||
parentTag, err := p.GetById(new(entity.TagTree), pid)
|
||||
parentTag, err := p.GetById(pid)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("父节点不存在")
|
||||
}
|
||||
@@ -153,6 +153,208 @@ func (p *tagTreeAppImpl) SaveTag(ctx context.Context, pid uint64, tag *entity.Ta
|
||||
return p.UpdateById(ctx, tag)
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) SaveResourceTag(ctx context.Context, param *SaveResourceTagParam) error {
|
||||
code := param.ResourceTag.Code
|
||||
tagType := param.ResourceTag.Type
|
||||
parentTagCodePaths := param.ParentTagCodePaths
|
||||
|
||||
if code == "" {
|
||||
return errorx.NewBiz("保存资源标签失败: 资源编号不能为空")
|
||||
}
|
||||
if tagType == 0 {
|
||||
return errorx.NewBiz("保存资源标签失败:资源类型不能为空")
|
||||
}
|
||||
|
||||
// 如果tagIds为空数组,则为删除该资源标签
|
||||
if len(parentTagCodePaths) == 0 {
|
||||
return p.DeleteTagByParam(ctx, &DelResourceTagParam{
|
||||
ResourceType: tagType,
|
||||
ResourceCode: code,
|
||||
})
|
||||
}
|
||||
|
||||
// 获取所有关联的父标签
|
||||
parentTags, _ := p.ListByCond(model.NewCond().In("code_path", parentTagCodePaths))
|
||||
if len(parentTags) == 0 || len(parentTags) != len(parentTagCodePaths) {
|
||||
return errorx.NewBiz("保存资源标签失败: 存在错误的关联标签")
|
||||
}
|
||||
|
||||
newTags := p.toTags(parentTags, param.ResourceTag)
|
||||
|
||||
oldParentTagTree, _ := p.ListByCond(&entity.TagTree{Type: tagType, Code: code})
|
||||
|
||||
// 该资源对应的旧资源标签信息
|
||||
var oldChildrenTags []*entity.TagTree
|
||||
if len(oldParentTagTree) > 0 {
|
||||
// 获取所有旧的子标签
|
||||
p.ListByQuery(&entity.TagTreeQuery{
|
||||
CodePathLikes: collx.ArrayMap[*entity.TagTree, string](oldParentTagTree, func(val *entity.TagTree) string {
|
||||
return val.CodePath
|
||||
}),
|
||||
}, &oldChildrenTags)
|
||||
}
|
||||
|
||||
// 旧的codePath -> tag
|
||||
oldCodePath2Tag := collx.ArrayToMap[*entity.TagTree, string](oldChildrenTags, func(val *entity.TagTree) string { return val.CodePath })
|
||||
// 新的codePath -> tag
|
||||
newCodePath2Tag := collx.ArrayToMap[*entity.TagTree, string](newTags, func(val *entity.TagTree) string { return val.CodePath })
|
||||
|
||||
var addCodePaths, delCodePaths []string
|
||||
addCodePaths, delCodePaths, _ = collx.ArrayCompare(collx.MapKeys(newCodePath2Tag), collx.MapKeys(oldCodePath2Tag))
|
||||
|
||||
if len(addCodePaths) > 0 {
|
||||
logx.DebugfContext(ctx, "SaveResourceTag-新增标签[%v]", addCodePaths)
|
||||
addTags := make([]*entity.TagTree, 0)
|
||||
for _, addCodePath := range addCodePaths {
|
||||
addTags = append(addTags, newCodePath2Tag[addCodePath])
|
||||
}
|
||||
if err := p.BatchInsert(ctx, addTags); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(delCodePaths) > 0 {
|
||||
logx.DebugfContext(ctx, "SaveResourceTag-删除标签[%v]", delCodePaths)
|
||||
|
||||
var delTagIds []uint64
|
||||
for _, delCodePath := range delCodePaths {
|
||||
delTag := oldCodePath2Tag[delCodePath]
|
||||
if delTag != nil && delTag.Id != 0 {
|
||||
delTagIds = append(delTagIds, delTag.Id)
|
||||
}
|
||||
}
|
||||
|
||||
return p.deleteByIds(ctx, delTagIds)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) RelateTagsByCodeAndType(ctx context.Context, param *RelateTagsByCodeAndTypeParam) error {
|
||||
parentTagCode := param.ParentTagCode
|
||||
parentTagType := param.ParentTagType
|
||||
|
||||
// 获取满足指定编号与类型的所有标签信息
|
||||
parentTags, _ := p.ListByCond(&entity.TagTree{Type: parentTagType, Code: parentTagCode})
|
||||
// 标签codePaths(相当于需要关联的标签数组的父tag)
|
||||
parentTagCodePaths := collx.ArrayMap(parentTags, func(tag *entity.TagTree) string {
|
||||
return tag.CodePath
|
||||
})
|
||||
|
||||
if len(parentTagCodePaths) == 0 {
|
||||
return errorx.NewBiz("不存在满足[type=%d, code=%s]的标签", parentTagType, parentTagCode)
|
||||
}
|
||||
|
||||
for _, tag := range param.Tags {
|
||||
if err := (p.SaveResourceTag(ctx, &SaveResourceTagParam{
|
||||
ResourceTag: tag,
|
||||
ParentTagCodePaths: parentTagCodePaths,
|
||||
})); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) UpdateTagName(ctx context.Context, tagType entity.TagType, tagCode string, tagName string) error {
|
||||
return p.UpdateByCond(ctx, &entity.TagTree{Name: tagName}, &entity.TagTree{Type: tagType, Code: tagCode})
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) ChangeParentTag(ctx context.Context, tagType entity.TagType, tagCode string, parentTagType entity.TagType, newParentCode string) error {
|
||||
// 获取资源编号对应的资源标签信息
|
||||
resourceTags, _ := p.ListByCond(&entity.TagTree{Type: tagType, Code: tagCode})
|
||||
if len(resourceTags) == 0 {
|
||||
logx.WarnfContext(ctx, "ChangeParentTag-[%d-%s]标签信息不存在", tagType, tagCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
if p.CountByCond(&entity.TagTree{Type: parentTagType, Code: newParentCode}) == 0 {
|
||||
return errorx.NewBiz("该父标签不存在")
|
||||
}
|
||||
|
||||
// 获取该资源编号对应的所有子资源标签信息
|
||||
var resourceChildrenTags []*entity.TagTree
|
||||
p.ListByQuery(&entity.TagTreeQuery{CodePathLikes: collx.ArrayMap(resourceTags, func(tag *entity.TagTree) string {
|
||||
return tag.CodePath
|
||||
})}, &resourceChildrenTags)
|
||||
|
||||
// 更新父标签的codepath
|
||||
for _, tag := range resourceChildrenTags {
|
||||
pathSection := entity.GetTagPathSections(tag.CodePath)
|
||||
for i, ps := range pathSection {
|
||||
if ps.Type == tagType && ps.Code == tagCode {
|
||||
// 将父标签编号修改为对应的新编号与类型
|
||||
pathSection[i-1].Code = newParentCode
|
||||
pathSection[i-1].Type = parentTagType
|
||||
}
|
||||
}
|
||||
|
||||
tag.CodePath = pathSection.ToCodePath()
|
||||
if err := p.UpdateById(ctx, tag); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) MovingTag(ctx context.Context, fromTagPath string, toTagPath string) error {
|
||||
fromTag := &entity.TagTree{CodePath: fromTagPath}
|
||||
if err := p.GetByCond(fromTag); err != nil {
|
||||
return errorx.NewBiz("移动标签不存在")
|
||||
}
|
||||
|
||||
toTag := &entity.TagTree{CodePath: toTagPath}
|
||||
if err := p.GetByCond(toTag); err != nil {
|
||||
return errorx.NewBiz("目标标签不存在")
|
||||
}
|
||||
|
||||
// 获取要移动标签的所有子标签
|
||||
var childrenTags []*entity.TagTree
|
||||
p.ListByQuery(&entity.TagTreeQuery{CodePathLike: fromTagPath}, &childrenTags)
|
||||
|
||||
// 获取父路径, 若fromTagPath=tag1/tag2/1|xxx则返回 tag1/tag2/
|
||||
fromParentPath := entity.GetParentPath(fromTagPath, 0)
|
||||
for _, childTag := range childrenTags {
|
||||
// 替换path,若childPath = tag1/tag2/1|xxx/11|yyy, toTagPath=tag3/tag4则替换为tag3/tag4/1|xxx/11|yyy/
|
||||
childTag.CodePath = strings.Replace(childTag.CodePath, fromParentPath, toTagPath, 1)
|
||||
if err := p.UpdateById(ctx, childTag); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) DeleteTagByParam(ctx context.Context, param *DelResourceTagParam) error {
|
||||
// 获取资源编号对应的资源标签信息
|
||||
cond := &entity.TagTree{Type: param.ResourceType, Code: param.ResourceCode}
|
||||
cond.Id = param.Id
|
||||
resourceTags, _ := p.ListByCond(cond)
|
||||
|
||||
if len(resourceTags) == 0 {
|
||||
logx.DebugfContext(ctx, "TagTreeApp.DeleteTagByParam[%d-%s]不存在可删除的标签", param.ResourceType, param.ResourceCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
delTagType := param.ChildType
|
||||
for _, resourceTag := range resourceTags {
|
||||
// 获取所有关联的子标签
|
||||
childrenTag, _ := p.ListByCond(model.NewCond().RLike("code_path", resourceTag.CodePath).Eq("type", delTagType))
|
||||
if len(childrenTag) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
childrenTagIds := collx.ArrayMap(childrenTag, func(item *entity.TagTree) uint64 {
|
||||
return item.Id
|
||||
})
|
||||
// 删除code_path下的所有子标签
|
||||
return p.deleteByIds(ctx, childrenTagIds)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) ListByQuery(condition *entity.TagTreeQuery, toEntity any) {
|
||||
p.GetRepo().SelectByCondition(condition, toEntity)
|
||||
}
|
||||
@@ -231,218 +433,8 @@ func (p *tagTreeAppImpl) GetAccountTagCodePaths(accountId uint64, tagType entity
|
||||
return collx.MapKeys(code2Resource)
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) SaveResourceTag(ctx context.Context, param *SaveResourceTagParam) error {
|
||||
code := param.ResourceTag.Code
|
||||
tagType := param.ResourceTag.Type
|
||||
parentTagCodePaths := param.ParentTagCodePaths
|
||||
|
||||
if code == "" {
|
||||
return errorx.NewBiz("保存资源标签失败: 资源编号不能为空")
|
||||
}
|
||||
if tagType == 0 {
|
||||
return errorx.NewBiz("保存资源标签失败:资源类型不能为空")
|
||||
}
|
||||
|
||||
// 如果tagIds为空数组,则为删除该资源标签
|
||||
if len(parentTagCodePaths) == 0 {
|
||||
return p.DeleteTagByParam(ctx, &DelResourceTagParam{
|
||||
ResourceType: tagType,
|
||||
ResourceCode: code,
|
||||
})
|
||||
}
|
||||
|
||||
// 获取所有关联的父标签
|
||||
var parentTags []*entity.TagTree
|
||||
p.ListByCond(model.NewCond().In("code_path", parentTagCodePaths), &parentTags)
|
||||
if len(parentTags) == 0 || len(parentTags) != len(parentTagCodePaths) {
|
||||
return errorx.NewBiz("保存资源标签失败: 存在错误的关联标签")
|
||||
}
|
||||
|
||||
newTags := p.toTags(parentTags, param.ResourceTag)
|
||||
|
||||
var oldParentTagTree []*entity.TagTree
|
||||
p.ListByCond(&entity.TagTree{Type: tagType, Code: code}, &oldParentTagTree)
|
||||
|
||||
// 该资源对应的旧资源标签信息
|
||||
var oldChildrenTags []*entity.TagTree
|
||||
if len(oldParentTagTree) > 0 {
|
||||
// 获取所有旧的子标签
|
||||
p.ListByQuery(&entity.TagTreeQuery{
|
||||
CodePathLikes: collx.ArrayMap[*entity.TagTree, string](oldParentTagTree, func(val *entity.TagTree) string {
|
||||
return val.CodePath
|
||||
}),
|
||||
}, &oldChildrenTags)
|
||||
}
|
||||
|
||||
// 旧的codePath -> tag
|
||||
oldCodePath2Tag := collx.ArrayToMap[*entity.TagTree, string](oldChildrenTags, func(val *entity.TagTree) string { return val.CodePath })
|
||||
// 新的codePath -> tag
|
||||
newCodePath2Tag := collx.ArrayToMap[*entity.TagTree, string](newTags, func(val *entity.TagTree) string { return val.CodePath })
|
||||
|
||||
var addCodePaths, delCodePaths []string
|
||||
addCodePaths, delCodePaths, _ = collx.ArrayCompare(collx.MapKeys(newCodePath2Tag), collx.MapKeys(oldCodePath2Tag))
|
||||
|
||||
if len(addCodePaths) > 0 {
|
||||
logx.DebugfContext(ctx, "SaveResourceTag-新增标签[%v]", addCodePaths)
|
||||
addTags := make([]*entity.TagTree, 0)
|
||||
for _, addCodePath := range addCodePaths {
|
||||
addTags = append(addTags, newCodePath2Tag[addCodePath])
|
||||
}
|
||||
if err := p.BatchInsert(ctx, addTags); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
if len(delCodePaths) > 0 {
|
||||
logx.DebugfContext(ctx, "SaveResourceTag-删除标签[%v]", delCodePaths)
|
||||
|
||||
var delTagIds []uint64
|
||||
for _, delCodePath := range delCodePaths {
|
||||
delTag := oldCodePath2Tag[delCodePath]
|
||||
if delTag != nil && delTag.Id != 0 {
|
||||
delTagIds = append(delTagIds, delTag.Id)
|
||||
}
|
||||
}
|
||||
|
||||
return p.deleteByIds(ctx, delTagIds)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) RelateTagsByCodeAndType(ctx context.Context, param *RelateTagsByCodeAndTypeParam) error {
|
||||
parentTagCode := param.ParentTagCode
|
||||
parentTagType := param.ParentTagType
|
||||
|
||||
// 获取满足指定编号与类型的所有标签信息
|
||||
var parentTags []*entity.TagTree
|
||||
p.ListByCond(&entity.TagTree{Type: parentTagType, Code: parentTagCode}, &parentTags)
|
||||
// 标签codePaths(相当于需要关联的标签数组的父tag)
|
||||
parentTagCodePaths := collx.ArrayMap(parentTags, func(tag *entity.TagTree) string {
|
||||
return tag.CodePath
|
||||
})
|
||||
|
||||
if len(parentTagCodePaths) == 0 {
|
||||
return errorx.NewBiz("不存在满足[type=%d, code=%s]的标签", parentTagType, parentTagCode)
|
||||
}
|
||||
|
||||
for _, tag := range param.Tags {
|
||||
if err := (p.SaveResourceTag(ctx, &SaveResourceTagParam{
|
||||
ResourceTag: tag,
|
||||
ParentTagCodePaths: parentTagCodePaths,
|
||||
})); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) UpdateTagName(ctx context.Context, tagType entity.TagType, tagCode string, tagName string) error {
|
||||
return p.UpdateByCond(ctx, &entity.TagTree{Name: tagName}, &entity.TagTree{Type: tagType, Code: tagCode})
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) ChangeParentTag(ctx context.Context, tagType entity.TagType, tagCode string, parentTagType entity.TagType, newParentCode string) error {
|
||||
// 获取资源编号对应的资源标签信息
|
||||
var resourceTags []*entity.TagTree
|
||||
p.ListByCond(&entity.TagTree{Type: tagType, Code: tagCode}, &resourceTags)
|
||||
if len(resourceTags) == 0 {
|
||||
logx.WarnfContext(ctx, "ChangeParentTag-[%d-%s]标签信息不存在", tagType, tagCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
if p.CountByCond(&entity.TagTree{Type: parentTagType, Code: newParentCode}) == 0 {
|
||||
return errorx.NewBiz("该父标签不存在")
|
||||
}
|
||||
|
||||
// 获取该资源编号对应的所有子资源标签信息
|
||||
var resourceChildrenTags []*entity.TagTree
|
||||
p.ListByQuery(&entity.TagTreeQuery{CodePathLikes: collx.ArrayMap(resourceTags, func(tag *entity.TagTree) string {
|
||||
return tag.CodePath
|
||||
})}, &resourceChildrenTags)
|
||||
|
||||
// 更新父标签的codepath
|
||||
for _, tag := range resourceChildrenTags {
|
||||
pathSection := entity.GetTagPathSections(tag.CodePath)
|
||||
for i, ps := range pathSection {
|
||||
if ps.Type == tagType && ps.Code == tagCode {
|
||||
// 将父标签编号修改为对应的新编号与类型
|
||||
pathSection[i-1].Code = newParentCode
|
||||
pathSection[i-1].Type = parentTagType
|
||||
}
|
||||
}
|
||||
|
||||
tag.CodePath = pathSection.ToCodePath()
|
||||
if err := p.UpdateById(ctx, tag); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) MovingTag(ctx context.Context, fromTagPath string, toTagPath string) error {
|
||||
fromTag := &entity.TagTree{CodePath: fromTagPath}
|
||||
if err := p.GetByCond(fromTag); err != nil {
|
||||
return errorx.NewBiz("移动标签不存在")
|
||||
}
|
||||
|
||||
toTag := &entity.TagTree{CodePath: toTagPath}
|
||||
if err := p.GetByCond(toTag); err != nil {
|
||||
return errorx.NewBiz("目标标签不存在")
|
||||
}
|
||||
|
||||
// 获取要移动标签的所有子标签
|
||||
var childrenTags []*entity.TagTree
|
||||
p.ListByQuery(&entity.TagTreeQuery{CodePathLike: fromTagPath}, &childrenTags)
|
||||
|
||||
// 获取父路径, 若fromTagPath=tag1/tag2/1|xxx则返回 tag1/tag2/
|
||||
fromParentPath := entity.GetParentPath(fromTagPath, 0)
|
||||
for _, childTag := range childrenTags {
|
||||
// 替换path,若childPath = tag1/tag2/1|xxx/11|yyy, toTagPath=tag3/tag4则替换为tag3/tag4/1|xxx/11|yyy/
|
||||
childTag.CodePath = strings.Replace(childTag.CodePath, fromParentPath, toTagPath, 1)
|
||||
if err := p.UpdateById(ctx, childTag); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) DeleteTagByParam(ctx context.Context, param *DelResourceTagParam) error {
|
||||
// 获取资源编号对应的资源标签信息
|
||||
var resourceTags []*entity.TagTree
|
||||
cond := &entity.TagTree{Type: param.ResourceType, Code: param.ResourceCode}
|
||||
cond.Id = param.Id
|
||||
p.ListByCond(cond, &resourceTags)
|
||||
|
||||
if len(resourceTags) == 0 {
|
||||
logx.DebugfContext(ctx, "TagTreeApp.DeleteTagByParam[%d-%s]不存在可删除的标签", param.ResourceType, param.ResourceCode)
|
||||
return nil
|
||||
}
|
||||
|
||||
delTagType := param.ChildType
|
||||
for _, resourceTag := range resourceTags {
|
||||
// 获取所有关联的子标签
|
||||
var childrenTag []*entity.TagTree
|
||||
|
||||
p.ListByCond(model.NewCond().RLike("code_path", resourceTag.CodePath).Eq0("type", delTagType), &childrenTag)
|
||||
if len(childrenTag) == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
childrenTagIds := collx.ArrayMap(childrenTag, func(item *entity.TagTree) uint64 {
|
||||
return item.Id
|
||||
})
|
||||
// 删除code_path下的所有子标签
|
||||
return p.deleteByIds(ctx, childrenTagIds)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) ListTagPathByTypeAndCode(resourceType int8, resourceCode string) []string {
|
||||
var trs []*entity.TagTree
|
||||
p.ListByCond(&entity.TagTree{Type: entity.TagType(resourceType), Code: resourceCode}, &trs)
|
||||
trs, _ := p.ListByCond(&entity.TagTree{Type: entity.TagType(resourceType), Code: resourceCode})
|
||||
return collx.ArrayMap(trs, func(tr *entity.TagTree) string {
|
||||
return tr.CodePath
|
||||
})
|
||||
@@ -485,13 +477,16 @@ func (p *tagTreeAppImpl) FillTagInfo(resourceTagType entity.TagType, resources .
|
||||
|
||||
for _, tr := range tagResources {
|
||||
// 赋值标签信息
|
||||
resourceCode2Resouce[tr.Code].SetTagInfo(entity.ResourceTag{TagId: tr.Id, CodePath: tr.GetTagPath()})
|
||||
resource := resourceCode2Resouce[tr.Code]
|
||||
if resource != nil {
|
||||
resource.SetTagInfo(entity.ResourceTag{TagId: tr.Id, CodePath: tr.GetTagPath()})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *tagTreeAppImpl) Delete(ctx context.Context, id uint64) error {
|
||||
accountId := contextx.GetLoginAccount(ctx).Id
|
||||
tag, err := p.GetById(new(entity.TagTree), id)
|
||||
tag, err := p.GetById(id)
|
||||
if err != nil {
|
||||
return errorx.NewBiz("该标签不存在")
|
||||
}
|
||||
|
||||
@@ -51,8 +51,7 @@ func (tr *tagTreeRelateAppImpl) RelateTag(ctx context.Context, relateType entity
|
||||
return errorx.NewBiz("存在错误标签路径")
|
||||
}
|
||||
|
||||
var oldRelates []*entity.TagTreeRelate
|
||||
tr.ListByCond(&entity.TagTreeRelate{RelateType: relateType, RelateId: relateId}, &oldRelates)
|
||||
oldRelates, _ := tr.ListByCond(&entity.TagTreeRelate{RelateType: relateType, RelateId: relateId})
|
||||
oldTagIds := collx.ArrayMap[*entity.TagTreeRelate, uint64](oldRelates, func(val *entity.TagTreeRelate) uint64 {
|
||||
return val.TagId
|
||||
})
|
||||
@@ -111,21 +110,22 @@ func (tr *tagTreeRelateAppImpl) FillTagInfo(relateType entity.TagRelateType, rel
|
||||
return rt.GetRelateId()
|
||||
})
|
||||
|
||||
var relateTags []*entity.TagTreeRelate
|
||||
tr.ListByCond(model.NewCond().Eq("relate_type", relateType).In("relate_id", collx.MapKeys(relateIds2Relate)), &relateTags)
|
||||
relateTags, _ := tr.ListByCond(model.NewCond().Eq("relate_type", relateType).In("relate_id", collx.MapKeys(relateIds2Relate)))
|
||||
|
||||
tagIds := collx.ArrayMap(relateTags, func(rt *entity.TagTreeRelate) uint64 {
|
||||
return rt.TagId
|
||||
})
|
||||
var tags []*entity.TagTree
|
||||
tr.tagTreeApp.GetByIds(&tags, tagIds)
|
||||
tags, _ := tr.tagTreeApp.GetByIds(tagIds)
|
||||
|
||||
tagId2Tag := collx.ArrayToMap(tags, func(t *entity.TagTree) uint64 {
|
||||
return t.Id
|
||||
})
|
||||
for _, rt := range relateTags {
|
||||
// 赋值标签信息
|
||||
relate := relateIds2Relate[rt.RelateId]
|
||||
tag := tagId2Tag[rt.TagId]
|
||||
relateIds2Relate[rt.RelateId].SetTagInfo(entity.ResourceTag{CodePath: tag.CodePath, TagId: tag.Id})
|
||||
if relate != nil && tag != nil {
|
||||
// 赋值标签信息
|
||||
relate.SetTagInfo(entity.ResourceTag{CodePath: tag.CodePath, TagId: tag.Id})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ type TagTreeQuery struct {
|
||||
model.Model
|
||||
|
||||
Type TagType `json:"type"`
|
||||
Types []TagType
|
||||
Codes []string
|
||||
CodePaths []string // 标识路径
|
||||
Name string `json:"name"` // 名称
|
||||
|
||||
@@ -38,6 +38,10 @@ func (p *tagTreeRepoImpl) SelectByCondition(condition *entity.TagTreeQuery, toEn
|
||||
sql = sql + " AND p.type = ?"
|
||||
params = append(params, condition.Type)
|
||||
}
|
||||
if len(condition.Types) > 0 {
|
||||
sql = sql + " AND p.type = IN (?)"
|
||||
params = append(params, condition.Types)
|
||||
}
|
||||
if len(condition.CodePathLikes) > 0 {
|
||||
sql = sql + " AND ("
|
||||
for i, v := range condition.CodePathLikes {
|
||||
|
||||
@@ -19,5 +19,5 @@ func (p *teamRepoImpl) GetPageList(condition *entity.TeamQuery, pageParam *model
|
||||
qd := model.NewCond().
|
||||
Like("name", condition.Name).
|
||||
OrderBy(orderBy...)
|
||||
return p.PageByCond(qd, pageParam, toEntity)
|
||||
return p.PageByCondToAny(qd, pageParam, toEntity)
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ func newTeamMemberRepo() repository.TeamMember {
|
||||
}
|
||||
|
||||
func (p *teamMemberRepoImpl) ListMemeber(condition *entity.TeamMember, toEntity any, orderBy ...string) {
|
||||
p.SelectByCond(model.NewModelCond(condition).OrderBy(orderBy...), toEntity)
|
||||
p.SelectByCondToAny(model.NewModelCond(condition).OrderBy(orderBy...), toEntity)
|
||||
}
|
||||
|
||||
func (p *teamMemberRepoImpl) GetPageList(condition *entity.TeamMember, pageParam *model.PageParam, toEntity any) (*model.PageResult[any], error) {
|
||||
|
||||
@@ -31,27 +31,40 @@ type App[T model.ModelI] interface {
|
||||
DeleteById(ctx context.Context, id ...uint64) error
|
||||
|
||||
// DeleteByCond 根据条件进行删除
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
DeleteByCond(ctx context.Context, cond any) error
|
||||
|
||||
// Save 保存实体,实体IsCreate返回true则新增,否则更新
|
||||
Save(ctx context.Context, e T) error
|
||||
|
||||
// GetById 根据实体id查询
|
||||
GetById(e T, id uint64, cols ...string) (T, error)
|
||||
GetById(id uint64, cols ...string) (T, error)
|
||||
|
||||
// GetByIds 根据实体id数组查询
|
||||
GetByIds(list any, ids []uint64, orderBy ...string) error
|
||||
GetByIds(ids []uint64, cols ...string) ([]T, error)
|
||||
|
||||
// GetByCond 根据实体条件查询实体信息(获取单个实体)
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
GetByCond(cond any) error
|
||||
|
||||
// ListByCond 根据条件查询数据映射至res
|
||||
ListByCond(cond any, res any) error
|
||||
// ListByCondToAny 根据条件查询数据映射至res
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
ListByCondToAny(cond any, res any) error
|
||||
|
||||
// PageByCond 分页查询
|
||||
PageByCond(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error)
|
||||
// ListByCond 根据条件查询
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
ListByCond(cond any, cols ...string) ([]T, error)
|
||||
|
||||
// PageByCondToAny 分页查询并绑定至指定toModels
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
PageByCondToAny(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error)
|
||||
|
||||
// PageByCond 根据指定条件分页查询
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
PageByCond(cond any, pageParam *model.PageParam, cols ...string) (*model.PageResult[[]T], error)
|
||||
|
||||
// CountByCond 根据指定条件统计model表的数量
|
||||
// @param cond 可为*model.QueryCond也可以为普通查询model
|
||||
CountByCond(cond any) int64
|
||||
|
||||
// Tx 执行事务操作
|
||||
@@ -112,15 +125,12 @@ func (ai *AppImpl[T, R]) DeleteByCond(ctx context.Context, cond any) error {
|
||||
}
|
||||
|
||||
// 根据实体id查询
|
||||
func (ai *AppImpl[T, R]) GetById(e T, id uint64, cols ...string) (T, error) {
|
||||
if err := ai.GetRepo().GetById(e, id, cols...); err != nil {
|
||||
return e, err
|
||||
}
|
||||
return e, nil
|
||||
func (ai *AppImpl[T, R]) GetById(id uint64, cols ...string) (T, error) {
|
||||
return ai.GetRepo().GetById(id, cols...)
|
||||
}
|
||||
|
||||
func (ai *AppImpl[T, R]) GetByIds(list any, ids []uint64, orderBy ...string) error {
|
||||
return ai.GetRepo().GetByIds(list, ids, orderBy...)
|
||||
func (ai *AppImpl[T, R]) GetByIds(ids []uint64, cols ...string) ([]T, error) {
|
||||
return ai.GetRepo().GetByIds(ids, cols...)
|
||||
}
|
||||
|
||||
// 根据实体条件查询实体信息
|
||||
@@ -128,13 +138,21 @@ func (ai *AppImpl[T, R]) GetByCond(cond any) error {
|
||||
return ai.GetRepo().GetByCond(cond)
|
||||
}
|
||||
|
||||
func (ai *AppImpl[T, R]) ListByCond(cond any, res any) error {
|
||||
return ai.GetRepo().SelectByCond(cond, res)
|
||||
func (ai *AppImpl[T, R]) ListByCondToAny(cond any, res any) error {
|
||||
return ai.GetRepo().SelectByCondToAny(cond, res)
|
||||
}
|
||||
|
||||
// PageByCond 分页查询
|
||||
func (ai *AppImpl[T, R]) PageByCond(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error) {
|
||||
return ai.GetRepo().PageByCond(cond, pageParam, toModels)
|
||||
func (ai *AppImpl[T, R]) ListByCond(cond any, cols ...string) ([]T, error) {
|
||||
return ai.GetRepo().SelectByCond(cond, cols...)
|
||||
}
|
||||
|
||||
// PageByCondToAny 分页查询
|
||||
func (ai *AppImpl[T, R]) PageByCondToAny(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error) {
|
||||
return ai.GetRepo().PageByCondToAny(cond, pageParam, toModels)
|
||||
}
|
||||
|
||||
func (ai *AppImpl[T, R]) PageByCond(cond any, pageParam *model.PageParam, cols ...string) (*model.PageResult[[]T], error) {
|
||||
return ai.GetRepo().PageByCond(cond, pageParam, cols...)
|
||||
}
|
||||
|
||||
// 根据指定条件统计model表的数量, cond为条件可以为map等
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"mayfly-go/pkg/gormx"
|
||||
"mayfly-go/pkg/model"
|
||||
"mayfly-go/pkg/utils/collx"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -14,9 +15,6 @@ import (
|
||||
// 基础repo接口
|
||||
type Repo[T model.ModelI] interface {
|
||||
|
||||
// GetModel 获取表的模型实例
|
||||
GetModel() T
|
||||
|
||||
// 新增一个实体
|
||||
Insert(ctx context.Context, e T) error
|
||||
|
||||
@@ -67,19 +65,25 @@ type Repo[T model.ModelI] interface {
|
||||
ExecBySql(sql string, params ...any) error
|
||||
|
||||
// 根据实体id查询
|
||||
GetById(e T, id uint64, cols ...string) error
|
||||
GetById(id uint64, cols ...string) (T, error)
|
||||
|
||||
// 根据实体id数组查询对应实体列表,并将响应结果映射至list
|
||||
GetByIds(list any, ids []uint64, orderBy ...string) error
|
||||
GetByIds(ids []uint64, cols ...string) ([]T, error)
|
||||
|
||||
// GetByCond 根据实体条件查询实体信息(单个结果集)
|
||||
// @param cond 支持普通结构体或*model.QueryCond。如果cond为model.QueryCond,则需要使用Dest方法绑定值的指针
|
||||
GetByCond(cond any) error
|
||||
|
||||
// SelectByCond 根据实体条件查询数据映射至res
|
||||
SelectByCond(cond any, res any) error
|
||||
// SelectByCondToAny 根据条件查询数据映射至res
|
||||
SelectByCondToAny(cond any, res any) error
|
||||
|
||||
// SelectByCond 根据条件查询模型实例数组
|
||||
SelectByCond(cond any, cols ...string) ([]T, error)
|
||||
|
||||
// PageByCondToAny 根据查询条件分页查询,并绑定至指定res
|
||||
PageByCondToAny(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error)
|
||||
|
||||
// PageByCond 根据查询条件分页查询
|
||||
PageByCond(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error)
|
||||
PageByCond(cond any, pageParam *model.PageParam, cols ...string) (*model.PageResult[[]T], error)
|
||||
|
||||
// SelectBySql 根据sql语句查询数据
|
||||
SelectBySql(sql string, res any, params ...any) error
|
||||
@@ -91,6 +95,8 @@ type Repo[T model.ModelI] interface {
|
||||
// 基础repo接口
|
||||
type RepoImpl[T model.ModelI] struct {
|
||||
M T // 模型实例
|
||||
|
||||
modelType reflect.Type // 模型类型
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) Insert(ctx context.Context, e T) error {
|
||||
@@ -164,9 +170,9 @@ func (br *RepoImpl[T]) UpdateByCondWithDb(ctx context.Context, db *gorm.DB, valu
|
||||
}
|
||||
|
||||
if db == nil {
|
||||
return gormx.UpdateByCond(br.GetModel(), values, toQueryCond(cond))
|
||||
return gormx.UpdateByCond(br.getModel(), values, toQueryCond(cond))
|
||||
}
|
||||
return gormx.UpdateByCondWithDb(db, br.GetModel(), values, toQueryCond(cond))
|
||||
return gormx.UpdateByCondWithDb(db, br.getModel(), values, toQueryCond(cond))
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) Save(ctx context.Context, e T) error {
|
||||
@@ -187,49 +193,65 @@ func (br *RepoImpl[T]) DeleteById(ctx context.Context, id ...uint64) error {
|
||||
if db := contextx.GetDb(ctx); db != nil {
|
||||
return br.DeleteByIdWithDb(ctx, db, id...)
|
||||
}
|
||||
return gormx.DeleteById(br.GetModel(), id...)
|
||||
return gormx.DeleteById(br.getModel(), id...)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) DeleteByIdWithDb(ctx context.Context, db *gorm.DB, id ...uint64) error {
|
||||
return gormx.DeleteByIdWithDb(db, br.GetModel(), id...)
|
||||
return gormx.DeleteByIdWithDb(db, br.getModel(), id...)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) DeleteByCond(ctx context.Context, cond any) error {
|
||||
if db := contextx.GetDb(ctx); db != nil {
|
||||
return br.DeleteByCondWithDb(ctx, db, cond)
|
||||
}
|
||||
return gormx.DeleteByCond(br.GetModel(), toQueryCond(cond))
|
||||
return gormx.DeleteByCond(br.getModel(), toQueryCond(cond))
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) DeleteByCondWithDb(ctx context.Context, db *gorm.DB, cond any) error {
|
||||
return gormx.DeleteByCondWithDb(db, br.GetModel(), toQueryCond(cond))
|
||||
return gormx.DeleteByCondWithDb(db, br.getModel(), toQueryCond(cond))
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) ExecBySql(sql string, params ...any) error {
|
||||
return gormx.ExecSql(sql, params...)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) GetById(e T, id uint64, cols ...string) error {
|
||||
if err := gormx.GetById(e, id, cols...); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
func (br *RepoImpl[T]) GetById(id uint64, cols ...string) (T, error) {
|
||||
e := br.NewModel()
|
||||
return e, gormx.GetById(e, id, cols...)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) GetByIds(list any, ids []uint64, orderBy ...string) error {
|
||||
return br.SelectByCond(model.NewCond().In(model.IdColumn, ids).OrderBy(orderBy...), list)
|
||||
func (br *RepoImpl[T]) GetByIds(ids []uint64, cols ...string) ([]T, error) {
|
||||
var models []T
|
||||
return models, br.SelectByCondToAny(model.NewCond().In(model.IdColumn, ids).Columns(cols...), &models)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) GetByCond(cond any) error {
|
||||
return gormx.GetByCond(br.GetModel(), toQueryCond(cond))
|
||||
return gormx.GetByCond(br.getModel(), toQueryCond(cond))
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) SelectByCond(cond any, res any) error {
|
||||
return gormx.SelectByCond(br.GetModel(), toQueryCond(cond).Dest(res))
|
||||
func (br *RepoImpl[T]) SelectByCondToAny(cond any, res any) error {
|
||||
return gormx.SelectByCond(br.getModel(), toQueryCond(cond).Dest(res))
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) PageByCond(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error) {
|
||||
return gormx.PageByCond(br.GetModel(), toQueryCond(cond), pageParam, toModels)
|
||||
func (br *RepoImpl[T]) SelectByCond(cond any, cols ...string) ([]T, error) {
|
||||
var models []T
|
||||
return models, br.SelectByCondToAny(toQueryCond(cond).Dest(models).Columns(cols...), &models)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) PageByCondToAny(cond any, pageParam *model.PageParam, toModels any) (*model.PageResult[any], error) {
|
||||
return gormx.PageByCond(br.getModel(), toQueryCond(cond), pageParam, toModels)
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) PageByCond(cond any, pageParam *model.PageParam, cols ...string) (*model.PageResult[[]T], error) {
|
||||
var models []T
|
||||
res, err := br.PageByCondToAny(toQueryCond(cond).Columns(cols...), pageParam, &models)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &model.PageResult[[]T]{
|
||||
List: models,
|
||||
Total: res.Total,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) SelectBySql(sql string, res any, params ...any) error {
|
||||
@@ -237,14 +259,47 @@ func (br *RepoImpl[T]) SelectBySql(sql string, res any, params ...any) error {
|
||||
}
|
||||
|
||||
func (br *RepoImpl[T]) CountByCond(cond any) int64 {
|
||||
return gormx.CountByCond(br.GetModel(), toQueryCond(cond))
|
||||
return gormx.CountByCond(br.getModel(), toQueryCond(cond))
|
||||
}
|
||||
|
||||
// GetModel 获取表的模型实例
|
||||
func (br *RepoImpl[T]) GetModel() T {
|
||||
// NewModel 新建模型实例
|
||||
func (br *RepoImpl[T]) NewModel() T {
|
||||
newModel := reflect.New(br.getModelType()).Interface()
|
||||
return newModel.(T)
|
||||
}
|
||||
|
||||
// func (br *RepoImpl[T]) NewModes() *[]T {
|
||||
// // 创建一个空的切片
|
||||
// slice := reflect.MakeSlice(reflect.SliceOf(reflect.PointerTo(br.getModelType())), 0, 0)
|
||||
// // 创建指向切片的指针
|
||||
// ptrToSlice := reflect.New(slice.Type())
|
||||
// // 设置指向切片的指针为创建的空切片
|
||||
// ptrToSlice.Elem().Set(slice)
|
||||
// // 转换指向切片的指针
|
||||
// return ptrToSlice.Interface().(*[]T)
|
||||
// }
|
||||
|
||||
// getModel 获取表的模型实例
|
||||
func (br *RepoImpl[T]) getModel() T {
|
||||
return br.M
|
||||
}
|
||||
|
||||
// getModelType 获取模型类型(非指针模型)
|
||||
func (br *RepoImpl[T]) getModelType() reflect.Type {
|
||||
if br.modelType != nil {
|
||||
return br.modelType
|
||||
}
|
||||
|
||||
modelType := reflect.TypeOf(br.M)
|
||||
// 检查 model 是否为指针类型
|
||||
if modelType.Kind() == reflect.Ptr {
|
||||
// 获取指针指向的类型
|
||||
modelType = modelType.Elem()
|
||||
}
|
||||
br.modelType = modelType
|
||||
return modelType
|
||||
}
|
||||
|
||||
// 从上下文获取登录账号信息,并赋值至实体
|
||||
func (br *RepoImpl[T]) fillBaseInfo(ctx context.Context, e T) T {
|
||||
// 默认使用数据库id策略, 若要改变则实体结构体自行覆盖FillBaseInfo方法。可参考 sys/entity.Resource
|
||||
|
||||
Reference in New Issue
Block a user