package controllers

import (
	"ColdVerify_server/conf"
	"ColdVerify_server/lib"
	"ColdVerify_server/models/Account"
	"ColdVerify_server/models/Certificate"
	"ColdVerify_server/models/Device"
	"ColdVerify_server/models/System"
	beego "github.com/beego/beego/v2/server/web"
	"math"
)

type DeviceClassController struct {
	beego.Controller
}

// 列表 -
func (c *DeviceClassController) List() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	var r_jsons lib.R_JSONS
	page, _ := c.GetInt("page")
	if page < 1 {
		page = 1
	}
	page_z, _ := c.GetInt("page_z")
	if page_z < 1 {
		page_z = conf.Page_size
	}

	T_name := c.GetString("T_name")

	var cnt int64
	List, cnt := Device.Read_DeviceClass_List(User_r.T_uuid, T_name, page, page_z)
	page_size := math.Ceil(float64(cnt) / float64(page_z))
	r_jsons.List = List
	r_jsons.Page = page
	r_jsons.Page_size = int(page_size)
	r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
	r_jsons.Num = int(cnt)

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
	c.ServeJSON()
	return
}

// 获取-
func (c *DeviceClassController) Get() {
	// 验证登录 User_is, User_r
	_, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	Id, err := c.GetInt("Id")
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	r, is := Device.Read_DeviceClass_ById(Id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r}
	c.ServeJSON()
	return
}

// 添加-
func (c *DeviceClassController) Add() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	T_name := c.GetString("T_name")

	var_ := Device.DeviceClass{
		T_uuid:  User_r.T_uuid,
		T_name:  T_name,
		T_State: 1,
	}

	Id, is := Device.Add_DeviceClass(var_)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "添加", var_)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
	c.ServeJSON()
	return
}

// 修改-
func (c *DeviceClassController) Up() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	T_name := c.GetString("T_name")

	Id, err := c.GetInt("Id")
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	r, is := Device.Read_DeviceClass_ById(Id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	// .......
	if len(T_name) > 0 {
		r.T_name = T_name
	}

	// .......
	if !Device.Update_DeviceClass(r, "T_name") {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "修改", r)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}

// 删除-
func (c *DeviceClassController) Del() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	Id, err := c.GetInt("Id")
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	if r, is := Device.Read_DeviceClass_ById(Id); is {

		if !Device.Delete_DeviceClass_(r) {
			c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败！"}
			c.ServeJSON()
			return
		}
		System.Add_UserLogs_T(User_r.T_uuid, "分类管理", "删除", r)

		c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
		c.ServeJSON()
		return
	}

	c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
	c.ServeJSON()
	return
}

//--------------------------

// 列表 -
func (c *DeviceClassController) List_List() {
	// 验证登录 User_is, User_r
	_, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	var r_jsons lib.R_JSONS
	page, _ := c.GetInt("page")
	if page < 1 {
		page = 1
	}
	page_z, _ := c.GetInt("page_z")
	if page_z < 1 {
		page_z = conf.Page_size
	}

	T_class, _ := c.GetInt("T_class")
	T_sn := c.GetString("T_sn")

	_, is := Device.Read_DeviceClass_ById(T_class)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
		c.ServeJSON()
		return
	}

	var cnt int64
	//List, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, 9999)
	List, cnt := Device.Read_DeviceClassList_OrderList(T_class, T_sn, page, 9999)

	page_size := math.Ceil(float64(cnt) / float64(page_z))
	r_jsons.List = List
	r_jsons.Page = page
	r_jsons.Page_size = int(page_size)
	r_jsons.Pages = lib.Func_page(int64(page), int64(page_size))
	r_jsons.Num = int(cnt)

	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: r_jsons}
	c.ServeJSON()
	return
}

// 添加-
func (c *DeviceClassController) List_Add() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	T_class, _ := c.GetInt("T_class")
	T_id := c.GetString("T_id")
	T_sn := c.GetString("T_sn")
	T_remark := c.GetString("T_remark")

	_, is := Device.Read_DeviceClass_ById(T_class)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
		c.ServeJSON()
		return
	}

	if _, is = Device.Read_Device(T_sn); !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_sn 不存在!"}
		c.ServeJSON()
		return
	}

	if dc, is := Device.Read_DeviceClassList_T_class_T_sn(T_class, T_sn); is && dc.Id > 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复添加"}
		c.ServeJSON()
		return
	}
	if _, is = Certificate.Read_Certificate(T_sn); !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "证书不存在!"}
		c.ServeJSON()
		return
	}

	var pdf Certificate.CertificatePdf
	pdfList, _ := Certificate.Read_CertificatePdf_Newest(T_sn)
	if len(pdfList) > 0 {
		pdf = pdfList[0]
	}

	var_ := Device.DeviceClassList{
		T_class:        T_class,
		T_id:           T_id,
		T_sn:           T_sn,
		T_failure_time: pdf.T_failure_time,
		T_pdf:          pdf.T_pdf,
		T_remark:       T_remark,
		T_State:        1,
	}

	Id, is := Device.Add_DeviceClassList(var_)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "添加", var_)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
	c.ServeJSON()
	return
}

// 修改-
func (c *DeviceClassController) List_Up() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	T_id := c.GetString("T_id")
	T_remark := c.GetString("T_remark")

	Id, err := c.GetInt("Id")
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	r, is := Device.Read_DeviceClassList_ById(Id)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	_, is = Device.Read_DeviceClass_ById(r.T_class)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
		c.ServeJSON()
		return
	}

	if len(T_id) > 0 {
		r.T_id = T_id
	}
	if len(T_remark) > 0 {
		r.T_remark = T_remark
	}

	if !Device.Update_DeviceClassList(r, "T_id", "T_remark") {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "修改", r)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}

// 删除-
func (c *DeviceClassController) List_Del() {
	// 验证登录 User_is, User_r
	User_r, User_is := Account.Verification_Admin(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	if !User_is {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "请重新登录!"}
		c.ServeJSON()
		return
	}

	Id, err := c.GetInt("Id")
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
		c.ServeJSON()
		return
	}

	if r, is := Device.Read_DeviceClassList_ById(Id); is {

		_, is = Device.Read_DeviceClass_ById(r.T_class)
		if !is {
			c.Data["json"] = lib.JSONS{Code: 202, Msg: "T_class 错误!"}
			c.ServeJSON()
			return
		}

		if !Device.Delete_DeviceClassList_(r) {
			c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败！"}
			c.ServeJSON()
			return
		}
		System.Add_UserLogs_T(User_r.T_uuid, "分类设备管理", "删除", r)

		c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
		c.ServeJSON()
		return
	}

	c.Data["json"] = lib.JSONS{Code: 202, Msg: "Id 错误!"}
	c.ServeJSON()
	return
}
