2021-05-08 18:00:33 +08:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
import (
|
2021-12-11 11:19:47 +08:00
|
|
|
"fmt"
|
2022-09-09 18:26:08 +08:00
|
|
|
"mayfly-go/internal/db/domain/entity"
|
|
|
|
|
"mayfly-go/internal/db/domain/repository"
|
2022-06-02 17:41:11 +08:00
|
|
|
"mayfly-go/pkg/biz"
|
|
|
|
|
"mayfly-go/pkg/model"
|
2021-05-08 18:00:33 +08:00
|
|
|
)
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
type dbRepoImpl struct{}
|
2021-05-08 18:00:33 +08:00
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func newDbRepo() repository.Db {
|
|
|
|
|
return new(dbRepoImpl)
|
|
|
|
|
}
|
2021-05-08 18:00:33 +08:00
|
|
|
|
|
|
|
|
// 分页获取数据库信息列表
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) GetDbList(condition *entity.Db, pageParam *model.PageParam, toEntity interface{}, orderBy ...string) *model.PageResult {
|
2021-12-11 11:19:47 +08:00
|
|
|
sql := "SELECT d.* FROM t_db d JOIN t_project_member pm ON d.project_id = pm.project_id WHERE 1 = 1 "
|
|
|
|
|
if condition.CreatorId != 0 {
|
|
|
|
|
// 使用创建者id模拟项目成员id
|
|
|
|
|
sql = fmt.Sprintf("%s AND pm.account_id = %d", sql, condition.CreatorId)
|
|
|
|
|
}
|
|
|
|
|
if condition.ProjectId != 0 {
|
|
|
|
|
sql = fmt.Sprintf("%s AND d.project_id = %d", sql, condition.ProjectId)
|
|
|
|
|
}
|
2021-12-13 09:49:05 +08:00
|
|
|
if condition.EnvId != 0 {
|
|
|
|
|
sql = fmt.Sprintf("%s AND d.env_id = %d", sql, condition.EnvId)
|
|
|
|
|
}
|
2021-12-11 11:19:47 +08:00
|
|
|
if condition.Host != "" {
|
|
|
|
|
sql = sql + " AND d.host LIKE '%" + condition.Host + "%'"
|
|
|
|
|
}
|
2021-12-13 09:49:05 +08:00
|
|
|
if condition.Database != "" {
|
|
|
|
|
sql = sql + " AND d.database LIKE '%" + condition.Database + "%'"
|
|
|
|
|
}
|
2021-12-11 11:19:47 +08:00
|
|
|
sql = sql + " ORDER BY d.create_time DESC"
|
|
|
|
|
return model.GetPageBySql(sql, pageParam, toEntity)
|
2021-05-08 18:00:33 +08:00
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) Count(condition *entity.Db) int64 {
|
2021-09-11 14:04:09 +08:00
|
|
|
return model.CountBy(condition)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-08 18:00:33 +08:00
|
|
|
// 根据条件获取账号信息
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) GetDb(condition *entity.Db, cols ...string) error {
|
2021-05-08 18:00:33 +08:00
|
|
|
return model.GetBy(condition, cols...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 根据id获取
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) GetById(id uint64, cols ...string) *entity.Db {
|
2021-05-08 18:00:33 +08:00
|
|
|
db := new(entity.Db)
|
|
|
|
|
if err := model.GetById(db, id, cols...); err != nil {
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
return db
|
|
|
|
|
}
|
2021-07-28 18:03:19 +08:00
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) Insert(db *entity.Db) {
|
2021-07-28 18:03:19 +08:00
|
|
|
biz.ErrIsNil(model.Insert(db), "新增数据库信息失败")
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) Update(db *entity.Db) {
|
2021-07-28 18:03:19 +08:00
|
|
|
biz.ErrIsNil(model.UpdateById(db), "更新数据库信息失败")
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-09 18:26:08 +08:00
|
|
|
func (d *dbRepoImpl) Delete(id uint64) {
|
2021-07-28 18:03:19 +08:00
|
|
|
model.DeleteById(new(entity.Db), id)
|
|
|
|
|
}
|