package services

import (
	"github.com/gogf/gf/util/guid"
	"sparrow/pkg/models"
	"sparrow/services/knowoapi/model"
)

// DeviceStatusService 业务接口
type DeviceStatusService interface {
	Create(*models.DeviceStatus) error
	Delete(*models.DeviceStatus) error
	Update(*models.DeviceStatus) (models.DeviceStatus, error)
	GetListByDeviceTypeId(deviceTypeId string, pi, ps int, name string) ([]models.DeviceStatus, int, error)
	QueryOne(recordID string) (models.DeviceStatus, error)
}

type deviceStatusService struct {
	model *model.All
}

// NewDeviceStatusService new device status manager
func NewDeviceStatusService(m *model.All) DeviceStatusService {
	return &deviceStatusService{
		model: m,
	}
}

func (a *deviceStatusService) Create(item *models.DeviceStatus) error {
	item.RecordID = guid.S()
	// 为子数据生成 record_id
	for i := range item.Data {
		item.Data[i].RecordID = guid.S()
		item.Data[i].StatusID = item.RecordID
	}
	return a.model.DeviceStatus.Create(item)
}

func (a *deviceStatusService) Delete(item *models.DeviceStatus) error {
	return a.model.DeviceStatus.Delete(item)
}

func (a *deviceStatusService) Update(item *models.DeviceStatus) (models.DeviceStatus, error) {
	// 为子数据生成 record_id
	for i := range item.Data {
		if item.Data[i].RecordID == "" {
			item.Data[i].RecordID = guid.S()
		}
		item.Data[i].StatusID = item.RecordID
	}
	return a.model.DeviceStatus.Update(item)
}

func (a *deviceStatusService) GetListByDeviceTypeId(deviceTypeId string, pi, ps int, name string) ([]models.DeviceStatus, int, error) {
	return a.model.DeviceStatus.GetListByDeviceTypeId(deviceTypeId, pi, ps, name)
}

func (a *deviceStatusService) QueryOne(recordID string) (models.DeviceStatus, error) {
	return a.model.DeviceStatus.QueryOne(recordID)
}
