package dao

import (
	"iot_manager_service/util/common"
)

type ViewsAllCode struct {
	DeviceType     int
	DeviceTypeName string
	DeviceId       int
	DeviceName     string
	Sn             string
	Address        string
	LampPoleName   string
	LampPoleSn     string
	LampPoleId     int
	InstallTime    string
	CreateTime     common.Time
	IsDeleted      int
	TenantId       string
}

func (ViewsAllCode) TableName() string {
	return "view_all_code" //此处不是真实表，是使用视图查询的
}

// GetAlls 全部数据
func (v *ViewsAllCode) GetAlls() ([]ViewsAllCode, error) {
	var viewsAllCodes []ViewsAllCode
	err := Db.Debug().Model(&viewsAllCodes).Where("tenant_id=?", v.TenantId).Find(&viewsAllCodes).Error
	return viewsAllCodes, err
}

// GetGroupByDeviceType 根据不能设备类型列出
func (v *ViewsAllCode) GetGroupByDeviceType(searchValue string) ([]ViewsAllCode, error) {
	var viewsAllCodes []ViewsAllCode
	db := Db.Debug().Model(&viewsAllCodes).Select("device_type,device_type_name").Where("is_deleted=0 and tenant_id=?", v.TenantId)
	if searchValue != "" {
		db.Where("device_name=? or sn=?", searchValue, searchValue)
	}
	err := db.Group("device_type,device_type_name").Find(&viewsAllCodes).Error
	return viewsAllCodes, err
}

// GetDatasByDeviceType 根据设备类型查设备
func (v *ViewsAllCode) GetDatasByDeviceType(searchValue string) ([]ViewsAllCode, error) {
	var viewsAllCodes []ViewsAllCode
	db := Db.Debug().Model(&viewsAllCodes).Where("is_deleted=0 and tenant_id=? and device_type=?", v.TenantId, v.DeviceType)
	if searchValue != "" {
		db.Where("device_name=? or sn=?", searchValue, searchValue)
	}
	err := db.Find(&viewsAllCodes).Error
	return viewsAllCodes, err
}
