Jelajahi Sumber

灯控策略 查询

terry 2 tahun lalu
induk
melakukan
32fc3225d7

+ 30 - 0
app/device/controller/IntelligentLightingStrategy.go

@@ -5,6 +5,7 @@ import (
 	"iot_manager_service/app/device/model"
 	"iot_manager_service/app/device/service"
 	"iot_manager_service/util"
+	"math"
 	"net/http"
 	"strconv"
 )
@@ -50,7 +51,36 @@ func (c *intelligentLightingCtl) Detail(ctx *gin.Context) {
 }
 
 func (c *intelligentLightingCtl) List(ctx *gin.Context) {
+	searchValue := ctx.Query("searchValue")
+	groupId := ctx.Query("groupId")
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+	if groupId != "" {
+
+	}
 
+	strategies, err := service.IntelligentLightingService.List(searchValue, current, size)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	pages := math.Ceil(float64(len(strategies)) / float64(size))
+	rsp := model.RspIntelligentLightList{
+		Current: current,
+		Size:    size,
+		Total:   len(strategies),
+		Pages:   int(pages),
+	}
+	for _, strategy := range strategies {
+		rsp.Records = append(rsp.Records, strategy)
+	}
+	ctx.JSON(http.StatusOK, util.SuccessResponse(util.Succeeded, rsp))
 }
 
 func (c *intelligentLightingCtl) RelationList(ctx *gin.Context) {

+ 7 - 7
app/device/dao/LightStrategyDao.go

@@ -7,21 +7,21 @@ import (
 
 type LightStrategy struct {
 	ID                 int       `gorm:"primary key" json:"id"`                        //编号
-	LightName          string    `gorm:"type: varchar(64" json:"lightName"`            //策略名称
+	LightName          string    `gorm:"type:varchar(64)" json:"lightName"`            //策略名称
 	LightControlType   int       `gorm:"type:int" json:"lightControlType"`             //灯控类型 设置 1=485,2=nb-iot
 	OptoSensorID       int       `gorm:"type:int" json:"optoSensorID"`                 //光传感器ID
 	StartTime          string    `gorm:"type:date" json:"startTime"`                   //策略期限开始时间
 	EndTime            string    `gorm:"type:date" json:"endTime"`                     //策略期限结束时间
 	IsAllYear          int       `gorm:"type:int;default 1" json:"isAllYear"`          //全年设置2=是,1=否
 	IsAutomaticRenewal int       `gorm:"type:int;default 1" json:"isAutomaticRenewal"` //自动续期设置 2=是,1=否
-	CreateTime         time.Time `gorm:"type: datetime" json:"createTime"`             //新增时间
+	CreateTime         time.Time `gorm:"type:datetime" json:"createTime"`              //新增时间
 	CreateUser         string    `gorm:"type:varchar(60)" json:"createUser"`           //新增记录操作用户ID
-	UpdateTime         time.Time `gorm:"type: datetime" json:"updateTime"`             //修改时间
+	UpdateTime         time.Time `gorm:"type:datetime" json:"updateTime"`              //修改时间
 	UpdateUser         string    `gorm:"type:varchar(60)" json:"updateUser"`           //修改用户
 	IsDeleted          int       `gorm:"type:int;default 0" json:"isDeleted"`          //是否删除 0=未删除,1=删除
 	Status             int       `gorm:"type:int;default 1" json:"status"`             //状态 0=正常,1=异常
 	TenantID           string    `gorm:"type:varchar(12)" json:"tenantID"`             //租户id
-	LightSn            string    `gorm:"type:varchar(64" json:"lightSn"`               //策略编码
+	LightSn            string    `gorm:"type:varchar(64)" json:"lightSn"`              //策略编码
 	ManualTime         int       `gorm:"type:int;default 0" json:"manualTime"`         //手动控制时间
 }
 
@@ -42,13 +42,13 @@ func (c LightStrategy) IsExistedBySN() bool {
 }
 
 func (c LightStrategy) IsExistedByNameAndCode() bool {
-	var Strategys []LightStrategy
+	var Strategies []LightStrategy
 	err := Db.Model(&c).Where("gateway_sn = ? and is_deleted = ?",
-		c.TenantID, c.IsDeleted).Find(&Strategys).Error
+		c.TenantID, c.IsDeleted).Find(&Strategies).Error
 	if gorm.IsRecordNotFoundError(err) {
 		return false
 	}
-	for _, d := range Strategys {
+	for _, d := range Strategies {
 		if d.ID != c.ID {
 			return true
 		}

+ 2 - 0
app/device/dao/common.go

@@ -38,6 +38,8 @@ func InitDB(db *gorm.DB) {
 		&OptoSensor{},
 		&IntelligentLight{},
 		&TimeCondition{},
+		&LightStrategy{},
+		&LightCondition{},
 	).Error
 	if err != nil {
 		panic(fmt.Sprintf("AutoMigrate err : %v", err))

+ 6 - 0
app/device/dao/timeConditionDao.go

@@ -30,3 +30,9 @@ func (c *TimeCondition) BatchGet(ids []int) ([]TimeCondition, error) {
 	err := Db.Model(&c).Where("id in ?", ids).Find(&conditions).Error
 	return conditions, err
 }
+
+func (c *TimeCondition) GetByLightId() ([]TimeCondition, error) {
+	var conditions []TimeCondition
+	err := Db.Model(&c).Where("light_id = ?", c.LightId).Find(&conditions).Error
+	return conditions, err
+}

+ 10 - 0
app/device/model/intelligentLighting.go

@@ -7,6 +7,8 @@ const (
 	RelationTypeInvalid       = "查询参数(关联类型)异常"
 	RelationTypeLight         = 1 //关联类型 灯控
 	RelationTypeLampPoleGroup = 2 //关联类型 灯杆分组
+
+	IsYear = 2 //全年设置2=是,1=否
 )
 
 type RspIntelligentLightDetail struct {
@@ -33,3 +35,11 @@ type TimeConditionSimple struct {
 	Luminance string `json:"luminance"`
 	Times     string `json:"times"`
 }
+
+type RspIntelligentLightList struct {
+	Records []dao.IntelligentLight `json:"records"` //记录列表
+	Current int                    `json:"current"` //当前分页
+	Size    int                    `json:"size"`    //每页数量
+	Pages   int                    `json:"pages"`   //总页数
+	Total   int                    `json:"total"`   //总数
+}

+ 45 - 7
app/device/service/intelligentLightingService.go

@@ -1,9 +1,12 @@
 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)
@@ -34,17 +37,47 @@ func (s *intelligentLightingService) GetDetailByGroup(id int) (model.RspIntellig
 	return detail, nil
 }
 
-func (s *intelligentLightingService) GetLightRelation(id, relationType int) model.IntelligentLightSimple {
+func (s *intelligentLightingService) GetLightRelation(rId, relationType int) model.IntelligentLightSimple {
 	detail := model.IntelligentLightSimple{}
-	intelligentLight := dao.IntelligentLight{Rid: id, RelationType: relationType}
+
+	// 获取照明策略关联
+	intelligentLight := dao.IntelligentLight{Rid: rId, RelationType: relationType}
 	relations, _ := intelligentLight.GetByRidAndType()
-	if len(relations) > 0 {
-		light, _ := LightControlService.Get(relations[0].LightID)
-		if light != nil {
-			detail.LightName = light.Name
-			detail.LightSn = light.SN
+	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
 }
 
@@ -57,3 +90,8 @@ func (s *intelligentLightingService) BatchGet(ids []int) []dao.IntelligentLight
 	}
 	return conditions
 }
+
+func (s *intelligentLightingService) List(searchValue string, current, size int) ([]dao.IntelligentLight,
+	*util.Errors) {
+	return nil, nil
+}

+ 12 - 0
app/device/service/timeConditionService.go

@@ -35,3 +35,15 @@ func (s *timeConditionService) BatchGet(ids []int) []dao.TimeCondition {
 func (s *timeConditionService) Save([]dao.TimeCondition) error {
 	return nil
 }
+
+func (s *timeConditionService) GetByLightId(id int) ([]dao.TimeCondition, *util.Errors) {
+	// 创建查询实例
+	condition := &dao.TimeCondition{
+		LightId: id,
+	}
+	conditions, err := condition.GetByLightId()
+	if err != nil {
+		return nil, util.FailResponse(err.Error(), nil)
+	}
+	return conditions, nil
+}