package logic import ( "fmt" "git.rtzhtech.cn/iss/public-lib/dao" "git.rtzhtech.cn/iss/public-lib/dto" "git.rtzhtech.cn/iss/public-lib/model" "git.rtzhtech.cn/iss/public-lib/svc" "time" ) // NewStatisticsLogic // @函数名:NewStatisticsLogic // @函数功能描述: 初始化统计数据 // @返回值:*Statistics // func NewStatisticsLogic() *Statistics { return &Statistics{ SvcCtx: svc.SvcCtx, } } type Statistics struct { SvcCtx *svc.ServiceContext } // GetStatisticInfo // @函数名:GetStatisticInfo // @函数功能描述: 获取统计信息 // @对象名:c // @参数定义:req // @返回值:*dto.GetStatisticInfoResponse // @返回值:error // func (c *Statistics) GetStatisticInfo(req *dto.GetStatisticInfoRequest) (*dto.GetStatisticInfoResponse, error) { var ( resp = new(dto.GetStatisticInfoResponse) ) deviceCond := new(dao.Condition) devicewhere := map[string]any{} if req.AppId != 0 { devicewhere[c.SvcCtx.DevDevinfo.Appid.String()] = req.AppId } deviceCond.Where = devicewhere //设备总数、在线、离线 err := c.SvcCtx.DevDevinfo.Base.Count(deviceCond, &resp.DeviceCount) if err != nil { return resp, err } if resp.DeviceCount != 0 { deviceCond.Where[c.SvcCtx.DevDevinfo.Online.String()] = 2 c.SvcCtx.DevDevinfo.Base.Count(deviceCond, &resp.DeviceOnlineCount) resp.DeviceDownCount = resp.DeviceCount - resp.DeviceOnlineCount } //总告警数、未处理告警数 alarmCond := new(dao.Condition) alarmWhere := map[string]any{} var ids []int64 if req.AppId != 0 { strategyidCond := new(dao.Condition) strategyidWhere := map[string]any{} strategyidWhere[c.SvcCtx.AlarmStrategy.Appid.String()] = req.AppId strategyidCond.Where = strategyidWhere strategyidCond.Fields = []string{"strategyid"} var md []*model.AlarmStrategy err := c.SvcCtx.AlarmStrategy.Base.Find(strategyidCond, &md) if err != nil { return resp, err } for _, v := range md { ids = append(ids, v.Strategyid) } } if len(ids) > 0 { alarmWhere["strategyid"] = ids } alarmCond.Where = alarmWhere err = c.SvcCtx.AlarmEvent.Base.Count(alarmCond, &resp.AlarmCount) if err != nil { return resp, err } if resp.AlarmCount != 0 { alarmCond.Where["confirm"] = 1 err = c.SvcCtx.AlarmEvent.Base.Count(alarmCond, &resp.UntreatedAlarmCount) if err != nil { return resp, err } } //联动事件 linkCond := new(dao.Condition) linkWhere := map[string]any{} var linkIds []int64 if req.AppId != 0 { linkstrateCond := dao.Condition{ Where: map[string]any{ "appid": req.AppId, }, } linkstrateCond.Fields = []string{"strategyid"} var md []*model.LinkStrategy err = c.SvcCtx.LinkStrategy.Base.Find(&linkstrateCond, &md) if err != nil { return resp, err } for _, val := range md { linkIds = append(linkIds, val.Strategyid) } } if len(linkIds) > 0 { linkWhere["strategyid"] = linkIds } linkCond.Where = linkWhere err = c.SvcCtx.LinkEvent.Base.Count(linkCond, &resp.LinkCount) if err != nil { return resp, err } if resp.LinkCount > 0 { linkCond.Where["create_at > ?"] = time.Now().Format("2006-01-02 00:00:00") err = c.SvcCtx.LinkEvent.Base.Count(linkCond, &resp.NowDateLinkCount) if err != nil { return resp, err } } return resp, nil } // GetDevStatus // @函数名:GetDevStatus // @函数功能描述: 获取设备统计信息 // @对象名:c // @参数定义:req // @返回值:*dto.GetDevStatusResponse // @返回值:error // func (c *Statistics) GetDevStatus(req *dto.GetDevStatusRequest) (*dto.GetDevStatusResponse, error) { var ( resp = new(dto.GetDevStatusResponse) devStatusInfo []dto.DevStatusInfo ) //算总数 cond := dao.Condition{} if req.AppId != 0 { cond.Where = map[string]any{ "appid": req.AppId, } } err := c.SvcCtx.DevDevinfo.Base.Count(&cond, &resp.Total) if err != nil { return resp, err } if req.Page < 1 { req.Page = 1 } if req.Limit < 1 { req.Limit = 20 } where := `1 = 1` if req.AppId != 0 { where = fmt.Sprintf(`%s%s%d`, where, ` and appid = `, req.AppId) } sql := "SELECT \ndev_info.deviceid as deviceId,\ndev_info.devicename as deviceName,\n( SELECT sys_station.`name` FROM sys_station WHERE dev_info.stationid = sys_station.stationid LIMIT 1 ) AS station_name,\n(select app.appname from sys_app as app where app.appid = dev_info.appid limit 1) AS app_name,\ndev_info.`online` as online\nFROM\n\tdev_devinfo AS dev_info \nWHERE\n\t %s ORDER BY id desc limit %d,%d;" sql = fmt.Sprintf(sql, where, (req.Page-1)*req.Limit, req.Limit) err = c.SvcCtx.DevDevinfo.Base.Raw(sql, &devStatusInfo) if err != nil { return resp, err } for key, val := range devStatusInfo { cond := dao.Condition{ Where: map[string]any{ "deviceid": val.DeviceId, }, } var md []model.DevMpinfo err = c.SvcCtx.DevMpinfo.Base.Find(&cond, &md) if err != nil { return resp, err } for _, v := range md { var tem dto.MpinfoList tem.MpName = v.Mpname tem.MpZone = v.Zonename tem.MpPosition = v.Positionname devStatusInfo[key].Mpinfo = append(devStatusInfo[key].Mpinfo, tem) } } resp.List = devStatusInfo return resp, nil }