sixian 2 лет назад
Родитель
Сommit
460f67e8df

+ 65 - 0
app/operation/controller/deviceCountController.go

@@ -0,0 +1,65 @@
+package controller
+
+import (
+	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/middleware"
+	"iot_manager_service/app/operation/model"
+	"iot_manager_service/app/operation/service"
+	"iot_manager_service/util/common"
+	"net/http"
+)
+
+var Device = new(deviceCtl)
+
+type deviceCtl struct{}
+
+func (c deviceCtl) CountDevice(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	var req model.ResuestDeviceCountFilter
+	err := ctx.ShouldBindQuery(&req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	records, err := service.DeviceCountService.CountDevice(claims.TenantId, req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
+}
+
+func (c deviceCtl) MonthList(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	var req model.ResuestDeviceCountFilter
+	err := ctx.ShouldBindQuery(&req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	records, err := service.DeviceCountService.GetMonthList(claims.TenantId, req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
+}
+
+func (c deviceCtl) YearList(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	var req model.ResuestDeviceCountFilter
+	err := ctx.ShouldBindQuery(&req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	records, err := service.DeviceCountService.GetYearList(claims.TenantId, req)
+	if err != nil {
+		ctx.JSON(http.StatusOK, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, records))
+}

+ 21 - 0
app/operation/dao/common.go

@@ -0,0 +1,21 @@
+package dao
+
+import (
+	"fmt"
+	"gorm.io/gorm"
+	"iot_manager_service/util/common"
+)
+
+var Db *gorm.DB
+
+func InitDB(db *gorm.DB) {
+	Db = db
+	models := []common.TableModelAuto{}
+	for _, val := range models {
+		//fmt.Println(val.Model)
+		err := Db.Set("gorm:table_options", "comment '"+val.Comment+"'").AutoMigrate(val.Model)
+		if err != nil {
+			panic(fmt.Sprintf("AutoMigrate err : %v", err))
+		}
+	}
+}

+ 129 - 0
app/operation/dao/viewsAllCodeDeviceDao.go

@@ -0,0 +1,129 @@
+package dao
+
+import "iot_manager_service/app/operation/model"
+
+type ViewsAllCodeDevice struct {
+	TenantId       int    `json:"tenantId"`
+	DeviceType     int    `json:"deviceType"`     //类型 1-14
+	DeviceTypeName string `json:"deviceTypeName"` //类型中文名
+	MonthTime      string `json:"monthTime,omitempty"`
+	YearTime       string `json:"yearTime,omitempty"`
+	OffLineCountT  int    `json:"offLine"`    //掉线数
+	AlarmCountT    int    `json:"countAlarm"` //报警数
+
+	DeviceType1CountT  int `json:"deviceType1CountT"`
+	DeviceType2CountT  int `json:"deviceType2CountT"`
+	DeviceType3CountT  int `json:"deviceType3CountT"`
+	DeviceType4CountT  int `json:"deviceType4CountT"`
+	DeviceType5CountT  int `json:"deviceType5CountT"`
+	DeviceType6CountT  int `json:"deviceType6CountT"`
+	DeviceType7CountT  int `json:"deviceType7CountT"`
+	DeviceType8CountT  int `json:"deviceType8CountT"`
+	DeviceType9CountT  int `json:"deviceType9CountT"`
+	DeviceType10CountT int `json:"deviceType10CountT"`
+	DeviceType11CountT int `json:"deviceType11CountT"`
+	DeviceType12CountT int `json:"deviceType12CountT"`
+	DeviceType13CountT int `json:"deviceType13CountT"`
+	DeviceType14CountT int `json:"deviceType14CountT"`
+	DeviceType15CountT int `json:"deviceType15CountT"`
+	CountT             int `json:"countT"` //设备数
+}
+
+func (ViewsAllCodeDevice) TableName() string {
+	return "view_all_code" //此处不是真实表,是使用视图查询的
+}
+
+// 总设备数
+func (v ViewsAllCodeDevice) GetDeviceCount() ([]ViewsAllCodeDevice, error) {
+	var list []ViewsAllCodeDevice
+	err := Db.Debug().Model(&v).Raw("SELECT device_type,device_type_name,count(*) as count_t FROM view_all_code where tenant_id=? GROUP BY device_type,device_type_name", v.TenantId).Scan(&list).Error
+	return list, err
+}
+
+// 按月统计
+func (v ViewsAllCodeDevice) GetMonthDeviceCount(req model.ResuestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
+	var list []ViewsAllCodeDevice
+	sql := `
+SELECT
+	month_time,
+	sum(count_t) AS count_t,
+	SUM(CASE device_type WHEN 1 THEN count_t ELSE 0 END) as device_type1_count_t,
+	SUM(CASE device_type WHEN 2 THEN count_t ELSE 0 END) as device_type2_count_t,
+	SUM(CASE device_type WHEN 3 THEN count_t ELSE 0 END) as device_type3_count_t,
+	SUM(CASE device_type WHEN 4 THEN count_t ELSE 0 END) as device_type4_count_t,
+	SUM(CASE device_type WHEN 5 THEN count_t ELSE 0 END) as device_type5_count_t,
+	SUM(CASE device_type WHEN 6 THEN count_t ELSE 0 END) as device_type6_count_t,
+	SUM(CASE device_type WHEN 7 THEN count_t ELSE 0 END) as device_type7_count_t,
+	SUM(CASE device_type WHEN 8 THEN count_t ELSE 0 END) as device_type8_count_t,
+	SUM(CASE device_type WHEN 9 THEN count_t ELSE 0 END) as device_type9_count_t,
+	SUM(CASE device_type WHEN 10 THEN count_t ELSE 0 END) as device_type10_count_t,
+	SUM(CASE device_type WHEN 11 THEN count_t ELSE 0 END) as device_type11_count_t,
+	SUM(CASE device_type WHEN 12 THEN count_t ELSE 0 END) as device_type12_count_t,
+	SUM(CASE device_type WHEN 13 THEN count_t ELSE 0 END) as device_type13_count_t,
+	SUM(CASE device_type WHEN 14 THEN count_t ELSE 0 END) as device_type14_count_t,
+	SUM(CASE device_type WHEN 15 THEN count_t ELSE 0 END) as device_type15_count_t
+FROM
+	(
+	SELECT
+		device_type,
+		count(*) AS count_t,
+		date_format( create_time, '%Y-%m' ) AS month_time 
+	FROM
+		view_all_code 
+	where tenant_id=? and create_time >= ? and create_time<=?
+	GROUP BY
+		device_type,
+		month_time 
+	ORDER BY
+		month_time DESC 
+	) a 
+GROUP BY
+	month_time
+`
+	err := Db.Debug().Model(&v).Raw(sql, v.TenantId, req.StartTime, req.EndTime).Scan(&list).Error
+	return list, err
+}
+
+// 按年统计
+func (v ViewsAllCodeDevice) GetYearDeviceCount(req model.ResuestDeviceCountFilter) ([]ViewsAllCodeDevice, error) {
+	var list []ViewsAllCodeDevice
+	sql := `
+SELECT
+	year_time,
+	sum(count_t) AS count_t,
+	SUM(CASE device_type WHEN 1 THEN count_t ELSE 0 END) as device_type1_count_t,
+	SUM(CASE device_type WHEN 2 THEN count_t ELSE 0 END) as device_type2_count_t,
+	SUM(CASE device_type WHEN 3 THEN count_t ELSE 0 END) as device_type3_count_t,
+	SUM(CASE device_type WHEN 4 THEN count_t ELSE 0 END) as device_type4_count_t,
+	SUM(CASE device_type WHEN 5 THEN count_t ELSE 0 END) as device_type5_count_t,
+	SUM(CASE device_type WHEN 6 THEN count_t ELSE 0 END) as device_type6_count_t,
+	SUM(CASE device_type WHEN 7 THEN count_t ELSE 0 END) as device_type7_count_t,
+	SUM(CASE device_type WHEN 8 THEN count_t ELSE 0 END) as device_type8_count_t,
+	SUM(CASE device_type WHEN 9 THEN count_t ELSE 0 END) as device_type9_count_t,
+	SUM(CASE device_type WHEN 10 THEN count_t ELSE 0 END) as device_type10_count_t,
+	SUM(CASE device_type WHEN 11 THEN count_t ELSE 0 END) as device_type11_count_t,
+	SUM(CASE device_type WHEN 12 THEN count_t ELSE 0 END) as device_type12_count_t,
+	SUM(CASE device_type WHEN 13 THEN count_t ELSE 0 END) as device_type13_count_t,
+	SUM(CASE device_type WHEN 14 THEN count_t ELSE 0 END) as device_type14_count_t,
+	SUM(CASE device_type WHEN 15 THEN count_t ELSE 0 END) as device_type15_count_t
+FROM
+	(
+	SELECT
+		device_type,
+		count(*) AS count_t,
+		date_format( create_time, '%Y' ) AS year_time 
+	FROM
+		view_all_code 
+	where tenant_id=? 
+	GROUP BY
+		device_type,
+		year_time 
+	ORDER BY
+		year_time DESC 
+	) a 
+GROUP BY
+	year_time
+`
+	err := Db.Debug().Model(&v).Raw(sql, v.TenantId).Scan(&list).Error
+	return list, err
+}

+ 6 - 0
app/operation/model/deviceCount.go

@@ -0,0 +1,6 @@
+package model
+
+type ResuestDeviceCountFilter struct {
+	StartTime string `form:"queryStartDate"`
+	EndTime   string `form:"queryEndDate"`
+}

+ 42 - 0
app/operation/service/deviceCoutService.go

@@ -0,0 +1,42 @@
+package service
+
+import (
+	"iot_manager_service/app/operation/dao"
+	"iot_manager_service/app/operation/model"
+	"iot_manager_service/app/warn/service"
+)
+
+var DeviceCountService = new(deviceCountService)
+
+type deviceCountService struct{}
+
+func (s deviceCountService) CountDevice(tenantId int, req model.ResuestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+	list := dao.ViewsAllCodeDevice{
+		TenantId: tenantId,
+	}
+	//TODO: 在线数 未做
+	counts, err := list.GetDeviceCount()
+	for i, count := range counts {
+		warnCount, _ := service.PlatformAlarmService.GetWarnCount(tenantId, count.DeviceType)
+		counts[i].AlarmCountT = int(warnCount)
+		counts[i].OffLineCountT = 0 // 在线数 未做
+	}
+	if err != nil {
+		return nil, err
+	}
+	return counts, nil
+}
+
+func (s deviceCountService) GetYearList(tenantId int, req model.ResuestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+	list := dao.ViewsAllCodeDevice{
+		TenantId: tenantId,
+	}
+	return list.GetYearDeviceCount(req)
+}
+
+func (s deviceCountService) GetMonthList(tenantId int, req model.ResuestDeviceCountFilter) ([]dao.ViewsAllCodeDevice, error) {
+	list := dao.ViewsAllCodeDevice{
+		TenantId: tenantId,
+	}
+	return list.GetMonthDeviceCount(req)
+}

+ 0 - 1
app/warn/README.md

@@ -1 +0,0 @@
-## 告警设置

+ 2 - 3
app/warn/controller/BusinessTacticsController.go

@@ -11,10 +11,10 @@ import (
 	"strconv"
 )
 
+// BusinessTactics 业务 策略 告警
 var BusinessTactics = new(businessTacticsCtl)
 
-type businessTacticsCtl struct {
-}
+type businessTacticsCtl struct{}
 
 func (c businessTacticsCtl) List(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
@@ -52,7 +52,6 @@ func (c businessTacticsCtl) List(ctx *gin.Context) {
 func (c businessTacticsCtl) SaveOrUpdate(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
-
 	var submitData model.RequestBusinessSubmit
 	ctx.ShouldBindJSON(&submitData)
 	err := service.BusinessTacticsService.SaveOrUpdate(claims.TenantId, submitData)

+ 1 - 2
app/warn/controller/noticeRecordController.go

@@ -11,6 +11,7 @@ import (
 	"strconv"
 )
 
+// NoticeRecord 发送记录
 var NoticeRecord = new(noticeRecordCtl)
 
 type noticeRecordCtl struct {
@@ -33,7 +34,6 @@ func (c noticeRecordCtl) List(ctx *gin.Context) {
 		ctx.JSON(http.StatusBadRequest, err)
 		return
 	}
-
 	list, total, err := service.NoticeRecordService.List(claims.TenantId, filter)
 	if err != nil {
 		ctx.JSON(http.StatusBadRequest, err)
@@ -48,5 +48,4 @@ func (c noticeRecordCtl) List(ctx *gin.Context) {
 		Records: list,
 	}
 	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
-
 }

Разница между файлами не показана из-за своего большого размера
+ 3 - 11
app/warn/controller/noticeSetContoller.go


+ 3 - 3
app/warn/controller/platformAlarmController.go

@@ -11,10 +11,10 @@ import (
 	"strconv"
 )
 
+// PlatformAlarm 运维告警记录
 var PlatformAlarm = new(platformAlarmCrl)
 
-type platformAlarmCrl struct {
-}
+type platformAlarmCrl struct{}
 
 func (c platformAlarmCrl) List(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
@@ -95,7 +95,7 @@ func (c platformAlarmCrl) Refresh(ctx *gin.Context) {
 	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
 }
 
-func (c platformAlarmCrl) HannleAlart(ctx *gin.Context) {
+func (c platformAlarmCrl) HandleAlert(ctx *gin.Context) {
 	value, _ := ctx.Get(middleware.Authorization)
 	claims := value.(*middleware.Claims)
 	id, _ := strconv.Atoi(ctx.Query("id"))

+ 13 - 1
app/warn/dao/platformAlarmDao.go

@@ -5,6 +5,7 @@ import (
 	"gorm.io/gorm"
 	"iot_manager_service/app/warn/model"
 	"iot_manager_service/util/common"
+	"time"
 )
 
 // PlatformAlarm 平台所有告警-来源边缘端接口
@@ -62,7 +63,6 @@ func (a PlatformAlarm) Gets(filter model.RequestPlatFormAlartFilter) ([]Platform
 	if filter.ArmSource != 0 {
 		db = db.Where(&PlatformAlarm{ArmSource: filter.ArmSource})
 	}
-
 	err := db.Order("id desc").Find(&list).Error
 	if errors.Is(err, gorm.ErrRecordNotFound) {
 		err = nil
@@ -87,3 +87,15 @@ func (a PlatformAlarm) Update() error {
 	err := Db.Debug().Model(&a).Updates(a).Error
 	return err
 }
+
+func (a PlatformAlarm) GetWarnCount() (int64, error) {
+	var count int64
+	nowTime := time.Now()
+	getTime := nowTime.AddDate(0, 0, -30)
+	err := Db.Debug().Model(&a).Where(&PlatformAlarm{
+		TenantId:      a.TenantId,
+		ArmDeviceType: a.ArmDeviceType,
+		ArmHandle:     1,
+	}).Where("arm_time>?", getTime).Count(&count).Error
+	return count, err
+}

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

@@ -8,6 +8,7 @@ import (
 	"strconv"
 )
 
+//TODO: 这里需要对接边缘接口
 var BusinessTacticsService = new(businessTacticsService)
 
 type businessTacticsService struct{}
@@ -15,7 +16,6 @@ type businessTacticsService struct{}
 func (s businessTacticsService) GetList(tenantId int, filter model.RequestBusinessTacticFilter) ([]dao.BusinessTactics, int64, error) {
 	tactics := dao.BusinessTactics{TenantId: tenantId}
 	businessTactics, i, err := tactics.Gets(filter)
-
 	for i2, tactic := range businessTactics {
 		idStr := strconv.Itoa(tactic.DeviceType)
 		businessTactics[i2].DeviceTypeName = systemService.DictService.GetCacheDict("device_type"+idStr, "device_type", tactic.DeviceType)
@@ -26,13 +26,10 @@ func (s businessTacticsService) GetList(tenantId int, filter model.RequestBusine
 }
 
 func (s businessTacticsService) SaveOrUpdate(tenantId int, d model.RequestBusinessSubmit) error {
-	//TODO: 这里需要对接边缘接口
-
 	MaxValue, _ := strconv.ParseFloat(d.MaxValue, 64)
 	MinValue, _ := strconv.ParseFloat(d.MinValue, 64)
 	Duration, _ := strconv.Atoi(d.Duration)
 	tactics := dao.BusinessTactics{
-
 		AlarmType:    d.AlarmType,
 		DeviceType:   d.DeviceType,
 		BusinessName: d.BusinessName,

+ 0 - 1
app/warn/service/noticeRecordService.go

@@ -14,7 +14,6 @@ func (s noticeRecordService) List(tenantId int, filter model.RequestNoticeRecord
 	dao := dao.NoticeRecord{}
 	records, total, err := dao.GetList(filter)
 	for _, record := range records {
-		//fmt.Printf("record = %v", record)
 		var SendUser, ArmClassifyName string
 		if record.ArmClassify == 1 {
 			SendUser = record.NoticeSet.DevUserIdsName

+ 0 - 1
app/warn/service/noticeSetService.go

@@ -56,7 +56,6 @@ func (s noticeSetService) GetList(tenantId int, searchValue string) ([]model.Not
 			BusinessUserIds:         set.BusinessUserIds,
 		})
 	}
-
 	key := strconv.Itoa(tenantId) + strconv.Itoa(1) + strconv.Itoa(0) + strconv.Itoa(999999)
 	set := noteiceSets[key]
 	data := []model.NoticeSetRecords{

+ 6 - 0
app/warn/service/platformAlarmService.go

@@ -114,3 +114,9 @@ func (s platformAlarmService) HannleAlart(tenant_id int, id int, armHandle int,
 	}
 	return dao.Update()
 }
+
+// GetWarnCount 根据设备类型 取
+func (s platformAlarmService) GetWarnCount(tenantId int, deviceType int) (int64, error) {
+	alarm := dao.PlatformAlarm{TenantId: strconv.Itoa(tenantId), ArmDeviceType: deviceType}
+	return alarm.GetWarnCount()
+}

+ 17 - 0
app/warn/service/taskWarnService.go

@@ -0,0 +1,17 @@
+package service
+
+// 定时任务处理的service
+type TaskWarnService struct {
+}
+
+// HandlingAlarms 报警处理
+func (receiver TaskWarnService) HandlingAlarms() {
+	/*
+		TODO: 未处理
+		1. 查出平台 platformAlarmService.go 最近3天的 status = 0 未处理的 报警记录
+		2. 根据报警 sn 查出设置的报警人相关信息,可能是多个报警方式
+		3. 执行发短信或邮件逻辑,发送内容
+		4. 保存相关日志到 noticeRecordService.go
+	*/
+
+}

+ 0 - 1
go.mod

@@ -13,7 +13,6 @@ require (
 	github.com/robfig/cron v1.2.0
 	github.com/satori/go.uuid v1.2.0
 	github.com/sirupsen/logrus v1.9.0
-	github.com/tosone/minimp3 v1.0.1
 	gopkg.in/yaml.v2 v2.4.0
 	gorm.io/driver/mysql v1.4.4
 	gorm.io/gorm v1.24.1

+ 3 - 0
main.go

@@ -9,6 +9,7 @@ import (
 	data "iot_manager_service/app/data/dao"
 	device "iot_manager_service/app/device/dao"
 	multimedia "iot_manager_service/app/multimedia/dao"
+	operation "iot_manager_service/app/operation/dao"
 	record "iot_manager_service/app/record/dao"
 	system "iot_manager_service/app/system/dao"
 	warn "iot_manager_service/app/warn/dao"
@@ -87,6 +88,8 @@ func initDB() {
 	data.InitDB(db)
 	record.InitDB(db)
 	warn.InitDB(db)
+	operation.InitDB(db)
+
 }
 
 // isDevEnv 是否开发环境 本地开发需要添加环境变量

+ 9 - 3
router/router.go

@@ -557,7 +557,13 @@ func InitRouter(engine *gin.Engine) {
 		manholeGroup.GET("/now", operation.Manhole.LiveData)
 		manholeGroup.GET("/history-list", operation.Manhole.LiveData)
 	}
-
+	//运营统计-设备
+	operationDeviceGroup := operationGroup.Group("/device")
+	{
+		operationDeviceGroup.POST("count-device", operation.Device.CountDevice)
+		operationDeviceGroup.POST("list", operation.Device.MonthList)
+		operationDeviceGroup.POST("year-list", operation.Device.YearList)
+	}
 	warnGroup := engine.Group("/api/longchi/alarm")
 	{
 		//告警设置
@@ -580,11 +586,11 @@ func InitRouter(engine *gin.Engine) {
 			alarmGroup.GET("/list", warn.PlatformAlarm.List)
 			alarmGroup.GET("/detail", warn.PlatformAlarm.Detail)
 			alarmGroup.GET("/refresh", warn.PlatformAlarm.Refresh)
-			alarmGroup.POST("/changeHandleType", warn.PlatformAlarm.HannleAlart)
+			alarmGroup.POST("/changeHandleType", warn.PlatformAlarm.HandleAlert)
 		}
 		//业务告警记录
 		warnGroup.GET("/alarmrecords/list", warn.NoticeRecord.List)
-
+		// 业务 策略 告警
 		businesstacticsGroup := warnGroup.Group("businesstactics")
 		{
 			businesstacticsGroup.GET("/list", warn.BusinessTactics.List)