package dao

import (
	"fmt"
	"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.Model(&LampPole{}).Select(" count(*) count_num, 'lamppole' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&CameraDevice{}).Select(" count(*) count_num, 'camera' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&Gateway{}).Select(" count(*) count_num, 'gateway' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&LightControl{}).Select(" count(*) count_num, 'control' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&SwitchBox{}).Select(" count(*) count_num, 'box' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&InfoBoard{}).Select(" count(*) count_num, 'board' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&OptoSensor{}).Select(" count(*) count_num, 'sensor' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&Zigbee{}).Select(" count(*) count_num, 'zigbee' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&AlarmTerminal{}).Select(" count(*) count_num, 'terminal' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.Model(&Alarm{}).Select(" count(*) count_num, 'alarmserve' device_type ").
		Where("tenant_id = ? and is_deleted = 0", tenantId).Scan(&count)
	result = append(result, count)
	Db.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 DeviceState struct {
	DeviceType string `json:"deviceType"` //设备类型
	Name       string `json:"name"`       //设备名称
	Sn         string `json:"sn"`         //设备序列号
}

// 获取设备离线的状态
func GetNotification(tenantId string) ([]DeviceState, error) {
	var result []DeviceState
	//灯杆设备
	Db.Model(&LampPole{}).Select(" pole_name as  name, pole_sn as sn,status,'灯杆设备' device_type").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&result)
	result = append(result)
	////摄像机
	var camera []DeviceState
	Db.Model(&CameraDevice{}).Select(" device_name as name,device_sn as sn,'摄像机' device_type ").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&camera)
	result = append(result, camera...)
	//网关
	var gateway []DeviceState
	Db.Model(&Gateway{}).Select(" gateway_name as name ,gateway_sn as sn,'网关' device_type ").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&gateway)
	result = append(result, gateway...)
	//灯控
	var light []DeviceState
	Db.Model(&LightControl{}).Select(" name,sn,'灯控' device_type").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&light)
	result = append(result, light...)
	//供电箱
	var box []DeviceState
	Db.Model(&SwitchBox{}).Select(" box_name as name ,box_sn as sn,'供电箱' device_type ").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&box)
	result = append(result, box...)
	//信息基本屏
	var info []DeviceState
	Db.Model(&InfoBoard{}).Select(" 'info_name as name,sn,'信息基本屏' device_type ").
		Where("  tenant_id = ? and is_enable!=1", tenantId).Find(&info)
	result = append(result, info...)
	//集控器
	var sensor []DeviceState
	Db.Model(&OptoSensor{}).Select(" name,sn,'集控器' device_type ").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&sensor)
	result = append(result, sensor...)
	//Zigbee集控器
	var zig []DeviceState
	Db.Model(&Zigbee{}).Select("name,sn,'Zigbee集控器' device_type").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&zig)
	result = append(result, zig...)
	//一键求助终端
	var terminal []DeviceState
	Db.Model(&AlarmTerminal{}).Select(" terminal_name as name ,terminal_sn as sn,'一键求助终端' device_type").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&terminal)
	result = append(result, terminal...)
	//一键求助管理机
	var alarm []DeviceState
	Db.Model(&Alarm{}).Select("serve_name as name ,serve_sn as sn,'一键求助管理机' device_type").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&alarm)
	result = append(result, alarm...)
	//ip音柱
	var cast []DeviceState
	Db.Model(&IpBroadcast{}).Select("cast_name as name,cast_sn as sn,'ip音柱' device_type ").
		Where("tenant_id = ? and  is_deleted!=0", tenantId).Find(&cast)
	result = append(result, cast...)
	//桥梁传感器
	var bridge []DeviceState
	Db.Model(&BridgeSensor{}).Select("name,sn,'桥梁传感器' device_type").
		Where("tenant_id = ? and status!=-1", tenantId).Find(&bridge)
	result = append(result, bridge...)
	fmt.Println(result)
	return result, 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展示)

}

// 获取aqi
func GetAqi(tenantId string) (*OptoSensorVO, error) {
	var sensorVO OptoSensorVO                       // 声明一个OptoSensorVO类型的变量sensorVO
	var optoSensor OptoSensor                       // 声明一个OptoSensor类型的变量optoSensor
	optoSensor.TenantId = tenantId                  // 将传入的tenantId赋值给optoSensor的TenantId字段
	optoSensor.IsDefault = 1                        // 将optoSensor的IsDefault字段设置为1
	isSet := len(optoSensor.GetDevicesByTenantId()) // 调用optoSensor的GetDevicesByTenantId方法获取设备数量,并将结果赋值给isSet
	if isSet == 0 {                                 // 如果isSet为0,即没有设备
		optoSensor.IsDefault = 0 // 将optoSensor的IsDefault字段设置为0
	}
	list := optoSensor.getList(optoSensor) // 调用optoSensor的getList方法获取设备列表,并将结果赋值给list
	if isSet == 0 {                        // 如果isSet为0,即没有设备
		for _, sensor := range list { // 遍历设备列表
			if sensor.NetworkState == 1 { // 如果设备的NetworkState字段为1
				sensorVO = sensor // 将该设备赋值给sensorVO
				break             // 跳出循环
			}
		}
	} else { // 如果isSet不为0,即有设备
		if len(list) != 0 { // 如果设备列表不为空
			sensorVO = list[0] // 将设备列表的第一个设备赋值给sensorVO
		}
	}
	if sensorVO.RealTimeTemperature != "" { // 如果sensorVO的RealTimeTemperature字段不为空
		temp := strings.Split(sensorVO.RealTimeTemperature, " ") // 使用空格将RealTimeTemperature字段拆分成字符串数组temp
		if len(temp) == 2 {                                      // 如果拆分后的数组长度为2
			temperature := temp[0] + " " + temp[1]     // 将temp数组的第一个元素和第二个元素拼接成字符串,并赋值给temperature
			sensorVO.RealTimeTemperature = temperature // 将temperature赋值给sensorVO的RealTimeTemperature字段
		}
	}
	return &sensorVO, nil // 返回sensorVO的地址和nil作为错误值
}