package controllers

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

type DeviceController struct {
	beego.Controller
}

// 列表 -
func (c *DeviceController) 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_MSISDN := c.GetString("T_MSISDN")

	var cnt int64
	List, cnt := Device.Read_Device_List(T_sn, T_MSISDN, 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 *DeviceController) 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_MSISDN := c.GetString("T_MSISDN")

	var_ := Device.Device{
		T_sn:     T_sn,
		T_MSISDN: T_MSISDN,
		T_State:  1,
	}

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

	Id, is := Device.Add_Device(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 *DeviceController) 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_sn := c.GetString("T_sn")
	T_MSISDN := c.GetString("T_MSISDN")

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

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

	r.T_sn = T_sn
	r.T_MSISDN = T_MSISDN
	// .......
	if !Device.Update_Device(r, "T_sn", "T_MSISDN") {
		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 *DeviceController) 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_Device_ById(Id); is {
		if !Device.Delete_Device(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 *DeviceController) Device_Class() {
	// 验证登录 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_s
	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")

	r, 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
	//DeviceList, cnt := Device.Read_DeviceClassList_List(T_class, T_sn, page, 9999)
	DeviceList, cnt := Device.Read_DeviceClassList_OrderList(T_class, T_sn, page, 9999)

	for _, v := range DeviceList {
		Device_r, _ := Device.Read_Device(v.T_sn)
		Device_r.T_note_file_num = Device.Read_DeviceSensorData_List_z(v.T_sn, r.CreateTime.Format("2006-01-02 15:04:05"))
		r_jsons.List = append(r_jsons.List, Device_r)
	}
	page_size := math.Ceil(float64(cnt) / float64(page_z))
	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 *DeviceController) Device_Data() {
	// 验证登录 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
	}

	type R_JSONS_s struct {
		//必须的大写开头
		List      []Device.DeviceData_
		Num       int
		Page      int
		Page_size int
		Pages     []lib.Page_T
	}

	var r_jsons R_JSONS_s
	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")
	Time_start := c.GetString("Time_start")
	Time_and := c.GetString("Time_end")

	var cnt int
	r_jsons.List, cnt = Device.Read_DeviceSensorData_List(T_sn, Time_start, Time_and, page, page_z)

	page_size := math.Ceil(float64(cnt) / float64(page_z))
	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
}
