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 UserLogs struct {
	Id      int    `orm:"column(ID);size(11);auto;pk"`
	T_uuid  string `orm:"size(32);null"`   //
	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 UserLogs_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 *UserLogs) TableName() string {
	return "user_logs" // 数据库名称   // ************** 替换 FormulaList **************
}

func init() {
	//注册模型
	orm.RegisterModel(new(UserLogs))

}
func UserLogsToUserLogs_R(r UserLogs) (m UserLogs_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_UserLogs("MqttServer","参数请求 [Rt_Parameter]","base")
func Add_UserLogs(T_uuid string, UserLogs_class string, UserLogs_title string, UserLogs_txt string) {
	o := orm.NewOrm()
	m := UserLogs{T_uuid: T_uuid, T_class: UserLogs_class, T_title: UserLogs_title, T_txt: UserLogs_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(lib.FuncName(), err)
	}
	m := UserLogs{T_uuid: Logs_uuid, T_class: Logs_class, T_title: Logs_Title, T_txt: string(jsonStu)}
	_, err = o.Insert(&m)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
}

// 获取列表
func Read_UserLogs_List(T_uuid string, UserLogs_class string, UserLogs_title string, page int, page_z int) (r_ []UserLogs_R, 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("T_uuid", T_uuid) //.And("T_State", 1) .AndNot("status__in", 1).Or("profile__age__gt", 2000)

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

	// 查询
	var r []UserLogs
	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_, UserLogsToUserLogs_R(v))
	}

	return r_, cnt
}

type CLASS_lists struct {
	UserLogs_class string
}

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

	return pl_lists
}
