123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- package Device
- import (
- "Cold_Api/conf"
- "Cold_Api/models/Admin"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- orm2 "github.com/beego/beego/v2/client/orm"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "strings"
- "time"
- )
- // 模板
- type Device struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_MSISDN string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_devName string `orm:"size(256);null"` //设备名称
- //T_class int `orm:"size(200);null"` // 分类
- //T_img string `orm:"size(256);null"` // 图片
- T_sensor int `orm:"size(22);null"` // 传感器数量
- T_l_p int `orm:"size(22);null"` // 1物流端 2药店端
- T_give int `orm:"size(2);1"` // 0 丢弃 1 正常
- T_Putime_Parameter time.Time `orm:"type(timestamp);null;"` // 参数时间
- T_Bind string `orm:"size(256);null"` //设备绑定 Uid
- T_monitor int `orm:"size(22);null"` // 监控状态 1 监控 0 未监控
- T_online int `orm:"size(22);1"` // 在线状态 1 在线 0 离线
- T_Dattery int `orm:"size(256);null"` //电量
- T_err int `orm:"size(2);1"` // 0 正常 1 异常
- T_State int `orm:"size(2);1"` // 0 删除 1 正常
- 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 Device_task struct {
- T_sn string
- T_task string
- }
- func (t *Device) TableName() string {
- return "Device" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_Device cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(Device))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_Device", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_Device, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Device == nil {
- errMsg := "failed to init redis"
- fmt.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- //Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_Set(r Device) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- fmt.Print(err)
- return
- }
- err = redisCache_Device.Put(r.T_sn, str, 24*time.Hour)
- if err != nil {
- fmt.Println("set key:", r.T_sn, ",value:", str, err)
- }
- return
- }
- //if r,is :=Redis_Get(T_sn);is{
- //return r,nil
- //}
- func Redis_Get(key string) (r Device, is bool) {
- if redisCache_Device.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_Device.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- //println("没有 找到key:",key)
- return Device{}, false
- }
- func Redis_DelK(key string) (err error) {
- err = redisCache_Device.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_Device_ById(id int) (r Device) {
- o := orm.NewOrm()
- r = Device{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- }
- return r
- }
- // 获取 ById
- func Read_Device_ByT_sn(T_sn string) (r Device, err error) {
- if r, is := Redis_Get(T_sn); is {
- //println("Redis_Get OK")
- return r, nil
- }
- //println("没有 Redis_Get SN")
- o := orm.NewOrm()
- r = Device{T_sn: T_sn, T_State: 1}
- err = o.Read(&r, "T_sn", "T_State") // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- } else {
- Redis_Set(r) // Redis 更新缓存
- }
- return r, err
- }
- // 添加
- func Add_Device(m Device) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- fmt.Println(err)
- }
- m.Id = int(id)
- Redis_Set(m) // Redis 更新缓存
- return id, err
- }
- // 删除
- func Delete_Device(m Device) (err error) {
- o := orm.NewOrm()
- if num, err := o.Delete(&m); err == nil {
- fmt.Println("Number of records deleted in database:", num)
- }
- Redis_DelK(m.T_sn)
- return
- }
- // 修改
- func Update_Device(r Device, 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_Set(r) // Redis 更新缓存
- return true
- }
- return false
- }
- // 修改
- //func Update_Device_T_devName(T_sn string, T_devName string) (err error) {
- // o := orm.NewOrm()
- // v := Device{T_sn: T_sn}
- // // ascertain id exists in the database
- // if err = o.Read(&v, "T_sn"); err == nil {
- // v.T_devName = T_devName
- // o.Update(&v, "T_devName")
- // }
- // Redis_Set( v) // Redis 更新缓存
- // return err
- //}
- // 修改
- //func Update_Device_T_l_p(T_sn string, T_l_p int) (err error) {
- // o := orm.NewOrm()
- // v := Device{T_sn: T_sn}
- // // ascertain id exists in the database
- // if err = o.Read(&v, "T_sn"); err == nil {
- // v.T_l_p = T_l_p
- // o.Update(&v, "T_l_p")
- // }
- // Redis_Set( v) // Redis 更新缓存
- // return err
- //}
- //// 获取列表
- //func Read_Device_ALL_1(user_ Admin.Admin, page int, T_sn string, T_devName string, class string) (r []Device, cnt int64) {
- //
- // o := orm.NewOrm()
- // // 也可以直接使用 Model 结构体作为表名
- //
- // qs := o.QueryTable(new(Device))
- // var offset int64
- // if page <= 1 {
- // offset = 0
- // } else {
- // offset = int64((page - 1) * conf.Page_size)
- // }
- // T_Bind := "U" + strconv.Itoa(user_.Id)
- // if user_.Admin_master <= 1 {
- // T_Bind = ""
- // }
- // fmt.Println("T_Bind:", T_Bind)
- // qs.Limit(conf.Page_size, offset).Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_devName__icontains", T_devName).OrderBy("-UpdateTime").Filter("T_State", 1).All(&r)
- // cnt, _ = qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_devName__icontains", T_devName).Filter("T_State", 1).Count()
- //
- // return r, cnt
- //}
- // 获取列表
- func Read_Device_ALL_bind_1(user_ Admin.Admin, page int, page_z int, T_sn string, T_devName string, class string, T_monitor string) (r []Device, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- 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_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- cond := orm.NewCondition()
- cond1 := cond.And("T_Bind__icontains", T_Bind).And("T_sn__icontains", T_sn).And("T_devName__icontains", T_devName).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- if T_monitor == "1" {
- //cond1.AndCond(cond.And("T_class", class))
- cond1 = cond.AndCond(cond1).AndCond(cond.And("T_monitor", 1))
- } else if T_monitor == "0" {
- //cond1.AndCond(cond.And("T_class", class))
- cond1 = cond.AndCond(cond1).AndCond(cond.And("T_monitor", 0))
- }
- // 非内部权限
- println("user_.Admin_power:", user_.Admin_power)
- if user_.Admin_power > 6 {
- cond1 = cond.AndCond(cond1).AndCond(cond.And("T_give", 1))
- }
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("Id").OrderBy("-T_give").All(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- return r, cnt
- }
- // 获取列表
- func Read_Device_ALL_bind(user_ Admin.Admin) (r []Device) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- qs.Filter("T_Bind__icontains", T_Bind).All(&r)
- return r
- }
- // 获取列表
- func Read_Device_ALL_bind_T_l_p(user_ Admin.Admin, T_l_p int) (r []Device) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- qs.Filter("T_Bind__icontains", T_Bind).Filter("T_l_p", T_l_p).All(&r)
- return r
- }
- // 获取列表
- func Read_Device_ALL_T_sn_T_devName_bind_1(T_sn string, T_devName string, user_ Admin.Admin) (r []Device) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_devName__icontains", T_devName).Filter("T_State", 1).All(&r)
- return r
- }
- // 获取列表
- func Read_Device_ALL_T_sn_bind_1(T_sn string, user_ Admin.Admin) (r []Device) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- qs.Filter("T_Bind__icontains", T_Bind).Filter("T_sn__icontains", T_sn).Filter("T_State", 1).All(&r)
- return r
- }
- // 修改
- func Device_Bind_Add(T_sn string, user_ Admin.Admin) string {
- o := orm.NewOrm()
- v := Device{T_sn: T_sn}
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if err := o.Read(&v, "T_sn"); err == nil {
- v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
- v.T_Bind = v.T_Bind + T_Bind
- o.Update(&v, "T_Bind")
- }
- Redis_Set(v) // Redis 更新缓存
- return v.T_Bind
- }
- // 修改
- func Device_Bind_Del(T_sn string, user_ Admin.Admin) string {
- o := orm.NewOrm()
- v := Device{T_sn: T_sn}
- T_Bind := "U" + strconv.Itoa(user_.Id) + "|"
- if err := o.Read(&v, "T_sn"); err == nil {
- v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
- o.Update(&v, "T_Bind")
- }
- Redis_Set(v) // Redis 更新缓存
- return v.T_Bind
- }
- // 修改
- func Device_Bind_ALL_Del(admin_r Admin.Admin) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(Device))
- var r []Device
- T_Bind := "U" + strconv.Itoa(admin_r.Id) + "|"
- qs.Filter("T_Bind__icontains", T_Bind).All(&r)
- for _, v := range r {
- v.T_Bind = strings.Replace(v.T_Bind, T_Bind, "", -1)
- o.Update(&v, "T_Bind")
- Redis_Set(v) // Redis 更新缓存
- }
- }
- //
- func Read_Device_ALL_T_Bind_Count(user_ Admin.Admin, T_sn string, T_l_p int) int {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- T_Bind := "U" + strconv.Itoa(user_.Id)
- if user_.Admin_master <= 1 {
- T_Bind = ""
- }
- fmt.Println("T_Bind:", T_Bind)
- var cnt int64
- if T_l_p > 0 {
- cnt, _ = qs.Filter("T_sn__icontains", T_sn).Filter("T_Bind__icontains", T_Bind).Filter("T_l_p", T_l_p).Count()
- return int(cnt)
- }
- cnt, _ = qs.Filter("T_sn__icontains", T_sn).Filter("T_Bind__icontains", T_Bind).Count()
- return int(cnt)
- }
|