package nats_server

import (
	"cold-delivery/common/nats"
	"errors"
	"sort"
	"time"

	"github.com/vmihailenco/msgpack/v5"
)

func Cold_CompanyDeviceSensor_List_ByKey(T_sn, T_key string) (data []DeviceSensor_R, count int64, err error) {

	if len(T_sn) == 0 {
		err = errors.New("sn不能为空！")
		return
	}
	if len(T_key) == 0 {
		err = errors.New("公司秘钥不能为空！")
		return
	}
	type T_Req struct {
		T_sn  string `xml:"T_sn"`
		T_key string `xml:"T_key"`
	}
	t_Req := T_Req{
		T_sn:  T_sn,
		T_key: T_key,
	}
	b, _ := msgpack.Marshal(&t_Req)

	msg, err := nats.Nats.Request("Cold_CompanyDeviceSensor_List_ByKey", b, 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code  int16            `xml:"Code"`
		Msg   string           `xml:"Msg"`
		Count int64            `xml:"Count"`
		Data  []DeviceSensor_R `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}

	return t_R.Data, t_R.Count, nil
}
func Cold_ReadDeviceDataListBy_T_snid(T_snid, startTime, endTime string, page, page_z int) (data []DeviceData_R, count int64, err error) {

	type T_Req struct {
		T_snid     string `xml:"T_snid"`
		Time_start string `xml:"Time_start"`
		Time_end   string `xml:"Time_end"`
		Page       int    `xml:"Page"`
		Page_z     int    `xml:"Page_z"`
	}
	t_Req := T_Req{
		T_snid:     T_snid,
		Time_start: startTime,
		Time_end:   endTime,
		Page:       page,
		Page_z:     page_z,
	}
	b, _ := msgpack.Marshal(&t_Req)

	msg, err := nats.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code  int16          `xml:"Code"`
		Msg   string         `xml:"Msg"`
		Count int64          `xml:"Count"`
		Data  []DeviceData_R `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	list := t_R.Data
	sort.Slice(list, func(i, j int) bool {
		// 先按 T_time 字段排序，如果 T_time 相同则按 T_name 字段排序
		if list[i].T_time == list[j].T_time {
			return list[i].T_name < list[j].T_name
		}
		return list[i].T_time > list[j].T_time
	})

	return list, t_R.Count, nil
}
func Cold_ReadDeviceDataBy_T_snid_T_time(T_sn string, T_id int, Time string) (data DeviceData_, err error) {

	type T_Req struct {
		T_sn string `xml:"T_sn"`
		T_id int    `xml:"T_id"`
		Time string `xml:"Time"`
	}
	t_Req := T_Req{
		T_sn: T_sn,
		T_id: T_id,
		Time: Time,
	}
	b, _ := msgpack.Marshal(&t_Req)

	msg, err := nats.Nats.Request("Cold_ReadDeviceDataBy_T_snid_T_time", b, 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code int16       `xml:"Code"`
		Msg  string      `xml:"Msg"`
		Data DeviceData_ `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}

	return t_R.Data, nil
}

// 查询轨迹信息
func Cold_ReadDeviceDataListBy_T_snidForLocus(T_snid, startTime, endTime string, page, page_z int) (data []DeviceData_R2, count int64, err error) {

	type T_Req struct {
		T_snid     string `xml:"T_snid"`
		Time_start string `xml:"Time_start"`
		Time_end   string `xml:"Time_end"`
		Page       int    `xml:"Page"`
		Page_z     int    `xml:"Page_z"`
	}
	t_Req := T_Req{
		T_snid:     T_snid,
		Time_start: startTime,
		Time_end:   endTime,
		Page:       page,
		Page_z:     page_z,
	}
	b, _ := msgpack.Marshal(&t_Req)

	msg, err := nats.Nats.Request("Cold_ReadDeviceDataListBy_T_snid", b, 120*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code  int16           `xml:"Code"`
		Msg   string          `xml:"Msg"`
		Count int64           `xml:"Count"`
		Data  []DeviceData_R2 `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}

	return t_R.Data, t_R.Count, nil
}
func Cold_ReadDevice_List(name, T_key string, page, page_z int) (data []Device_R, count int64, err error) {

	type T_Req struct {
		Key    string `xml:"Key"`
		Name   string `xml:"Code"`
		Page   int    `xml:"Page"`
		Page_z int    `xml:"Page_z"`
	}
	t_Req := T_Req{
		Key:    T_key,
		Name:   name,
		Page:   page,
		Page_z: page_z,
	}
	b, _ := msgpack.Marshal(&t_Req)

	msg, err := nats.Nats.Request("Cold_ReadDevice_List", b, 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code  int16      `xml:"Code"`
		Msg   string     `xml:"Msg"`
		Count int64      `xml:"Count"`
		Data  []Device_R `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	list := t_R.Data

	return list, t_R.Count, nil
}
func Cold_ReadCompanyByT_key(T_key string) (data Company_R, err error) {

	msg, err := nats.Nats.Request("Cold_ReadCompanyByT_key", []byte(T_key), 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code int16     `xml:"Code"`
		Msg  string    `xml:"Msg"`
		Data Company_R `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}

	return t_R.Data, nil
}

func Cold_CompanyTreeByT_name(T_key string) (data []Company_R, err error) {

	msg, err := nats.Nats.Request("Cold_User_CompanyTreeByT_name", []byte(T_key), 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code int16       `xml:"Code"`
		Msg  string      `xml:"Msg"`
		Data []Company_R `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}

	return t_R.Data, nil
}

func Cold_ReadDeviceByT_sn(T_sn string) (data Device, err error) {

	msg, err := nats.Nats.Request("Cold_ReadDeviceByT_sn", []byte(T_sn), 3*time.Second)
	if err != nil {
		return
	}

	type T_R struct {
		Code int16  `xml:"Code"`
		Msg  string `xml:"Msg"`
		Data Device `xml:"Data"` // 泛型
	}
	var t_R T_R

	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	data = t_R.Data

	return data, nil
}

// Read_DeviceTask_List_By_Condition 获取设备任务状态
func Read_DeviceTask_List_By_Condition(T_sn string) (data DeviceTask, err error) {
	type T_R struct {
		Code int16      `xml:"Code"`
		Msg  string     `xml:"Msg"`
		Data DeviceTask `xml:"Data"` // 泛型
	}
	type T_Req struct {
		T_sn string `xml:"T_sn"`
	}
	if len(T_sn) == 0 {
		err = errors.New("sn不能为空！")
		return
	}
	t_req := T_Req{
		T_sn: T_sn,
	}
	b, _ := msgpack.Marshal(&t_req)
	msg, err := nats.Nats.Request("Read_DeviceTask_List_By_Condition", b, 3*time.Second)
	if err != nil {
		return
	}
	var t_R T_R
	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	data = t_R.Data
	return data, nil
}
func Read_New_DeviceData(T_sn string, T_id int) (data DeviceSensor, err error) {
	type T_R struct {
		Code int16        `xml:"Code"`
		Msg  string       `xml:"Msg"`
		Data DeviceSensor `xml:"Data"` // 泛型
	}
	type T_Req struct {
		T_sn string `xml:"T_sn"`
		T_id int    `xml:"T_id"`
	}
	if len(T_sn) == 0 {
		err = errors.New("sn不能为空！")
		return
	}
	t_req := T_Req{
		T_sn: T_sn,
		T_id: T_id,
	}
	b, _ := msgpack.Marshal(&t_req)
	msg, err := nats.Nats.Request("Read_New_DeviceData", b, 3*time.Second)
	if err != nil {
		return
	}
	var t_R T_R
	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	data = t_R.Data
	return data, nil
}

func Read_Start_Time_DeviceData(T_sn string, T_id int, StartTime, EndTime string) (data []DeviceData_, err error) {
	type T_R struct {
		Code int16         `xml:"Code"`
		Msg  string        `xml:"Msg"`
		Data []DeviceData_ `xml:"Data"` // 泛型
	}
	type T_Req struct {
		T_sn      string `xml:"T_sn"`
		T_id      int    `xml:"T_id"`
		StartTime string `xml:"StartTime"`
		EndTime   string `xml:"EndTime"`
	}
	if len(T_sn) == 0 {
		err = errors.New("sn不能为空！")
		return
	}
	t_req := T_Req{
		T_sn:      T_sn,
		T_id:      T_id,
		StartTime: StartTime,
		EndTime:   EndTime,
	}
	b, _ := msgpack.Marshal(&t_req)
	msg, err := nats.Nats.Request("Read_Start_Time_DeviceData", b, 3*time.Second)
	if err != nil {
		return
	}
	var t_R T_R
	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	data = t_R.Data
	return data, nil
}

// Read_Device_ByT_sn 根据sn查询设备信息
func Read_Device_ByT_sn(T_sn string) (data Device, err error) {
	type T_R struct {
		Code int16  `xml:"Code"`
		Msg  string `xml:"Msg"`
		Data Device `xml:"Data"` // 泛型
	}
	type T_Req struct {
		T_sn string `xml:"T_sn"`
	}
	if len(T_sn) == 0 {
		err = errors.New("sn不能为空！")
		return
	}
	t_req := T_Req{
		T_sn: T_sn,
	}
	b, _ := msgpack.Marshal(&t_req)
	msg, err := nats.Nats.Request("Read_Device_ByT_sn", b, 3*time.Second)
	if err != nil {
		return
	}
	var t_R T_R
	err = msgpack.Unmarshal(msg.Data, &t_R)
	if err != nil {
		return
	}
	if t_R.Code != 200 {
		err = errors.New(t_R.Msg)
		return
	}
	data = t_R.Data
	return data, nil
}
