package service

import (
	"iot_manager_service/app/device/dao"
	"iot_manager_service/util/common"
	"iot_manager_service/util/logger"
)

// 灯控策略-时间策略服务
var TimeConditionService = new(timeConditionService)

type timeConditionService struct{}

func (s *timeConditionService) Get(id int) (*dao.TimeCondition, *common.Errors) {
	// 创建查询实例
	condition := &dao.TimeCondition{
		ID: id,
	}
	err := condition.Get()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return condition, nil
}

func (s *timeConditionService) Save(conditions []dao.TimeCondition, lightId int) error {
	for _, condition := range conditions {
		condition.LightId = lightId
		if condition.ID == 0 {
			condition.Save()
		} else {
			condition.Update()
		}
	}
	return nil
}

func (s *timeConditionService) GetByLightId(id int) []dao.TimeCondition {
	// 创建查询实例
	condition := &dao.TimeCondition{
		LightId: id,
	}
	conditions, err := condition.GetByLightId()
	if err != nil {
		logger.Logger.Errorf("GetByLightIds err = %v  \n", err)
		return nil
	}
	return conditions
}

func (s *timeConditionService) GetAll() ([]dao.TimeCondition, *common.Errors) {
	// 创建查询实例
	condition := &dao.TimeCondition{}
	conditions, err := condition.GetAll()
	if err != nil {
		return nil, common.FailResponse(err.Error(), nil)
	}
	return conditions, nil
}
