123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210 |
- package Product
- import (
- "AIOTCOER/conf"
- "AIOTCOER/logs"
- "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 ProductPush struct {
- Id int `json:"Id"`
- T_uuid string `orm:"size(8);index" json:"T_uuid" form:"T_uuid"` // 用户识别码(8位) ,管理员可以为空
- T_name string `json:"T_name"` // 协议名称 Mqtt
- T_PushModeId int `json:"T_PushModeId"` //推送模式ID
- T_Config string `orm:"type(text);default('{}')" json:"T_Config" form:"T_Config"` // 产品模型
- T_ConfigJson map[string]interface{} `orm:"-" json:"T_ConfigJson"` // 产品模型 json
- T_log string `orm:"type(text);default('')" json:"T_log" form:"T_log"` //启动日志
- T_state int `orm:"default(0)" json:"T_state"` //状态 0停用 1 开启
- }
- func (t *ProductPush) TableName() string {
- return "ProductPush" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redis_ProductPush cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(ProductPush))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "ProductPush", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- var err error
- redis_ProductPush, err = cache.NewCache("redis", config)
- if err != nil || redis_ProductPush == nil {
- logs.PrintlnError(config, err)
- panic(any(err))
- }
- }
- // ---------------- Redis -------------------
- func (t *ProductPush) redis_Set() (err error) {
- // T_ConfigJson 格式化
- var T_datajson map[string]interface{}
- json.Unmarshal([]byte(t.T_Config), &T_datajson)
- t.T_ConfigJson = T_datajson
- //json序列化
- str, err := json.Marshal(t)
- if err != nil {
- logs.PrintlnError("Redis_Set", err)
- return
- }
- err = redis_ProductPush.Put(fmt.Sprintf("%d", t.Id), str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", t.Id, ",value:", str, err)
- }
- return
- }
- func (t *ProductPush) redis_Get(key string) (is bool) {
- if redis_ProductPush.IsExist(key) {
- //println("找到key:",key)
- v := redis_ProductPush.Get(key)
- if v == nil {
- return false
- }
- json.Unmarshal(v.([]byte), t)
- return true
- }
- //println("没有 找到key:",key)
- return false
- }
- func (t *ProductPush) redis_DelK() (err error) {
- err = redis_ProductPush.Delete(fmt.Sprintf("%d", t.Id))
- return
- }
- // ---------------- 方法 -------------------
- // 添加
- func (t *ProductPush) 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 *ProductPush) Read() (bool bool) {
- if t.redis_Get(fmt.Sprintf("%d", t.Id)) {
- return true
- }
- if !(t.Id > 0) {
- logs.Println("ProductPush.Id:", t.Id)
- return false
- }
- o := orm.NewOrm()
- err := o.Read(t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- return false
- }
- t.redis_Set() // Redis 更新缓存
- return true
- }
- // 修改
- func (t *ProductPush) Update(cols ...string) bool {
- 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 *ProductPush) 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 *ProductPush) Lists(PageIndex int, PageSize int) (r []ProductPush, Total int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ProductPush))
- var offset int64
- if PageIndex <= 1 {
- offset = 0
- } else {
- offset = int64((PageIndex - 1) * PageSize)
- }
- // 筛选参数
- cond := orm.NewCondition()
- //if t.T_mode > 0 {
- // cond = cond.And("T_mode", t.T_mode) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- //}
- //if len(t.T_name) > 0 {
- // cond = cond.And("T_name__icontains", t.T_name) // .AndNot("status__in", 1).Or("profile__age__gt", 2000)
- //}
- if len(t.T_uuid) == 8 {
- cond = cond.And("T_uuid", t.T_uuid) // 用户识别码
- }
- // 执行
- 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_Config) > 0 {
- var T_datajson map[string]interface{}
- json.Unmarshal([]byte(r[i].T_Config), &T_datajson)
- r[i].T_ConfigJson = T_datajson
- }
- }
- return r, Total
- }
- // 获取列表
- func ProductPushListALL() (r []ProductPush) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ProductPush))
- // 筛选参数
- cond := orm.NewCondition()
- cond = cond.And("T_state", 1) // 用户识别码
- // 执行
- qs.SetCond((*orm2.Condition)(cond)).All(&r)
- for i, _ := range r {
- if len(r[i].T_Config) > 0 {
- var T_datajson map[string]interface{}
- json.Unmarshal([]byte(r[i].T_Config), &T_datajson)
- r[i].T_ConfigJson = T_datajson
- }
- }
- return r
- }
|