package dao

import (
	"time"
)

type LightRecord struct {
	Id               int       `gorm:"column:id;primary_key;AUTO_INCREMENT; comment:编号" json:"id"`
	GroupId          int       `gorm:"column:group_id; comment:灯杆分组ID" json:"groupId"`
	GroupName        string    `gorm:"column:group_name; comment:灯杆分组名" json:"groupName"`
	LampPoleId       int       `gorm:"column:lamp_pole_id; comment:灯杆ID" json:"lampPoleId"`
	LampPoleName     string    `gorm:"column:lamp_pole_name; comment:灯杆名称" json:"lampPoleName"`
	LampPoleSn       string    `gorm:"column:lamp_pole_sn; comment:灯杆编码" json:"lampPoleSn"`
	LightControlId   int       `gorm:"column:light_control_id; comment:灯控ID" json:"lightControlId"`
	LightControlName string    `gorm:"column:light_control_name; comment:灯控名称" json:"lightControlName"`
	LightControlSn   string    `gorm:"column:light_control_sn; comment:灯控编号" json:"lightControlSn"`
	Luminance        int       `gorm:"column:luminance; comment:亮度" json:"luminance"`
	LightingState    int       `gorm:"column:lighting_state;default:0; comment:亮灯状态：0-关闭，1-开启" json:"lightingState"`
	StartTime        time.Time `gorm:"column:start_time; comment:亮灯开始时间; type:datetime" json:"startTime"`
	EndTime          time.Time `gorm:"column:end_time; comment:亮灯结束时间; type:datetime" json:"endTime"`
	CountTime        int       `gorm:"column:count_time; comment:亮灯时长(分钟)" json:"countTime"`
	TenantId         string    `gorm:"column:tenant_id; comment:租户ID" json:"tenantId"`
	CreateTime       time.Time `gorm:"column:create_time;NOT NULL; comment:创建日期;type:datetime" json:"createTime"`
	UpdateTime       time.Time `gorm:"column:update_time;default:CURRENT_TIMESTAMP; comment:新时间 默认为当前时间;type:datetime" json:"updateTime"`
	IsDeleted        int       `gorm:"column:is_deleted;default:0; comment:是否删除：0-否，1-删除" json:"isDeleted"`
}

func (l LightRecord) TableName() string {
	return "device_lamp_events_view"
}

// GetRecords 查记录
func (c LightRecord) GetRecords(offset int, limit int, start, end, searchValue string, groupId int) ([]LightRecord, int64, error) {
	var LightRecords []LightRecord
	db := Db.Debug().Model(&c)
	if groupId > 0 {
		db.Where("group_id=?", groupId)
	}
	if searchValue != "" {
		db = db.Where("light_control_sn like ? or lamp_pole_name like ?", "%"+searchValue+"%", "%"+searchValue+"%")
	}
	if start != "" {
		start = start + " 00:00:00"
		end = end + " 23:59:59"
		db = db.Where("start_time >=? and  start_time <= ?", start, end)
	}
	db = db.Where("is_deleted = 0")
	err := db.Order("id desc").Offset(offset).Limit(limit).Find(&LightRecords).Error
	var count int64
	db.Count(&count)
	return LightRecords, count, err
}

func (l LightRecord) Get() (LightRecord, error) {
	var lightRecord LightRecord
	err := Db.Debug().Model(&l).First(&lightRecord).Error
	return lightRecord, err
}
