Explorar o código

Merge branch 'feature/eta2.5.9_api_stat' into debug

xyxie hai 3 días
pai
achega
cbb343d9a8

+ 96 - 0
controllers/data_manage/edb_inspection_message.go

@@ -0,0 +1,96 @@
+package data_manage
+
+import (
+	"encoding/json"
+	"eta/eta_api/controllers"
+	"eta/eta_api/models"
+	"eta/eta_api/models/data_manage/edb_inspection"
+	"eta/eta_api/services/data"
+)
+
+type EdbInspectionMessageController struct {
+	controllers.BaseAuthController
+}
+
+
+// List
+// @Title 巡检消息列表
+// @Description 巡检消息列表
+// @Param   PageSize   query   int  true       "每页数据条数"
+// @Param   CurrentIndex   query   int  true       "当前页页码,从1开始"
+// @Success 200 {object} response.EdbInspectionMessageListResp
+// @router /edb_inspection/message/list [get]
+func (c *EdbInspectionMessageController) List() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		c.Data["json"] = br
+		c.ServeJSON()
+	}()
+	sysUser := c.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+
+	pageSize, _ := c.GetInt("PageSize")
+	currentIndex, _ := c.GetInt("CurrentIndex")	
+	resp, err := data.GetInspectionMessageList(sysUser.AdminId, currentIndex, pageSize)
+	if err != nil {
+		br.Msg = "获取失败"
+		br.ErrMsg = err.Error()
+		return
+	}
+
+	br.Data = resp
+	br.Msg = "获取成功"
+	br.Ret = 200
+	br.Success = true
+}
+
+// Read
+// @Title 巡检消息已读
+// @Description 巡检消息已读
+// @Param   request body request.EdbInspectionMessageReadReq  true  "消息ID"
+// @Success 200 {object} models.BaseResponse
+// @router /edb_inspection/message/read [post]
+func (m *EdbInspectionMessageController) Read() {
+	br := new(models.BaseResponse).Init()
+	defer func() {
+		m.Data["json"] = br
+		m.ServeJSON()
+	}()
+
+	sysUser := m.SysUser
+	if sysUser == nil {
+		br.Msg = "请登录"
+		br.ErrMsg = "请登录,SysUser Is Empty"
+		br.Ret = 408
+		return
+	}
+	var req edb_inspection.EdbInspectionMessageReadReq
+	if err := json.Unmarshal(m.Ctx.Input.RequestBody, &req); err != nil {
+		br.Msg = "参数错误"
+		br.ErrMsg = "参数错误,err:" + err.Error()
+		return
+	}
+	if req.MessageId <= 0 {
+		br.Msg = "参数错误"
+		return
+	}
+
+	msg, err := data.ReadEdbInspectionMessage(req.MessageId, sysUser.AdminId)
+	if err != nil {
+		if msg == "" {
+			msg = "系统错误"
+		}
+		br.Msg = msg
+		br.ErrMsg = "读取消息失败,err:" + err.Error()
+		return
+	}
+
+	br.Msg = "已读成功"
+	br.Ret = 200
+	br.Success = true
+}

+ 24 - 26
controllers/data_stat/edb_terminal.go

@@ -162,35 +162,33 @@ func (this *EdbTerminalController) List() {
 			return
 		}
 		v.UsedQuota = strconv.Itoa(num)
