package dao

import (
	"gorm.io/gorm"
	"time"
)

type LightStrategy struct {
	ID                 int       `gorm:"primary key" json:"id"`                        //编号
	LightName          string    `gorm:"type:varchar(64)" json:"lightName"`            //策略名称
	LightControlType   int       `gorm:"type:int" json:"lightControlType"`             //灯控类型 设置 1=485,2=nb-iot
	OptoSensorID       int       `gorm:"type:int" json:"optoSensorId"`                 //光传感器ID
	StartTime          string    `gorm:"type:varchar(60)" json:"startTime"`            //策略期限开始时间
	EndTime            string    `gorm:"type:varchar(60)" json:"endTime"`              //策略期限结束时间
	IsAllYear          int       `gorm:"type:int;default 1" json:"isAllYear"`          //全年设置2=是，1=否
	IsAutomaticRenewal int       `gorm:"type:int;default 1" json:"isAutomaticRenewal"` //自动续期设置 2=是，1=否
	TenantId           string    `gorm:"type:varchar(12)" json:"tenantId"`             //租户ID
	CreateTime         time.Time `gorm:"type:datetime" json:"createTime"`              //新增时间
	CreateUser         int64     `gorm:"type:bigint" json:"createUser"`                //新增记录操作用户ID
	UpdateTime         time.Time `gorm:"type:datetime" json:"updateTime"`              //修改时间
	UpdateUser         int64     `gorm:"type:bigint" json:"updateUser"`                //修改用户
	IsDeleted          int       `gorm:"type:int;default 0" json:"isDeleted"`          //是否删除 0=未删除,1=删除
	Status             int       `gorm:"type:int;default 1" json:"status"`             //状态 0=正常,1=异常
	LightSn            string    `gorm:"type:varchar(64)" json:"lightSn"`              //策略编码
	ManualTime         int       `gorm:"type:int;default 0" json:"manualTime"`         //手动控制时间
}

func (LightStrategy) TableName() string {
	return "strategy_light"
}

func (c *LightStrategy) Delete() error {
	return Db.Debug().Model(&c).Where("id = ?", c.ID).Updates(map[string]interface{}{"update_time": c.UpdateTime,
		"update_user": c.UpdateUser, "is_deleted": c.IsDeleted}).Error
}

func (c LightStrategy) IsExistedBySN() bool {
	var count int64
	_ = Db.Debug().Model(&c).Where(" light_sn = ? and is_deleted = ?",
		c.LightSn, c.IsDeleted).Count(&count).Error
	return count > 0
}

func (c LightStrategy) IsExistedByNameAndCode() bool {
	var Strategies []LightStrategy
	err := Db.Debug().Model(&c).Where("gateway_sn = ? and is_deleted = ?",
		c.TenantId, c.IsDeleted).Find(&Strategies).Error
	if err == gorm.ErrRecordNotFound {
		return false
	}
	for _, d := range Strategies {
		if d.ID != c.ID {
			return true
		}
	}
	return false
}

func (c *LightStrategy) Create() error {
	return Db.Debug().Model(&c).Save(&c).Error
}

func (c *LightStrategy) Update() error {
	return Db.Debug().Model(&c).Where(" id = ? and is_deleted = 0", c.ID).Updates(&c).Error
}

func (c *LightStrategy) GetStrategy() error {
	err := Db.Debug().Model(&c).Where(" id = ? and is_deleted = 0", c.ID).First(&c).Error
	return err
}

func (c LightStrategy) GetStrategies(offset, limit int) ([]LightStrategy, int64, error) {
	var Strategies []LightStrategy
	var count int64
	db := Db.Debug().Model(&c).Where("is_deleted = 0")
	if c.LightSn != "" {
		db = db.Where(" light_name like ? or light_sn like ?", "%"+c.LightSn+"%", "%"+c.LightSn+"%")
	}
	db.Count(&count)
	err := db.Offset(offset).Limit(limit).Find(&Strategies).Error
	return Strategies, count, err

}

func (c LightStrategy) GetAllStrategies() ([]*LightStrategy, error) {
	var Strategies []*LightStrategy
	err := Db.Debug().Model(&c).Where(" tenant_id = ? and is_deleted = ? ", c.TenantId,
		c.IsDeleted).Scan(&Strategies).Error
	return Strategies, err
}
