123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- 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 ProductProt struct {
- Id int `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
- T_uuid string `orm:"size(8);index" json:"T_uuid" form:"T_uuid"` // 用户识别码(8位) ,管理员可以为空
- T_name string `orm:"size(256);" json:"T_name" form:"T_name"` // 协议名称
- 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_prot int `orm:"size(1);default(0)" json:"T_Prot" form:"T_prot"` //接入协议ID 0:统一协议
- T_lang int `orm:"size(1);default(1)" json:"T_lang" form:"T_lang"` //编程语言 0: 无解析 1: go 2: C
- T_analysis string `orm:"size(100);default('')" json:"T_analysis" form:"T_analysis"` //数据解析
- T_text string `orm:"type(text);default('')" json:"T_text" form:"T_text"` // 代码内容
- T_describe string `orm:"type(text);default('')" json:"T_describe" form:"T_describe"` // 描述内容
- //T_reply string `orm:"size(100);default(_reply)" json:"T_reply" form:"T_reply"` //返回后缀 _reply
- }
- func (t *ProductProt) TableName() string {
- return "ProductProt" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redis_ProductProt cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(ProductProt))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "ProductProt", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- var err error
- redis_ProductProt, err = cache.NewCache("redis", config)
- if err != nil || redis_ProductProt == nil {
- logs.Println(config)
- println(err)
- panic(any(err))
- }
- }
- // ---------------- Redis -------------------
- func (t *ProductProt) redis_Set() (err error) {
- //json序列化
- str, err := json.Marshal(t)
- if err != nil {
- logs.PrintlnError("Redis_Set", err)
- return
- }
- err = redis_ProductProt.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 *ProductProt) redis_Get(key string) (is bool) {
- if redis_ProductProt.IsExist(key) {
- //println("找到key:",key)
- v := redis_ProductProt.Get(key)
- if v == nil {
- return false
- }
- json.Unmarshal(v.([]byte), t)
- return true
- }
- //println("没有 找到key:",key)
- return false
- }
- func (t *ProductProt) redis_DelK() (err error) {
- err = redis_ProductProt.Delete(fmt.Sprintf("%d", t.Id))
- return
- }
- // ---------------- 方法 -------------------
- // 添加
- func (t *ProductProt) 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 *ProductProt) Read() (bool bool) {
- if t.redis_Get(fmt.Sprintf("%d", t.Id)) {
- return true
- }
- if !(t.Id > 0) {
- logs.Println("ProductProt.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 *ProductProt) 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 *ProductProt) 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 *ProductProt) Lists(PageIndex int, PageSize int) (r []ProductProt, Total int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ProductProt))
- var offset int64
- if PageIndex <= 1 {
- offset = 0
- } else {
- offset = int64((PageIndex - 1) * PageSize)
- }
- // 筛选参数
- cond := orm.NewCondition()
- cond = cond.And("T_mode", t.T_mode)
- 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()
- return r, Total
- }
- // 获取列表
- func (t *ProductProt) Lists_All() (r []ProductProt) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ProductProt))
- // 筛选参数
- 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.SetCond((*orm2.Condition)(cond)).All(&r)
- return r
- }
|