package controllers

import (
	"Cold_Data/lib"
	"Cold_Data/models/Account"
	"Cold_Data/models/Device"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"log"
	"strings"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {

	T_key := c.GetString("T_key")
	T_mode, _ := c.GetInt("T_mode", 1)
	T_key_s := strings.Split(T_key, ",")

	Company_r, err := Account.Read_Company_T_key(T_key_s[len(T_key_s)-1])
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_key!"}
		c.ServeJSON()
		return
	}

	//if !strings.Contains(Company_r.T_data, "_") {
	//	c.TplName = "error.html"
	//	return
	//}

	Company_R, ok := lib.Company_Map[Company_r.Id]
	if !ok {
		c.TplName = "error.html"
		return
	}
	Company_R.Company_name = Company_r.T_name
	Company_R.Company_key = Company_r.T_key
	Company_R.Company_plan = Company_r.T_plan
	Company_R.Company_data = Company_r.T_data
	Company_R.Company_v3d = Company_r.T_v3d
	Company_R.Company_Address = Company_r.T_Address
	Company_R.Company_coordinate = Company_r.T_coordinate

	switch T_mode {
	case 1: // 智慧大屏
		fmt.Println("智慧大屏:", Company_r.T_data)
		LOOPnum := 0

	LOOP:
		if len(Company_r.T_data) == 0 {
			// 复用上一级
			err, Company_r = Account.Read_Company_id(Company_r.T_mid)
			if err != nil {
				c.TplName = "error.html"
				return
			}

			if LOOPnum < 10 {
				LOOPnum += 1
				goto LOOP
			} else {
				c.TplName = "error.html"
				return
			}
		}
		c.Data["C_keys"] = T_key
		c.TplName = Company_r.T_data + ".html"
		log.Println("Company_data:", c.TplName)
		// 全国
		if strings.Contains(Company_R.Company_name, "主") || strings.Contains(Company_R.Company_name, "全国") {
			c.Data["map"] = "china"
			c.TplName = "B_Mode1.html"
			return
		}
		return

	case 2: // 3D大屏
		fmt.Println("3D大屏:", Company_r.T_v3d) // C_Mode_v3D_精简版|5QIRTWYLG6NE7OC349JP8M02UKBZHV1S
		T_v3d_list := strings.Split(Company_r.T_v3d, "|")
		if len(T_v3d_list) != 2 {
			c.TplName = "error.html"
			return
		}
		c.Data["C_keys"] = T_key
		c.TplName = T_v3d_list[0] + ".html"
		return

	}

	c.TplName = "error.html"
	return
}

// 列表 - 接口
func (c *MainController) Company_Data() {
	T_key := c.GetString("T_key")
	Company_r, err := Account.Read_Company_T_key(T_key)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_key!"}
		c.ServeJSON()
		return
	}

	Company_map := lib.Company_Map[Company_r.Id]
	Company_map.Company_name = Company_r.T_name
	Company_map.Company_key = Company_r.T_key
	Company_map.Company_plan = Company_r.T_plan
	Company_map.Company_data = Company_r.T_data
	Company_map.Company_v3d = Company_r.T_v3d
	Company_map.Company_Address = Company_r.T_Address
	Company_map.Company_coordinate = Company_r.T_coordinate

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

//func (c *MainController) Company_Device_Sensor_List() {
//
//	T_key := c.GetString("T_key")
//	Company_r,err := Account.Read_Company_T_key(T_key)
//	if err != nil {
//		c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_key!"}
//		c.ServeJSON()
//		return
//	}
//
//	// 查询公司下面所有子公司id
//	T_pids := Account.ReadCompanyIds_T_path(Company_r.T_path)
//
//	//Account.Read_Company_All_Maps()
//	Sensor_List := Device.Read_CompanyDeviceSensor_List_For_Data(T_pids)
//
//	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: Sensor_List}
//	c.ServeJSON()
//	return
//
//}

// 列表 - 接口
func (c *MainController) DeviceTask_List() {
	//// 验证登录
	//b_, admin_r := lib.Verification(c.Ctx.GetCookie("User_tokey"), c.GetString("User_tokey"))
	//if !b_ {
	//	c.Data["json"] = lib.JSONS{Code: 201, Msg: "User_tokey Err!"}
	//	c.ServeJSON()
	//	return
	//}
	T_sn := c.GetString("T_sn")
	DeviceTask_list := Device.Read_DeviceTask_List(T_sn)
	DeviceTask_r := []Device.DeviceTask_R{}
	for _, v := range DeviceTask_list {
		x := Device.DeviceTask_R{}
		x.T_Ut_start = v.T_Ut_start.Format("2006-01-02 15:04:05")
		x.T_Ut_end = v.T_Ut_end.Format("2006-01-02 15:04:05")
		x.T_State = v.T_State
		DeviceTask_r = append(DeviceTask_r, x)
	}
	c.Data["json"] = lib.JSONS{Code: 200, Msg: "ok!", Data: DeviceTask_r}
	c.ServeJSON()
	return
}

// 列表 - 接口
func (c *MainController) DeviceData_List() {

	T_sn := c.GetString("T_sn")
	//T_id, _ := c.GetInt("T_id")
	T_id := 1
	Time_start := c.GetString("Time_start")
	Time_end := c.GetString("Time_end")
	r_Device, err := Device.Read_Device_ByT_sn(T_sn)
	if err != nil {
		c.Data["json"] = lib.JSONS{Code: 201, Msg: "T_sn!"}
		c.ServeJSON()
		return
	}
	DeviceData_list := Device.Read_DeviceData_List(T_sn, T_id, Time_start, Time_end)
	DeviceData_r := []Device.DeviceData_RR{}
	for _, v := range DeviceData_list {
		x := Device.DeviceData_RR{}
		x.T_t = v.T_t
		x.T_rh = v.T_rh
		x.T_site = v.T_site
		x.T_time = v.T_time.Format("2006-01-02 15:04:05")

		x.T_ist = r_Device.T_ist
		x.T_ist = r_Device.T_ist

		if dsp, is := Device.Read_DeviceSensorParameter(T_sn, T_id); is {
			x.T_name = dsp.T_name
			x.T_Tlower = dsp.T_Tlower
			x.T_Tupper = dsp.T_Tupper
			x.T_RHlower = dsp.T_RHlower
			x.T_RHupper = dsp.T_RHupper
		}

		DeviceData_r = append(DeviceData_r, x)

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