package System

import (
	"fmt"
	"github.com/beego/beego/v2/adapter/orm"
	orm2 "github.com/beego/beego/v2/client/orm"
	"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 第一次保存时才设置时间
}

type L_JSON struct {
}

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}
	o.Insert(&m)
}

// 获取列表
func Read_Logs_ALL(page, page_z int, Logs_class string) (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)
	}

	qs.Limit(page_z, offset).Filter("Logs_class__icontains", Logs_class).OrderBy("-Id").All(&r)
	cnt, _ = qs.Filter("Logs_class__icontains", Logs_class).Count()

	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
	num, err := o.Raw("SELECT DISTINCT logs_class FROM Logs LIMIT 0,1000").ValuesFlat(&pl_lists)
	if err == nil {
		fmt.Println("user nums: ", num)
	}
	fmt.Println(len(pl_lists))

	return pl_lists
}
