package repository

import (
	"be.ems/src/framework/datasource"
	"be.ems/src/framework/logger"
	"be.ems/src/modules/monitor/model"
	"gorm.io/gorm"
)

// 实例化数据层 MonitorImpl 结构体
var NewMonitorImpl = &MonitorImpl{
	db: datasource.DefaultDB,
}

// MonitorImpl 监控服务资源相关信息 数据层处理
type MonitorImpl struct {
	// 数据库实例
	db func() *gorm.DB
}

// CreateMonitorBase 创建监控_基本信息
func (r *MonitorImpl) CreateMonitorBase(m model.MonitorBase) error {
	return r.db().Create(&m).Error
}

// DelMonitorBase 删除监控_基本信息
func (r *MonitorImpl) DelMonitorBase(ltTime int64) error {
	return r.db().Where("create_time < ?", ltTime).Delete(&model.MonitorBase{}).Error
}

// SelectMonitorBase 查询监控_基本信息
func (r *MonitorImpl) SelectMonitorBase(query map[string]any) []model.MonitorBase {
	var bases []model.MonitorBase
	dbConn := r.db()
	if query["neType"] != "" && query["neId"] != "" {
		dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])
	}
	dbConn = dbConn.Where("create_time >= ? and create_time <= ?", query["startTime"], query["endTime"])
	err := dbConn.Order("create_time asc").Find(&bases).Error
	if err != nil {
		logger.Errorf("SelectMonitorBase %v", err)
		return bases
	}
	return bases
}

// BatchCreateMonitorIO 批量创建监控_IO
func (r *MonitorImpl) BatchCreateMonitorIO(ioList []model.MonitorIO) error {
	return r.db().CreateInBatches(ioList, len(ioList)).Error
}

// DelMonitorIO 删除监控_IO
func (r *MonitorImpl) DelMonitorIO(ltTime int64) error {
	return r.db().Where("create_time < ?", ltTime).Delete(&model.MonitorIO{}).Error
}

// SelectMonitorIO 查询监控_IO
func (r *MonitorImpl) SelectMonitorIO(query map[string]any) []model.MonitorIO {
	var ios []model.MonitorIO
	dbConn := r.db()
	if query["name"] != "" {
		dbConn = dbConn.Where("name = ?", query["name"])
	}
	if query["neType"] != "" && query["neId"] != "" {
		dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])
	}
	dbConn = dbConn.Where("create_time >= ? and create_time <= ?", query["startTime"], query["endTime"])
	err := dbConn.Order("create_time asc").Find(&ios).Error
	if err != nil {
		logger.Errorf("SelectMonitorIO %v", err)
		return ios
	}
	return ios
}

// BatchCreateMonitorNet 批量创建监控_网络
func (r *MonitorImpl) BatchCreateMonitorNet(netList []model.MonitorNetwork) error {
	return r.db().CreateInBatches(netList, len(netList)).Error
}

// DelMonitorNet 删除监控_网络
func (r *MonitorImpl) DelMonitorNet(ltTime int64) error {
	return r.db().Where("create_time < ?", ltTime).Delete(&model.MonitorNetwork{}).Error
}

// SelectMonitorNetwork 查询监控_网络
func (r *MonitorImpl) SelectMonitorNetwork(query map[string]any) []model.MonitorNetwork {
	var networks []model.MonitorNetwork
	dbConn := r.db()
	if query["name"] != "" {
		dbConn = dbConn.Where("name = ?", query["name"])
	} else {
		dbConn = dbConn.Where("name = ?", "all")
	}
	if query["neType"] != "" && query["neId"] != "" {
		dbConn = dbConn.Where("ne_type = ? and ne_id = ?", query["neType"], query["neId"])
	}
	dbConn = dbConn.Where("create_time >= ? and create_time <= ?", query["startTime"], query["endTime"])
	err := dbConn.Order("create_time asc").Find(&networks).Error
	if err != nil {
		logger.Errorf("SelectMonitorNetwork %v", err)
		return networks
	}
	return networks
}
