package System import ( "Cold_Api/controllers/lib" "encoding/json" "github.com/beego/beego/v2/adapter/orm" orm2 "github.com/beego/beego/v2/client/orm" "github.com/beego/beego/v2/core/logs" "time" ) type UserLogs struct { Id int `orm:"column(ID);size(11);auto;pk"` Logs_uuid string `orm:"size(256);null"` // Logs_class string `orm:"size(256);null"` // Logs_Title string `orm:"size(256);null"` // 标题 Logs_Txt string `orm:"type(text);null"` // 详情 CreateTime time.Time `orm:"column(create_time);type(timestamp);null;auto_now_add"` //auto_now 每次 model 保存时都会对时间自动更新 UpdateTime time.Time `orm:"column(update_time);type(timestamp);null;auto_now"` //auto_now_add 第一次保存时才设置时间 } func (t *UserLogs) TableName() string { return "user_logs" // 数据库名称 // ************** 替换 FormulaList ************** } func init() { //注册模型 orm.RegisterModel(new(UserLogs)) } // 添加 System.Add_UserLogs("MqttServer","参数请求 [Rt_Parameter]","base") func Add_UserLogs(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt string) { o := orm.NewOrm() m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: Logs_Txt} _, err := o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) } } func Add_UserLogs_T(Logs_uuid string, Logs_class string, Logs_Title string, Logs_Txt_T interface{}) { o := orm.NewOrm() jsonStu, err := json.Marshal(Logs_Txt_T) if err != nil { logs.Error("Add_UserLogs_T JSON ,err=", err) } m := UserLogs{Logs_uuid: Logs_uuid, Logs_class: Logs_class, Logs_Title: Logs_Title, Logs_Txt: string(jsonStu)} _, err = o.Insert(&m) if err != nil { logs.Error(lib.FuncName(), err) } } // 获取列表 func Read_UserLogs_ALL(Logs_uuid string, Logs_class string, page int, page_z int) (r []UserLogs, cnt int64) { o := orm.NewOrm() // 也可以直接使用 Model 结构体作为表名 qs := o.QueryTable(new(UserLogs)) var offset int64 if page <= 1 { offset = 0 } else { offset = int64((page - 1) * page_z) } cond := orm.NewCondition() cond1 := cond.And("Logs_uuid", Logs_uuid) if len(Logs_class) > 0 { cond1 = cond1.And("Logs_class", Logs_class) } _, err := qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r) if err != nil { logs.Error(lib.FuncName(), err) } cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count() if err != nil { logs.Error(lib.FuncName(), err) } return r, cnt } // 获取列表 func Read_UserLogs_Class() (lists orm2.ParamsList) { o := orm.NewOrm() var pl_lists orm2.ParamsList _, err := o.Raw("SELECT DISTINCT logs_class FROM user_logs LIMIT 0,1000").ValuesFlat(&pl_lists) if err != nil { logs.Error(lib.FuncName(), err) return } return pl_lists }