123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- 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 ProductTab struct {
- Id int `orm:"column(ID);size(11);auto;pk" json:"Id" form:"Id"`
- T_ProductID string `orm:"size(8);index" json:"T_ProductID" form:"T_ProductID"` // 产品型号 随机生成(8位)
- T_fid int `orm:"size(11);index" json:"T_fid" form:"T_fid"` // 父级ID,0为根
- T_name string `orm:"size(256);" json:"T_name" form:"T_name"` // 名称
- T_tab string `orm:"size(32);" json:"T_tab" form:"T_tab"` // 标签
- T_type int `orm:"size(6);" json:"T_type" form:"T_type"` // 数据类型 1 数值 2 文本 3 枚举
- T_attr string `orm:"size(200);" json:"T_attr" form:"T_attr"` // 属性
- T_text string `orm:"size(200);" json:"T_text" form:"T_text"` // 说明
- T_read bool `orm:"default(false);" json:"T_read" form:"T_read"` // 只读
- // T_attr属性 定义
- /*
- 1 数值
- unit 单位
- dvalue 默认值
- accuracy 精度
- min 最小值
- max 最大值
- 2 文本
- dvalue 默认值
- 3 枚举
- map [] 枚举
- value 值
- text 描述
- */
- }
- func (t *ProductTab) TableName() string {
- return "ProductTab" // 数据库名称 // ************** 替换 FormulaList **************
- }
- var redis_ProductTab cache.Cache
- func init() {
- //注册模型
- orm.RegisterModel(new(ProductTab))
- config := fmt.Sprintf(`{"key":"%s","conn":"%s","dbNum":"%s","password":"%s"}`,
- "redis_ProductTab", conf.Redis_address, conf.Redis_dbNum, conf.Redis_password)
- var err error
- redis_ProductTab, err = cache.NewCache("redis", config)
- if err != nil || redis_ProductTab == nil {
- logs.Println(config)
- panic(any(err))
- }
- }
- // ---------------- Redis -------------------
- func (t *ProductTab) redis_Set() (err error) {
- //json序列化
- str, err := json.Marshal(t)
- if err != nil {
- logs.PrintlnError("Redis_Set", err)
- return
- }
- err = redis_ProductTab.Put(t.T_ProductID, str, 24*time.Hour)
- if err != nil {
- logs.Println("set key:", t.T_ProductID, ",value:", str, err)
- }
- return
- }
- func (t *ProductTab) redis_Get(key string) (is bool) {
- if redis_ProductTab.IsExist(key) {
- //println("找到key:",key)
- v := redis_ProductTab.Get(key)
- if v == nil {
- return false
- }
- json.Unmarshal(v.([]byte), t)
- return true
- }
- //println("没有 找到key:",key)
- return false
- }
- func (t *ProductTab) redis_DelK() (err error) {
- err = redis_ProductTab.Delete(t.T_ProductID)
- return
- }
- // ---------------- 方法 -------------------
- // 添加
- func (t *ProductTab) 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 *ProductTab) Read(T_type string) (bool bool) {
- if t.redis_Get(T_type) {
- return true
- }
- o := orm.NewOrm()
- t.T_ProductID = T_type
- err := o.Read(&t) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
- if err != nil {
- return false
- }
- t.redis_Set() // Redis 更新缓存
- return true
- }
- // 修改
- func (t *ProductTab) 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 *ProductTab) 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 *ProductTab) Lists(PageIndex int, PageSize int) (r []ProductTab, Total int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(ProductTab))
- 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)
- }
- cond = cond.And("T_fid", t.T_fid) // .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
- }
|