123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- package Device
- import (
- "Cold_Api/conf"
- "Cold_Api/models/Admin"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- _ "github.com/astaxie/beego/cache/redis"
- "github.com/beego/beego/v2/adapter/orm"
- _ "github.com/go-sql-driver/mysql"
- "strconv"
- "time"
- )
- type DeviceClass struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_uuid string `orm:"size(256);null"` //
- T_name string `orm:"size(256);null"` // 分类
- T_Notice_wx string `orm:"type(text);null"` //w微信公众号 appid/名字|
- T_Notice_wx2 string `orm:"type(text);null"` //w微信公众号 appid/名字|
- T_Notice_phone string `orm:"type(text);null"` //p手机 1111111|
- T_Notice_message string `orm:"type(text);null"` //m短信 1111111|
- T_Notice_mailbox string `orm:"type(text);null"` //e邮箱 1111111|
- T_Notice_mechanism string `orm:"type(text);null"` // 报警机制
- // 湿度超下限预警,处理,w启用,数量,上限,~|
- // 湿度超下限预警,0,0,0,0,0,0,0,0,0,0,0,0|
- 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 保存时都会对时间自动更新
- }
- func (t *DeviceClass) TableName() string {
- return "DeviceClass" // 数据库名称 // ************** 替换 DesignClass **************
- }
- var redisCache_DeviceClass cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(DeviceClass))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_DeviceClass", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_DeviceClass, err = cache.NewCache("redis", config)
- if err != nil || redisCache_DeviceClass == nil {
- errMsg := "failed to init redis"
- fmt.Println(errMsg, err)
- panic(errMsg)
- }
- }
- //Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_DeviceClass_Set(r DeviceClass) (err error) {
- key := strconv.Itoa(r.Id)
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- fmt.Print(err)
- return
- }
- err = redisCache_DeviceClass.Put(key, str, 2*time.Hour)
- if err != nil {
- fmt.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- //if r,is :=Redis_Get(T_sn);is{
- //return r,nil
- //}
- func Redis_DeviceClass_Get(key string) (DeviceClass, bool) {
- println("找到key:", key)
- if redisCache_DeviceClass.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_DeviceClass.Get(key)
- var r DeviceClass
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- return DeviceClass{}, false
- }
- func Redis_DeviceClass_DelK(key string) (err error) {
- err = redisCache_DeviceClass.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_Class_ById(id int) (r DeviceClass, err error) {
- key := strconv.Itoa(id)
- if r, is := Redis_DeviceClass_Get(key); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- r = DeviceClass{Id: id}
- err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- fmt.Println(err)
- return r, err
- }
- Redis_DeviceClass_Set(r)
- return r, err
- }
- // 添加
- func Add_Class(m DeviceClass) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- fmt.Println(err)
- }
- Redis_DeviceClass_Set(m)
- return
- }
- // 修改
- func Update_Class_ById(m DeviceClass) (err error) {
- o := orm.NewOrm()
- v := DeviceClass{Id: m.Id}
- // ascertain id exists in the database
- if err = o.Read(&v); err == nil {
- var num int64
- if num, err = o.Update(&m, "T_name", "T_Notice_wx", "T_Notice_wx2", "T_Notice_phone", "T_Notice_message", "T_Notice_mailbox", "T_Notice_mechanism"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- }
- }
- Redis_DeviceClass_Set(m)
- return
- }
- // 删除
- func Delete_Class_ById(id int) DeviceClass {
- o := orm.NewOrm()
- v := DeviceClass{Id: id}
- // ascertain id exists in the database
- if err := o.Read(&v); err == nil {
- var num int64
- v.T_State = 0
- if num, err = o.Update(&v, "T_State"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- }
- key := strconv.Itoa(v.Id)
- Redis_DeviceClass_DelK(key)
- }
- return v
- }
- // 删除
- func Delete_Class_ByUuid_All(user_ Admin.Admin) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(DeviceClass))
- var r []DeviceClass
- qs.Filter("T_State", 1).All(&r)
- for _, v := range r {
- v.T_State = 0
- o.Update(&v, "T_State")
- }
- }
- // 获取全部
- func Read_Class_All_1() (r []DeviceClass) {
- o := orm.NewOrm()
- qs := o.QueryTable(new(DeviceClass))
- qs.Filter("T_State", 1).All(&r)
- return r
- }
- // 获取列表
- func Read_DeviceClass_ALL_1(T_uuid string, page int, T_name string) (r []DeviceClass, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceClass))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * conf.Page_size)
- }
- qs.Limit(conf.Page_size, offset).Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).OrderBy("-Id").Filter("T_State", 1).All(&r)
- cnt, _ = qs.Filter("T_uuid", T_uuid).Filter("T_name__icontains", T_name).Filter("T_State", 1).Count()
- return r, cnt
- }
- // 获取列表
- func Read_DeviceClass_ALL_T_uuid_1(T_uuid string) (r []DeviceClass) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceClass))
- qs.Filter("T_uuid", T_uuid).OrderBy("-Id").Filter("T_State", 1).All(&r)
- return r
- }
|