123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- 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 Logs struct {
- Id int `orm:"column(ID);size(11);auto;pk"`
- 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 *Logs) TableName() string {
- return "logs" // 数据库名称 // ************** 替换 FormulaList **************
- }
- func init() {
- //注册模型
- orm.RegisterModel(new(Logs))
- }
- // 添加 System.Add_Logs("MqttServer","参数请求 [Rt_Parameter]","base")
- func Add_Logs(Logs_class string, Logs_Title string, Logs_Txt string) {
- o := orm.NewOrm()
- m := Logs{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_Logs_T(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(lib.FuncName(), err)
- }
- m := Logs{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_Logs_ALL(Logs_class string, page int, page_z int) (r []Logs, cnt int64) {
- o := orm.NewOrm()
- // 也可以直接使用 Model 结构体作为表名
- qs := o.QueryTable(new(Logs))
- var offset int64
- if page <= 1 {
- offset = 0
- } else {
- offset = int64((page - 1) * page_z)
- }
- cond := orm.NewCondition()
- cond1 := cond
- 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)
- return
- }
- cnt, err = qs.SetCond((*orm2.Condition)(cond1)).Count()
- if err != nil {
- logs.Error(lib.FuncName(), err)
- return
- }
- return r, cnt
- }
- type CLASS_lists struct {
- Logs_class string
- }
- // 获取列表
- func Read_Logs_Class() (lists orm2.ParamsList) {
- o := orm.NewOrm()
- var pl_lists orm2.ParamsList
- _, err := o.Raw("SELECT DISTINCT logs_class FROM Logs LIMIT 0,1000").ValuesFlat(&pl_lists)
- if err != nil {
- logs.Error(lib.FuncName(), err)
- }
- return pl_lists
- }
|