package dao

import (
	"gorm.io/gorm"
	"iot_manager_service/app/warn/model"
	"iot_manager_service/util/common"
	"time"
)

type BusinessTactics struct {
	AlarmType    int     `comment:"告警类型-数据字典" json:"alarmType,omitempty"`
	DeviceType   int     `comment:"设备告警类型" json:"deviceType,omitempty"`
	BusinessName string  `comment:"业务告警名称" json:"businessName,omitempty"`
	MaxValue     float64 `comment:"上限值" json:"maxValue,omitempty"`
	MinValue     float64 `comment:"下限值" json:"minValue,omitempty"`
	Duration     int     `comment:"持续时间" json:"duration,omitempty"`
	IsEnable     int     `comment:"1-启用，2-禁用；默认启用" json:"isEnable,omitempty"`
	TenantId     string  `gorm:"type:varchar(12)" comment:"租户ID" json:"tenantId,omitempty"`
	ID           uint    `gorm:"primarykey" json:"id"`
	CreatedAt    time.Time
	UpdatedAt    time.Time
	DeletedAt    gorm.DeletedAt `gorm:"index"`

	RelevanceLampNum int    `gorm:"_" json:"relevanceLampNum"` //关联灯杆数
	AlarmTypeName    string `gorm:"_" json:"alarmTypeName,omitempty"`
	DeviceTypeName   string `gorm:"_" json:"deviceTypeName,omitempty"`
}

func (BusinessTactics) TableName() string {
	return "warn_business_tactics"
}

func (t BusinessTactics) Gets(filter model.RequestBusinessTacticFilter) ([]BusinessTactics, int64, error) {
	var list []BusinessTactics
	db := Db.Debug().Model(&t).Where(&BusinessTactics{TenantId: t.TenantId})
	db = db.Scopes(common.Paginate(filter.Current, filter.Size))
	// filter
	if filter.BusinessName != "" {
		db = db.Where("business_name like ?", "%"+filter.BusinessName+"%")
	}
	if filter.ArmClassify != 0 {
		db = db.Where("alarm_type = ?", filter.ArmClassify)
	}
	err := db.Find(&list).Error
	var count int64
	db.Count(&count)
	return list, count, err
}

func (t BusinessTactics) Create() error {
	err := Db.Debug().Create(&t).Error
	return err
}

func (t BusinessTactics) Update(id int) error {
	err := Db.Debug().Where("id=?", id).Updates(&t).Error
	return err
}

func (t BusinessTactics) Delete(id int) error {
	err := Db.Debug().Model(&t).Delete(&t, id).Error
	return err
}
