123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298 |
- 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(3)" json:"T_online" form:"T_online"` // 在线状态 1 在线 2 离线 3 未激活
- T_project string `orm:"size(256);default('')" json:"T_project" form:"T_project"` // 项目地址
- 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(1);index" json:"T_state" form:"T_state"` // 1 正常 2 禁用\删除 3 无效
- CreateTime models.Time `orm:"column(create_time);type(timestamp);auto_now_add" json:"CreateTime"`
- UpdateTime models.Time `orm:"column(update_time);type(timestamp);auto_now" json:"UpdateTime"`
- }
- 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
- }
- t.Redis_Set() // Redis 更新缓存
- 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)
- }
- if len(t.T_sn) > 0 {
- cond = cond.And("T_sn__icontains", t.T_sn) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- }
- if t.T_online > 0 {
- cond = cond.And("T_online", t.T_online) // .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
- }
- // 删除 关联设备缓存
- func Device_CacheDelK_All(T_ProductID string) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- // 筛选参数
- cond := orm.NewCondition()
- cond = cond.And("T_ProductID", T_ProductID) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- // 执行
- var r []Device
- qs.SetCond((*orm2.Condition)(cond)).All(&r)
- for _, v := range r {
- v.Redis_DelK()
- }
- return
- }
|