123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- package service
- import (
- "fmt"
- "iot_manager_service/app/device/dao"
- "iot_manager_service/app/device/model"
- "iot_manager_service/util"
- "strconv"
- "time"
- )
- var IntelligentLightingService = new(intelligentLightingService)
- type intelligentLightingService struct{}
- func (s *intelligentLightingService) GetDetailByLight(tenantId, id int) (model.RspIntelligentLightDetail,
- *util.Errors) {
- detail := model.RspIntelligentLightDetail{}
- lightControl, _ := LightControlService.GetOne(id)
- if lightControl != nil {
- lightControl.LampPoleName = lightControl.LampPoleName + "(" + lightControl.LampPoleSN + ")"
- detail.LightControlList = []dao.LightControl{*lightControl}
- detail.LampPoleName = lightControl.LampPoleName
- }
- detail.LightRelMap = s.GetLightRelation(tenantId, id, model.RelationTypeLight)
- return detail, nil
- }
- func (s *intelligentLightingService) GetLightRelation(tenantId, rId, relationType int) model.IntelligentLightSimple {
- detail := model.IntelligentLightSimple{}
- // 获取照明策略关联
- intelligentLight := dao.IntelligentLight{
- Rid: rId,
- RelationType: relationType,
- TenantId: tenantId,
- }
- relations, _ := intelligentLight.GetByRidAndType()
- if len(relations) == 0 {
- return detail
- }
- lightId := relations[0].LightID
- // 获取策略详情(日期,如果是年,使用当前年起始日期)
- lightStrategy, _ := LightStrategyService.GetOne(lightId)
- if lightStrategy == nil {
- return detail
- }
- //获取灯控名称和SN
- detail.LightName = lightStrategy.LightName
- detail.LightSn = lightStrategy.LightSn
- //全年
- if lightStrategy.IsAllYear == model.IsYear {
- year := time.Now().Year()
- detail.EffectiveDate = fmt.Sprintf("%d-01-01 ~ %d-12-31", year, year)
- } else {
- detail.EffectiveDate = lightStrategy.StartTime + " ~ " + lightStrategy.EndTime
- }
- // 获取策略时间(每日的时间策略)
- timeConditions := TimeConditionService.GetByLightId(lightId)
- for _, timeCondition := range timeConditions {
- if timeCondition.StartTime == "" && timeCondition.EndTime == "" {
- timeCondition.StartTime = "日落"
- timeCondition.EndTime = "日出"
- }
- detail.TimeConditionList = append(detail.TimeConditionList, model.TimeConditionSimple{
- Luminance: strconv.Itoa(timeCondition.Luminance) + "%",
- Times: timeCondition.StartTime + " ~ " + timeCondition.EndTime,
- })
- }
- return detail
- }
- func (s *intelligentLightingService) BatchGet(ids []int) []dao.IntelligentLight {
- // 创建查询实例
- intelligent := &dao.IntelligentLight{}
- intelligentLights, err := intelligent.BatchGet(ids)
- if err != nil {
- return nil
- }
- return intelligentLights
- }
- func (s *intelligentLightingService) List(tenantId int, searchValue string, current, size,
- groupId int) ([]model.RspIntelligentLightListDetail, error) {
- var result []model.RspIntelligentLightListDetail
- //获取策略关联信息
- intelligent := &dao.IntelligentLight{
- RelationType: model.RelationTypeLampPoleGroup,
- TenantId: tenantId,
- }
- list, err := intelligent.GetByGroup(searchValue, current, size, groupId)
- if err != nil {
- return nil, nil
- }
- for _, intelligentLight := range list {
- detail := model.RspIntelligentLightListDetail{
- PublicName: intelligentLight.LightControl.Name,
- PublicId: intelligentLight.LightControl.ID,
- LampPoleName: intelligentLight.LightControl.LampPoleName,
- LampPoleSn: intelligentLight.LightControl.LampPoleSN,
- StrategyName: intelligentLight.LightName,
- DeviceSn: intelligentLight.LightControl.SN,
- ControlType: intelligentLight.LightControl.ControlType,
- IsShowOpts: true,
- }
- if intelligentLight.Luminance > 0 {
- detail.LightControlState = 1
- } else {
- detail.LightControlState = 2
- }
- detail.RunState = util.GetDeviceState(detail.DeviceSn)
- result = append(result, detail)
- }
- return result, nil
- }
|