package dao

import "server/global"

type InductanceDetails struct {
	ID                 int     `json:"id" form:"id" gorm:"primarykey"`               // 主键ID
	DeviceSn           string  `json:"deviceSn" form:"deviceSn" gorm:"comment:电感编码"` // 电感编码
	VoltageA           int64   `json:"voltageA" form:"voltageA" gorm:"comment:电压A"`
	VoltageB           int64   `json:"voltageB" form:"voltageB" gorm:"comment:电压B"`
	VoltageC           int64   `json:"voltageC" form:"voltageC" gorm:"comment:电压C"`
	CurrentA           float64 `json:"currentA" form:"currentA" gorm:"comment:电流A"`
	CurrentB           float64 `json:"currentB" form:"currentB" gorm:"comment:电流B"`
	CurrentC           float64 `json:"currentC" form:"currentC" gorm:"comment:电流C"`
	ActivePowerA       int     `json:"activePowerA" form:"activePowerA" gorm:"comment:有功功率A"`
	ActivePowerB       int     `json:"activePowerB" form:"activePowerB" gorm:"comment:有功功率B"`
	ActivePowerC       int     `json:"activePowerC" form:"activePowerC" gorm:"comment:有功功率C"`
	TotalActivePower   int     `json:"totalActivePower" form:"totalActivePower" gorm:"comment:总有功功率"`
	ApparentPowerA     int     `json:"apparentPowerA" form:"apparentPowerA" gorm:"comment:视在功率A"`
	ApparentPowerB     int     `json:"apparentPowerB" form:"apparentPowerB" gorm:"comment:视在功率B"`
	ApparentPowerC     int     `json:"apparentPowerC" form:"apparentPowerC" gorm:"comment:视在功率C"`
	TotalApparentPower int     `json:"totalApparentPower" form:"totalApparentPower" gorm:"comment:总视在功率"`
	PowerFactorA       float64 `json:"powerFactorA" form:"powerFactorA" gorm:"comment:功率因数A"`
	PowerFactorB       float64 `json:"powerFactorB" form:"powerFactorB" gorm:"comment:功率因数B"`
	PowerFactorC       float64 `json:"powerFactorC" form:"powerFactorC" gorm:"comment:功率因数C"`
	TotalPowerFactor   float64 `json:"totalPowerFactor" form:"totalPowerFactor" gorm:"comment:总功率因数"`
	UploadTime         string  `json:"uploadTime" form:"uploadTime" gorm:"comment:上传时间"`
}

func (InductanceDetails) TableName() string {
	return "inductance_details"
}

func (id InductanceDetails) CreateInductanceDetails() error {
	return global.GVA_DB.Create(&id).Error
}

func QueryInductanceDetailsByCode(inductanceCode string) (inductanceDetails []InductanceDetails, err error) {
	err = global.GVA_DB.Model(&InductanceDetails{}).Where("device_sn =?", inductanceCode).Scan(&inductanceDetails).Error
	return
}

// QueryInductanceDetailsList 查询电感详情列表
func QueryInductanceDetailsList(limit, offset int, code string) (list []InductanceDetails, total int64, err error) {
	db := global.GVA_DB.Model(&InductanceDetails{}).Where("device_sn", code).Order("upload_time DESC")
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&list).Error

	return list, total, err
}

func QueryInductanceDetailsOneDay(sn string) (list []InductanceDetails, err error) {
	err = global.GVA_DB.Model(&InductanceDetails{}).Where("upload_time < NOW() AND upload_time > DATE_SUB(NOW(), INTERVAL 1 DAY) AND device_sn = ?", sn).Find(&list).Error
	return
}

// TimingDeleteInductanceDetails 定时删除电感详情
func TimingDeleteInductanceDetails() error {
	return global.GVA_DB.Unscoped().Delete(&InductanceDetails{}, "upload_time < DATE_SUB(NOW(), INTERVAL 5 DAY)").Error
}
