package service

import (
	"cold-delivery/app/admin/model"
	"cold-delivery/app/admin/service/dto"
	"cold-delivery/common/actions"
	"cold-delivery/common/nats/nats_server"
	"fmt"
	"gogs.baozhida.cn/zoie/OAuth-core/service"
	"strings"
)

type Device struct {
	service.Service
}

// GetPage 获取Device列表
func (e *Device) GetDeviceSensorList(c *dto.DeviceSensorGetPageReq, p *actions.DataPermission) (list []nats_server.DeviceSensor_R, count int64, err error) {

	if len(c.Sn) == 0 {
		return list, count, nil
	}

	company, err := model.GetCompanyById(p.DeptId)
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return list, count, model.GetCompanyKeyErr
	}
	list, count, err = nats_server.Cold_CompanyDeviceSensor_List_ByKey(c.Sn, company.ColdKey)
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return list, count, err
	}
	return list, count, nil
}
func (e *Device) GetData(c *dto.DeviceSensorGetDataReq, p *actions.DataPermission) (list []nats_server.DeviceData_R, count int64, err error) {
	T_snid := ""
	ids := strings.Split(strings.Trim(c.T_ids, ","), ",")
	for _, id := range ids {
		T_snid += fmt.Sprintf("%s,%s|", c.T_sn, id)
	}

	list, count, err = nats_server.Cold_ReadDeviceDataListBy_T_snid(T_snid, c.StartTime, c.EndTime, c.GetPageIndex(), c.GetPageSize())
	if err != nil {
		e.Log.Errorf("db error: %s", err)
		return list, count, err
	}
	return list, count, nil
}
