123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- package Device
- import (
- "ColdVerify_server/conf"
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- orm2 "github.com/beego/beego/v2/client/orm"
- "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"
- "time"
- )
- // 模板
- type Device struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_sn string `orm:"size(256);null"` // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_id int `orm:"size(256);null"` // 编号
- T_t float32 `orm:"size(10);null"` // 温度
- T_rh float32 `orm:"size(10);null"` // 湿度
- T_time time.Time `orm:"type(timestamp);null;"` // 采集时间
- T_MSISDN string `orm:"size(256);null"` // 物联网卡
- T_version int `orm:"size(2);default(1)"` // 版本
- T_signal int `orm:"size(2);default(1)"` // 信号强度
- T_electric int `orm:"size(2);default(1)"` // 电量百分比
- T_up_time_interval int `orm:"size(2);default(1)"` // 上传时间间隔
- T_note_time_interval int `orm:"size(2);default(1)"` // 记录时间间隔
- T_note_file_num int `orm:"size(2);default(1)"` // 记录文件数量
- T_State int `orm:"size(2);default(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_R struct {
- Id int
- T_sn string // 设备序列号 KF开头,环境监测主机。 YD开头,温途监测主机
- T_id string // 编号
- T_t float32 // 温度
- T_rh float32 // 湿度
- T_time string // 采集时间
- T_MSISDN string // 物联网卡
- T_version int // 版本
- T_signal int // 信号强度
- T_electric int // 电量百分比
- T_up_time_interval int // 上传时间间隔
- T_note_time_interval int // 记录时间间隔
- T_note_file_num int // 记录文件数量
- T_State int // 0 删除 1 正常
- }
- func DeviceToDevice_R(t Device, T_id string) (r Device_R) {
- r.Id = t.Id
- r.T_sn = t.T_sn
- r.T_id = T_id
- r.T_t = t.T_t
- r.T_rh = t.T_rh
- if !t.T_time.IsZero() {
- r.T_time = t.T_time.Format("2006-01-02 15:04:05")
- }
- r.T_MSISDN = t.T_MSISDN
- r.T_version = t.T_version
- r.T_signal = t.T_signal
- r.T_electric = t.T_electric
- r.T_up_time_interval = t.T_up_time_interval
- r.T_note_time_interval = t.T_note_time_interval
- r.T_note_file_num = t.T_note_file_num
- r.T_State = t.T_State
- return r
- }
- 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)
- logs.Println(config)
- var err error
- redisCache_Device, err = cache.NewCache("redis", config)
- if err != nil || redisCache_Device == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_Device_Set(key string, r Device) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_Device.Put(key, str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", key, ",value:", str, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_Device_Get(key string) (r Device, is bool) {
- if redisCache_Device.IsExist(key) {
- logs.Println("找到key:", key)
- v := redisCache_Device.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- logs.Println("没有 找到key:", key)
- return Device{}, false
- }
- func Redis_Device_DelK(key string) (err error) {
- err = redisCache_Device.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_Device_ById(id int) (r Device, is bool) {
- o := orm.NewOrm()
- r = Device{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 获取 By
- func Read_Device(T_sn string) (r Device, is bool) {
- if r, is = Redis_Device_Get(T_sn); is == true {
- return r, true
- }
- o := orm.NewOrm()
- qs := o.QueryTable(new(Device))
- err := qs.Filter("T_sn", T_sn).One(&r)
- if err != nil {
- logs.Println(lib.FuncName(), err)
- return r, false
- }
- Redis_Device_Set(T_sn, r)
- return r, true
- }
- // 添加
- func Add_Device(r Device) (id int64, is bool) {
- o := orm.NewOrm()
- id, err := o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0, false
- }
- CREATE_DeviceData(r.T_sn)
- Redis_Device_Set(r.T_sn, r)
- return id, true
- }
- // 删除
- func Delete_Device(v Device) bool {
- o := orm.NewOrm()
- if num, err := o.Delete(&v); err == nil {
- logs.Println("Number of records deleted in database:", num)
- } else {
- logs.Println(lib.FuncName(), err)
- return false
- }
- Redis_Device_DelK(v.T_sn)
- return true
- }
- // 删除
- func Delete_Device_(v Device) bool {
- o := orm.NewOrm()
- v.T_State = 0
- if num, err := o.Update(&v, "T_State"); err == nil {
- fmt.Println("Number of records updated in database:", num)
- } else {
- logs.Println(lib.FuncName(), err)
- return false
- }
- Redis_Device_DelK(v.T_sn)
- return true
- }
- // 修改
- func Update_Device(m Device, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- Redis_Device_Set(m.T_sn, m)
- return true
- } else {
- logs.Println(lib.FuncName(), err)
- }
- return false
- }
- // 获取列表
- func Read_Device_List(T_sn string, T_MSISDN string, page int, page_z int) (r []Device_R, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Device))
- var offset int64
- var map_r []Device
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_MSISDN__icontains", T_MSISDN).And("T_sn__icontains", T_sn).And("T_State", 1) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&map_r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- for _, v := range map_r {
- r = append(r, DeviceToDevice_R(v, "0"))
- }
- return r, cnt
- }
|