Browse Source

首页-亮灯率

sixian 2 years ago
parent
commit
cecaeb3412

+ 6 - 1
app/device/controller/workbenchController.go

@@ -2,6 +2,7 @@ package controller
 
 import (
 	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/device/model"
 	"iot_manager_service/app/device/service"
 	"iot_manager_service/app/middleware"
 	"iot_manager_service/util/common"
@@ -76,8 +77,12 @@ func (c *workbenchCtl) Notification(ctx *gin.Context) {
 func (c *workbenchCtl) LightRate(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
+	var req model.ReqLightRates
+	if err := ctx.ShouldBindQuery(&req); err != nil {
+		ctx.JSON(http.StatusServiceUnavailable, err)
+	}
 	//queryType=month&startDate=2022-09-01&endDate=2022-10-04
-	alarms, err := service.WorkbenchService.LightRate(claims.TenantId)
+	alarms, err := service.WorkbenchService.LightRate(claims.TenantId, req)
 	if err != nil {
 		ctx.JSON(http.StatusOK, err)
 		return

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

@@ -133,9 +133,3 @@ func (c LightControl) GetDevicesByLampPole() []LightControl {
 		c.LampPoleId).Find(&devices)
 	return devices
 }
-
-func (c LightControl) GetSnList() ([]string, error) {
-	var sns []string
-	err := Db.Debug().Model(&c).Select("sn").Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&sns).Error
-	return sns, err
-}

+ 6 - 0
app/device/model/workbench.go

@@ -9,6 +9,12 @@ type RspCountDevice struct {
 	CountDevices []dao.CountDevice `json:"data"`
 }
 
+type ReqLightRates struct {
+	QueryType string `form:"queryType"`
+	StartDate string `form:"startDate"`
+	EndDate   string `form:"endDate"`
+}
+
 type RspLightRate struct {
 	LightRate []LightRate `json:"lightRate"`
 	Energy    []LightRate `json:"energy"`

+ 126 - 2
app/device/service/workbenchService.go

@@ -5,7 +5,12 @@ import (
 	dataDao "iot_manager_service/app/data/dao"
 	"iot_manager_service/app/device/dao"
 	"iot_manager_service/app/device/model"
+	operationDao "iot_manager_service/app/operation/dao"
+	"iot_manager_service/app/operation/edge_service"
+	operationModel "iot_manager_service/app/operation/model"
 	"iot_manager_service/util/common"
+	"strconv"
+	"time"
 )
 
 // 中间件管理服务
@@ -45,8 +50,45 @@ func (s *workbenchService) Notification(tenantId int) (*dao.Notification, *commo
 	return notification, nil
 }
 
-func (s *workbenchService) LightRate(tenantId int) (*model.RspLightRate, *common.Errors) {
-	var rsp model.RspLightRate
+func (s *workbenchService) LightRate(tenantId int, req model.ReqLightRates) (*model.RspLightRate, *common.Errors) {
+
+	var list []operationModel.ResponseLightingRate
+	var err error
+	if req.QueryType == "month" {
+		list, err = s.getLightRateData("day", tenantId, operationModel.RequestLightingRateFilter{
+			StartTime: req.StartDate,
+			EndTime:   req.EndDate,
+			QueryType: "day",
+		})
+	} else {
+		list, err = s.getLightRateData("month", tenantId, operationModel.RequestLightingRateFilter{
+			StartTime: req.StartDate,
+			EndTime:   req.EndDate,
+			QueryType: "month",
+		})
+	}
+	if err != nil {
+		return nil, common.FailResponse(err.Error(), nil)
+	}
+	var lightRate []model.LightRate
+	var energy []model.LightRate
+	for _, rate := range list {
+		parse, _ := time.Parse("2006-01-02", rate.CountTime)
+		if req.QueryType == "month" {
+			parse, _ = time.Parse("2006-01-02", rate.CountTime)
+		}
+		lightRate = append(lightRate, model.LightRate{
+			ColumnValue: fmt.Sprintf("%.2f", rate.LightingRate),
+			TimeLine:    parse,
+		})
+		energy = append(energy, model.LightRate{
+			ColumnValue: fmt.Sprintf("%.2f", rate.EnergyNum),
+			TimeLine:    parse,
+		})
+	}
+	rsp := model.RspLightRate{
+		lightRate, energy,
+	}
 	return &rsp, nil
 }
 
@@ -82,3 +124,85 @@ func (s *workbenchService) getNewsDate(vo *dao.OptoSensorVO) *dao.OptoSensorVO {
 	vo.RealTimeWindSpeed = common.CalculateSpeed(fmt.Sprintf("%.2f", environmentData.WindSpeed)) //风力等级
 	return vo
 }
+
+// getLightRateData 能耗和光照
+func (s *workbenchService) getLightRateData(method string, tenantId int, req operationModel.RequestLightingRateFilter) ([]operationModel.ResponseLightingRate, error) {
+	lightControl := operationDao.Operation{
+		TenantId: tenantId,
+	}
+	codes, err := lightControl.GetSnList()
+	if err != nil {
+		return nil, err
+	}
+	flag := 1  //能耗请求类型:按天
+	flag2 := 0 //亮灯率请求类型:按天
+	if method == "month" {
+		flag = 2  //能耗请求类型:按天
+		flag2 = 1 //亮灯率请求类型:按月
+	}
+	//能耗
+	forLightEnergy := edge_service.ForLightEnergy{}
+	lightEnergys, err := forLightEnergy.GetLightEnergy(edge_service.ForLightEnergyReq{
+		Codes: codes,
+		Start: req.StartTime,
+		End:   req.EndTime,
+		Flag:  flag,
+	})
+	if err != nil {
+		return nil, err
+	}
+	//亮灯率
+	forLightRate := edge_service.ForLightRate{}
+	lightRates, err := forLightRate.GetLightRate(edge_service.ForLightRateReq{
+		Tenant: strconv.Itoa(tenantId),
+		Start:  req.StartTime,
+		End:    req.EndTime,
+		Flag:   flag2,
+	})
+	if err != nil {
+		return nil, err
+	}
+	var list []operationModel.ResponseLightingRate
+	//fmt.Printf("LightEnergys = %v", LightEnergys)
+	var months []string
+	if method == "month" {
+		months = common.GetTimeMonths(req.StartTime, req.EndTime) //时间范围 月
+	} else {
+		months = common.GetTimeDays(req.StartTime, req.EndTime) //时间范围 天
+	}
+	//fmt.Printf("months = %v \n", months)
+	for _, month := range months {
+		// 能耗数据汇总
+		var monthEnergyNum float64 //能耗
+		for _, LightEnergy := range lightEnergys {
+			for _, data := range LightEnergy {
+				if month == data.Date {
+					monthEnergyNum += data.Difference
+				}
+			}
+		}
+		//亮灯率
+		var lightingRate float64
+		var lightControlNum, lightingNum int
+		for _, rate := range lightRates {
+			date := rate.Date
+			if method == "month" {
+				date = rate.Month + "-01"
+			}
+			if date == month {
+				lightingRate += rate.Rate     //亮灯率
+				lightControlNum += rate.Total //灯数
+				lightingNum += rate.Number    //亮灯数
+			}
+		}
+		//组合数据到前端接口展示
+		list = append(list, operationModel.ResponseLightingRate{
+			CountTime:       month,
+			EnergyNum:       common.Decimal(monthEnergyNum),
+			LightingRate:    common.Decimal(lightingRate),
+			LightControlNum: lightControlNum,
+			LightingNum:     lightingNum,
+		})
+	}
+	return list, nil
+}

+ 13 - 0
app/operation/dao/operationDao.go

@@ -1,4 +1,17 @@
 package dao
 
 type Operation struct {
+	TenantId  int    `gorm:"type:int" json:"tenantId"`            //租户ID
+	Sn        string `gorm:"type:varchar(60)" json:"sn"`          //设备序列号
+	IsDeleted int    `gorm:"type:int;default 0" json:"isDeleted"` //是否删除 0=未删除,1=删除
+}
+
+func (Operation) TableName() string {
+	return "device_light_control"
+}
+
+func (c Operation) GetSnList() ([]string, error) {
+	var sns []string
+	err := Db.Debug().Model(&c).Select("sn").Where(" tenant_id = ? and is_deleted = ? ", c.TenantId, c.IsDeleted).Scan(&sns).Error
+	return sns, err
 }

+ 2 - 2
app/operation/service/lightingRateService.go

@@ -1,7 +1,7 @@
 package service
 
 import (
-	"iot_manager_service/app/device/dao"
+	"iot_manager_service/app/operation/dao"
 	"iot_manager_service/app/operation/edge_service"
 	"iot_manager_service/app/operation/model"
 	"iot_manager_service/util/common"
@@ -22,7 +22,7 @@ func (s lightingRateService) GetMonthList(tenantId int, req model.RequestLightin
 
 // 公用请求边缘端数据,月和日
 func (s lightingRateService) getData(method string, tenantId int, req model.RequestLightingRateFilter) ([]model.ResponseLightingRate, error) {
-	lightControl := dao.LightControl{
+	lightControl := dao.Operation{
 		TenantId: tenantId,
 	}
 	codes, err := lightControl.GetSnList()

+ 1 - 1
app/warn/service/businessTacticsService.go

@@ -8,7 +8,7 @@ import (
 	"strconv"
 )
 
-//TODO: 这里需要对接边缘接口
+//业务 策略 告警 TODO: 这里需要对接边缘接口
 var BusinessTacticsService = new(businessTacticsService)
 
 type businessTacticsService struct{}