-		if len(subNumList) == 1 {
-			v.UsedQuota = strconv.Itoa(subNumList[0].Num)
-		} else if len(subNumList) > 1 {
-			if v.Source == utils.DATA_SOURCE_THS {
-				edbNum := 0
-				dateNum := 0
-				hfNum := 0
-				for _, subNum := range subNumList {
-					if subNum.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY {
-						hfNum = subNum.Num
-					} else if subNum.SubSource == utils.DATA_SUB_SOURCE_EDB {
-						edbNum = subNum.Num
-					} else if subNum.SubSource == utils.DATA_SUB_SOURCE_DATE {
-						dateNum = subNum.Num
-					}
+		if v.Source == utils.DATA_SOURCE_THS {
+			edbNum := 0
+			dateNum := 0
+			hfNum := 0
+			for _, subNum := range subNumList {
+				if subNum.SubSource == utils.DATA_SUB_SOURCE_HIGH_FREQUENCY {
+					hfNum = subNum.Num
+				} else if subNum.SubSource == utils.DATA_SUB_SOURCE_EDB {
+					edbNum = subNum.Num
+				} else if subNum.SubSource == utils.DATA_SUB_SOURCE_DATE {
+					dateNum = subNum.Num
 				}
-				v.UsedQuota = fmt.Sprintf("EDB:%d\n日期序列:%d\n高频序列:%d", edbNum, dateNum, hfNum)
-			}else if v.Source == utils.DATA_SOURCE_WIND {
-				edbNum := 0
-				dateNum := 0
-				for _, subNum := range subNumList {
-					if subNum.SubSource == utils.DATA_SUB_SOURCE_EDB {
-						edbNum = subNum.Num
-					} else if subNum.SubSource == utils.DATA_SUB_SOURCE_DATE {
-						dateNum = subNum.Num
-					}
+			}
+			v.UsedQuota = fmt.Sprintf("EDB:%d\n日期序列:%d\n高频序列:%d", edbNum, dateNum, hfNum)
+		}else if v.Source == utils.DATA_SOURCE_WIND {
+			edbNum := 0
+			dateNum := 0
+			for _, subNum := range subNumList {
+				if subNum.SubSource == utils.DATA_SUB_SOURCE_EDB {
+					edbNum = subNum.Num
+				} else if subNum.SubSource == utils.DATA_SUB_SOURCE_DATE {
+					dateNum = subNum.Num
 				}
-				v.UsedQuota = fmt.Sprintf("EDB:%d\n日期序列:%d", edbNum, dateNum)
 			}
+			v.UsedQuota = fmt.Sprintf("EDB:%d\n日期序列:%d", edbNum, dateNum)
+		}else if len(subNumList) == 1 {
+			v.UsedQuota = strconv.Itoa(subNumList[0].Num)
 		}
 	}
 

+ 4 - 1
controllers/edb_monitor/edb_monitor_message.go

@@ -6,6 +6,7 @@ import (
 	"eta/eta_api/models"
 	"eta/eta_api/models/edb_monitor/request"
 	edbmonitor "eta/eta_api/services/edb_monitor"
+	"eta/eta_api/services"
 	"eta/eta_api/utils"
 	"net/http"
 	"strconv"
@@ -125,7 +126,9 @@ func (m *EdbMonitorMessageController) Connect() {
 			utils.FileLog.Error("指标预警信息已读失败,err:%s, adminId:%d", err.Error(), sysUser.AdminId)
 		}
 	}()
-
+	
+	// 其他消息处理
+	services.DealWebSocketMsg(conn, sysUser.AdminId)
 	for {
 		ok = utils.Rc.IsExist(connKey)
 		if !ok {

+ 19 - 2
controllers/message.go

@@ -3,6 +3,7 @@ package controllers
 import (
 	"eta/eta_api/models"
 	"eta/eta_api/models/data_manage/data_manage_permission"
+	"eta/eta_api/models/data_manage/edb_inspection"
 	edbmonitor "eta/eta_api/models/edb_monitor"
 	"eta/eta_api/models/report_approve"
 	"fmt"
@@ -36,7 +37,7 @@ func (c *MessageController) UnReadMessageNum() {
 		return
 	}
 
-	var unReadReportNum, unReadDataPermissionNum, unReadEdbMonitorNum int
+	var unReadReportNum, unReadDataPermissionNum, unReadEdbMonitorNum, unReadEdbInspectionNum int
 
 	// 获取报告审批消息
 	{
@@ -93,8 +94,24 @@ func (c *MessageController) UnReadMessageNum() {
 		unReadEdbMonitorNum = unreadTotal
 	}
 
+	// 获取巡检消息
+	{
+		cond := ` AND admin_id = ? AND is_read = ?`
+		pars := make([]interface{}, 0)
+		pars = append(pars, sysUser.AdminId, 0)
+
+		messageOb := new(edb_inspection.EdbInspectionMessage)
+		unreadTotal, e := messageOb.GetCountByCondition(cond, pars)
+		if e != nil {
+			br.Msg = "获取失败"
+			br.ErrMsg = "获取资产消息列表总数失败, Err: " + e.Error()
+			return
+		}
+		unReadEdbInspectionNum = int(unreadTotal)
+	}
+
 	// 汇总数
-	num := unReadReportNum + unReadDataPermissionNum + unReadEdbMonitorNum
+	num := unReadReportNum + unReadDataPermissionNum + unReadEdbMonitorNum + unReadEdbInspectionNum
 
 	br.Data = num
 	br.Ret = 200

+ 77 - 0
models/data_manage/edb_inspection/edb_inspection_message.go

@@ -4,6 +4,8 @@ import (
 	"eta/eta_api/global"
 	"eta/eta_api/utils"
 	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
 )
 
 // EdbInspectionMessage
@@ -14,6 +16,9 @@ type EdbInspectionMessage struct {
 	AdminId            int64     `gorm:"column:admin_id"`
 	Message            string    `gorm:"column:message"`
 	IsRead             int8      `gorm:"column:is_read"`
+	Source             int8      `gorm:"column:source"`
+	TerminalCode       string    `gorm:"column:terminal_code"`
+	InspectionTime     time.Time `gorm:"column:inspection_time"`
 	CreateTime         time.Time `gorm:"column:create_time"`
 	ModifyTime         time.Time `gorm:"column:modify_time"`
 }
@@ -82,4 +87,76 @@ func (m *EdbInspectionMessage) UpdateSendStatus(sendStatus int8) (err error) {
 	sql := `UPDATE edb_inspection_message SET send_status = ?, modify_time = ? WHERE message_id = ?`
 	err = global.DbMap[utils.DbNameIndex].Exec(sql, sendStatus, time.Now(), m.MessageId).Error
 	return
+}
+
+// GetCountByCondition
+// @Description: 根据条件获取巡检消息数量
+// @param cond string
+// @param pars []interface{}
+// @return int64
+// @return err error
+func (m *EdbInspectionMessage) GetCountByCondition(cond string, pars []interface{}) (count int64, err error) {
+	sql := `SELECT COUNT(*) FROM edb_inspection_message WHERE 1=1` + cond
+	err = global.DbMap[utils.DbNameIndex].Raw(sql, pars...).Scan(&count).Error
+	return
+}
+
+type EdbInspectionMessageResp struct {
+	MessageId          int64
+	AdminId            int64
+	InspectionRecordId int64
+	Content            string    
+	Remark             string    
+	IsRead             int8      
+	Source             int8
+	TerminalCode       string
+	InspectionTime     string
+}
+
+type EdbInspectionMessageListResp struct {
+	List   []*EdbInspectionMessageResp `json:"list"`
+	Paging *paging.PagingItem           `json:"paging"`
+}
+
+type EdbInspectionMessageReadReq struct {
+	MessageId int64 
+}
+
+
+func BatchModifyEdbInspectionMessageIsRead(ids []int64, adminId int) (err error) {
+	if len(ids) == 0 {
+		return
+	}
+	o := global.DbMap[utils.DbNameIndex]
+	sql := `UPDATE edb_inspection_message SET is_read =1, modify_time = ? WHERE admin_id =? AND is_read = 0 AND message_id IN (` + utils.GetOrmInReplace(len(ids)) + `)`
+	err = o.Exec(sql, time.Now(), adminId, ids).Error
+	return
+}
+
+func GetEdbInspectionMessageById(id int) (item *EdbInspectionMessage, err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := "SELECT * FROM edb_inspection_message WHERE message_id =?"
+	err = o.Raw(sql, id).First(&item).Error
+	return
+}
+
+func GetEdbInspectionMessageByAdminId(adminId int) (items []*EdbInspectionMessage, err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := "SELECT * FROM edb_inspection_message WHERE admin_id =? AND is_read = 0 ORDER BY create_time DESC"
+	err = o.Raw(sql, adminId).Find(&items).Error
+	return
+}
+
+func GetEdbInspectionMessageCountByAdminId(adminId int) (count int, err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := "SELECT COUNT(*) FROM edb_inspection_message WHERE admin_id =? ORDER BY is_read ASC, create_time DESC"
+	err = o.Raw(sql, adminId).Scan(&count).Error
+	return
+}
+
+func GetEdbInspectionMessagePageByAdminId(adminId, startSize, pageSize int) (items []*EdbInspectionMessage, err error) {
+	o := global.DbMap[utils.DbNameIndex]
+	sql := "SELECT * FROM edb_inspection_message WHERE admin_id =? ORDER BY is_read ASC, create_time DESC LIMIT?,?"
+	err = o.Raw(sql, adminId, startSize, pageSize).Find(&items).Error
+	return
 } 

+ 7 - 0
models/message.go

@@ -0,0 +1,7 @@
+package models
+
+type WebsocketMessageResponse struct {
+	MessageType int `description:"消息类型:0-预警消息;1-巡检消息" json:"message_type"`
+	Data  interface{} `description:"消息数据" json:"data"`
+}
+

+ 18 - 0
routers/commentsRouter.go

@@ -6514,6 +6514,24 @@ func init() {
             Filters: nil,
             Params: nil})
 
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInspectionMessageController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInspectionMessageController"],
+        beego.ControllerComments{
+            Method: "List",
+            Router: `/edb_inspection/message/list`,
+            AllowHTTPMethods: []string{"get"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
+    beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInspectionMessageController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:EdbInspectionMessageController"],
+        beego.ControllerComments{
+            Method: "Read",
+            Router: `/edb_inspection/message/read`,
+            AllowHTTPMethods: []string{"post"},
+            MethodParams: param.Make(),
+            Filters: nil,
+            Params: nil})
+
     beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:FactorEdbSeriesController"] = append(beego.GlobalControllerRouter["eta/eta_api/controllers/data_manage:FactorEdbSeriesController"],
         beego.ControllerComments{
             Method: "Add",

+ 1 - 0
routers/router.go

@@ -208,6 +208,7 @@ func init() {
 				&data_manage.BaseFromPurangController{},
 				&data_manage.BaseFromRadishResearchController{},
 				&data_manage.EdbInspectionController{},
+				&data_manage.EdbInspectionMessageController{},
 			),
 		),
 		web.NSNamespace("/my_chart",

+ 109 - 0
services/data/edb_inspection_message.go

@@ -0,0 +1,109 @@
+package data
+
+import (
+	"errors"
+	"eta/eta_api/models/data_manage/edb_inspection"
+	"eta/eta_api/utils"
+	"time"
+
+	"github.com/rdlucklib/rdluck_tools/paging"
+)
+
+func ReadEdbInspectionMessage(messageId int64, adminId int) (msg string, err error) {
+	message, err := edb_inspection.GetMessageById(int64(messageId))
+	if err != nil {
+		if utils.IsErrNoRow(err) {
+			msg = "消息不存在"
+			return
+		}
+		msg = "获取消息失败"
+		return
+	}
+	if message.AdminId != int64(adminId) {
+		msg = "您没有权限查看该消息"
+		err = errors.New("no permission")
+		return
+	}
+	message.IsRead = 1
+	message.ModifyTime = time.Now()
+	err = message.Update([]string{"IsRead", "ModifyTime"})
+	if err != nil {
+		msg = "已读失败"
+		return
+	}
+	return
+}
+
+func ReadEdbInspectionMessageList(messageId []int64, adminId int) (msg string, err error) {
+	err = edb_inspection.BatchModifyEdbInspectionMessageIsRead(messageId, adminId)
+	if err != nil {
+		msg = "已读失败"
+		return
+	}
+	return
+}
+
+func SendInspectionMessages(adminId int, message *edb_inspection.EdbInspectionMessage) (data *edb_inspection.EdbInspectionMessageResp, err error) {
+	resp := edb_inspection.EdbInspectionMessageResp{
+		MessageId: message.MessageId,
+		Content: "巡检状态异常",
+		Remark: message.Message,
+		IsRead: message.IsRead,
+		Source: message.Source,
+		TerminalCode: message.TerminalCode,
+		InspectionTime: message.InspectionTime.Format(utils.FormatDateTime),
+	}
+	return &resp, nil
+}
+
+func GetHistoryInspectionMessages(adminId int) (items []*edb_inspection.EdbInspectionMessage, err error) {
+	messageList, err := edb_inspection.GetEdbInspectionMessageByAdminId(adminId)
+	if err != nil {
+		return
+	}
+
+	items = messageList
+	return
+}
+
+func GetInspectionMessageList(adminid int, currentIndex, pageSize int) (resp edb_inspection.EdbInspectionMessageListResp, err error) {
+	startSize := utils.StartIndex(currentIndex, pageSize)
+
+	total, err := edb_inspection.GetEdbInspectionMessageCountByAdminId(adminid)
+	if err != nil {
+		return
+	}
+	if total == 0 {
+		resp.List = make([]*edb_inspection.EdbInspectionMessageResp, 0)
+		resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
+		return
+	}
+
+	messageList, err := edb_inspection.GetEdbInspectionMessagePageByAdminId(adminid, startSize, pageSize)
+	if err != nil {
+		return
+	}
+
+	resp.List = toEdbInspectionMessageResp(messageList)
+	resp.Paging = paging.GetPaging(currentIndex, pageSize, total)
+	return
+}
+
+func toEdbInspectionMessageResp(items []*edb_inspection.EdbInspectionMessage) (list []*edb_inspection.EdbInspectionMessageResp) {
+	list = make([]*edb_inspection.EdbInspectionMessageResp, 0)
+	for _, message := range items {
+		item := edb_inspection.EdbInspectionMessageResp{
+			MessageId: message.MessageId,
+			InspectionRecordId: message.InspectionRecordId,
+			AdminId: message.AdminId,
+			Content: "巡检状态异常",
+			Remark: message.Message,
+			IsRead: message.IsRead,
+			Source: message.Source,
+			TerminalCode: message.TerminalCode,
+			InspectionTime: message.InspectionTime.Format(utils.FormatDateTime),
+		}
+		list = append(list, &item)
+	}
+	return
+} 

+ 12 - 8
services/edb_monitor/edb_monitor_message.go

@@ -2,6 +2,7 @@ package edbmonitor
 
 import (
 	"errors"
+	"eta/eta_api/models"
 	edbmonitor "eta/eta_api/models/edb_monitor"
 	"eta/eta_api/models/edb_monitor/response"
 	"eta/eta_api/utils"
@@ -95,15 +96,18 @@ func SendMessages(adminId, edbInfoId, edbInfoType int, classifyId int, edbUnique
 	if conn == nil {
 		return errors.New("no connection")
 	}
-	msg := response.EdbMonitorMessageResp{
-		EdbInfoId:     edbInfoId,
-		EdbInfoType:   edbInfoType,
-		EdbUniqueCode: edbUniqueCode,
-		EdbClassifyId: classifyId,
-		Message:       message,
-		TriggerTime:   triggerTime,
+	resp := models.WebsocketMessageResponse{
+		MessageType: 0,
+		Data: response.EdbMonitorMessageResp{
+			EdbInfoId:     edbInfoId,
+			EdbInfoType:   edbInfoType,
+			EdbUniqueCode: edbUniqueCode,
+			EdbClassifyId: classifyId,
+			Message:       message,
+			TriggerTime:   triggerTime,
+		},
 	}
-	return conn.WriteJSON(msg)
+	return conn.WriteJSON(resp)
 }
 
 func GetHistoryMessages(adminId int) (items []*response.EdbMonitorMessageResp, err error) {

+ 62 - 0
services/websocket_msg.go

@@ -0,0 +1,62 @@
+package services
+
+import (
+	"eta/eta_api/models"
+	"eta/eta_api/services/data"
+	"eta/eta_api/utils"
+
+	"github.com/gorilla/websocket"
+)
+
+func DealWebSocketMsg(conn *websocket.Conn, adminId int) {
+	DealEdbInspectionMessage(conn, adminId)
+}
+
+// 处理巡检消息
+func DealEdbInspectionMessage(conn *websocket.Conn, adminId int) {
+	messageList, err := data.GetHistoryInspectionMessages(adminId)
+	if err != nil {
+		utils.FileLog.Error("获取巡检信息历史失败,err:%s, adminId:%d", err.Error(), adminId)
+	}
+	success := make(chan int64, 10)
+	go func() {
+		defer close(success)
+		for i, msg := range messageList {
+			if i == 0 {
+				// 多条消息仅发送最新一条
+				respData, err := data.SendInspectionMessages(adminId, msg)
+				if err != nil {
+					utils.FileLog.Error("巡检信息发送失败,err:%s, adminId:%d", err.Error(), adminId)
+				} else {
+					resp := models.WebsocketMessageResponse{
+						MessageType: 1,
+						Data: respData,
+					}
+					err = conn.WriteJSON(resp)
+					if err != nil {
+						utils.FileLog.Error("巡检信息发送失败,err:%s, adminId:%d", err.Error(), adminId)
+					} else {
+						utils.FileLog.Info("巡检信息发送成功,adminId:%d, messageId:%d", adminId, msg.MessageId)
+						success <- msg.MessageId
+					}
+				}
+			} else {
+				success <- msg.MessageId
+			}
+		}
+	}()
+	go func() {
+		readList := make([]int64, 0)
+		for {
+			msgId, ok := <-success
+			if !ok {
+				break
+			}
+			readList = append(readList, msgId)
+		}
+		_, err = data.ReadEdbInspectionMessageList(readList, adminId)
+		if err != nil {
+			utils.FileLog.Error("巡检信息已读失败,err:%s, adminId:%d", err.Error(), adminId)
+		}
+	}()
+}