| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259 |
- package logic
- import (
- "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"
- "git.rtzhtech.cn/iss/public-lib/utils"
- "github.com/gogf/gf/v2/util/gconv"
- "gorm.io/gen/field"
- "time"
- )
- type AlarmEventLogic struct {
- SvcCtx *svc.ServiceContext
- }
- // NewAlarmEventLogic
- // @Description: 告警事件
- // @return *AlarmEventLogic
- func NewAlarmEventLogic() *AlarmEventLogic {
- return &AlarmEventLogic{
- SvcCtx: svc.SvcCtx,
- }
- }
- // AddAlarmEvent
- // @Description: 添加
- // @receiver l
- // @param in
- // @return error
- func (l *AlarmEventLogic) AddAlarmEvent(in *model.AlarmEvent) error {
- in.ID = 0
- err := l.SvcCtx.AlarmEvent.Create(in)
- return err
- }
- // DelAlarmEvent
- // @Description: 删除
- // @receiver l
- // @param id
- // @return error
- func (l *AlarmEventLogic) DelAlarmEvent(id int) error {
- do := l.SvcCtx.AlarmEvent
- _, err := do.Where(do.ID.Eq(int32(id))).Delete()
- return err
- }
- // ModifyAlarmEvent
- // @Description: 修改
- // @receiver l
- // @param in
- // @param fields 更新字段,当field为null时,更新in的全部字段
- // @return error
- func (l *AlarmEventLogic) ModifyAlarmEvent(in *model.AlarmEvent, fields ...string) error {
- do := l.SvcCtx.AlarmEvent
- var err error
- _do := do.Omit(do.ID).Where(do.Eventid.Eq(in.Eventid))
- if fields == nil {
- m := gconv.Map(in)
- _, err = _do.Updates(m)
- return err
- }
- var fieldList []field.Expr
- for _, v := range fields {
- _f, ok := do.GetFieldByName(v)
- if ok {
- fieldList = append(fieldList, _f)
- }
- }
- _, err = _do.Select(fieldList...).Updates(in)
- return err
- }
- func (l *AlarmEventLogic) getAlarmEventCond(req *dto.GetAlarmEventReq) (*dao.Condition, error) {
- _dao := l.SvcCtx.AlarmEvent
- _strategy := l.SvcCtx.AlarmStrategy
- _app := l.SvcCtx.SysApp
- _relation := l.SvcCtx.AlarmRelation
- _mpinfo := l.SvcCtx.DevMpinfo
- cond := NewCondition(&req.LimitPage)
- if req.EventId > 0 {
- cond.Where[_dao.Eventid.String()] = req.EventId
- return cond, nil
- }
- if req.SubEventId > 0 {
- cond.Where[_dao.Subeventid.String()] = req.SubEventId
- return cond, nil
- }
- var strategyId []int64
- isStrategyIdFirstQ := true //是否是第一次查询StrategyId标示
- if req.AppName != "" {
- var appId int32
- err := _app.Select(_app.Appid).Where(_app.Appname.Eq(req.AppName)).Scan(&appId)
- if err == nil {
- cond.Where[_dao.Appid.String()] = appId
- }
- //var id []int64
- //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Columns(_strategy.Appid).Eq(_app.Select(_app.Appid).Where(_app.Appname.Eq(req.AppName)))).
- // Scan(&id)
- //if err != nil {
- // return nil, err
- //}
- //strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
- //if len(strategyId) == 0 {
- // return nil, nil
- //}
- }
- if req.AppId > 0 {
- cond.Where[_dao.Appid.String()] = req.AppId
- //var id []int64
- ////测点告警
- //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.Eq(req.AppId)).Scan(&id)
- //if err != nil {
- // return nil, err
- //}
- //strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
- //if len(strategyId) == 0 {
- // return nil, nil
- //}
- }
- if req.StartTime != "" {
- cond.Where[_dao.CreateAt.String()+" >= ?"] = req.StartTime
- }
- if req.EndTime != "" {
- cond.Where[_dao.CreateAt.String()+" <= ?"] = req.EndTime
- }
- if req.EventType > 0 {
- var id []int64
- cond.Where[_dao.Eventtype.String()] = req.EventType
- //测点告警
- if req.EventType == 1 && req.PositionName != "" {
- err := _relation.Select(_relation.Strategyid).Where(_relation.Columns(_relation.Mpid).Eq(_mpinfo.Select(_mpinfo.Mpid).Where(_mpinfo.Positionname.Eq(req.PositionName)))).
- Scan(&id)
- if err != nil {
- return nil, err
- }
- strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
- if len(strategyId) == 0 {
- return nil, nil
- }
- }
- } else {
- cond.Where[_dao.Eventtype.String()+" !=?"] = 3
- }
- if len(req.AlarmLevel) > 0 {
- var id []int64
- err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Alarmlevel.In(req.AlarmLevel...)).Scan(&id)
- if err != nil {
- return nil, err
- }
- strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ)
- if len(strategyId) == 0 {
- return nil, nil
- }
- }
- if req.Confirm > 0 {
- cond.Where[_dao.Confirm.String()] = req.Confirm
- }
- if len(strategyId) > 0 {
- cond.Where[_dao.Strategyid.String()] = utils.RemoveRep(strategyId)
- }
- return cond, nil
- }
- // GetAlarmEvent
- // @Description: 查询
- // @receiver l
- // @param req
- // @return *dto.GetAlarmEventResp
- // @return error
- func (l *AlarmEventLogic) GetAlarmEvent(req *dto.GetAlarmEventReq) (*dto.GetAlarmEventResp, error) {
- _dao := l.SvcCtx.AlarmEvent
- _strategy := l.SvcCtx.AlarmStrategy
- _app := l.SvcCtx.SysApp
- _relation := l.SvcCtx.AlarmRelation
- _mpinfo := l.SvcCtx.DevMpinfo
- cond, err := l.getAlarmEventCond(req)
- if cond == nil && err == nil {
- return &dto.GetAlarmEventResp{}, err
- }
- if err != nil {
- return nil, err
- }
- var list []*model.AlarmEvent
- err = _dao.Base.Find(cond, &list)
- if err != nil {
- return nil, err
- }
- resp := &dto.GetAlarmEventResp{}
- for _, v := range list {
- tmp := &dto.AlarmEventDto{}
- alarmStrategy, err := _strategy.Where(_strategy.Strategyid.Eq(v.Strategyid)).Last()
- if err == nil {
- tmp.AlarmStrategy = *alarmStrategy
- tmp.AlarmLevel = alarmStrategy.Alarmlevel
- //err = _app.Select(_app.Nickname).Where(_app.Appid.Eq(alarmStrategy.Appid)).Scan(&tmp.AppName)
- _ = _mpinfo.Select(_mpinfo.Positionname).Where(_mpinfo.Columns(_mpinfo.Mpid).In(
- _relation.Select(_relation.Mpid).Where(_relation.Strategyid.Eq(v.Strategyid)),
- )).Scan(&tmp.PositionName)
- //if err == nil {
- // return nil, err
- //}
- }
- _app.Select(_app.Nickname).Where(_app.Appid.Eq(v.Appid)).Scan(&tmp.AppName)
- tmp.AlarmEvent = *v
- resp.List = append(resp.List, tmp)
- }
- err = _dao.Base.Count(cond, &resp.Total)
- if err != nil {
- return nil, err
- }
- return resp, err
- }
- // GetAlarmEventCount
- // @Description:查询满足条件的总数
- // @receiver l
- // @param where 查询条件
- // @return int64
- // @return error
- //func (l *AlarmEventLogic) GetAlarmEventCount(where map[string]any) (int64, error) {
- // cond := &dao.Condition{}
- // cond.Where = where
- // var count int64
- // err := l.SvcCtx.AlarmEvent.Base.Count(cond, &count)
- // if err != nil {
- // return 0, err
- // }
- // return count, err
- //}
- // SetAlarmEvent
- // @Description: 设置告警事件
- // @receiver l
- // @param id 告警ID
- // @param confirmTime 确认时间
- // @param result 处理结果
- // @return error
- func (l *AlarmEventLogic) SetAlarmEvent(id int32, confirmTime time.Time, result string) error {
- do := l.SvcCtx.AlarmEvent
- var in model.AlarmEvent
- in.ID = id
- in.Confirm = 2
- in.Confirmtime = confirmTime
- in.Result = result
- _, err := do.Updates(in)
- return err
- }
|