intelligentLightingService.go 3.7 KB

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