Browse Source

策略告警相关

sixian 2 years ago
parent
commit
e9bc91edee

+ 93 - 0
app/warn/controller/BusinessTacticsController.go

@@ -0,0 +1,93 @@
+package controller
+
+import (
+	"github.com/gin-gonic/gin"
+	"iot_manager_service/app/middleware"
+	"iot_manager_service/app/warn/model"
+	"iot_manager_service/app/warn/service"
+	"iot_manager_service/util/common"
+	"math"
+	"net/http"
+	"strconv"
+)
+
+var BusinessTactics = new(businessTacticsCtl)
+
+type businessTacticsCtl struct {
+}
+
+func (c businessTacticsCtl) List(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	filter := model.RequestBusinessTacticFilter{}
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+	err := ctx.ShouldBind(&filter)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	list, total, err := service.BusinessTacticsService.GetList(claims.TenantId, filter)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	pages := math.Ceil(float64((total)) / float64(size))
+	rsp := model.ResposeBusinessTactic{
+		Current: current,
+		Size:    size,
+		Total:   total,
+		Pages:   int(pages),
+		Records: list,
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
+}
+
+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)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
+}
+
+func (c businessTacticsCtl) Detail(ctx *gin.Context) {
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
+}
+
+func (c businessTacticsCtl) Remove(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	id, _ := strconv.Atoi(ctx.Query("id"))
+	err := service.BusinessTacticsService.Remove(claims.TenantId, id)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
+}
+
+func (c businessTacticsCtl) Enable(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	var submitData model.RequestBusinessSubmit
+	ctx.ShouldBind(&submitData)
+	err := service.BusinessTacticsService.SaveOrUpdate(claims.TenantId, submitData)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, nil))
+}

+ 35 - 0
app/warn/controller/platformAlarmController.go

@@ -33,6 +33,41 @@ func (c platformAlarmCrl) List(ctx *gin.Context) {
 		ctx.JSON(http.StatusBadRequest, err)
 		return
 	}
