package Device import ( "Yunlot/conf" "Yunlot/lib" "Yunlot/logs" "Yunlot/models" "Yunlot/models/Product" "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" "time" ) // 设备 type Device struct { T_sn string `orm:"size(256);pk" json:"T_sn" form:"T_sn"` // Sn T_password string `orm:"size(256);" json:"T_password" form:"T_password"` // 密码 T_online int `orm:"size(1);index;default(0)" json:"T_online" form:"T_online"` // 在线状态 0 离线 1 在线 3 无效 T_ProductID string `orm:"size(8);index" json:"T_ProductID" form:"T_ProductID"` // 产品类型 T_ProductJson Product.ProductType `orm:"-" json:"T_ProductJson"` // 产品类型 json T_data string `orm:"column(t_data);type(text);default('')" json:"T_data" ` // 设备数据 T_dataJson map[string]interface{} `orm:"-" json:"T_dataJson"` // 设备数据 T_state int `orm:"size(2);default(0);index" json:"T_state" form:"T_state"` // 0 未激活 1 正常 2 禁用\删除 CreateTime models.Time `orm:"column(create_time);type(timestamp);auto_now_add"` UpdateTime models.Time `orm:"column(update_time);type(timestamp);auto_now"` } func (t *Device) TableName() string { return "Device" // 数据库名称 // ************** 替换 FormulaList ************** } var redis_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) var err error redis_Device, err = cache.NewCache("redis", config) if err != nil || redis_Device == nil { logs.Println(config) panic(any(err)) } } // ---------------- Redis ------------------- func (t *Device) redis_Set() (err error) { if len(t.T_data) > 5 { var T_datajson map[string]interface{} json.Unmarshal([]byte(t.T_data), &T_datajson) t.T_dataJson = T_datajson } if len(t.T_ProductID) > 0 { t.T_ProductJson.T_ProductID = t.T_ProductID t.T_ProductJson.Read() } //json序列化 str, err := json.Marshal(t) if err != nil { logs.PrintlnError("Redis_Set", err) return } err = redis_Device.Put(t.T_sn, str, 24*time.Hour) if err != nil { logs.Println("set key:", t.T_sn, ",value:", str, err) } return } func (t *Device) redis_Get(key string) (is bool) { if redis_Device.IsExist(key) { //println("找到key:",key) v := redis_Device.Get(key) if v == nil { return false } json.Unmarshal(v.([]byte), t) if t.T_state == 3 { // 3 无效 return false } return true } //println("没有 找到key:",key) return false } func (t *Device) redis_DelK() (err error) { err = redis_Device.Delete(t.T_sn) return } // ---------------- 方法 ------------------- // 获取 func (t *Device) Read() (is bool) { o := orm.NewOrm() err := o.Read(t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { return false } t.redis_Set() return true } // 获取-高效的 func (t *Device) Read_Tidy() (bool bool) { if t.redis_Get(t.T_sn) { return true } o := orm.NewOrm() err := o.Read(t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名 if err != nil { t.T_state = 3 // 3 无效 t.redis_Set() // Redis 更新缓存 return false } return true } func (Device_r *Device) CreateSn(GetRandstringI int) bool { GetRandstringI += GetRandstringI * 100 if len(Device_r.T_sn) < 10 { for true { Device_r.T_sn = lib.WeekByDate() + lib.GetRandstring(10, "0123456789123456789", int64(GetRandstringI)) Device_is := Device{T_sn: Device_r.T_sn} if !Device_is.Read() { break } GetRandstringI += 1 } } else { Device_is := Device{T_sn: Device_r.T_sn} if Device_is.Read() { return false } Device_r.Add() } GetRandstringI += 1 // mysql 秘钥 Device_r.T_password = lib.GetRandstring(16, "", int64(GetRandstringI)) //println(Device_r.T_password) //Device_r.T_password = lib.Sha256(Device_r.T_password) Device_r.Add() return true } // 添加 func (t *Device) Add() (is bool) { o := orm.NewOrm() id, err := o.Insert(t) if err != nil { return false } println(id) t.redis_Set() return true } // 修改 func (t *Device) Update(cols ...string) bool { data, _ := json.Marshal(t.T_dataJson) t.T_data = string(data) o := orm.NewOrm() if num, err := o.Update(t, cols...); err == nil { logs.Println("Number of records updated in database:", num) t.redis_Set() // Redis 更新缓存 return true } return false } // 删除 func (t *Device) Delete() bool { o := orm.NewOrm() if num, err := o.Delete(t); err == nil { logs.Println("Number of records deleted in database:", num) } else { return false } t.redis_DelK() return true } // 获取列表 func (t *Device) Lists(PageIndex int, PageSize int) (r []Device, Total int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Device)) var offset int64 if PageIndex <= 1 { offset = 0 } else { offset = int64((PageIndex - 1) * PageSize) } // 筛选参数 cond := orm.NewCondition() if len(t.T_ProductID) > 0 { cond = cond.And("T_ProductID", t.T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000) } // 执行 qs.Limit(PageSize, offset).SetCond((*orm2.Condition)(cond)).All(&r) Total, _ = qs.SetCond((*orm2.Condition)(cond)).Count() for i, _ := range r { if len(r[i].T_ProductID) > 0 { r[i].T_ProductJson.T_ProductID = r[i].T_ProductID r[i].T_ProductJson.Read() } if len(t.T_data) > 5 { var T_datajson map[string]interface{} json.Unmarshal([]byte(t.T_data), &T_datajson) t.T_dataJson = T_datajson } } return r, Total } // 获取列表 func (t *Device) Lists_All() (r []Device) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(Device)) // 筛选参数 cond := orm.NewCondition() if len(t.T_ProductID) > 0 { cond = cond.And("T_ProductID", t.T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000) } // 执行 qs.SetCond((*orm2.Condition)(cond)).All(&r) for i, _ := range r { if len(r[i].T_ProductID) > 0 { r[i].T_ProductJson.T_ProductID = r[i].T_ProductID r[i].T_ProductJson.Read() } if len(t.T_data) > 5 { var T_datajson map[string]interface{} json.Unmarshal([]byte(t.T_data), &T_datajson) t.T_dataJson = T_datajson } } return r }