123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- package Product
- import (
- "Yunlot/conf"
- "Yunlot/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 ProductRelay struct {
- Id int `orm:"column(ID);size(11);auto;pk" json:"ID"`
- T_ProductID string `orm:"size(8);index" json:"T_ProductID" form:"T_ProductID"` // 产品型号 随机生成(8位)
- T_name string `orm:"size(256);" json:"T_name" form:"T_name"` // 名称
- T_rtab string `orm:"size(32);index" json:"T_rtab" form:"T_rtab"` //转发 TAB 标识 拼接:AAAA.BBBB. 对象后面加点
- T_mode int `orm:"size(1);default(0)" json:"T_mode" form:"T_mode"` //接入方式 0:Mqtt 1:http 2:tcp 3:CoAP 4:websocket
- T_pub string `orm:"size(256);" json:"T_pub" form:"T_pub"` //发布号
- }
- func (t *ProductRelay) TableName() string {
- return "ProductRelay" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redis_ProductRelay cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(ProductRelay))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_ProductRelay", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- var err error
- redis_ProductRelay, err = cache.NewCache("redis", config)
- if err != nil || redis_ProductRelay == nil {
- logs.Println(config)
- panic(any(err))
- }
- }
- // ---------------- Redis -------------------
- func (t *ProductRelay) redis_Set() (err error) {
- //json序列化
- str, err := json.Marshal(t)
- if err != nil {
- logs.PrintlnError("Redis_Set", err)
- return
- }
- err = redis_ProductRelay.Put(t.T_ProductID+"-"+t.T_rtab, str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", t.T_ProductID+"-"+t.T_rtab, ",value:", str, err)
- }
- return
- }
- func (t *ProductRelay) redis_Get(key string) (is bool) {
- if redis_ProductRelay.IsExist(key) {
- //println("找到key:",key)
- v := redis_ProductRelay.Get(key)
- if v == nil {
- return false
- }
- json.Unmarshal(v.([]byte), t)
- return true
- }
- //println("没有 找到key:",key)
- return false
- }
- func (t *ProductRelay) redis_DelK() (err error) {
- err = redis_ProductRelay.Delete(t.T_ProductID)
- return
- }
- // ---------------- 方法 -------------------
- // 添加
- func (t *ProductRelay) 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 *ProductRelay) Read() (bool bool) {
- if t.redis_Get(t.T_ProductID+"-"+t.T_rtab) {
- return true
- }
- o := orm.NewOrm()
- err := o.Read(&t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- return false
- }
- t.redis_Set() // Redis 更新缓存
- return true
- }
- // 修改
- func (t *ProductRelay) 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 *ProductRelay) 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 *ProductRelay) Lists(PageIndex int, PageSize int) (r []ProductRelay, Total int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ProductRelay))
- 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)
- }
- // 执行
- qs.Limit(PageSize, offset).SetCond((*orm2.Condition)(cond)).All(&r)
- Total, _ = qs.SetCond((*orm2.Condition)(cond)).Count()
- return r, Total
- }
|