package repository

import (
	"be.ems/src/framework/database/db"
	"be.ems/src/framework/logger"
	"be.ems/src/modules/monitor/model"
)

// 实例化数据层 Monitor 结构体
var NewMonitor = &Monitor{}

// Monitor 监控服务资源相关信息 数据层处理
type Monitor struct{}

// SelectByBase 查询监控_基本信息
func (r Monitor) SelectByBase(query map[string]any) []model.MonitorBase {
	tx := db.DB("").Model(&model.MonitorBase{})
	// 查询条件拼接
	if v, ok := query["neType"]; ok && v != "" {
		tx = tx.Where("ne_type = ?", v)
	}
	if v, ok := query["neId"]; ok && v != "" {
		tx = tx.Where("ne_id = ?", v)
	}
	tx.Where("create_time >= ? and create_time <= ?", query["beginTime"], query["endTime"])

	// 查询数据
	rows := []model.MonitorBase{}
	if err := tx.Order("create_time asc").Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// InsertByBase 创建监控_基本信息
func (r Monitor) InsertByBase(param model.MonitorBase) error {
	return db.DB("").Create(&param).Error
}

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

// SelectByIO 查询监控_IO
func (r Monitor) SelectByIO(query map[string]any) []model.MonitorIO {
	tx := db.DB("").Model(&model.MonitorIO{})
	// 查询条件拼接
	if v, ok := query["name"]; ok && v != "" {
		tx = tx.Where("name = ?", v)
	}
	if v, ok := query["neType"]; ok && v != "" {
		tx = tx.Where("ne_type = ?", v)
	}
	if v, ok := query["neId"]; ok && v != "" {
		tx = tx.Where("ne_id = ?", v)
	}
	tx.Where("create_time >= ? and create_time <= ?", query["beginTime"], query["endTime"])

	// 查询数据
	rows := []model.MonitorIO{}
	if err := tx.Order("create_time asc").Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// BatchInsertByIO 批量创建监控_IO
func (r Monitor) BatchInsertByIO(arr []model.MonitorIO) error {
	return db.DB("").CreateInBatches(arr, 100).Error
}

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

// SelectByNetwork 查询监控_网络
func (r Monitor) SelectByNetwork(query map[string]any) []model.MonitorNetwork {
	tx := db.DB("").Model(&model.MonitorNetwork{})
	// 查询条件拼接
	if v, ok := query["name"]; ok && v != "" {
		tx = tx.Where("name = ?", v)
	} else {
		tx = tx.Where("name = 'all'")
	}
	if v, ok := query["neType"]; ok && v != "" {
		tx = tx.Where("ne_type = ?", v)
	}
	if v, ok := query["neId"]; ok && v != "" {
		tx = tx.Where("ne_id = ?", v)
	}
	tx.Where("create_time >= ? and create_time <= ?", query["beginTime"], query["endTime"])

	// 查询数据
	rows := []model.MonitorNetwork{}
	if err := tx.Order("create_time asc").Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// BatchInsertByNetwork 批量创建监控_网络
func (r Monitor) BatchInsertByNetwork(arr []model.MonitorNetwork) error {
	return db.DB("").CreateInBatches(arr, 100).Error
}

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