mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	refactor: base.repo与app重构优化
This commit is contained in:
		@@ -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)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user