package Device import ( "Cold_Api/conf" "Cold_Api/controllers/lib" "Cold_Api/models/Account" "encoding/json" "fmt" "github.com/astaxie/beego/cache" _ "github.com/astaxie/beego/cache/redis" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" _ "github.com/go-sql-driver/mysql" "strconv" "strings" "time" ) // 模板 type DeviceSensor struct { Id int `orm:"column(ID);size(11);auto;pk"` T_sn string `orm:"index;size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int `orm:"index;size(11);null"` // 传感器编号 T_sp int `orm:"size(11);null"` // 传感器参数id T_name string `orm:"size(256);null"` // 标题 T_pid int `orm:"index;size(256);null"` // Account.Company 绑定公司 T_Class string `orm:"size(256);null"` // Device.DeviceClass.Id 设备分类 C1|C2| T_Notice string `orm:"size(256);null"` // 通知绑定 T_datashow int `orm:"size(2);default(1)"` // 0 屏蔽数据展示 1 正常数据展示 (屏蔽后 数据展示无法看到,设备管理中 不受影响) T_sort int `orm:"size(200);default(1)"` // 排序 T_3dview string `orm:"size(256);null"` // 3D 视图ID T_type int `orm:"size(4);null"` // Device.DeviceSensorType 1库房 2移动 T_link int `orm:"size(4);default(1)"` // 0:断开/故障 1连接 实时数据 T_State int `orm:"index;size(2);default(1)"` // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到) // 设备同步参数 T_Dattery int `orm:"size(4);null"` // 电量 T_Site string `orm:"size(200);null"` // GPS T_monitor int `orm:"size(2);null"` // 监控状态 0 未监控 1 监控 T_online int `orm:"size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线 T_online_s int `orm:"size(2);default(0)"` // 在线状态-备用 0 未启用 1 在线 2 离线 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now_add 第一次保存时才设置时间 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now 每次 model 保存时都会对时间自动更新 } type DeviceSensor_Del struct { T_sn string T_id int } func (t *DeviceSensor) TableName() string { return "device_sensor" // 数据库名称 // ************** 替换 FormulaList ************** } // 模板 type DeviceSensor_R struct { T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int // 传感器编号 T_name string // 标题 T_pid int // 公司id T_pid_name string // 公司名称 T_3dview string // 3D 视图ID T_sort int // 排序 T_Dattery int // 电量 T_Site string // GPS T_monitor int // 记录状态 T_online int // 在线状态 1 在线 0 离线 T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线 T_datashow int // 0 屏蔽数据展示 1 正常数据展示 T_type int // 类型 T_link int // 0:断开/故障 1连接 实时数据 T_State int // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到) T_DeviceSensorData DeviceData_R // 传感器最新数据 T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数 T_DeviceSensorType DeviceSensorType_R // 传感器类型 } type DeviceSensor_Applet struct { T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int // 传感器编号 T_name string // 标题 T_pid int // 公司id T_pid_name string // 公司名称 T_3dview string // 3D 视图ID T_sort int // 排序 T_Dattery int // 电量 T_Site string // GPS T_monitor int // 记录状态 T_online int // 在线状态 1 在线 0 离线 T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线 T_datashow int // 0 屏蔽数据展示 1 正常数据展示 T_type int // 1库房 2移动 T_link int // 0:断开/故障 1连接 实时数据 T_State int // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到) T_DeviceSensorData DeviceData_R // 传感器最新数据 T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数 T_DeviceSensorType DeviceSensorType_R // 传感器类型 T_Device Device_R // 传感器类型 } type DeviceSensor_RealTime struct { T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int // 传感器编号 T_name string // 标题 T_pid int // 公司id T_pid_name string // 公司名称 T_3dview string // 3D 视图ID T_sort int // 排序 T_Dattery int // 电量 T_Site string // GPS T_monitor int // 记录状态 T_online int // 在线状态 1 在线 0 离线 T_online_s int // 在线状态-备用 0 未启用 1 在线 2 离线 T_datashow int // 0 屏蔽数据展示 1 正常数据展示 T_type int // 类型 T_link int // 0:断开/故障 1连接 实时数据 T_State int // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到) // 传感器参数 // 报警 T_Tlower float32 // 温度下限 T_Tupper float32 // 温度上限 T_RHlower float32 // 湿度下限 T_RHupper float32 // 湿度上限 T_en int // en:是否启用传感器, T_free int // free:监测点是否为闲置状态(空库,只监测不报警) } // 传感器管理 type DeviceSensor_P struct { T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int // 传感器编号 T_name string // 标题 T_type int // 类型 T_type_name string // 类型 T_sort int // 排序 T_datashow int // 0 屏蔽数据展示 1 正常数据展示 T_State int // 0 屏蔽 1 正常 // DeviceSensorParameter T_Tlower *float32 // 温度下限 T_Tupper *float32 // 温度上限 T_RHlower *float32 // 湿度下限 T_RHupper *float32 // 湿度上限 T_enprel *int // 是否启用预警 1开启 0 关闭 T_tprel *float32 // 温度预警下限 T_tpreu *float32 // 温度预警上限 T_hprel *float32 // 湿度预警下限 T_hpreu *float32 // 温度预警上限 T_en *int // en:是否启用传感器, T_free *int // free:监测点是否为闲置状态(空库,只监测不报警) } type DeviceSensor_ struct { T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机 T_id int // 传感器编号 T_name string // 标题 } var redisCache_DeviceSensor cache.Cache func init() { //注册模型 orm.RegisterModel(new(DeviceSensor)) config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`, "redis_DeviceSensor", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password) fmt.Println(config) var err error redisCache_DeviceSensor, err = cache.NewCache("redis", config) if err != nil || redisCache_DeviceSensor == nil { errMsg := "failed to init redis" logs.Error(errMsg, err) panic(errMsg) } } // ---------------- Redis ------------------- // Redis_Device_Set(m.T_sn,m) // Redis 更新缓存 func Redis_DeviceSensor_Set(r DeviceSensor) (err error) { key := r.T_sn + "|" + strconv.Itoa(r.T_id) //json序列化 str, err := json.Marshal(r) if err != nil { logs.Error(lib.FuncName(), err) return } err = redisCache_DeviceSensor.Put(key, str, 24*time.Hour) if err != nil { logs.Error("set key:", key, ",value:", str, err) } return } func Redis_DeviceSensor_Get(key string) (r DeviceSensor, is bool) { if redisCache_DeviceSensor.IsExist(key) { //println("找到key:",key) v := redisCache_DeviceSensor.Get(key) err := json.Unmarshal(v.([]byte), &r) if err != nil { logs.Error(lib.FuncName(), err) return DeviceSensor{}, false } return r, true } return DeviceSensor{}, false } func Redis_DeviceSensor_DelK(r DeviceSensor) (err error) { key := r.T_sn + "|" + strconv.Itoa(r.T_id) err = redisCache_DeviceSensor.Delete(key) if err != nil { logs.Error(lib.FuncName(), err) } return } // ---------------- 特殊方法 ------------------- func DeviceSensorToDeviceSensor_R(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_R) { device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn) //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据 lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据 DeviceSensor_r.T_sn = DeviceSensor_.T_sn DeviceSensor_r.T_id = DeviceSensor_.T_id DeviceSensor_r.T_name = DeviceSensor_.T_name DeviceSensor_r.T_pid = DeviceSensor_.T_pid DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid) DeviceSensor_r.T_Site = DeviceSensor_.T_Site DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview DeviceSensor_r.T_type = DeviceSensor_.T_type DeviceSensor_r.T_sort = DeviceSensor_.T_sort DeviceSensor_r.T_link = DeviceSensor_.T_link DeviceSensor_r.T_State = DeviceSensor_.T_State DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery DeviceSensor_r.T_online = DeviceSensor_.T_online DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor //if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 { // DeviceSensor_r.T_monitor = 2 //} // 最新系统参数 DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id) // 最新数据 //key_data := DeviceSensor_.T_sn + "|" + strconv.Itoa(DeviceSensor_.T_id) //DeviceSensor_r.T_DeviceSensorData, _ = RedisDeviceData_Get(key_data) DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id) // 获取传感器数据 DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData) // 获取传感器类型数据 DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(DeviceSensor_.T_type) return } func DeviceSensorRealTimeToDeviceSensor_R(DeviceSensor_ DeviceSensor_RealTime) (DeviceSensor_r DeviceSensor_R) { device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn) //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据 lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据 DeviceSensor_r.T_sn = DeviceSensor_.T_sn DeviceSensor_r.T_id = DeviceSensor_.T_id DeviceSensor_r.T_name = DeviceSensor_.T_name DeviceSensor_r.T_pid = DeviceSensor_.T_pid DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid) DeviceSensor_r.T_Site = DeviceSensor_.T_Site DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview DeviceSensor_r.T_type = DeviceSensor_.T_type DeviceSensor_r.T_sort = DeviceSensor_.T_sort DeviceSensor_r.T_link = DeviceSensor_.T_link DeviceSensor_r.T_State = DeviceSensor_.T_State DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery DeviceSensor_r.T_online = DeviceSensor_.T_online DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor // 传感器参数 DeviceSensor_r.T_DeviceSensorParameter.T_Tlower = DeviceSensor_.T_Tlower DeviceSensor_r.T_DeviceSensorParameter.T_Tupper = DeviceSensor_.T_Tupper DeviceSensor_r.T_DeviceSensorParameter.T_RHlower = DeviceSensor_.T_RHlower DeviceSensor_r.T_DeviceSensorParameter.T_RHupper = DeviceSensor_.T_RHupper DeviceSensor_r.T_DeviceSensorParameter.T_en = DeviceSensor_.T_en DeviceSensor_r.T_DeviceSensorParameter.T_free = DeviceSensor_.T_free DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id) DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData) DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(DeviceSensor_.T_type) return } func DeviceSensorToDeviceSensor_Applet(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_Applet) { device, _ := Read_Device_ByT_sn(DeviceSensor_.T_sn) //lib.DeviceRealSnMap[DeviceSensor_.T_sn] = 3 // 连续请求 实时数据 lib.DeviceRealSnMap.Store(DeviceSensor_.T_sn+"|"+device.T_mqttid, 0) // 连续请求 实时数据 DeviceSensor_r.T_sn = DeviceSensor_.T_sn DeviceSensor_r.T_id = DeviceSensor_.T_id DeviceSensor_r.T_name = DeviceSensor_.T_name DeviceSensor_r.T_pid = DeviceSensor_.T_pid DeviceSensor_r.T_pid_name = Account.Read_Company_Get(DeviceSensor_.T_pid) DeviceSensor_r.T_Site = DeviceSensor_.T_Site DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor DeviceSensor_r.T_3dview = DeviceSensor_.T_3dview DeviceSensor_r.T_type = DeviceSensor_.T_type DeviceSensor_r.T_sort = DeviceSensor_.T_sort DeviceSensor_r.T_link = DeviceSensor_.T_link DeviceSensor_r.T_State = DeviceSensor_.T_State DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow DeviceSensor_r.T_Dattery = DeviceSensor_.T_Dattery DeviceSensor_r.T_online = DeviceSensor_.T_online DeviceSensor_r.T_online_s = DeviceSensor_.T_online_s DeviceSensor_r.T_monitor = DeviceSensor_.T_monitor //if DeviceSensor_.T_online == 2 && (DeviceSensor_.T_online_s == 0 || DeviceSensor_.T_online_s == 2) && DeviceSensor_.T_monitor == 1 { // DeviceSensor_r.T_monitor = 2 //} // 最新系统参数 DeviceSensor_r.T_DeviceSensorParameter, _ = Read_DeviceSensorParameter(DeviceSensor_.T_sn, DeviceSensor_.T_id) // 最新数据 DeviceData := Read_DeviceData(DeviceSensor_.T_sn, DeviceSensor_.T_id) DeviceSensor_r.T_DeviceSensorData = DeviceDataToDeviceData_R2(device, DeviceSensor_r.T_DeviceSensorParameter, DeviceData) DeviceSensor_r.T_DeviceSensorType = DeviceSensorTypeToDeviceSensorType_R(DeviceSensor_.T_type) DeviceSensor_r.T_Device = DeviceToDevice_R(device) return } func DeviceSensorToDeviceSensor_(r DeviceSensor) (t DeviceSensor_) { t.T_sn = r.T_sn t.T_id = r.T_id t.T_name = r.T_name return } func DeviceSensorToDeviceSensor_P(r DeviceSensor_P) (t DeviceSensor_P) { t = r if r.T_type == 0 { t.T_type_name = "默认" } if r.T_type > 0 { deviceSensorType := Read_DeviceSensorType_Get(r.T_type) t.T_type_name = deviceSensorType.T_name } return t } // ---------------- 特殊方法 ------------------- // 获取列表 - 总数 func Read_DeviceSensor_Num_ByT_sn(T_sn string) int { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) cnt, err := qs.Filter("T_sn", T_sn).Count() if err != nil { logs.Error(lib.FuncName(), err) return 0 } return int(cnt) } // 获取列表 func Read_DeviceSensorList(admin_r *Account.Admin, bindSN []string, T_pid int, T_sn string, T_name string, T_Class_id, T_datashow int, T_type string, T_State int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid", T_pid) if T_Class_id > 0 { T_Class := "C" + strconv.Itoa(T_Class_id) + "|" cond1 = cond1.And("T_Class__icontains", T_Class) } if len(bindSN) > 0 { cond1 = cond1.And("T_sn__in", bindSN) } if len(T_type) > 0 { list := strings.Split(T_type, ",") cond1 = cond1.And("T_type__in", list) } if len(T_sn) > 0 { cond1 = cond1.And("T_sn__icontains", T_sn) } else { // 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常 if admin_r.T_pid > 0 || T_State == 1 { cond1 = cond1.And("T_State", 1) } } if len(T_name) > 0 { cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)) } if T_datashow == 1 { // 0 屏蔽数据展示 1 正常数据展示 cond1 = cond1.And("T_datashow", 1) } var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) } if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } // 获取 func Read_DeviceSensor_ByT_sn(T_sn string, T_id int) (r DeviceSensor, is bool) { key := r.T_sn + "|" + strconv.Itoa(r.T_id) if r, is := Redis_DeviceSensor_Get(key); is { return r, true } o := orm.NewOrm() r = DeviceSensor{T_sn: T_sn, T_id: T_id} err := o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { logs.Error(lib.FuncName(), err) return r, false } Redis_DeviceSensor_Set(r) return r, true } // 删除 func Delete_DeviceSensor_ById(T_sn string, T_id int) (err error) { o := orm.NewOrm() r := DeviceSensor{T_sn: T_sn, T_id: T_id} err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { logs.Error("Delete_DeviceSensor_ById", err) return err } _, err = o.Delete(&DeviceSensor{Id: r.Id}) if err != nil { logs.Error(lib.FuncName(), err) return err } Redis_DeviceSensor_DelK(r) return } // 修改 func Update_DeviceSensor(r DeviceSensor, cols ...string) bool { o := orm.NewOrm() num, err := o.Update(&r, cols...) if err != nil { logs.Error(lib.FuncName(), err) return false } logs.Info("Number of records updated in database:", num) Redis_DeviceSensor_Set(r) return true } // 修改T_Class,替换为"" func DeviceSensor_Bind_T_Class_Del(T_sn string, T_id int, T_Class_id int) (err error) { o := orm.NewOrm() v := DeviceSensor{T_sn: T_sn, T_id: T_id} T_Class := "C" + strconv.Itoa(T_Class_id) + "|" err = o.Read(&v, "T_sn", "T_id") if err != nil { logs.Error(lib.FuncName(), err) return err } v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1) _, err = o.Update(&v, "T_Class") if err != nil { logs.Error(lib.FuncName(), err) return err } Redis_DeviceSensor_Set(v) return nil } // 修改T_Class,追加 func DeviceSensor_Bind_T_Class_Add(T_sn string, T_id int, T_Class_id int) (err error) { o := orm.NewOrm() v := DeviceSensor{T_sn: T_sn, T_id: T_id} T_Class := "C" + strconv.Itoa(T_Class_id) + "|" err = o.Read(&v, "T_sn", "T_id") if err != nil { logs.Error(lib.FuncName(), err) return err } v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1) v.T_Class = v.T_Class + T_Class _, err = o.Update(&v, "T_Class") if err != nil { logs.Error(lib.FuncName(), err) return err } Redis_DeviceSensor_Set(v) return nil } // 获取 ById func Read_DeviceSensor_ByTsn_Tid(T_sn string, T_id int) (r DeviceSensor, err error) { o := orm.NewOrm() r = DeviceSensor{T_sn: T_sn, T_id: T_id} err = o.Read(&r, "T_sn", "T_id") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { logs.Error(lib.FuncName(), err) } return r, err } // 获取列表 func Read_DeviceSensor_ByTsn(T_sn string) ([]DeviceSensor, int) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var r []DeviceSensor _, err := qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return r, 0 } cnt, err := qs.Filter("T_sn", T_sn).Count() if err != nil { logs.Error(lib.FuncName(), err) return r, 0 } return r, int(cnt) } // 获取列表 func Read_DeviceSensor_List_T_Class(T_pid int, T_Class_id int, T_sn string, T_name string, T_type int, page int, page_z int) (DeviceSensor_r []DeviceSensor_, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } T_Class := "" if T_Class_id != 0 { T_Class = "C" + strconv.Itoa(T_Class_id) + "|" } var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Class__icontains", T_Class).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000) if T_type > 0 { cond1 = cond1.AndCond(cond.And("T_type", T_type)) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v)) } return DeviceSensor_r, cnt } // 获取列表 func Read_DeviceSensor_ALL_T_sn_T_id_T_Class(T_sn string, T_id int, T_Class_id int) (r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) T_Class := "C" + strconv.Itoa(T_Class_id) + "|" _, err := qs.Filter("T_Class__icontains", T_Class).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } // 获取列表 func Read_DeviceSensor_ALL_List_T_sn(T_sn string) (r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) _, err := qs.Filter("T_sn", T_sn).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } func Read_DeviceSensor_List_T_sn_T_datashow(T_sn string) (r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) _, err := qs.Filter("T_sn", T_sn).Filter("T_State", 1).Filter("T_datashow", 1).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } // 获取列表 func Read_DeviceSensor_ALL_Class_Id(T_Class_id int) (r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) T_Class := "C" + strconv.Itoa(T_Class_id) + "|" _, err := qs.Filter("T_Class__icontains", T_Class).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } func DELETE_DeviceSensor(SN string) bool { sql := "DELETE FROM `cold`.`device_sensor` WHERE `t_sn` = '" + SN + "' " o := orm.NewOrm() _, err := o.Raw(sql).Exec() if err != nil { logs.Error(lib.FuncName(), err) return false } return true } // 报警策略 ------------------- // 通过T_sn T_id T_Notice_id 获取全部列表 func Read_DeviceSensor_ALL_T_sn_T_id_T_Notice(T_sn string, T_id int, T_Notice_id int) (r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|" _, err := qs.Filter("T_Notice__icontains", T_Notice).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } // 修改T_Notice,替换为"" func DeviceSensor_Bind_T_Notice_Del(T_sn string, T_id int, T_Notice_id int) (err error) { o := orm.NewOrm() v := DeviceSensor{T_sn: T_sn, T_id: T_id} T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|" err = o.Read(&v, "T_sn", "T_id") if err != nil { logs.Error(lib.FuncName(), err) return } v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1) _, err = o.Update(&v, "T_Notice") if err != nil { logs.Error(lib.FuncName(), err) return } Redis_DeviceSensor_Set(v) return nil } // 修改T_Notice,追加 func DeviceSensor_Bind_T_Notice_Add(T_sn string, T_id int, T_Notice_id int) (err error) { o := orm.NewOrm() v := DeviceSensor{T_sn: T_sn, T_id: T_id} T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|" err = o.Read(&v, "T_sn", "T_id") if err != nil { logs.Error(lib.FuncName(), err) return } v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1) v.T_Notice = v.T_Notice + T_Notice _, err = o.Update(&v, "T_Notice") if err != nil { logs.Error(lib.FuncName(), err) return } Redis_DeviceSensor_Set(v) return nil } // 获取列表 func Read_DeviceSensor_ALL_Notice_Id(T_Notice_id int) (r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|" _, err := qs.Filter("T_Notice__icontains", T_Notice).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } return r } // 获取列表 func Read_DeviceSensor_List_T_Notice(T_pid int, T_Notice_id int, page int, page_z int, T_sn string, T_name string, T_type int) (DeviceSensor_r []DeviceSensor_, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } T_Notice := "" if T_Notice_id != 0 { T_Notice = "N" + strconv.Itoa(T_Notice_id) + "|" } var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid", T_pid).And("T_State", 1).And("T_Notice__icontains", T_Notice).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000) if T_type > 0 { cond1 = cond1.AndCond(cond.And("T_type", T_type)) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_(v)) } return DeviceSensor_r, cnt } // 设备同步参数 func Update_Device_To_DeviceSensor(r_Device Device) bool { DeviceSensor_list, _ := Read_DeviceSensor_ByTsn(r_Device.T_sn) for _, v := range DeviceSensor_list { // 设备同步参数 v.T_Dattery = r_Device.T_Dattery // 电量 v.T_Site = r_Device.T_Site // GPS v.T_monitor = r_Device.T_monitor // 监控状态 0 未监控 1 监控 v.T_online = r_Device.T_online // 在线状态 0 未启用 1 在线 2 离线 v.T_online_s = r_Device.T_online_s // 在线状态-备用 0 未启用 1 在线 2 离线 v.T_State = r_Device.T_State // 0 屏蔽 1 正常 (屏蔽后 只有内部管理员才能看到,用户 输入SN\名称 搜索时 也能看到) Update_DeviceSensor(v, "T_Dattery", "T_Site", "T_type", "T_State", "T_monitor", "T_online", "T_online_s") } return true } // 传感器管理列列表 ------------------- // 与Read_DeviceSensorManageList功能一样(优化前) func Read_DeviceSensorManageList1(admin_r *Account.Admin, bindSN []string, T_pid int, T_name string, T_calss_id, T_en, T_free, T_datashow, T_sort, page, page_z int) (DeviceSensor_p []DeviceSensor_P, cnt int64) { o := orm.NewOrm() var maps []DeviceSensor_P var maps_z []orm2.ParamsList var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } sql_WHERE := "t_pid = " + strconv.Itoa(T_pid) if len(bindSN) > 0 { sql_WHERE += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN)) } if admin_r.T_pid > 0 { sql_WHERE += " AND t__state = 1" } if len(T_name) > 0 { sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%')" } if T_calss_id > 0 { T_calss := "C" + strconv.Itoa(T_calss_id) + "|" sql_WHERE += " AND t__class like '%" + T_calss + "%'" } if T_en != -1 { sql_WHERE += " AND t_en = " + strconv.Itoa(T_en) } if T_free != -1 { sql_WHERE += " AND t_free = " + strconv.Itoa(T_free) } if T_datashow != -1 { sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow) } sql_ORDER := " ORDER BY t_sort ASC" if T_sort == 1 { sql_ORDER = " ORDER BY t_sort DESC" } // ------------- sql := "SELECT COUNT(ds.ID) FROM " + "device_sensor ds WHERE " + sql_WHERE logs.Debug(sql) _, err := o.Raw(sql).ValuesList(&maps_z) if err != nil { logs.Error(lib.FuncName(), err) return DeviceSensor_p, 0 } if len(maps_z) == 0 { return DeviceSensor_p, 0 } sql = "SELECT * FROM device_sensor ds " + "LEFT JOIN ( SELECT t_sn AS tsn,t_id AS tid,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu " + "FROM device_sensor_parameter WHERE id IN (SELECT MAX(id) FROM device_sensor_parameter WHERE t__state=1 GROUP BY t_sn,t_id)) AS dsp " + "ON ds.t_sn = dsp.tsn AND ds.t_id = dsp.tid " + "WHERE " + sql_WHERE + sql_ORDER if page_z != 9999 { sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z) } logs.Debug(sql) _, err = o.Raw(sql).QueryRows(&maps) if err != nil { logs.Error(lib.FuncName(), err) return } key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64) for _, v := range maps { DeviceSensor_p = append(DeviceSensor_p, DeviceSensorToDeviceSensor_P(v)) } return DeviceSensor_p, key } // 使用联表查询 func Read_DeviceSensorManageList(admin_r *Account.Admin, bindSN []string, T_pid int, T_name string, T_calss_id, T_en, T_free, T_datashow, T_sort, T_abandon, page, page_z int) (DeviceSensor_p []DeviceSensor_P, cnt int64) { o := orm.NewOrm() var maps []DeviceSensor_P var maps_z []orm2.ParamsList var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } sql_WHERE := "t_pid = " + strconv.Itoa(T_pid) if len(bindSN) > 0 { sql_WHERE += fmt.Sprintf(" AND ds.t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN)) } //if admin_r.T_pid > 0 { // sql_WHERE += " AND ds.t__state = 1" //} if T_abandon == 1 { sql_WHERE += " AND ds.t__state = 0" } else { sql_WHERE += " AND ds.t__state = 1" } if len(T_name) > 0 { sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%')" } if T_calss_id > 0 { T_calss := "C" + strconv.Itoa(T_calss_id) + "|" sql_WHERE += " AND t__class like '%" + T_calss + "%'" } if T_en != -1 { sql_WHERE += " AND t_en = " + strconv.Itoa(T_en) } if T_free != -1 { sql_WHERE += " AND t_free = " + strconv.Itoa(T_free) } if T_datashow != -1 { sql_WHERE += " AND t_datashow =" + strconv.Itoa(T_datashow) } sql_ORDER := " ORDER BY ds.t_sort ASC,ds.t_id,ds.id" if T_sort == 1 { sql_ORDER = " ORDER BY ds.t_sort DESC,ds.t_id,ds.id" } // ------------- sql := "SELECT COUNT(ds.ID) FROM " + "device_sensor ds " + "LEFT JOIN device_sensor_parameter dsp " + "ON ds.t_sp=dsp.id " + "WHERE " + sql_WHERE + sql_ORDER logs.Debug(sql) _, err := o.Raw(sql).ValuesList(&maps_z) if err != nil { logs.Error(lib.FuncName(), err) return DeviceSensor_p, 0 } if len(maps_z) == 0 { return DeviceSensor_p, 0 } //"LEFT JOIN (SELECT id,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu " + //"FROM device_sensor_parameter) dsp " + sql = "SELECT ds.t_sn,ds.t_id,ds.t_name,t_type,t_sort,t_datashow,ds.t__state,t_en,t_free,t__tlower,t__tupper,t__r_hlower,t__r_hupper,t_enprel,t_tprel,t_tpreu,t_hprel,t_hpreu FROM device_sensor ds " + "LEFT JOIN device_sensor_parameter dsp " + "ON ds.t_sp=dsp.id " + "WHERE " + sql_WHERE + sql_ORDER if page_z != 9999 { sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z) } logs.Debug(sql) _, err = o.Raw(sql).QueryRows(&maps) if err != nil { logs.Error(lib.FuncName(), err) return } key, _ := strconv.ParseInt(maps_z[0][0].(string), 10, 64) for _, v := range maps { DeviceSensor_p = append(DeviceSensor_p, DeviceSensorToDeviceSensor_P(v)) } return DeviceSensor_p, key } // 数据展示菜单下 传感器参数列表 // 实时数据页面 温湿度异常数据往前排 // 轨迹展示页面 func Read_DeviceSensor_List_For_Data(bindSN []string, T_pid int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow, T_abandon int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid", T_pid) if len(bindSN) > 0 { cond1 = cond1.And("T_sn__in", bindSN) } if T_Class_id > 0 { T_Class := "C" + strconv.Itoa(T_Class_id) + "|" cond1 = cond1.And("T_Class__icontains", T_Class) } if T_type > 0 { cond1 = cond1.And("T_type", T_type) } if T_abandon == 1 { cond1 = cond1.And("T_State", 0) } else { cond1 = cond1.And("T_State", 1) } //if len(T_name) >= 16 { // cond1 = cond1.And("T_sn", T_name) //} else if len(T_name) > 0 && len(T_name) < 16 { // cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1) //} else { // cond1 = cond1.And("T_State", 1).And("T_datashow", 1) //} if len(T_name) >= 16 { cond1 = cond1.And("T_sn", T_name) } else if len(T_name) > 0 && len(T_name) < 16 { cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_datashow", 1) } else { cond1 = cond1.And("T_datashow", 1) } if T_MapShow == 1 { cond1 = cond1.AndNot("T_Site", "") } cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } // 实时数据页面,温度或者湿度不在正常区间,排在最前面 if T_RealTime == 1 { offset_z := offset + page_z if cnt < int64(offset_z) { offset_z = int(cnt) } if offset > offset_z { offset = offset_z } _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id", "Id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } var DeviceSensor_unusual []DeviceSensor_R var DeviceSensor_monitor []DeviceSensor_R for _, v := range r { v_r := DeviceSensorToDeviceSensor_R(v) data := v_r.T_DeviceSensorData // 状态在监控中,温度或者湿度不在正常区间,排在最前面 if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v.T_link == 1 && v_r.T_DeviceSensorParameter.T_free == 0 { DeviceSensor_unusual = append(DeviceSensor_unusual, v_r) } else if v.T_monitor == 1 && v.T_link == 1 && v_r.T_DeviceSensorParameter.T_free == 0 { DeviceSensor_monitor = append(DeviceSensor_monitor, v_r) } else { DeviceSensor_r = append(DeviceSensor_r, v_r) } } DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_monitor...) DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...) return DeviceSensor_unusual[offset:offset_z], cnt } _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id", "Id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } // 与Read_DeviceSensor_List_For_Data功能一样(优化前) func Read_DeviceSensor_List_For_Data1(bindSN []string, T_pid int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() var maps_z []orm2.ParamsList // 也可以直接使用 Model 结构体作为表名 var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } var r []DeviceSensor_RealTime sql_WHERE := "t_pid = " + strconv.Itoa(T_pid) if len(bindSN) > 0 { sql_WHERE += fmt.Sprintf(" AND t_sn in (%s) ", lib.StringListToDotStr(bindSN)) } if T_Class_id > 0 { T_calss := "C" + strconv.Itoa(T_Class_id) + "|" sql_WHERE += " AND t__class like '%" + T_calss + "%'" } if T_type > 0 { sql_WHERE += " AND t_type = " + strconv.Itoa(T_type) } if len(T_name) >= 16 { sql_WHERE += " AND t_sn = '" + T_name + "'" } else if len(T_name) > 0 && len(T_name) < 16 { sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%') AND ds.t__state = 1 AND t_datashow = 1" } else { sql_WHERE += " AND ds.t__state = 1 AND t_datashow = 1" } if T_MapShow == 1 { sql_WHERE += " AND t__site is not null" } // ------------- sql := "SELECT COUNT(*) FROM " + "device_sensor ds WHERE " + sql_WHERE logs.Debug(sql) _, err := o.Raw(sql).ValuesList(&maps_z) if err != nil { logs.Error(lib.FuncName(), err) return DeviceSensor_r, 0 } if len(maps_z) == 0 { return DeviceSensor_r, 0 } cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64) sql = "SELECT *,ds.t_sn as t_sn,ds.t_id as t_id,ds.t_name as t_name,ds.t__state as t__state FROM device_sensor ds " + "LEFT JOIN device_sensor_parameter dsp " + "ON ds.t_sp=dsp.id " + "WHERE " + sql_WHERE + " ORDER BY ds.t_sort, ds.id ASC" realTimeSql := sql if page_z != 9999 { sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z) } logs.Debug(sql) _, err = o.Raw(sql).QueryRows(&r) if err != nil { logs.Error(lib.FuncName(), err) return } if T_RealTime == 1 { offset_z := offset + page_z if cnt < int64(offset_z) { offset_z = int(cnt) } if offset > offset_z { offset = offset_z } _, err = o.Raw(realTimeSql).QueryRows(&r) if err != nil { logs.Error(lib.FuncName(), err) return } var DeviceSensor_unusual []DeviceSensor_R for _, v := range r { v_r := DeviceSensorRealTimeToDeviceSensor_R(v) data := v_r.T_DeviceSensorData // 温度或者湿度不在正常区间,排在最前面 if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v_r.T_DeviceSensorParameter.T_free == 0 { DeviceSensor_unusual = append(DeviceSensor_unusual, v_r) } else { DeviceSensor_r = append(DeviceSensor_r, v_r) } } DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...) return DeviceSensor_unusual[offset:offset_z], cnt } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorRealTimeToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } // 公司管理 实时数据 // 查询当前公司及其下面所有子公司的传感器列表 func Read_CompanyDeviceSensor_List_For_Data(T_pids []int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid__in", T_pids) if T_Class_id > 0 { T_Class := "C" + strconv.Itoa(T_Class_id) + "|" cond1 = cond1.And("T_Class__icontains", T_Class) } if T_type > 0 { cond1 = cond1.And("T_type", T_type) } if len(T_name) >= 16 { cond1 = cond1.And("T_sn", T_name) } else if len(T_name) > 0 && len(T_name) < 16 { cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1) } else { cond1 = cond1.And("T_State", 1).And("T_datashow", 1) } if T_MapShow == 1 { cond1 = cond1.AndNot("T_Site", "") } cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } // 实时数据页面,温度或者湿度不在正常区间,排在最前面 if T_RealTime == 1 { offset_z := offset + page_z if cnt < int64(offset_z) { offset_z = int(cnt) } if offset > offset_z { offset = offset_z } _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } var DeviceSensor_unusual []DeviceSensor_R for _, v := range r { v_r := DeviceSensorToDeviceSensor_R(v) data := v_r.T_DeviceSensorData // 温度或者湿度不在正常区间,排在最前面 if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v_r.T_DeviceSensorParameter.T_free == 0 { DeviceSensor_unusual = append(DeviceSensor_unusual, v_r) } else { DeviceSensor_r = append(DeviceSensor_r, v_r) } } DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...) return DeviceSensor_unusual[offset:offset_z], cnt } _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } func Read_CompanyDeviceSensor_List_For_Data1(T_pids []int, T_name string, T_Class_id, T_type, T_RealTime, T_MapShow int, page int, page_z int) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() var maps_z []orm2.ParamsList // 也可以直接使用 Model 结构体作为表名 var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } var r []DeviceSensor_RealTime sql_WHERE := "" if len(T_pids) == 1 { sql_WHERE += " AND t_pid = " + strconv.Itoa(T_pids[0]) } else if len(T_pids) > 1 { sql_WHERE += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids)) } if T_Class_id > 0 { T_calss := "C" + strconv.Itoa(T_Class_id) + "|" sql_WHERE += " AND t__class like '%" + T_calss + "%'" } if T_type > 0 { sql_WHERE += " AND t_type = " + strconv.Itoa(T_type) } if len(T_name) >= 16 { sql_WHERE += " AND t_sn = '" + T_name + "'" } else if len(T_name) > 0 && len(T_name) < 16 { sql_WHERE += " AND (ds.t_sn like '%" + T_name + "%' OR ds.t_name like '%" + T_name + "%') AND ds.t__state = 1 AND t_datashow = 1" } else { sql_WHERE += " AND ds.t__state = 1 AND t_datashow = 1" } if T_MapShow == 1 { sql_WHERE += " AND t__site is not null" } sql_WHERE = strings.TrimLeft(sql_WHERE, " AND") // ------------- sql := "SELECT COUNT(*) FROM " + "device_sensor ds WHERE " + sql_WHERE logs.Debug(sql) _, err := o.Raw(sql).ValuesList(&maps_z) if err != nil { logs.Error(lib.FuncName(), err) return DeviceSensor_r, 0 } if len(maps_z) == 0 { return DeviceSensor_r, 0 } cnt, _ = strconv.ParseInt(maps_z[0][0].(string), 10, 64) sql = "SELECT *,ds.t_sn as t_sn,ds.t_id as t_id,ds.t_name as t_name,ds.t__state as t__state FROM device_sensor ds " + "LEFT JOIN device_sensor_parameter dsp " + "ON ds.t_sp=dsp.id " + "WHERE " + sql_WHERE + " ORDER BY ds.t_sort, ds.id ASC" realTimeSql := sql if page_z != 9999 { sql = sql + " LIMIT " + strconv.Itoa(offset) + "," + strconv.Itoa(page_z) } logs.Debug(sql) _, err = o.Raw(sql).QueryRows(&r) if err != nil { logs.Error(lib.FuncName(), err) return } if T_RealTime == 1 { offset_z := offset + page_z if cnt < int64(offset_z) { offset_z = int(cnt) } if offset > offset_z { offset = offset_z } _, err = o.Raw(realTimeSql).QueryRows(&r) if err != nil { logs.Error(lib.FuncName(), err) return } var DeviceSensor_unusual []DeviceSensor_R for _, v := range r { v_r := DeviceSensorRealTimeToDeviceSensor_R(v) data := v_r.T_DeviceSensorData // 温度或者湿度不在正常区间,排在最前面 if (data.T_t > data.T_tu || data.T_t < data.T_tl || data.T_rh > data.T_rhu || data.T_rh < data.T_rhl) && v.T_monitor == 1 && v_r.T_DeviceSensorParameter.T_free == 0 { DeviceSensor_unusual = append(DeviceSensor_unusual, v_r) } else { DeviceSensor_r = append(DeviceSensor_r, v_r) } } DeviceSensor_unusual = append(DeviceSensor_unusual, DeviceSensor_r...) return DeviceSensor_unusual[offset:offset_z], cnt } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorRealTimeToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } func Read_CompanyDeviceSensor_List_For_Data_ByKey(T_pids []int, T_sn string) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid__in", T_pids) if len(T_sn) > 0 { cond1 = cond1.And("T_sn", T_sn) } cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } _, err = qs.SetCond((*orm2.Condition)(cond1)).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } // 小程序-获取传感器类型ID func Read_DeviceSensor_T_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int) (lists orm2.ParamsList, err error) { o := orm.NewOrm() var pl_lists orm2.ParamsList sql := "SELECT DISTINCT t_type FROM device_sensor WHERE t_datashow=1 AND t__state=1" if admin.T_pid > 0 { // 客户访问自己及其子公司数据 if len(T_pids) > 0 { sql += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids)) } } else { // 管理员访问当前公司数据 sql += " AND t_pid = " + strconv.Itoa(T_pid) } if len(bindSN) > 0 { sql += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN)) } sql += " LIMIT 0,1000" _, err = o.Raw(sql).ValuesFlat(&pl_lists) if err != nil { logs.Error(lib.FuncName(), err) return } return pl_lists, nil } // 数据展示菜单下 传感器参数列表 func Read_DeviceSensor_List_For_Applet(admin *Account.Admin, bindSN []string, T_pid int, T_pids []int, T_name, T_online string, T_RealTime, T_type, page, page_z int) (DeviceSensor_r []DeviceSensor_Applet, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 var offset int if page <= 1 { offset = 0 } else { offset = (page - 1) * page_z } qs := o.QueryTable(new(DeviceSensor)) var r []DeviceSensor cond := orm.NewCondition() cond1 := orm.NewCondition() if admin.T_pid > 0 { cond1 = cond.And("T_pid__in", T_pids) } else { cond1 = cond.And("T_pid", T_pid) } if T_type > 0 { cond1 = cond1.And("T_type", T_type) } if len(bindSN) > 0 { cond1 = cond1.And("T_sn__in", bindSN) } if admin.T_pid > 0 { if len(T_name) >= 16 { cond1 = cond1.And("T_sn", T_name) } else if len(T_name) > 0 && len(T_name) < 16 { cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1) } else { cond1 = cond1.And("T_State", 1).And("T_datashow", 1) } } else { if len(T_name) >= 16 { cond1 = cond1.And("T_sn", T_name) } else if len(T_name) > 0 && len(T_name) < 16 { cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name)).And("T_State", 1).And("T_datashow", 1) } else { cond1 = cond1.And("T_State", 1).And("T_datashow", 1) } } // 0 未监控 1 监控中 2 无网络 3 全部 if T_online == "1" { cond1 = cond1.And("T_monitor", 1) } else if T_online == "2" { cond1 = cond1.AndCond(cond.AndCond(cond.AndNot("T_online", 1)). AndCond(cond.AndNot("T_online_s", 1))) } else if T_online == "0" { cond1 = cond1.And("T_monitor", 0) } var err error if page_z == 9999 { _, err = qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) } else { _, err = qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) } if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_Applet(v)) } return DeviceSensor_r, cnt } // 小程序-通过传感器类型获取sn func Read_DeviceSensor_T_sn_ByT_type(admin *Account.Admin, T_pid int, bindSN []string, T_pids []int, T_type int) (lists []string, err error) { o := orm.NewOrm() sql := "t__state=1 AND t_datashow=1 AND t_type = " + strconv.Itoa(T_type) if admin.T_pid > 0 { // 客户访问自己及其子公司数据 if len(T_pids) > 0 { sql += fmt.Sprintf(" AND t_pid in (%s) ", lib.IntListToDotStr(T_pids)) } } else { // 管理员访问当前公司数据 sql += " AND t_pid = " + strconv.Itoa(T_pid) } if len(bindSN) > 0 { sql += fmt.Sprintf(" AND t_sn in (%s)", lib.StringListToQuotesDotStr(bindSN)) } _, err = o.Raw("SELECT DISTINCT T_sn FROM device_sensor WHERE " + sql + " LIMIT 0,1000").QueryRows(&lists) if err != nil { logs.Error(lib.FuncName(), err) return } return lists, nil } func Read_DeviceSensorCount_ByT_pids(T_pids []int) (cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) cond := orm.NewCondition() cond1 := cond.And("T_pid__in", T_pids).And("T_State", 1) //// 不是内部权限(T_pid>0),T_State=1 0 屏蔽 1 正常 //cond1 = cond1.And("T_State", 1) //// 0 屏蔽数据展示 1 正常数据展示 //cond1 = cond1.And("T_datashow", 1) cnt, err := qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) } return cnt } func Read_DeviceSensor_All() (DeviceSensor_r []DeviceSensor) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) _, err := qs.All(&DeviceSensor_r) if err != nil { logs.Error(lib.FuncName(), err) } return DeviceSensor_r } // 获取对接电信的实时数据 func Read_Docking_Real_Data(T_pids []int) (DeviceData_r []DeviceData_Docking) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_pid__in", T_pids) _, err := qs.SetCond((*orm2.Condition)(cond1)).All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { sp, _ := Read_DeviceSensorParameter(v.T_sn, v.T_id) deviceDate := Read_DeviceData(v.T_sn, v.T_id) data := DeviceData_Docking{ T_monitor: v.T_monitor, T_online: v.T_online, T_online_s: v.T_online_s, T_sn: v.T_sn, T_id: v.T_id, T_t: deviceDate.T_t, T_rh: deviceDate.T_rh, T_site: deviceDate.T_site, T_time: deviceDate.T_time.Format("2006-01-02 15:04:05"), T_sp: deviceDate.T_sp, T_name: sp.T_name, T_tl: sp.T_Tlower, T_tu: sp.T_Tupper, T_rhl: sp.T_RHlower, T_rhu: sp.T_RHupper, } DeviceData_r = append(DeviceData_r, data) } return DeviceData_r } // 获取列表 func Read_DeviceSensorList_BySN(T_sn string) (DeviceSensor_r []DeviceSensor_R, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(DeviceSensor)) var r []DeviceSensor cond := orm.NewCondition() cond1 := cond.And("T_sn__icontains", T_sn) _, err := qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) return } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) return } for _, v := range r { DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v)) } return DeviceSensor_r, cnt } func Read_DeviceSensorList_By_T_snid(T_snid string) []DeviceSensor { T_snid_list := strings.Split(strings.Trim(T_snid, "|"), "|") var maps []DeviceSensor ids := []int{} for _, v := range T_snid_list { sn_id := strings.Split(v, ",") if len(sn_id) == 2 { ds, _ := Read_DeviceSensor_ByTsn_Tid(sn_id[0], lib.To_int(sn_id[1])) ids = append(ids, ds.Id) } } o := orm.NewOrm() cond := orm.NewCondition() cond1 := cond.And("id__in", ids) qs := o.QueryTable(new(DeviceSensor)) qs.SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&maps) return maps }