123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687 |
- 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/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_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"` // Device.DeviceNotice.Id 设备通知策略 N1|N2|
- T_datashow int `orm:"index;size(2);default(1)"` // 0 屏蔽数据展示 1 正常数据展示
- T_sort int `orm:"index;size(200);1"` // 排序
- T_3dview string `orm:"size(256);null"` // 3D 视图ID
- // 设备同步参数
- T_Dattery int `orm:"size(4);null"` // 电量
- T_Site string `orm:"size(200);null"` // GPS
- T_type int `orm:"index;size(4);null"` // Device.DeviceType 1库房 2移动
- T_give int `orm:"index;size(2);default(1)"` // 屏蔽状态 0 屏蔽 1 正常
- T_monitor int `orm:"index;size(2);null"` // 监控状态 0 未监控 1 监控
- T_online int `orm:"index;size(2);default(1)"` // 在线状态 0 未启用 1 在线 2 离线
- T_online_s int `orm:"index;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_3dview string // 3D 视图ID
- T_sort int // 排序
- T_Dattery int // 电量
- T_Site string // GPS
- T_monitor int // 记录状态
- T_online int // 在线状态 1 在线 0 离线
- T_datashow int // 0 屏蔽数据展示 1 正常数据展示
- T_DeviceSensorData DeviceData_R // 传感器最新数据
- T_DeviceSensorParameter DeviceSensorParameter_R // 设备参数
- }
- 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"
- fmt.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- // Redis_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 {
- fmt.Print(err)
- return
- }
- err = redisCache_DeviceSensor.Put(key, str, 24*time.Hour)
- if err != nil {
- fmt.Println("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)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- //println("没有 找到key:",key)
- 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)
- return
- }
- // ---------------- 特殊方法 -------------------
- func DeviceSensorToDeviceSensor_R(DeviceSensor_ DeviceSensor) (DeviceSensor_r DeviceSensor_R) {
- lib.DeviceRealSnMap[DeviceSensor_r.T_sn] = 3 // 连续请求 实时数据
- DeviceSensor_r.T_sn = DeviceSensor_.T_sn
- DeviceSensor_r.T_id = DeviceSensor_.T_id
- DeviceSensor_r.T_name = DeviceSensor_.T_name
- 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_sort = DeviceSensor_.T_sort
- DeviceSensor_r.T_datashow = DeviceSensor_.T_datashow
- // 最新系统参数
- 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)
- return
- }
- // ---------------- 特殊方法 -------------------
- // ----------------------------------- v2 -------------------------------------------------------------
- // 获取列表 - 总数
- func Read_V2_DeviceSensor_ByTsn_num(T_sn string) int {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- cnt, _ := qs.Filter("T_sn", T_sn).Count()
- return int(cnt)
- }
- // 获取列表
- func Read_DeviceSensorList(admin_r Account.Admin, T_pid int, T_sn string, T_name string, T_Class_id, T_Notice_id int, T_give int, T_datashow 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_z == 0 {
- page_z = conf.Page_size
- }
- 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) + "|"
- fmt.Println("T_Class:", T_Class)
- cond1 = cond1.And("T_Class__icontains", T_Class)
- }
- if T_Notice_id > 0 {
- T_Notice := "N" + strconv.Itoa(T_Notice_id) + "|"
- fmt.Println("T_Class:", T_Notice)
- cond1 = cond1.And("T_Notice__icontains", T_Notice)
- }
- if len(T_sn) > 0 {
- cond1 = cond1.And("T_sn__icontains", T_sn)
- }
- if len(T_name) > 0 {
- cond1 = cond1.AndCond(cond.Or("T_name__icontains", T_name).Or("T_sn__icontains", T_name))
- }
- // 不是内部权限(T_pid>0),只显示T_give=1 0 屏蔽 1 正常
- if admin_r.T_pid > 0 {
- cond1 = cond1.And("T_give", 1)
- } else if T_give != -1 {
- cond1 = cond1.And("T_give", T_give)
- }
- if T_datashow != -1 { // 空:正常显示 1:强制显示 屏蔽数据展示
- cond1 = cond1.And("T_datashow", 1)
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- //
- //qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", SN_type).Filter("T_sn__icontains", T_sn).Filter("T_name__icontains", T_name).OrderBy("T_sort").All(&r)
- //cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", T_sn).Filter("T_sn__icontains", SN_type).Filter("T_name__icontains", T_name).Count()
- //for i, v := range r {
- //
- // // 提前最新数据
- // DeviceSensorData, is := Read_DeviceSensorData_ById_New(v.T_sn, v.T_id)
- // if !is {
- // continue
- // }
- // if DeviceSensorData.T_time.After(v.T_time) {
- // //fmt.Println(v.T_sn, v.T_id, DeviceSensorData.T_time)
- // r[i].T_t = DeviceSensorData.T_t
- // r[i].T_rh = DeviceSensorData.T_rh
- // r[i].T_time = DeviceSensorData.T_time
- // }
- //
- //}
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- }
- return DeviceSensor_r, cnt
- //return r, cnt
- }
- // ----------------------------------- v1 -------------------------------------------------------------
- // 获取
- 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 {
- fmt.Println("Delete_DeviceSensor_ById", err)
- return r, false
- }
- Redis_DeviceSensor_Set(r)
- return r, true
- }
- // 删除
- func Delete_DeviceSensor_ById(T_sn string, T_id int) (err error) {
- fmt.Println("Delete_DeviceSensor : T_sn", T_sn, "T_id", T_id)
- 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 {
- fmt.Println("Delete_DeviceSensor_ById", err)
- return err
- }
- // ascertain id exists in the database
- var num int64
- if num, err = o.Delete(&DeviceSensor{Id: r.Id}); err == nil {
- fmt.Println("Number of records deleted in database:", num)
- }
- Redis_DeviceSensor_DelK(r)
- return
- }
- // 修改
- func Update_DeviceSensor(r DeviceSensor, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&r, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- Redis_DeviceSensor_Set(r)
- return true
- }
- return false
- }
- // 修改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) + "|"
- if err = o.Read(&v, "T_sn", "T_id"); err == nil {
- v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
- o.Update(&v, "T_Class")
- }
- return err
- }
- // 修改T_Class,追加
- func DeviceSensor_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) + "|"
- if err = o.Read(&v, "T_sn", "T_id"); err == nil {
- v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
- v.T_Class = v.T_Class + T_Class
- o.Update(&v, "T_Class")
- }
- return err
- }
- // 修改
- func DeviceSensor_T_Class_ALL_Del(T_Class_id int) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(DeviceSensor))
- var r []DeviceSensor
- T_Class := "C" + strconv.Itoa(T_Class_id) + "|"
- qs.Filter("T_Class__icontains", T_Class).All(&r)
- for _, v := range r {
- v.T_Class = strings.Replace(v.T_Class, T_Class, "", -1)
- o.Update(&v, "T_Class")
- }
- }
- //
- //// 修改
- //func Update_DeviceSensor_ByTsn_All_T_Bind(T_sn string, T_Bind string) (err error) {
- // o := orm.NewOrm()
- // var r_l []DeviceSensor
- // qs := o.QueryTable(new(DeviceSensor))
- //
- // qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r_l)
- // for _, v := range r_l {
- // v.T_Bind = T_Bind
- //
- // o.Update(&v, "T_Bind")
- // }
- //
- // return err
- //}
- // 获取最新数据
- func DeviceSensor_T_Bind_ALL(T_sn string, T_Bind string) {
- o := orm.NewOrm()
- res, err := o.Raw("UPDATE DeviceSensor SET `t__bind` = '" + T_Bind + "' WHERE `t_sn` = '" + T_sn + "' ").Exec()
- if err == nil {
- num, _ := res.RowsAffected()
- fmt.Println("mysql row affected nums: ", num)
- }
- }
- // 获取最新数据
- func DeviceSensor_T_type_ALL(T_sn string, T_type int) {
- o := orm.NewOrm()
- res, err := o.Raw("UPDATE DeviceSensor SET `t_type` = " + strconv.Itoa(T_type) + " WHERE `t_sn` = '" + T_sn + "' ").Exec()
- if err == nil {
- num, _ := res.RowsAffected()
- fmt.Println("mysql row affected nums: ", num)
- }
- }
- // 获取最新数据
- func DeviceSensor_t_give_ALL(T_sn string, t_give int) {
- o := orm.NewOrm()
- res, err := o.Raw("UPDATE DeviceSensor SET `t_give` = " + strconv.Itoa(t_give) + " WHERE `t_sn` = '" + T_sn + "' ").Exec()
- if err == nil {
- num, _ := res.RowsAffected()
- fmt.Println("mysql row affected nums: ", num)
- }
- }
- // 获取 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") 如果不是 主键 就得指定字段名
- return r, err
- }
- // 获取列表
- func Read_DeviceSensor_ByTsn(T_sn string) ([]DeviceSensor, int) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var r []DeviceSensor
- qs.Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
- cnt, _ := qs.Filter("T_sn", T_sn).Count()
- //var DeviceSensor_r []DeviceSensor_R
- //for _, v := range r {
- // DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- //}
- //
- //return DeviceSensor_r, int(cnt)
- return r, int(cnt)
- }
- // 获取列表
- func Read_DeviceSensor_ALL_T_sn(T_sn string, 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
- qs.Limit(page_z, offset).Filter("T_sn", T_sn).OrderBy("T_id").All(&r)
- cnt, _ = qs.Filter("T_sn", T_sn).Count()
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- }
- return DeviceSensor_r, cnt
- }
- // 获取列表
- func Read_DeviceSensor_class_ALL_1(admin Account.Admin, T_pid int, T_Class_id int, page int, page_z int, T_sn string, T_name string, SN_type string) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page_z == 0 {
- page_z = conf.Page_size
- }
- 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) + "|"
- }
- fmt.Println("T_Class:", T_Class)
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_Class__icontains", T_Class).And("T_sn__icontains", SN_type).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name).And("T_datashow", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- if len(T_sn) < 6 {
- cond1 = cond1.AndCond(cond.And("T_give", 1))
- }
- if admin.T_pid > 0 {
- cond1 = cond1.AndCond(cond.And("T_give", 1))
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- //
- //qs.Limit(page_z, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", SN_type).Filter("T_sn__icontains", T_sn).Filter("T_name__icontains", T_name).OrderBy("T_sort").All(&r)
- //cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", T_sn).Filter("T_sn__icontains", SN_type).Filter("T_name__icontains", T_name).Count()
- //for i, v := range r {
- //
- // // 提前最新数据
- // DeviceSensorData, is := Read_DeviceSensorData_ById_New(v.T_sn, v.T_id)
- // if !is {
- // continue
- // }
- // if DeviceSensorData.T_time.After(v.T_time) {
- // //fmt.Println(v.T_sn, v.T_id, DeviceSensorData.T_time)
- // r[i].T_t = DeviceSensorData.T_t
- // r[i].T_rh = DeviceSensorData.T_rh
- // r[i].T_time = DeviceSensorData.T_time
- // }
- //
- //}
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(v))
- }
- return DeviceSensor_r, cnt
- //return r, cnt
- }
- // 获取列表
- func Read_DeviceSensor_Map_ALL_1(T_pid int, T_Class_id int, T_sn string, T_name string, SN_type string) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- //now := time.Now()
- //// 一天前
- //d, _ := time.ParseDuration("-1h")
- //now = now.Add(d)
- T_Class := ""
- if T_Class_id != 0 {
- T_Class = "C" + strconv.Itoa(T_Class_id) + "|"
- }
- fmt.Println("T_Class:", T_Class)
- qs.Filter("T_pid", T_pid).Filter("T_Class__icontains", T_Class).Filter("T_sn__icontains", SN_type).Filter("T_sn__icontains", T_sn).Filter("T_name__icontains", T_name).OrderBy("-UpdateTime").All(&r) //.Filter("UpdateTime__gte", now)
- return r
- }
- // 获取列表
- func Read_DeviceSensor_ALL_T_sn_T_id_class_1(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) + "|"
- qs.Filter("T_Class__icontains", T_Class).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
- return r
- }
- // 获取列表
- func Read_DeviceSensor_ALL_List_T_sn(T_sn string) (r []DeviceSensor) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- qs.Filter("T_sn", T_sn).All(&r)
- 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) + "|"
- qs.Filter("T_Class__icontains", T_Class).All(&r)
- 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 {
- 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) + "|"
- qs.Filter("T_Notice__icontains", T_Notice).Filter("T_id", T_id).Filter("T_sn", T_sn).All(&r)
- 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) + "|"
- if err = o.Read(&v, "T_sn", "T_id"); err == nil {
- v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
- o.Update(&v, "T_Notice")
- }
- return err
- }
- // 修改T_Notice,追加
- func DeviceSensor_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) + "|"
- if err = o.Read(&v, "T_sn", "T_id"); err == nil {
- v.T_Notice = strings.Replace(v.T_Notice, T_Notice, "", -1)
- v.T_Notice = v.T_Notice + T_Notice
- o.Update(&v, "T_Notice")
- }
- return err
- }
- // 获取列表
- 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) + "|"
- qs.Filter("T_Notice__icontains", T_Notice).All(&r)
- return r
- }
- // 获取列表
- func Read_DeviceSensor_Notice_ALL_1(T_pid int, T_Notice_id int, page int, page_z int, T_sn string, T_name string, SN_type string) (DeviceSensor_r []DeviceSensor_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceSensor))
- var offset int64
- if page_z == 0 {
- page_z = conf.Page_size
- }
- 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) + "|"
- }
- fmt.Println("T_Notice:", T_Notice)
- var r []DeviceSensor
- cond := orm.NewCondition()
- cond1 := cond.And("T_pid", T_pid).And("T_Notice__icontains", T_Notice).And("T_sn__icontains", SN_type).And("T_sn__icontains", T_sn).And("T_name__icontains", T_name).And("T_datashow", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- if len(T_sn) < 6 {
- cond1 = cond1.AndCond(cond.And("T_give", 1))
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("T_sort", "T_id").All(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- for _, v := range r {
- DeviceSensor_r = append(DeviceSensor_r, DeviceSensorToDeviceSensor_R(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_type = r_Device.T_type // 1库房 2移动
- v.T_give = r_Device.T_give // 屏蔽状态 0 屏蔽 1 正常
- 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 离线
- Update_DeviceSensor(v, "T_Dattery", "T_Site", "T_type", "T_give", "T_monitor", "T_online", "T_online_s")
- }
- return true
- }
|