intelligentLightingService.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package service
  2. import (
  3. "fmt"
  4. "iot_manager_service/app/device/dao"
  5. "iot_manager_service/app/device/model"
  6. "iot_manager_service/util/cache"
  7. "iot_manager_service/util/common"
  8. "strconv"
  9. "time"
  10. )
  11. var IntelligentLightingService = new(intelligentLightingService)
  12. type intelligentLightingService struct{}
  13. func (s *intelligentLightingService) GetDetailByLight(tenantId, id int) (model.RspIntelligentLightDetail,
  14. *common.Errors) {
  15. detail := model.RspIntelligentLightDetail{}
  16. lightControl, _ := LightControlService.GetOne(id)
  17. if lightControl != nil {
  18. lightControl.LampPoleName = lightControl.LampPoleName + "(" + lightControl.LampPoleSn + ")"
  19. detail.LightControlList = []dao.LightControl{*lightControl}
  20. detail.LampPoleName = lightControl.LampPoleName
  21. }
  22. detail.LightRelMap = s.GetLightRelation(tenantId, id, model.RelationTypeLight)
  23. return detail, nil
  24. }
  25. func (s *intelligentLightingService) GetLightRelation(tenantId, rId, relationType int) model.IntelligentLightSimple {
  26. detail := model.IntelligentLightSimple{}
  27. // 获取照明策略关联
  28. intelligentLight := dao.IntelligentLight{
  29. Rid: rId,
  30. RelationType: relationType,
  31. TenantId: tenantId,
  32. }
  33. relations, _ := intelligentLight.GetByRidAndType()
  34. if len(relations) == 0 {
  35. return detail
  36. }
  37. lightId := relations[0].LightID
  38. // 获取策略详情(日期,如果是年,使用当前年起始日期)
  39. lightStrategy, _ := LightStrategyService.GetOne(lightId)
  40. if lightStrategy == nil {
  41. return detail
  42. }
  43. //获取灯控名称和SN
  44. detail.LightName = lightStrategy.LightName
  45. detail.LightSn = lightStrategy.LightSn
  46. //全年
  47. if lightStrategy.IsAllYear == model.IsYear {
  48. year := time.Now().Year()
  49. detail.EffectiveDate = fmt.Sprintf("%d-01-01 ~ %d-12-31", year, year)
  50. } else {
  51. detail.EffectiveDate = lightStrategy.StartTime + " ~ " + lightStrategy.EndTime
  52. }
  53. // 获取策略时间(每日的时间策略)
  54. timeConditions := TimeConditionService.GetByLightId(lightId)
  55. for _, timeCondition := range timeConditions {
  56. if timeCondition.StartTime == "" && timeCondition.EndTime == "" {
  57. timeCondition.StartTime = "日落"
  58. timeCondition.EndTime = "日出"
  59. }
  60. detail.TimeConditionList = append(detail.TimeConditionList, model.TimeConditionSimple{
  61. Luminance: strconv.Itoa(timeCondition.Luminance) + "%",
  62. Times: timeCondition.StartTime + " ~ " + timeCondition.EndTime,
  63. })
  64. }
  65. return detail
  66. }
  67. func (s *intelligentLightingService) BatchGet(ids []int) []dao.IntelligentLight {
  68. // 创建查询实例
  69. intelligent := &dao.IntelligentLight{}
  70. intelligentLights, err := intelligent.BatchGet(ids)
  71. if err != nil {
  72. return nil
  73. }
  74. return intelligentLights
  75. }
  76. func (s *intelligentLightingService) List(tenantId int, searchValue string, current, size,
  77. groupId int) ([]model.RspIntelligentLightListDetail, error) {
  78. var result []model.RspIntelligentLightListDetail
  79. //获取策略关联信息
  80. intelligent := &dao.IntelligentLight{
  81. RelationType: model.RelationTypeLampPoleGroup,
  82. TenantId: tenantId,
  83. }
  84. list, err := intelligent.GetByGroup(searchValue, current, size, groupId)
  85. if err != nil {
  86. return nil, nil
  87. }
  88. for _, intelligentLight := range list {
  89. detail := model.RspIntelligentLightListDetail{
  90. PublicName: intelligentLight.LightControl.Name,
  91. PublicId: intelligentLight.LightControl.ID,
  92. LampPoleName: intelligentLight.LightControl.LampPoleName,
  93. LampPoleSn: intelligentLight.LightControl.LampPoleSn,
  94. StrategyName: intelligentLight.LightName,
  95. DeviceSn: intelligentLight.LightControl.Sn,
  96. ControlType: intelligentLight.LightControl.ControlType,
  97. IsShowOpts: true,
  98. }
  99. if intelligentLight.Luminance > 0 {
  100. detail.LightControlState = 1
  101. } else {
  102. detail.LightControlState = 2
  103. }
  104. _, state := cache.GetDeviceState(intelligentLight.LightControl.Sn)
  105. detail.RunState = state
  106. result = append(result, detail)
  107. }
  108. return result, nil
  109. }