package models

type IotModel struct {
	TID      uint16 `gorm:"primary_key" sql:"type:INT(11) UNSIGNED NOT NULL"` //物模型ID
	Version  uint8  `gorm:"type:int"`                                         //物模型版本
	Device   string `gorm:"type:varchar(40)"`                                 //设备名称
	Model    string `gorm:"type:varchar(18)"`                                 //设备型号
	Protocol string `gorm:"type:varchar(18)"`                                 //协议类型，一般为modbus-RTU
	File     string `gorm:"type:text"`                                        //模型文件内容
	LcModel
}

func (IotModel) TableName() string {
	return "t_iot_model"
}

//上报数据项
type IotModelDataUp struct {
	TID      uint16  `gorm:"primary_key" sql:"type:INT(11) UNSIGNED NOT NULL"` //物模型ID
	Version  uint8   `gorm:"type:int"`                                         //物模型版本
	Device   string  `gorm:"type:varchar(40)"`                                 //设备名称
	Model    string  `gorm:"type:varchar(18)"`                                 //设备型号
	Protocol string  `gorm:"type:varchar(18)"`                                 //协议类型，一般为modbus-RTU
	SID      uint16  `gorm:"primary_key" sql:"type:INT(10) UNSIGNED NOT NULL"`
	Name     string  `gorm:"type:varchar(18)"` //英文名称
	NameZh   string  `gorm:"type:varchar(18)"` //中文名称
	Unit     string  `gorm:"type:varchar(8)"`  //单位
	Endian   uint8   `gorm:"type:int"`         //大小端规则,0,大端，1小端
	Type     uint8   `gorm:"type:int"`         //数据类型，0:uint; 1:int; 2:float; 3:double;4 string
	Ratio    float32 `gorm:"type:float"`       //比率系数
	Base     float32 `gorm:"type:float"`       //基数
	UpLimit  float32 `gorm:"type:float"`       //上限，用于告警判断
	LowLimit float32 `gorm:"type:float"`       //下限，用于告警判断
	Cid      uint8   `gorm:"type:int"`         //采集命令
	Start    uint8   `gorm:"type:int"`         //从返回的数据0字节开始算起的字节数
	Len      uint8   `gorm:"type:int"`         //数据项占几个字节
	Alarm    uint8   `gorm:"type:int"`         //是否告警,0,不告警；1,告警
	Duration uint8   `gorm:"type:int"`         //持续时间才告警，防止误判
	Desc     string  `gorm:"type:varchar(40)"` //遥信用，状态描述，类似0表示开，1表示关之类的描述
	Bit      uint8   `gorm:"type:int"`         //遥信用，该字节的多少位
	LcModel
}

func (IotModelDataUp) TableName() string {
	return "t_iot_model_dataup"
}

//下控数据项
type IotModelCmdDown struct {
	TID      uint16  `gorm:"primary_key" sql:"type:INT(11) UNSIGNED NOT NULL"` //物模型ID
	Version  uint8   `gorm:"type:int"`                                         //物模型版本
	Device   string  `gorm:"type:varchar(40)"`                                 //设备名称
	Model    string  `gorm:"type:varchar(18)"`                                 //设备型号
	Protocol string  `gorm:"type:varchar(18)"`                                 //协议类型，一般为modbus-RTU
	SID      uint16  `gorm:"primary_key" sql:"type:INT(10) UNSIGNED NOT NULL"`
	Name     string  `gorm:"type:varchar(18)"`
	NameZh   string  `gorm:"type:varchar(18)"` //中文名称
	Unit     string  `gorm:"type:varchar(8)"`
	Endian   uint8   `gorm:"type:int"`   //大小端规则,0,大端，1小端
	Type     uint8   `gorm:"type:int"`   //数据类型，0:uint; 1:int; 2:float; 3:double;4 string
	Ratio    float32 `gorm:"type:float"` //比率系数
	Base     float32 `gorm:"type:float"` //基数
	Code     uint8   `gorm:"type:int"`   //功能码
	Addr     uint16  `gorm:"type:int"`   //地址
	Quantity uint16  `gorm:"type:int"`   //数量
	Vallen   uint8   `gorm:"type:int"`   //字节数
	Maxval   float32 `gorm:"type:float"`
	Minval   float32 `gorm:"type:float"`
	Desc     string  `gorm:"type:varchar(40)"`
	LcModel
}

func (IotModelCmdDown) TableName() string {
	return "t_iot_model_cmddown"
}

type IotModelInfo struct {
	ID     string
	TID    uint16
	SID    uint16
	Name   string
	NameZh string
	Unit   string
	Type   uint8
}

func GetMutilIotModelInfo(codes []string, tp int) ([]IotModelInfo, error) {
	var (
		sql0      string
		sql1      string
		maintable string
	)
	switch tp {
	case 1: //环境监测仪
		maintable = "t_device_environment"
	case 2: //单灯控制器
		maintable = "t_device_lampcontroller"
	case 3: //液位计
		maintable = "t_device_sensor"
	case 4: //路面状态传感器
		maintable = "t_device_sensor"
	default: //默认未归类的设备
		maintable = "t_gateway_device"
	}
	sql0 = "SELECT a.id,b.t_id,b.s_id,b.name,b.name_zh,b.unit,0 AS 'type' FROM " + maintable + " a,t_iot_model_dataup b WHERE a.id IN (?) AND a.t_id = b.t_id"
	sql1 = "SELECT  a.id,b.t_id,b.s_id, b.name, b.name_zh, b.unit,1 AS 'type' FROM " + maintable + " a,t_iot_model_cmddown b WHERE a.id IN (?) AND a.t_id = b.t_id"
	var arr1 []IotModelInfo
	if err := G_db.Raw(sql0, codes).Scan(&arr1).Error; err != nil {
		return nil, err
	}
	var arr2 []IotModelInfo
	if err := G_db.Raw(sql1, codes).Scan(&arr2).Error; err != nil {
		return nil, err
	}
	arr1 = append(arr1, arr2...)
	return arr1, nil
}
