149 lines
4.7 KiB
Go
149 lines
4.7 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
|
|
"github.com/flipped-aurora/gin-vue-admin/server/global"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/plugin/monitor/model"
|
|
"github.com/flipped-aurora/gin-vue-admin/server/plugin/monitor/model/request"
|
|
)
|
|
|
|
var ChangeLog = new(CL)
|
|
|
|
type CL struct{}
|
|
|
|
// CreateChangeLog 创建变更日志记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (s *CL) CreateChangeLog(ctx context.Context, CL *model.ChangeLog) (err error) {
|
|
err = global.GVA_DB.Create(CL).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteChangeLog 删除变更日志记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (s *CL) DeleteChangeLog(ctx context.Context, ID string) (err error) {
|
|
err = global.GVA_DB.Delete(&model.ChangeLog{}, "id = ?", ID).Error
|
|
return err
|
|
}
|
|
|
|
// DeleteChangeLogByIds 批量删除变更日志记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (s *CL) DeleteChangeLogByIds(ctx context.Context, IDs []string) (err error) {
|
|
err = global.GVA_DB.Delete(&[]model.ChangeLog{}, "id in ?", IDs).Error
|
|
return err
|
|
}
|
|
|
|
// UpdateChangeLog 更新变更日志记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (s *CL) UpdateChangeLog(ctx context.Context, CL model.ChangeLog) (err error) {
|
|
err = global.GVA_DB.Model(&model.ChangeLog{}).Where("id = ?", CL.ID).Updates(&CL).Error
|
|
return err
|
|
}
|
|
|
|
// GetChangeLog 根据ID获取变更日志记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (s *CL) GetChangeLog(ctx context.Context, ID string) (CL model.ChangeLog, err error) {
|
|
err = global.GVA_DB.Where("id = ?", ID).First(&CL).Error
|
|
return
|
|
}
|
|
|
|
// GetChangeLogInfoList 分页获取变更日志记录
|
|
// Author [yourname](https://github.com/yourname)
|
|
func (s *CL) GetChangeLogInfoList(ctx context.Context, info request.ChangeLogSearch) (list []model.ChangeLog, total int64, err error) {
|
|
limit := info.PageSize
|
|
offset := info.PageSize * (info.Page - 1)
|
|
// 创建db
|
|
db := global.GVA_DB.Model(&model.ChangeLog{})
|
|
var CLs []model.ChangeLog
|
|
// 如果有条件搜索 下方会自动创建搜索语句
|
|
if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
|
|
db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
|
|
}
|
|
|
|
if info.Database != nil && *info.Database != "" {
|
|
db = db.Where("database = ?", *info.Database)
|
|
}
|
|
if info.Table != nil && *info.Table != "" {
|
|
db = db.Where("db_table = ?", *info.Table)
|
|
}
|
|
if info.Column != nil && *info.Column != "" {
|
|
db = db.Where("db_column = ?", *info.Column)
|
|
}
|
|
if info.OldValue != nil && *info.OldValue != "" {
|
|
db = db.Where("old_value LIKE ?", "%"+*info.OldValue+"%")
|
|
}
|
|
if info.StartChangedAt != nil && info.EndChangedAt != nil {
|
|
db = db.Where("changed_at BETWEEN ? AND ? ", info.StartChangedAt, info.EndChangedAt)
|
|
}
|
|
if info.OperationType != nil && *info.OperationType != "" {
|
|
db = db.Where("operation_type = ?", *info.OperationType)
|
|
}
|
|
err = db.Count(&total).Error
|
|
if err != nil {
|
|
return
|
|
}
|
|
var OrderStr string
|
|
orderMap := make(map[string]bool)
|
|
orderMap["ID"] = true
|
|
orderMap["CreatedAt"] = true
|
|
orderMap["changed_at"] = true
|
|
if orderMap[info.Sort] {
|
|
OrderStr = info.Sort
|
|
if info.Order == "descending" {
|
|
OrderStr = OrderStr + " desc"
|
|
}
|
|
db = db.Order(OrderStr)
|
|
}
|
|
|
|
if limit != 0 {
|
|
db = db.Limit(limit).Offset(offset)
|
|
}
|
|
err = db.Find(&CLs).Error
|
|
return CLs, total, err
|
|
}
|
|
func (s *CL) GetChangeLogDataSource(ctx context.Context) (res map[string][]map[string]any, err error) {
|
|
res = make(map[string][]map[string]any)
|
|
|
|
column := make([]map[string]any, 0)
|
|
columnOptions := make([]map[string]any, 0)
|
|
global.GVA_DB.Table("monitor_config").Where("deleted_at IS NULL").Select("columns").Scan(&column)
|
|
for i, v := range column {
|
|
var m []string
|
|
err := json.Unmarshal([]byte(v["columns"].(string)), &m)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
column[i]["columns"] = m
|
|
for _, v1 := range m {
|
|
columnOptions = append(columnOptions, map[string]any{"label": v1, "value": v1})
|
|
}
|
|
}
|
|
// 对columnOptions进行去重
|
|
columnOptions = removeDuplicate(columnOptions)
|
|
res["column"] = columnOptions
|
|
database := make([]map[string]any, 0)
|
|
global.GVA_DB.Table("monitor_config").Where("deleted_at IS NULL").Select("DISTINCT database as label, database as value").Scan(&database)
|
|
res["database"] = database
|
|
table := make([]map[string]any, 0)
|
|
global.GVA_DB.Table("monitor_config").Where("deleted_at IS NULL").Select("DISTINCT db_table as label,db_table as value").Scan(&table)
|
|
res["table"] = table
|
|
return
|
|
}
|
|
|
|
func (s *CL) GetChangeLogPublic(ctx context.Context) {
|
|
|
|
}
|
|
func removeDuplicate(slice []map[string]any) []map[string]any {
|
|
seen := make(map[string]bool)
|
|
result := []map[string]any{}
|
|
for _, v := range slice {
|
|
label, ok := v["label"].(string)
|
|
if ok && !seen[label] {
|
|
seen[label] = true
|
|
result = append(result, v)
|
|
}
|
|
}
|
|
return result
|
|
}
|