package dao

import (
	"strings"
)

type CountDevice struct {
	CountNum   int    `json:"countNum"`
	DeviceType string `json:"deviceType"`
}

func GetDeviceCount(tenantId string) ([]CountDevice, error) {
	var result []CountDevice
	var count CountDevice
	Db.Debug().Model(&LampPole{}).Select(" count(*) count_num, 'lamppole' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&CameraDevice{}).Select(" count(*) count_num, 'camera' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&Gateway{}).Select(" count(*) count_num, 'gateway' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&LightControl{}).Select(" count(*) count_num, 'control' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&SwitchBox{}).Select(" count(*) count_num, 'box' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&InfoBoard{}).Select(" count(*) count_num, 'board' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&OptoSensor{}).Select(" count(*) count_num, 'sensor' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&Zigbee{}).Select(" count(*) count_num, 'zigbee' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&AlarmTerminal{}).Select(" count(*) count_num, 'terminal' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&Alarm{}).Select(" count(*) count_num, 'alarmserve' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Debug().Model(&IpBroadcast{}).Select(" count(*) count_num, 'ipcast' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	return result, nil
}

type CountAlarm struct {
	LastYearAlarm  int `json:"lastYearAlarm"`
	LastMonthAlarm int `json:"lastMonthAlarm"`
	Backlog        int `json:"backlog"`
}

func GetAlarmCount(tenantId string) (*CountAlarm, error) {
	var alarm CountAlarm
	return &alarm, nil
}

type CountJobTodo struct {
	BackJob      int `json:"backJob"`
	LastMonthJob int `json:"lastMonthJob"`
	LastYearJob  int `json:"lastYearJob"`
}

func GetCountJobTodo(tenantId string) (*CountJobTodo, error) {
	var todos CountJobTodo
	return &todos, nil
}

type Notification struct {
	Category int    `json:"category"`
	Title    string `json:"title"`
}

func GetNotification(tenantId string) (*Notification, error) {
	var notification Notification
	return &notification, nil
}

type AqiData struct {
	Name string
	Aqi  float64
}
type OptoSensorVO struct {
	OptoSensor
	Ids                   string `json:"ids"`                   //用于查询多个ID
	Brand                 string `json:"brand"`                 //品牌名称
	Model                 string `json:"model"`                 //型号名称
	RunState              string `json:"runState"`              //运行状态
	NetworkState          int    `json:"networkState"`          //网络状态
	RealTimeTemperature   string `json:"realTimeTemperature"`   //实时温度
	RealTimeWindSpeed     string `json:"realTimeWindSpeed"`     //实时风速
	AirQuality            string `json:"airQuality"`            //空气质量等级
	AirIndex              int    `json:"airIndex"`              //空气质量指数
	IlluminationIntensity string `json:"illuminationIntensity"` //光照强度
	Pm25                  string `json:"pm25"`                  //PM2.5
	Pm10                  string `json:"pm10"`                  //PM10
	Humidity              string `json:"humidity"`              //湿度
	Pressure              string `json:"pressure"`              //气压
	Noise                 string `json:"noise"`                 //噪音
	Direction             string `json:"direction"`             //风向
	EndLineTime           string `json:"endLineTime"`           //最后在线时间
	QueryGatewayIds       string `json:"queryGatewayIds"`       //虚拟字段---用作网关调用这里关联
	GatewayName           string `json:"gatewayName"`           //所属网关名称
	GatewaySn             string `json:"gatewaySn"`             //所属网关编码
	DistrictName          string `json:"districtName"`          //所在区(AQI展示)

}

func GetAqi(tenantId string) (*OptoSensorVO, error) {
	var sensorVO OptoSensorVO
	var optoSensor OptoSensor
	optoSensor.TenantId = tenantId
	optoSensor.IsDefault = 1
	isSet := len(optoSensor.GetDevicesByTenantId())
	if isSet == 0 {
		optoSensor.IsDefault = 0
	}
	list := optoSensor.getList(optoSensor)
	if isSet == 0 {
		for _, sensor := range list {
			if sensor.NetworkState == 1 {
				sensorVO = sensor
				break
			}
		}
	} else {
		if len(list) != 0 {
			sensorVO = list[0]
		}
	}
	if sensorVO.RealTimeTemperature != "" {
		temp := strings.Split(sensorVO.RealTimeTemperature, " ")
		if len(temp) == 2 {
			temperature := temp[0] + " " + temp[1]
			sensorVO.RealTimeTemperature = temperature
		}
	}
	return &sensorVO, nil
}
