timeConditionService.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package service
  2. import (
  3. "iot_manager_service/app/device/dao"
  4. "iot_manager_service/util/common"
  5. "iot_manager_service/util/logger"
  6. )
  7. // 灯控策略-时间策略服务
  8. var TimeConditionService = new(timeConditionService)
  9. type timeConditionService struct{}
  10. func (s *timeConditionService) Get(id int) (*dao.TimeCondition, *common.Errors) {
  11. // 创建查询实例
  12. condition := &dao.TimeCondition{
  13. ID: id,
  14. }
  15. err := condition.Get()
  16. if err != nil {
  17. return nil, common.FailResponse(err.Error(), nil)
  18. }
  19. return condition, nil
  20. }
  21. func (s *timeConditionService) Save(conditions []dao.TimeCondition, lightId int) error {
  22. for _, condition := range conditions {
  23. condition.LightId = lightId
  24. if condition.ID == 0 {
  25. condition.Save()
  26. } else {
  27. condition.Update()
  28. }
  29. }
  30. return nil
  31. }
  32. func (s *timeConditionService) GetByLightId(id int) []dao.TimeCondition {
  33. // 创建查询实例
  34. condition := &dao.TimeCondition{
  35. LightId: id,
  36. }
  37. conditions, err := condition.GetByLightId()
  38. if err != nil {
  39. logger.Logger.Errorf("GetByLightIds err = %v \n", err)
  40. return nil
  41. }
  42. return conditions
  43. }
  44. func (s *timeConditionService) GetAll() ([]dao.TimeCondition, *common.Errors) {
  45. // 创建查询实例
  46. condition := &dao.TimeCondition{}
  47. conditions, err := condition.GetAll()
  48. if err != nil {
  49. return nil, common.FailResponse(err.Error(), nil)
  50. }
  51. return conditions, nil
  52. }