intelligentLightingService.go 4.4 KB

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