+	filter.ArmSource = 1 //运维
+	list, total, err := service.PlatformAlarmService.GetList(claims.TenantId, filter)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	pages := math.Ceil(float64((total)) / float64(size))
+	rsp := model.ResposePlatFormAlart{
+		Current: current,
+		Size:    size,
+		Total:   total,
+		Pages:   int(pages),
+		Records: list,
+	}
+	ctx.JSON(http.StatusOK, common.SuccessResponse(common.Succeeded, rsp))
+}
+
+func (c platformAlarmCrl) List2(ctx *gin.Context) {
+	value, _ := ctx.Get(middleware.Authorization)
+	claims := value.(*middleware.Claims)
+	filter := model.RequestPlatFormAlartFilter{}
+	current, _ := strconv.Atoi(ctx.Query("current"))
+	size, _ := strconv.Atoi(ctx.Query("size"))
+	if current == 0 {
+		current = 1
+	}
+	if size <= 0 || size > 100 {
+		size = 10
+	}
+	err := ctx.ShouldBind(&filter)
+	if err != nil {
+		ctx.JSON(http.StatusBadRequest, err)
+		return
+	}
+	filter.ArmSource = 2 //运维
 	list, total, err := service.PlatformAlarmService.GetList(claims.TenantId, filter)
 	if err != nil {
 		ctx.JSON(http.StatusBadRequest, err)

+ 63 - 0
app/warn/dao/businessTacticsDao.go

@@ -0,0 +1,63 @@
+package dao
+
+import (
+	"gorm.io/gorm"
+	"iot_manager_service/app/warn/model"
+	"iot_manager_service/util/common"
+	"time"
+)
+
+type BusinessTactics struct {
+	AlarmType    int     `comment:"告警类型-数据字典" json:"alarmType,omitempty"`
+	DeviceType   int     `comment:"设备告警类型" json:"deviceType,omitempty"`
+	BusinessName string  `comment:"业务告警名称" json:"businessName,omitempty"`
+	MaxValue     float64 `comment:"上限值" json:"maxValue,omitempty"`
+	MinValue     float64 `comment:"下限值" json:"minValue,omitempty"`
+	Duration     int     `comment:"持续时间" json:"duration,omitempty"`
+	IsEnable     int     `comment:"1-启用,2-禁用;默认启用" json:"isEnable,omitempty"`
+	TenantId     int     `comment:"租户ID" json:"tenantId,omitempty"`
+	ID           uint    `gorm:"primarykey" json:"id"`
+	CreatedAt    time.Time
+	UpdatedAt    time.Time
+	DeletedAt    gorm.DeletedAt `gorm:"index"`
+
+	RelevanceLampNum int    `gorm:"_" json:"relevanceLampNum"` //关联灯杆数
+	AlarmTypeName    string `gorm:"_" json:"alarmTypeName,omitempty"`
+	DeviceTypeName   string `gorm:"_" json:"deviceTypeName,omitempty"`
+}
+
+func (BusinessTactics) TableName() string {
+	return "warn_business_tactics"
+}
+
+func (t BusinessTactics) Gets(filter model.RequestBusinessTacticFilter) ([]BusinessTactics, int64, error) {
+	var list []BusinessTactics
+	db := Db.Debug().Model(&t).Where(&BusinessTactics{TenantId: t.TenantId})
+	db = db.Scopes(common.Paginate(filter.Current, filter.Size))
+	// filter
+	if filter.BusinessName != "" {
+		db = db.Where("business_name like ?", "%"+filter.BusinessName+"%")
+	}
+	if filter.ArmClassify != 0 {
+		db = db.Where("alarm_type = ?", filter.ArmClassify)
+	}
+	err := db.Find(&list).Error
+	var count int64
+	db.Count(&count)
+	return list, count, err
+}
+
+func (t BusinessTactics) Create() error {
+	err := Db.Debug().Create(&t).Error
+	return err
+}
+
+func (t BusinessTactics) Update(id int) error {
+	err := Db.Debug().Where("id=?", id).Updates(&t).Error
+	return err
+}
+
+func (t BusinessTactics) Delete(id int) error {
+	err := Db.Debug().Model(&t).Delete(&t, id).Error
+	return err
+}

+ 23 - 0
app/warn/dao/businessTacticsRel.go

@@ -0,0 +1,23 @@
+package dao
+
+import (
+	"gorm.io/gorm"
+	"time"
+)
+
+type BusinessTacticsRel struct {
+	GroupId      string    `comment:"归属灯杆分组id:p+id" json:"groupId,omitempty"`
+	DeviceId     string    `comment:"归属设备id:sn" json:"deviceId,omitempty"`
+	ModelId      int       `comment:"物模id" json:"modelId,omitempty"`
+	ModelSid     string    `comment:"sn_(设备sn)_modelTid_(model_id)_sid_(物模属性sid)" json:"modelSid,omitempty"`
+	TacticsId    int       `comment:"业务告警策略id" json:"tacticsId,omitempty"`
+	LampPoleSn   string    `comment:"灯杆sn" json:"lampPoleSn,omitempty"`
+	OperaterUser string    `comment:"操作用户ID" json:"operaterUser,omitempty"`
+	OperaterTime time.Time `comment:"修改时间" json:"operaterTime"`
+	TenantId     string    `comment:"租户ID" json:"tenantId,omitempty"`
+	gorm.Model   `json:"Gorm.Model"`
+}
+
+func (BusinessTacticsRel) TableName() string {
+	return "warn_business_tactics_rel"
+}

+ 1 - 0
app/warn/dao/common.go

@@ -14,6 +14,7 @@ func InitDB(db *gorm.DB) {
 		{&NoticeSet{}, "告警通知设置"},
 		{&NoticeRecord{}, "告警通知记录"},
 		{&PlatformAlarm{}, "平台所有告警"},
+		{&BusinessTactics{}, "业务策略"},
 	}
 	for _, val := range models {
 		//fmt.Println(val.Model)

+ 12 - 10
app/warn/dao/noticeRecordDao.go

@@ -9,15 +9,17 @@ import (
 // NoticeRecord 告警通知记录
 type NoticeRecord struct {
 	gorm.Model
-	NoticeSetId  int `gorm:"comment:告警通知设置ID;" json:"noticeSetId"`
-	NoticeSet    NoticeSet
-	ArmClassify  int    `gorm:"comment:告警类型:1运维,2业务;" json:"classify"`
-	Content      string `gorm:"comment:发送内容;" json:"content"`
-	Status       int    `gorm:"comment:发送状态;" json:"status"`
-	SendType     int    `gorm:"comment:发送方式1短信2邮件" json:"sendType"`
-	SendTypeName string `gorm:"comment:发送方式" json:"SendTypeName"`
-	SendValue    int    `gorm:"comment:手机号或邮件" json:"sendValue"`
-	SendName     string `gorm:"comment:收件人" json:"sendName"`
+	NoticeSetId     int `gorm:"comment:告警通知设置ID;" json:"noticeSetId"`
+	NoticeSet       NoticeSet
+	ArmClassify     int    `gorm:"comment:告警类型:1运维,2业务;" json:"classify"`
+	Content         string `gorm:"comment:发送内容;" json:"content"`
+	Status          int    `gorm:"comment:发送状态;" json:"status"`
+	SendType        int    `gorm:"comment:发送方式1短信2邮件" json:"sendType"`
+	SendTypeName    string `gorm:"comment:发送方式" json:"SendTypeName"`
+	SendValue       int    `gorm:"comment:手机号或邮件" json:"sendValue"`
+	SendName        string `gorm:"comment:收件人" json:"sendName"`
+	PlatformAlarmId int
+	PlatformAlarm   PlatformAlarm
 }
 
 func (NoticeRecord) TableName() string {
@@ -52,7 +54,7 @@ func (r NoticeRecord) GetList(filter model.RequestNoticeRecordFilter) ([]NoticeR
 	if filter.SendName != "" {
 		db = db.Where("send_name=?", filter.SendName)
 	}
-	err := db.Find(&list).Error
+	err := db.Preload("PlatformAlarm").Find(&list).Error
 	db.Count(&total)
 	//fmt.Printf("total = %v", total)
 	return list, total, err

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

@@ -20,17 +20,16 @@ type PlatformAlarm struct {
 	LampPoleName      string      `gorm:"column:lamp_pole_name;type:varchar(64);comment:灯杆名称" json:"lampPoleName"`          // 所属灯杆名称
 	LampPoleSn        string      `gorm:"column:lamp_pole_sn;type:varchar(60);comment:灯杆唯一编码" json:"lampPoleSn"`            // 灯杆唯一编码
 	LampPoleLocation  string      `gorm:"column:lamp_pole_location;type:varchar(255);comment:灯杆地址" json:"lampPoleLocation"` // 灯杆地址
-	ArmSource         int         `gorm:"column:arm_source;type:int(11);comment:告警来源 1系统2人工" json:"armSource"`              // 告警来源 1系统2人工
-	ArmTime           common.Time `gorm:"column:arm_time;type:datetime;comment:告警开始时间" json:"armTime"`                      // 告警开始时间
-	ArmEndTime        common.Time `gorm:"column:arm_end_time;type:datetime;comment:告警结束时间" json:"armEndTime"`               // 告警结束时间
-	ArmHandle         int         `gorm:"column:arm_handle;type:int(11);comment:当前处理状态1待处理2忽略3已处理4其他" json:"armHandle"`     // 当前处理状态1待处理2忽略3已处理4其他
-	ArmLevel          int         `gorm:"column:arm_level;type:int(11);comment: 告警类型 1紧急2严重3普通" json:"armLevel"`            // 告警类型 1紧急2严重3普通
-	ArmContent        string      `gorm:"column:arm_content;type:varchar(255);comment:告警内容" json:"armContent"`              // 告警内容
-	ArmUrl            string      `gorm:"column:arm_url;type:varchar(1000);comment:运维告警图片地址" json:"armUrl"`                 // 运维告警图片地址
+	ArmSource         int         `gorm:"column:arm_source;type:int(11);comment:告警来源 1运维2业务" json:"armSource"`
+	ArmTime           common.Time `gorm:"column:arm_time;type:datetime;comment:告警开始时间" json:"armTime"`                  // 告警开始时间
+	ArmEndTime        common.Time `gorm:"column:arm_end_time;type:datetime;comment:告警结束时间" json:"armEndTime"`           // 告警结束时间
+	ArmHandle         int         `gorm:"column:arm_handle;type:int(11);comment:当前处理状态1待处理2忽略3已处理4其他" json:"armHandle"` // 当前处理状态1待处理2忽略3已处理4其他
+	ArmLevel          int         `gorm:"column:arm_level;type:int(11);comment: 告警类型 1紧急2严重3普通" json:"armLevel"`        // 告警类型 1紧急2严重3普通
+	ArmContent        string      `gorm:"column:arm_content;type:varchar(255);comment:告警内容" json:"armContent"`          // 告警内容
+	ArmUrl            string      `gorm:"column:arm_url;type:varchar(1000);comment:运维告警图片地址" json:"armUrl"`             // 运维告警图片地址
 	ArmHanleRemark    string      `gorm:"column:arm_handle_remark;type:text;comment:处理内容" json:"armHanleRemark"`
-	ArmHandleTime     common.Time `gorm:"column:arm_handle_time;type:datetime" json:"armHandleTime"`            // 告警处理时间
-	Status            int         `gorm:"column:status;type:int(11);comment:告警状态 1处理完成2待处理3无需处理" json:"status"` // 告警状态 1处理完成2待处理3无需处理
-	NoticeRecordId    int         `gorm:"comment:发送通知ID" json:"noticeRecordId"`
+	ArmHandleTime     common.Time `gorm:"column:arm_handle_time;type:datetime" json:"armHandleTime"` // 告警处理时间
+	Status            int         `gorm:"column:status;type:int(11);comment:通知发送 1已发送 0未发送" json:"status"`
 	TenantId          string      `gorm:"column:tenant_id;type:varchar(12);comment:租户ID" json:"tenantId"`                       // 租户ID
 	ProcessInstanceId string      `gorm:"column:process_instance_id;type:varchar(255);comment:流程实例主键" json:"processInstanceId"` // 流程实例主键
 	BusinessId        string      `gorm:"column:business_id;type:varchar(64);comment:流程实例主键ID" json:"businessId"`               // 流程实例主键ID
@@ -60,6 +59,10 @@ func (a PlatformAlarm) Gets(filter model.RequestPlatFormAlartFilter) ([]Platform
 	if filter.ArmDeviceName != "" {
 		db = db.Where(&PlatformAlarm{ArmDeviceName: filter.ArmDeviceName})
 	}
+	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

+ 34 - 0
app/warn/model/businessTacticsModel.go

@@ -0,0 +1,34 @@
+package model
+
+type RequestBusinessTacticFilter struct {
+	ArmDeviceType int    `form:"armDeviceType"` //告警类型
+	ArmDeviceName string `form:"armDeviceName"` //告警设备名称
+	ArmHandle     int    `form:"armHandle"`     //是否处理
+	ArmSource     int    `form:"armSource"`     //类型
+
+	ArmClassify  int    `form:"armClassify"`  //策略类型
+	BusinessName string `form:"businessName"` //策略备注名称
+
+	Current int `form:"current"` //当前分页
+	Size    int `form:"size"`    //每页数量
+}
+
+type ResposeBusinessTactic struct {
+	Records interface{} `json:"records"` //记录列表
+	Current int         `json:"current"` //当前分页
+	Size    int         `json:"size"`    //每页数量
+	Total   int64       `json:"total"`   //总数
+	Pages   int         `json:"pages"`   //总页数
+}
+
+type RequestBusinessSubmit struct {
+	ID               int    `json:"id" form:"id"`
+	BusinessName     string `json:"businessName"`
+	AlarmType        int    `json:"alarmType"`
+	DeviceType       int    `json:"deviceType"`
+	MaxValue         string `json:"maxValue"`
+	MinValue         string `json:"minValue"`
+	Duration         string `json:"duration"`
+	RelevanceLampNum string `json:"relevanceLampNum"`
+	Status           int    `json:"status"  form:"status"`
+}

+ 1 - 0
app/warn/model/platformAlarmModel.go

@@ -4,6 +4,7 @@ type RequestPlatFormAlartFilter struct {
 	ArmDeviceType int    `form:"armDeviceType"` //告警类型
 	ArmDeviceName string `form:"armDeviceName"` //告警设备名称
 	ArmHandle     int    `form:"armHandle"`     //是否处理
+	ArmSource     int    `form:"armSource"`     //类型
 
 	Current int `form:"current"` //当前分页
 	Size    int `form:"size"`    //每页数量

+ 60 - 0
app/warn/service/businessTacticsService.go

@@ -0,0 +1,60 @@
+package service
+
+import (
+	"errors"
+	systemService "iot_manager_service/app/system/service"
+	"iot_manager_service/app/warn/dao"
+	"iot_manager_service/app/warn/model"
+	"strconv"
+)
+
+var BusinessTacticsService = new(businessTacticsService)
+
+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)
+		id2Str := strconv.Itoa(tactic.AlarmType)
+		businessTactics[i2].AlarmTypeName = systemService.DictService.GetCacheDict("business_alarm_type2"+id2Str, "business_alarm_type2", tactic.AlarmType)
+	}
+	return businessTactics, i, err
+}
+
+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,
+		MaxValue:     MaxValue,
+		MinValue:     MinValue,
+		Duration:     Duration,
+		IsEnable:     1,
+		TenantId:     tenantId,
+	}
+	err := errors.New("default")
+	if d.ID == 0 {
+		err = tactics.Create()
+	} else {
+		if d.Status == 2 {
+			tactics.IsEnable = 2
+		}
+		err = tactics.Update(d.ID)
+	}
+	return err
+}
+
+func (s businessTacticsService) Remove(tenantId int, id int) error {
+	tactics := dao.BusinessTactics{TenantId: tenantId}
+	return tactics.Delete(id)
+}

+ 7 - 4
app/warn/service/platformAlarmService.go

@@ -70,7 +70,11 @@ func (s platformAlarmService) Refresh(tenantId int) error {
 	for _, data := range list {
 		viewInfo := newAllCode[data.Code]
 		parse := common.TimeStringToGoTime(data.Tend)
-		ArmTime := common.TimeStringToGoTime(data.Updatedat)
+		ArmTime := common.TimeStringToGoTime(data.Tstart)
+		ArmSource := 1
+		if data.Cid != 0 {
+			ArmSource = 2
+		}
 		alarms = append(alarms, dao.PlatformAlarm{
 			ID:                data.ID,
 			TenantId:          strconv.Itoa(tenantId),
@@ -83,14 +87,13 @@ func (s platformAlarmService) Refresh(tenantId int) error {
 			LampPoleName:      viewInfo.LampPoleName,
 			LampPoleSn:        viewInfo.LampPoleSn,
 			LampPoleLocation:  viewInfo.Address,
-			ArmSource:         1,
+			ArmSource:         ArmSource,
 			ArmEndTime:        common.Time(parse),
 			ArmTime:           common.Time(ArmTime),
 			ArmHandle:         1,
 			ArmLevel:          data.Level,
 			ArmContent:        data.Content,
-			Status:            2,
-			NoticeRecordId:    2222,
+			Status:            0, //告警发送
 			Cid:               uint(data.Cid),
 			Cname:             data.Cname,
 			Sid:               data.Sid,

+ 15 - 0
router/router.go

@@ -560,6 +560,7 @@ func InitRouter(engine *gin.Engine) {
 
 	warnGroup := engine.Group("/api/longchi/alarm")
 	{
+		//告警设置
 		noticeGroup := warnGroup.Group("notice")
 		{
 			noticeGroup.GET("/list", warn.NoticeSet.List)
@@ -568,10 +569,12 @@ func InitRouter(engine *gin.Engine) {
 			noticeGroup.GET("/detail", warn.NoticeSet.Detail)
 			noticeGroup.GET("/getUserList", warn.NoticeSet.GetUserList)
 		}
+		//发送记录
 		sendrecordGroup := warnGroup.Group("sendrecord")
 		{
 			sendrecordGroup.GET("/list", warn.NoticeRecord.List)
 		}
+		//运维告警记录
 		alarmGroup := warnGroup.Group("alarm")
 		{
 			alarmGroup.GET("/list", warn.PlatformAlarm.List)
@@ -579,5 +582,17 @@ func InitRouter(engine *gin.Engine) {
 			alarmGroup.GET("/refresh", warn.PlatformAlarm.Refresh)
 			alarmGroup.POST("/changeHandleType", warn.PlatformAlarm.HannleAlart)
 		}
+		//业务告警记录
+		warnGroup.GET("/alarmrecords/list", warn.NoticeRecord.List)
+
+		businesstacticsGroup := warnGroup.Group("businesstactics")
+		{
+			businesstacticsGroup.GET("/list", warn.BusinessTactics.List)
+			businesstacticsGroup.GET("/detail", warn.BusinessTactics.Detail)
+			businesstacticsGroup.POST("/submit", warn.BusinessTactics.SaveOrUpdate)
+			businesstacticsGroup.POST("/remove", warn.BusinessTactics.Remove)
+			businesstacticsGroup.POST("/enable-disable", warn.BusinessTactics.Enable)
+		}
+
 	}
 }