refactor: 新增base.Repo与base.App,重构repo与app层代码

This commit is contained in:
meilin.huang
2023-10-26 17:15:49 +08:00
parent 10f6b03fb5
commit a1303b52eb
115 changed files with 1867 additions and 1696 deletions

View File

@@ -5,7 +5,8 @@ import (
"mayfly-go/internal/machine/domain/entity"
"mayfly-go/internal/machine/domain/repository"
"mayfly-go/internal/machine/infrastructure/machine"
"mayfly-go/pkg/biz"
"mayfly-go/pkg/base"
"mayfly-go/pkg/errorx"
"mayfly-go/pkg/gormx"
"mayfly-go/pkg/model"
@@ -13,90 +14,93 @@ import (
)
type Machine interface {
// 根据条件获取账号信息
GetMachine(condition *entity.Machine, cols ...string) error
base.App[*entity.Machine]
Save(*entity.Machine)
Save(*entity.Machine) error
// 测试机器连接
TestConn(me *entity.Machine)
TestConn(me *entity.Machine) error
// 调整机器状态
ChangeStatus(id uint64, status int8)
ChangeStatus(id uint64, status int8) error
Count(condition *entity.MachineQuery) int64
Delete(id uint64)
// 根据id获取
GetById(id uint64, cols ...string) *entity.Machine
Delete(id uint64) error
// 分页获取机器信息列表
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) *model.PageResult[*[]*vo.MachineVO]
GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error)
// 获取机器连接
GetCli(id uint64) *machine.Cli
GetCli(id uint64) (*machine.Cli, error)
// 获取ssh隧道机器连接
GetSshTunnelMachine(id int) *machine.SshTunnelMachine
GetSshTunnelMachine(id int) (*machine.SshTunnelMachine, error)
}
func newMachineApp(machineRepo repository.Machine, authCertApp AuthCert) Machine {
return &machineAppImpl{
machineRepo: machineRepo,
app := &machineAppImpl{
authCertApp: authCertApp,
}
app.Repo = machineRepo
return app
}
type machineAppImpl struct {
machineRepo repository.Machine
base.AppImpl[*entity.Machine, repository.Machine]
authCertApp AuthCert
}
// 分页获取机器信息列表
func (m *machineAppImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) *model.PageResult[*[]*vo.MachineVO] {
return m.machineRepo.GetMachineList(condition, pageParam, toEntity, orderBy...)
func (m *machineAppImpl) GetMachineList(condition *entity.MachineQuery, pageParam *model.PageParam, toEntity *[]*vo.MachineVO, orderBy ...string) (*model.PageResult[*[]*vo.MachineVO], error) {
return m.GetRepo().GetMachineList(condition, pageParam, toEntity, orderBy...)
}
func (m *machineAppImpl) Count(condition *entity.MachineQuery) int64 {
return m.machineRepo.Count(condition)
return m.GetRepo().Count(condition)
}
func (m *machineAppImpl) Save(me *entity.Machine) {
func (m *machineAppImpl) Save(me *entity.Machine) error {
oldMachine := &entity.Machine{Ip: me.Ip, Port: me.Port, Username: me.Username}
if me.SshTunnelMachineId > 0 {
oldMachine.SshTunnelMachineId = me.SshTunnelMachineId
}
err := m.GetMachine(oldMachine)
err := m.GetBy(oldMachine)
me.PwdEncrypt()
if me.Id == 0 {
biz.IsTrue(err != nil, "该机器信息已存在")
if err == nil {
return errorx.NewBiz("该机器信息已存在")
}
// 新增机器,默认启用状态
me.Status = entity.MachineStatusEnable
m.machineRepo.Create(me)
return
return m.Insert(me)
}
// 如果存在该库,则校验修改的库是否为该库
if err == nil {
biz.IsTrue(oldMachine.Id == me.Id, "该机器信息已存在")
if err == nil && oldMachine.Id != me.Id {
return errorx.NewBiz("该机器信息已存在")
}
// 关闭连接
machine.DeleteCli(me.Id)
m.machineRepo.UpdateById(me)
return m.UpdateById(me)
}
func (m *machineAppImpl) TestConn(me *entity.Machine) {
func (m *machineAppImpl) TestConn(me *entity.Machine) error {
me.Id = 0
// 测试连接
biz.ErrIsNilAppendErr(machine.TestConn(*m.toMachineInfo(me), func(u uint64) *machine.Info {
mi, err := m.toMachineInfo(me)
if err != nil {
return err
}
return machine.TestConn(*mi, func(u uint64) (*machine.Info, error) {
return m.toMachineInfoById(u)
}), "该机器无法连接: %s")
})
}
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) {
func (m *machineAppImpl) ChangeStatus(id uint64, status int8) error {
if status == entity.MachineStatusDisable {
// 关闭连接
machine.DeleteCli(id)
@@ -104,63 +108,55 @@ func (m *machineAppImpl) ChangeStatus(id uint64, status int8) {
machine := new(entity.Machine)
machine.Id = id
machine.Status = status
m.machineRepo.UpdateById(machine)
return m.UpdateById(machine)
}
// 根据条件获取机器信息
func (m *machineAppImpl) Delete(id uint64) {
func (m *machineAppImpl) Delete(id uint64) error {
// 关闭连接
machine.DeleteCli(id)
gormx.Tx(
return gormx.Tx(
func(db *gorm.DB) error {
// 删除machine表信息
return gormx.DeleteByIdWithDb(db, new(entity.Machine), id)
},
func(db *gorm.DB) error {
// 删除machine_file
return gormx.DeleteByConditionWithDb(db, &entity.MachineFile{MachineId: id})
return gormx.DeleteByWithDb(db, &entity.MachineFile{MachineId: id})
},
func(db *gorm.DB) error {
// 删除machine_script
return gormx.DeleteByConditionWithDb(db, &entity.MachineScript{MachineId: id})
return gormx.DeleteByWithDb(db, &entity.MachineScript{MachineId: id})
},
)
}
// 根据条件获取机器信息
func (m *machineAppImpl) GetMachine(condition *entity.Machine, cols ...string) error {
return m.machineRepo.GetMachine(condition, cols...)
}
func (m *machineAppImpl) GetById(id uint64, cols ...string) *entity.Machine {
return m.machineRepo.GetById(id, cols...)
}
func (m *machineAppImpl) GetCli(machineId uint64) *machine.Cli {
cli, err := machine.GetCli(machineId, func(mid uint64) *machine.Info {
func (m *machineAppImpl) GetCli(machineId uint64) (*machine.Cli, error) {
return machine.GetCli(machineId, func(mid uint64) (*machine.Info, error) {
return m.toMachineInfoById(mid)
})
biz.ErrIsNilAppendErr(err, "获取客户端错误: %s")
return cli
}
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) *machine.SshTunnelMachine {
sshTunnel, err := machine.GetSshTunnelMachine(machineId, func(mid uint64) *machine.Info {
func (m *machineAppImpl) GetSshTunnelMachine(machineId int) (*machine.SshTunnelMachine, error) {
return machine.GetSshTunnelMachine(machineId, func(mid uint64) (*machine.Info, error) {
return m.toMachineInfoById(mid)
})
biz.ErrIsNilAppendErr(err, "获取ssh隧道连接失败: %s")
return sshTunnel
}
// 生成机器信息根据授权凭证id填充用户密码等
func (m *machineAppImpl) toMachineInfoById(machineId uint64) *machine.Info {
me := m.GetById(machineId)
biz.IsTrue(me.Status == entity.MachineStatusEnable, "该机器已被停用")
func (m *machineAppImpl) toMachineInfoById(machineId uint64) (*machine.Info, error) {
me, err := m.GetById(new(entity.Machine), machineId)
if err != nil {
return nil, errorx.NewBiz("机器信息不存在")
}
if me.Status != entity.MachineStatusEnable {
return nil, errorx.NewBiz("该机器已被停用")
}
return m.toMachineInfo(me)
}
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
func (m *machineAppImpl) toMachineInfo(me *entity.Machine) (*machine.Info, error) {
mi := new(machine.Info)
mi.Id = me.Id
mi.Name = me.Name
@@ -173,8 +169,10 @@ func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
mi.SshTunnelMachineId = me.SshTunnelMachineId
if me.UseAuthCert() {
ac := m.authCertApp.GetById(uint64(me.AuthCertId))
biz.NotNil(ac, "授权凭证信息已不存在,请重新关联")
ac, err := m.authCertApp.GetById(new(entity.AuthCert), uint64(me.AuthCertId))
if err != nil {
return nil, errorx.NewBiz("授权凭证信息已不存在,请重新关联")
}
mi.AuthMethod = ac.AuthMethod
ac.PwdDecrypt()
mi.Password = ac.Password
@@ -186,5 +184,5 @@ func (m *machineAppImpl) toMachineInfo(me *entity.Machine) *machine.Info {
}
mi.Password = me.Password
}
return mi
return mi, nil
}