package Account

import (
	"ColdVerify_server/conf"
	"ColdVerify_server/lib"
	"ColdVerify_server/logs"
	"fmt"
	"github.com/beego/beego/v2/adapter/orm"
	"time"
)

type UserPower struct {
	Id     int    `orm:"column(ID);size(11);auto;pk"`
	T_name string `orm:"size(256);null"` //  权限名称

	Logs_Sys_r  int `orm:"size(2);;default(0)"` //  系统日志
	Logs_User_r int `orm:"size(2);;default(0)"` //  用户日志

	T_State    int       `orm:"size(200);default(1)"`                                  //  0删除  1 正常
	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 *UserPower) TableName() string {
	return "user_power" // 数据库名称   // ************** 替换 FormulaList **************
}

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

}

// 修改
func Update_UserPower_ById_Basics(m UserPower) (err error) {
	o := orm.NewOrm()
	v := UserPower{Id: m.Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		m.Id = v.Id
		if num, err = o.Update(&m, "Power_name", "Power_Administration", "Power_User_r", "Power_User_entry", "Power_Device_r", "Power_Device_a", "Power_Device_Parameter_e", "Power_DeviceSensor_Class", "Power_DeviceSensor_Parameter_e", "Power_DeviceSensor_Compensate_e", "Power_Device_Bind_e", "Power_DataScreen_r", "Logs_Sys_r", "Logs_User_r"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return err
}

// 添加
func Add_UserPower(m UserPower) (id int64, err error) {
	o := orm.NewOrm()
	id, err = o.Insert(&m)
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return id, err
}

// 获取 ById
func Read_UserPower_ById(Id int) (r UserPower, err error) {
	o := orm.NewOrm()
	r = UserPower{Id: Id}
	err = o.Read(&r) // o.Read(&r,"Tokey") 如果不是 主键 就得指定字段名
	if err != nil {
		logs.Error(lib.FuncName(), err)
	}
	return r, err
}

// 获取列表
func Read_Power_List_ALL_1() (maps []UserPower) {

	o := orm.NewOrm()
	qs := o.QueryTable(new(UserPower))

	qs.Filter("T_State", 1).OrderBy("Id").All(&maps)

	return maps
}

// 获取列表
func Read_Power_List_ALL_Admin_Power(T_power int) (maps []UserPower) {

	o := orm.NewOrm()
	qs := o.QueryTable(new(UserPower))

	qs.Filter("T_State", 1).Filter("Id__gte", T_power).OrderBy("Id").All(&maps)

	return maps
}

// 获取列表
func Read_Power_List_ALL(page int, Power_name string) (maps []UserPower, cnt int64) {

	o := orm.NewOrm()
	// 也可以直接使用 Model 结构体作为表名

	qs := o.QueryTable(new(UserPower))
	var offset int64
	if page <= 1 {
		offset = 0
	} else {
		offset = int64((page - 1) * conf.Page_size)
	}

	qs.Limit(conf.Page_size, offset).Filter("Power_name__icontains", Power_name).Filter("T_State", 1).OrderBy("Id").All(&maps)
	cnt, _ = qs.Filter("Power_name__icontains", Power_name).Filter("T_State", 1).Count()

	return maps, cnt
}

// 修改
func Update_Delete_UserPower_Byid(Id int) (err error) {
	o := orm.NewOrm()
	v := UserPower{Id: Id}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		v.T_State = 0
		if num, err = o.Update(&v, "T_State"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return err
}

func UserPowerListToPowerMap(T []UserPower) map[int]string {
	maps := make(map[int]string, len(T))
	for _, v := range T {
		maps[v.Id] = v.T_name
	}
	return maps
}
