package System

import (
	"FollowUp_Notice/lib"
	"FollowUp_Notice/logs"
	"encoding/json"
	"fmt"
	"github.com/beego/beego/v2/adapter/orm"
	orm2 "github.com/beego/beego/v2/client/orm"
	"time"
)

type SysLogs struct {
	Id      int    `orm:"column(ID);size(11);auto;pk"`
	T_class string `orm:"size(256);null"`  //
	T_title string `orm:"size(256);null"`  // 标题
	T_txt   string `orm:"type(text);null"` // 详情

	CreateTime time.Time `orm:"auto_now_add;type(datetime)"` //auto_now 每次 model 保存时都会对时间自动更新
}
type SysLogs_R struct {
	T_class    string `orm:"size(256);null"`  //
	T_title    string `orm:"size(256);null"`  // 标题
	T_txt      string `orm:"type(text);null"` // 详情
	CreateTime string
}

func (t *SysLogs) TableName() string {
	return "sys_logs" // 数据库名称   // ************** 替换 FormulaList **************
}

func init() {
	//注册模型
	orm.RegisterModel(new(SysLogs))
	//go func() {
	//	time.Sleep(3 * time.Second)
	//	Add_SysLogs("系统", "服务启动", "")
	//}()
}
func SysLogsToSysLogs_R(r SysLogs) (m SysLogs_R) {
	m.T_class = r.T_class
	m.T_title = r.T_title
	m.T_txt = r.T_txt
	m.CreateTime = r.CreateTime.Format("2006-01-02 15:04:05")
	return
}

// 添加  System.Add_SysLogs("MqttServer","参数请求 [Rt_Parameter]","base")
func Add_SysLogs(SysLogs_class string, SysLogs_title string, SysLogs_txt string) {
	o := orm.NewOrm()
	m := SysLogs{T_class: SysLogs_class, T_title: SysLogs_title, T_txt: SysLogs_txt}
	_, err := o.Insert(&m)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
}

// 添加  System.Add_SysLogs("MqttServer","参数请求 [Rt_Parameter]","base")
func Add_SysLogs_T(SysLogs_class string, SysLogs_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 := SysLogs{T_class: SysLogs_class, T_title: SysLogs_title, T_txt: string(jsonStu)}
	_, err = o.Insert(&m)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
}

// 获取列表
func Read_SysLogs_List(SysLogs_class string, SysLogs_title string, page int, page_z int) (r_ []SysLogs_R, cnt int64) {
	o := orm.NewOrm()
	// 也可以直接使用 Model 结构体作为表名
	qs := o.QueryTable(new(SysLogs))
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = int64((page - 1) * page_z)
	}

	// 过滤
	cond := orm.NewCondition()
	cond1 := cond //.And("T_State", 1) .AndNot("status__in", 1).Or("profile__age__gt", 2000)

	if len(SysLogs_class) > 0 {
		//cond1.AndCond(cond.And("T_class", class))
		cond1 = cond1.And("T_class", SysLogs_class)
	}
	if len(SysLogs_title) > 0 {
		//cond1.AndCond(cond.And("T_class", class))
		cond1 = cond1.And("T_title", SysLogs_title)
	}

	// 查询
	var r []SysLogs
	qs.Limit(page_z, offset).SetCond((*orm2.Condition)(cond1)).OrderBy("-Id").All(&r)
	cnt, _ = qs.SetCond((*orm2.Condition)(cond1)).Count()

	for _, v := range r {
		r_ = append(r_, SysLogsToSysLogs_R(v))
	}

	return r_, cnt
}

// 获取列表
func Read_SysLogs_Class() (lists orm2.ParamsList) {
	o := orm.NewOrm()
	var pl_lists orm2.ParamsList
	num, err := o.Raw("SELECT DISTINCT t_class FROM SysLogs LIMIT 0,1000").ValuesFlat(&pl_lists)
	if err == nil {
		fmt.Println("user nums: ", num)
	}
	fmt.Println(len(pl_lists))

	return pl_lists
}
