123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- package Account
- import (
- "Cold_Api/conf"
- "Cold_Api/controllers/lib"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- "github.com/beego/beego/v2/adapter/orm"
- "github.com/beego/beego/v2/core/logs"
- "time"
- )
- type UserDevice struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_sn string `orm:"size(256);null"` // 设备序列号
- T_uuid string `orm:"index;size(256);null"` // Account.Company 绑定用户
- }
- var redisCache_UserDevice cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(UserDevice))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_UserDevice", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- fmt.Println(config)
- var err error
- redisCache_UserDevice, err = cache.NewCache("redis", config)
- if err != nil || redisCache_UserDevice == nil {
- errMsg := "failed to init redis"
- logs.Error(errMsg, err)
- panic(errMsg)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_UserDevice_Set(T_uuid string, deviceList []string) (err error) {
- //json序列化
- str, err := json.Marshal(deviceList)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_UserDevice.Put(T_uuid, str, 24*time.Hour)
- if err != nil {
- logs.Error("Redis_UserDevice_Set", "set key:", T_uuid, ",value:", deviceList, err)
- }
- return
- }
- // if r,is :=Redis_Get(T_sn);is{
- // return r,nil
- // }
- func Redis_UserDevice_Get(key string) (r []string, is bool) {
- if redisCache_UserDevice.IsExist(key) {
- //println("找到key:",key)
- v := redisCache_UserDevice.Get(key)
- err := json.Unmarshal(v.([]byte), &r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return []string{}, false
- }
- return r, true
- }
- //println("没有 找到key:",key)
- return []string{}, false
- }
- func Redis_UserDevice_DelK(key string) (err error) {
- err = redisCache_UserDevice.Delete(key)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return
- }
- // ---------------- 特殊方法 -------------------
- // 添加
- func Add_UserDevice(m UserDevice) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.Insert(&m)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- Redis_UserDevice_DelK(m.T_uuid)
- return id, err
- }
- // 批量添加主机
- func Add_UserDevice_Multi(T_uuid string, m []UserDevice) (id int64, err error) {
- o := orm.NewOrm()
- id, err = o.InsertMulti(len(m), &m)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return id, err
- }
- Redis_UserDevice_DelK(T_uuid)
- return id, err
- }
- func Delete_UserDevice(v UserDevice) error {
- o := orm.NewOrm()
- _, err := o.Delete(&v)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return err
- }
- Redis_UserDevice_DelK(v.T_uuid)
- return nil
- }
- func Read_UserDevice_List(admin Admin) (UserDeviceList []string, err error) {
- if admin.T_pid == 0 {
- return
- }
- T_uuid := admin.T_uuid
- if r, is := Redis_UserDevice_Get(T_uuid); is {
- //println("Redis_Get OK")
- return r, nil
- }
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(UserDevice))
- var maps []UserDevice
- _, err = qs.Filter("T_uuid", T_uuid).All(&maps)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- deviceList := []string{}
- for _, device := range maps {
- deviceList = append(deviceList, device.T_sn)
- }
- Redis_UserDevice_Set(T_uuid, deviceList)
- return deviceList, nil
- }
- func Read_UserDevice_ByT_uuid_T_sn(T_uuid, sn string) (r UserDevice, err error) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(UserDevice))
- err = qs.Filter("T_uuid", T_uuid).Filter("T_sn", sn).One(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return
- }
|