package repository

import (
	"time"

	"be.ems/src/framework/database/db"
	"be.ems/src/framework/logger"
	"be.ems/src/modules/ne_data/model"
)

// 实例化数据层 AlarmEvent 结构体
var NewAlarmEvent = &AlarmEvent{}

// AlarmEvent 告警 数据层处理
type AlarmEvent struct{}

// SelectByPage 分页查询集合
func (r AlarmEvent) SelectByPage(query model.AlarmEventQuery) ([]model.AlarmEvent, int64) {
	tx := db.DB("").Model(&model.AlarmEvent{})
	// 查询条件拼接
	if query.NeType != "" {
		tx = tx.Where("ne_type = ?", query.NeType)
	}
	if query.NeID != "" {
		tx = tx.Where("ne_id = ?", query.NeID)
	}
	if query.AlarmCode != "" {
		tx = tx.Where("alarm_code = ?", query.AlarmCode)
	}
	if query.AlarmStatus != "" {
		tx = tx.Where("alarm_status = ?", query.AlarmStatus)
	}
	if query.BeginTime != 0 {
		tx = tx.Where("event_time >= ?", query.BeginTime)
	}
	if query.EndTime != 0 {
		tx = tx.Where("event_time <= ?", query.EndTime)
	}

	// 查询结果
	var total int64 = 0
	rows := []model.AlarmEvent{}

	// 查询数量为0直接返回
	if err := tx.Count(&total).Error; err != nil || total <= 0 {
		return rows, total
	}

	// 排序
	if query.SortField != "" {
		sortField := query.SortField
		if query.SortOrder == "desc" {
			sortField = sortField + " desc"
		}
		tx = tx.Order(sortField)
	}

	// 查询数据分页
	pageNum, pageSize := db.PageNumSize(query.PageNum, query.PageSize)
	tx = tx.Limit(pageSize).Offset(pageSize * pageNum)
	err := tx.Find(&rows).Error
	if err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows, total
	}
	return rows, total
}

// Select 查询集合
func (r AlarmEvent) Select(param model.AlarmEvent) []model.AlarmEvent {
	tx := db.DB("").Model(&model.AlarmEvent{})
	// 查询条件拼接
	if param.NeType != "" {
		tx = tx.Where("ne_type = ?", param.NeType)
	}
	if param.NeId != "" {
		tx = tx.Where("ne_id = ?", param.NeId)
	}
	if param.AlarmId != "" {
		tx = tx.Where("alarm_id = ?", param.AlarmId)
	}
	if param.AlarmCode != 0 {
		tx = tx.Where("alarm_code = ?", param.AlarmCode)
	}
	if param.AlarmStatus != "" {
		tx = tx.Where("alarm_status = ?", param.AlarmStatus)
	}
	// 查询数据
	rows := []model.AlarmEvent{}
	if err := tx.Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// SelectByIds 通过ID查询
func (r AlarmEvent) SelectByIds(ids []int64) []model.AlarmEvent {
	rows := []model.AlarmEvent{}
	if len(ids) <= 0 {
		return rows
	}
	tx := db.DB("").Model(&model.AlarmEvent{})
	// 构建查询条件
	tx = tx.Where("id in ?", ids)
	// 查询数据
	if err := tx.Find(&rows).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return rows
	}
	return rows
}

// Insert 新增信息 返回新增数据ID
func (r AlarmEvent) Insert(param model.AlarmEvent) int64 {
	if param.Timestamp == 0 {
		param.Timestamp = time.Now().UnixMilli()
	}
	// 执行插入
	if err := db.DB("").Create(&param).Error; err != nil {
		logger.Errorf("insert err => %v", err.Error())
		return 0
	}
	return param.ID
}

// Update 修改信息 返回受影响的行数
func (r AlarmEvent) Update(param model.AlarmEvent) int64 {
	if param.ID <= 0 {
		return 0
	}
	tx := db.DB("").Model(&model.AlarmEvent{})
	// 构建查询条件
	tx = tx.Where("id = ?", param.ID)
	tx = tx.Omit("id", "timestamp")
	// 执行更新
	if err := tx.Updates(param).Error; err != nil {
		logger.Errorf("update err => %v", err.Error())
		return 0
	}
	return tx.RowsAffected
}

// DeleteByIds 批量删除信息
func (r AlarmEvent) DeleteByIds(ids []int64) int64 {
	if len(ids) <= 0 {
		return 0
	}
	tx := db.DB("").Where("id in ?", ids)
	if err := tx.Delete(&model.AlarmEvent{}).Error; err != nil {
		logger.Errorf("delete err => %v", err.Error())
		return 0
	}
	return tx.RowsAffected
}

// SelectAlarmEventSeqLast 查询网元告警最后一条序号
func (r AlarmEvent) SelectAlarmEventSeqLast(neType, neId string) int64 {
	tx := db.DB("").Model(&model.AlarmEvent{})
	tx = tx.Where("ne_type=? and ne_id=?", neType, neId)
	tx = tx.Select("alarm_seq").Order("alarm_seq DESC")
	// 查询数据
	var AlarmEventSeq int64 = 0
	if err := tx.Limit(1).Find(&AlarmEventSeq).Error; err != nil {
		logger.Errorf("query find err => %v", err.Error())
		return AlarmEventSeq
	}
	return AlarmEventSeq
}
