package controllers

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

type CertificateController struct {
	beego.Controller
}

// 列表-
func (c *CertificateController) 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_sn := c.GetString("T_sn")
	T_layout_no := c.GetString("T_layout_no")
	Time_start := c.GetString("Time_start")
	Time_end := c.GetString("Time_end")
	T_release_time_sort,_ := c.GetInt("T_release_time_sort") // 1 升序 2 降序
	T_failure_time_sort,_  := c.GetInt("T_failure_time_sort")// 1 升序 2 降序


	if len(Time_start) > 0 && !lib.IsDateStr(Time_start) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
		c.ServeJSON()
		return
	}

	if len(Time_end) > 0 && !lib.IsDateStr(Time_end) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
		c.ServeJSON()
		return
	}

	var cnt int
	List, cnt := Certificate.Read_Certificate_List(T_sn, T_layout_no, Time_start, Time_end, T_release_time_sort,T_failure_time_sort,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 = cnt

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

// 删除-
func (c *CertificateController) 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
	}

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

	r, err := Certificate.Read_CertificatePdf_Newest(T_sn)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "查询失败!"}
		c.ServeJSON()
		return
	}
	if len(r) == 0 {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "证书不存在!"}
		c.ServeJSON()
		return
	}

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

// 添加-
func (c *CertificateController) 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_sn := c.GetString("T_sn")
	T_layout_no := c.GetString("T_layout_no")

	var_ := Certificate.Certificate{
		T_sn:        T_sn,
		T_layout_no: T_layout_no,
		T_State:     1,
	}

	if _, is := Certificate.Read_Certificate(T_sn); is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "设备编号重复!"}
		c.ServeJSON()
		return
	}
	if _, is := Certificate.Read_Certificate_ByT_layout_no(T_layout_no); is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "布局编号重复!"}
		c.ServeJSON()
		return
	}

	Id, is := Certificate.Add_Certificate(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 *CertificateController) Edit() {
	// 验证登录 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
	}

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

	T_sn := c.GetString("T_sn")
	T_layout_no := c.GetString("T_layout_no")

	if _, is = Certificate.Read_Certificate(T_sn); is && r.T_sn != T_sn {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "设备编号重复!"}
		c.ServeJSON()
		return
	}
	if _, is = Certificate.Read_Certificate_ByT_layout_no(T_layout_no); is && r.T_layout_no != T_layout_no {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "布局编号重复!"}
		c.ServeJSON()
		return
	}
	r.T_sn = T_sn
	r.T_layout_no = T_layout_no
	is = Certificate.Update_Certificate(r, "T_sn", "T_layout_no")
	if !is {
		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!", Data: Id}
	c.ServeJSON()

	return
}

// 删除-
func (c *CertificateController) 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 := Certificate.Read_Certificate_ById(Id); is {
		if !Certificate.Delete_Certificate_(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 *CertificateController) Pdf_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_Certificate_sn := c.GetString("T_Certificate_sn")

	var cnt int64
	List, cnt := Certificate.Read_CertificatePdf_List(T_Certificate_sn, 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 *CertificateController) Pdf_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_Certificate_sn := c.GetString("T_Certificate_sn")
	T_release_time := c.GetString("T_release_time")
	T_failure_time := c.GetString("T_failure_time")
	T_pdf := c.GetString("T_pdf")

	if !lib.IsDateStr(T_release_time) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
		c.ServeJSON()
		return
	}

	if !lib.IsDateStr(T_failure_time) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
		c.ServeJSON()
		return
	}

	var_ := Certificate.CertificatePdf{
		T_Certificate_sn: T_Certificate_sn,
		T_release_time:   T_release_time,
		T_failure_time:   T_failure_time,
		T_pdf:            T_pdf,
		T_State:          1,
	}

	if _, err := Certificate.Read_CertificatePdf(T_Certificate_sn, T_release_time, T_failure_time); err == nil {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "重复添加!"}
		c.ServeJSON()
		return
	}

	Id, is := Certificate.Add_CertificatePdf(var_)
	if !is {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "添加失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(user_r.T_uuid, "校准证书Pdf", "添加", var_)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Id}
	c.ServeJSON()

	return
}

// 修改-
func (c *CertificateController) Pdf_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
	}

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

	if !lib.IsDateStr(T_release_time) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
		c.ServeJSON()
		return
	}

	if !lib.IsDateStr(T_failure_time) {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "时间格式错误!"}
		c.ServeJSON()
		return
	}

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

	_, err = Certificate.Read_CertificatePdf(T_Certificate_sn, T_release_time, T_failure_time)
	if err == nil && r.T_failure_time != T_failure_time && r.T_release_time != T_release_time {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "数据已存在!"}
		c.ServeJSON()
		return
	}

	if len(T_release_time) > 0 {
		r.T_release_time = T_release_time
	}
	if len(T_failure_time) > 0 {
		r.T_failure_time = T_failure_time
	}
	if len(T_pdf) > 0 {
		r.T_pdf = T_pdf
	}
	if !Certificate.Update_CertificatePdf(r, "T_release_time", "T_failure_time", "T_pdf") {
		c.Data["json"] = lib.JSONS{Code: 202, Msg: "修改失败!"}
		c.ServeJSON()
		return
	}
	System.Add_UserLogs_T(user_r.T_uuid, "校准证书Pdf", "修改", r)
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!"}
	c.ServeJSON()
	return
}

// 删除-
func (c *CertificateController) Pdf_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 := Certificate.Read_CertificatePdf_ById(Id); is {
		if !Certificate.Delete_CertificatePdf_(r) {
			c.Data["json"] = lib.JSONS{Code: 202, Msg: "删除失败！"}
			c.ServeJSON()
			return
		}
		System.Add_UserLogs_T(user_r.T_uuid, "校准证书Pdf", "删除", 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
}
