mirror of
				https://gitee.com/dromara/mayfly-go
				synced 2025-11-04 08:20:25 +08:00 
			
		
		
		
	feat: 完善数据库信息保存以及项目、redis相关操作
This commit is contained in:
		@@ -7,58 +7,95 @@ import (
 | 
			
		||||
	"mayfly-go/server/devops/domain/repository"
 | 
			
		||||
	"mayfly-go/server/devops/infrastructure/machine"
 | 
			
		||||
	"mayfly-go/server/devops/infrastructure/persistence"
 | 
			
		||||
 | 
			
		||||
	"gorm.io/gorm"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
type IMachine interface {
 | 
			
		||||
type Machine interface {
 | 
			
		||||
	// 根据条件获取账号信息
 | 
			
		||||
	GetMachine(condition *entity.Machine, cols ...string) error
 | 
			
		||||
 | 
			
		||||
	Save(entity *entity.Machine)
 | 
			
		||||
 | 
			
		||||
	Delete(id uint64)
 | 
			
		||||
 | 
			
		||||
	// 根据id获取
 | 
			
		||||
	GetById(id uint64, cols ...string) *entity.Machine
 | 
			
		||||
 | 
			
		||||
	// 分页获取机器信息列表
 | 
			
		||||
	GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) model.PageResult
 | 
			
		||||
	GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult
 | 
			
		||||
 | 
			
		||||
	// 获取机器连接
 | 
			
		||||
	GetCli(id uint64) *machine.Cli
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
type machineApp struct {
 | 
			
		||||
type machineAppImpl struct {
 | 
			
		||||
	machineRepo repository.Machine
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
var Machine IMachine = &machineApp{machineRepo: persistence.MachineDao}
 | 
			
		||||
var MachineApp Machine = &machineAppImpl{machineRepo: persistence.MachineDao}
 | 
			
		||||
 | 
			
		||||
// 分页获取机器信息列表
 | 
			
		||||
func (m *machineApp) GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) model.PageResult {
 | 
			
		||||
func (m *machineAppImpl) GetMachineList(condition *entity.Machine, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
 | 
			
		||||
	return m.machineRepo.GetMachineList(condition, pageParam, toEntity, orderBy...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据条件获取机器信息
 | 
			
		||||
func (m *machineApp) Save(entity *entity.Machine) {
 | 
			
		||||
	biz.ErrIsNil(machine.TestConn(entity), "该机器无法连接")
 | 
			
		||||
	if entity.Id != 0 {
 | 
			
		||||
		m.machineRepo.UpdateById(entity)
 | 
			
		||||
func (m *machineAppImpl) Save(me *entity.Machine) {
 | 
			
		||||
	biz.ErrIsNilAppendErr(machine.TestConn(me), "该机器无法连接: %s")
 | 
			
		||||
 | 
			
		||||
	oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
 | 
			
		||||
	err := m.GetMachine(oldMachine)
 | 
			
		||||
 | 
			
		||||
	if me.Id != 0 {
 | 
			
		||||
		// 如果存在该库,则校验修改的库是否为该库
 | 
			
		||||
		if err == nil {
 | 
			
		||||
			biz.IsTrue(oldMachine.Id == me.Id, "该机器信息已存在")
 | 
			
		||||
		}
 | 
			
		||||
		// 关闭连接
 | 
			
		||||
		machine.Close(me.Id)
 | 
			
		||||
		m.machineRepo.UpdateById(me)
 | 
			
		||||
	} else {
 | 
			
		||||
		m.machineRepo.Create(entity)
 | 
			
		||||
		biz.IsTrue(err != nil, "该机器信息已存在")
 | 
			
		||||
		m.machineRepo.Create(me)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据条件获取机器信息
 | 
			
		||||
func (m *machineApp) GetMachine(condition *entity.Machine, cols ...string) error {
 | 
			
		||||
func (m *machineAppImpl) Delete(id uint64) {
 | 
			
		||||
	// 关闭连接
 | 
			
		||||
	machine.Close(id)
 | 
			
		||||
	model.Tx(
 | 
			
		||||
		func(db *gorm.DB) error {
 | 
			
		||||
			// 删除machine表信息
 | 
			
		||||
			return db.Delete(new(entity.Machine), "id = ?", id).Error
 | 
			
		||||
		},
 | 
			
		||||
		func(db *gorm.DB) error {
 | 
			
		||||
			// 删除machine_file
 | 
			
		||||
			machineFile := &entity.MachineFile{MachineId: id}
 | 
			
		||||
			return db.Where(machineFile).Delete(machineFile).Error
 | 
			
		||||
		},
 | 
			
		||||
		func(db *gorm.DB) error {
 | 
			
		||||
			// 删除machine_script
 | 
			
		||||
			machineScript := &entity.MachineScript{MachineId: id}
 | 
			
		||||
			return db.Where(machineScript).Delete(machineScript).Error
 | 
			
		||||
		},
 | 
			
		||||
	)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 根据条件获取机器信息
 | 
			
		||||
func (m *machineAppImpl) GetMachine(condition *entity.Machine, cols ...string) error {
 | 
			
		||||
	return m.machineRepo.GetMachine(condition, cols...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *machineApp) GetById(id uint64, cols ...string) *entity.Machine {
 | 
			
		||||
func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
 | 
			
		||||
	return m.machineRepo.GetById(id, cols...)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (m *machineApp) GetCli(id uint64) *machine.Cli {
 | 
			
		||||
func (m *machineAppImpl) GetCli(id uint64) *machine.Cli {
 | 
			
		||||
	cli, err := machine.GetCli(id, func(machineId uint64) *entity.Machine {
 | 
			
		||||
		return m.GetById(machineId)
 | 
			
		||||
	})
 | 
			
		||||
	biz.ErrIsNil(err, "获取客户端错误")
 | 
			
		||||
	biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
 | 
			
		||||
	return cli
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user