123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- package Device
- import (
- "ColdVerify_server/conf"
- "ColdVerify_server/lib"
- "ColdVerify_server/logs"
- "encoding/json"
- "fmt"
- "github.com/astaxie/beego/cache"
- "github.com/beego/beego/v2/adapter/orm"
- orm2 "github.com/beego/beego/v2/client/orm"
- _ "github.com/go-sql-driver/mysql"
- "time"
- )
- // 模板
- type DeviceClass struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- T_uuid string `orm:"size(256);null"` // 管理员 UUID
- T_name string `orm:"size(256);null"` // 标题
- 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 保存时都会对时间自动更新
- }
- func (t *DeviceClass) TableName() string {
- return "device_class" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redisCache_DeviceClass cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(DeviceClass))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_"+"DeviceClass", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- logs.Println(config)
- var err error
- redisCache_DeviceClass, err = cache.NewCache("redis", config)
- if err != nil || redisCache_DeviceClass == nil {
- errMsg := "failed to init redis"
- logs.Println(errMsg, err)
- }
- }
- // ---------------- Redis -------------------
- // Redis_Set(m.T_sn,m) // Redis 更新缓存
- func Redis_DeviceClass_Set(key string, r DeviceClass) (err error) {
- //json序列化
- str, err := json.Marshal(r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- err = redisCache_DeviceClass.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_DeviceClass_Get(key string) (r DeviceClass, is bool) {
- if redisCache_DeviceClass.IsExist(key) {
- logs.Println("找到key:", key)
- v := redisCache_DeviceClass.Get(key)
- json.Unmarshal(v.([]byte), &r)
- return r, true
- }
- logs.Println("没有 找到key:", key)
- return DeviceClass{}, false
- }
- func Redis_DeviceClass_DelK(key string) (err error) {
- err = redisCache_DeviceClass.Delete(key)
- return
- }
- // ---------------- 特殊方法 -------------------
- // 获取 ById
- func Read_DeviceClass_ById(id int) (r DeviceClass, is bool) {
- o := orm.NewOrm()
- r = DeviceClass{Id: id}
- err := o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return r, false
- }
- return r, true
- }
- // 添加
- func Add_DeviceClass(r DeviceClass) (id int64, is bool) {
- o := orm.NewOrm()
- id, err := o.Insert(&r)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return 0, false
- }
- return id, true
- }
- // 删除
- func Delete_DeviceClass(v DeviceClass) 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
- }
- return true
- }
- // 删除
- func Delete_DeviceClass_(v DeviceClass) 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
- }
- return true
- }
- // 修改
- func Update_DeviceClass(m DeviceClass, cols ...string) bool {
- o := orm.NewOrm()
- if num, err := o.Update(&m, cols...); err == nil {
- fmt.Println("Number of records updated in database:", num)
- return true
- } else {
- logs.Println(lib.FuncName(), err)
- }
- return false
- }
- // 获取列表
- func Read_DeviceClass_List(T_uuid string, T_name string, page int, page_z int) (r []DeviceClass, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(DeviceClass))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond.And("T_uuid", T_uuid).And("T_name__icontains", T_name).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(&r)
- cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()
- return r, cnt
- }
|