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" "gorm.io/gen/field" "time" ) type LinkEventLogic struct { SvcCtx *svc.ServiceContext } // NewLinkEventLogic // @Description: 联动事件 // @return *LinkEventLogic func NewLinkEventLogic() *LinkEventLogic { return &LinkEventLogic{ SvcCtx: svc.SvcCtx, } } // AddLinkEvent // @Description: 添加 // @receiver l // @param in // @return error func (l *LinkEventLogic) AddLinkEvent(in *model.LinkEvent) error { in.ID = 0 err := l.SvcCtx.LinkEvent.Create(in) return err } // DelLinkEvent // @Description: 删除 // @receiver l // @param id // @return error func (l *LinkEventLogic) DelLinkEvent(req *dto.DelLinkEventReq) error { do := l.SvcCtx.LinkEvent var cond []gen.Condition if req.EventId > 0 { cond = append(cond, do.Eventid.Eq(req.EventId)) } if req.StartTime != "" { t, err := time.ParseInLocation("2006-01-02 15:04:05", req.StartTime, time.Local) if err != nil { return err } cond = append(cond, do.CreateAt.Gte(t)) } if req.EndTime != "" { t, err := time.ParseInLocation("2006-01-02 15:04:05", req.EndTime, time.Local) if err != nil { return err } cond = append(cond, do.CreateAt.Lte(t)) } _, err := do.Where(cond...).Delete() return err } // ModifyLinkEvent // @Description: 修改 // @receiver l // @param in // @param fields 更新字段,当field为null时,更新in的全部字段 // @return error func (l *LinkEventLogic) ModifyLinkEvent(in *model.LinkEvent, fields ...string) error { do := l.SvcCtx.LinkEvent 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 *LinkEventLogic) getLinkEventCond(req *dto.GetLinkEventReq) (*dao.Condition, error) { _dao := l.SvcCtx.LinkEvent //_strategy := l.SvcCtx.LinkStrategy //_app := l.SvcCtx.SysApp _relationObj := l.SvcCtx.LinkRelationObj _relationAction := l.SvcCtx.LinkRelationAction _mpInfo := l.SvcCtx.DevMpinfo _cpInfo := l.SvcCtx.DevCpinfo _devInfo := l.SvcCtx.DevDevinfo cond := NewCondition(&req.LimitPage) if req.StartTime != "" { cond.Where[_dao.CreateAt.String()+" >= ?"] = req.StartTime } if req.EndTime != "" { cond.Where[_dao.CreateAt.String()+" <= ?"] = req.EndTime } var strategyId []int64 isStrategyIdFirstQ := true //是否是第一次查询StrategyId标示 if len(req.SrcAppId) > 0 { cond.Where[_dao.Srcappid.String()+" in ?"] = req.SrcAppId //var id []int64 //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.In(req.SrcAppId...)).Scan(&id) //if err != nil { // return nil, err //} // //if req.RelationSrcDstAppId == 1 { // isStrategyIdFirstQ = false // strategyId = utils.RemoveRep(append(strategyId, id...)) //} else { // strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ) // if len(strategyId) == 0 { // return nil, nil // } //} } if len(req.DstAppId) > 0 { cond.Where[_dao.Dstappid.String()+" in ?"] = req.DstAppId //var id []int64 //err := _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.In(req.DstAppId...)).Scan(&id) //if err != nil { // return nil, err //} // //if req.RelationSrcDstAppId == 1 { // isStrategyIdFirstQ = false // strategyId = utils.RemoveRep(append(strategyId, id...)) //} else { // strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ) //} //if len(strategyId) == 0 { // return nil, nil //} } //if req.SrcAppName != "" { // var id []int64 // var appId int32 // err := _app.Select(_app.Appid).Where(_app.Appname.Eq(req.SrcAppName)).Scan(&appId) // if err != nil { // return nil, err // } // err = _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.Eq(appId)).Scan(&id) // if err != nil { // return nil, err // } // strategyId, err = l.removeSingleStrategyId(strategyId, id) // if err != nil { // return nil, err // } //} if req.SrcPositionName != "" { var id []int64 err := _relationObj.Select(_relationObj.Strategyid).Where(_relationObj.Columns(_relationObj.Linkfromid).Eq( _mpInfo.Select(_mpInfo.Mpid).Where(_mpInfo.Positionname.Eq(req.SrcPositionName)), )).Scan(&id) if err != nil { return nil, err } strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ) if len(strategyId) == 0 { return nil, nil } } //if req.DstAppName != "" { // var id []int64 // var appId int32 // err := _app.Select(_app.Appid).Where(_app.Appname.Eq(req.DstAppName)).Scan(&appId) // if err != nil { // return nil, err // } // err = _strategy.Select(_strategy.Strategyid).Where(_strategy.Appid.Eq(appId)).Scan(&id) // if err != nil { // return nil, err // } // strategyId, err = l.removeSingleStrategyId(strategyId, id) // if err != nil { // return nil, err // } //} if req.DstPositionName != "" { var id []int64 err := _relationAction.Select(_relationAction.Strategyid).Where(_relationAction.Columns(_relationAction.Linktoid).Eq( _cpInfo.Select(_cpInfo.Cpid).Where(_cpInfo.Positionname.Eq(req.DstPositionName)), )).Scan(&id) if err != nil { return nil, err } strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ) if len(strategyId) == 0 { return nil, nil } } if req.DstDeviceName != "" { var id []int64 var deviceId []int32 err := _devInfo.Select(_devInfo.Deviceid).Where(_devInfo.Devicename.Eq(req.DstDeviceName)).Scan(&deviceId) if err != nil { return nil, err } var cpId []int64 err = _cpInfo.Select(_cpInfo.Cpid).Where(_cpInfo.Deviceid.In(deviceId...)).Scan(&cpId) if err != nil { return nil, err } err = _relationAction.Select(_relationAction.Strategyid).Where(_relationAction.Linktoid.In(cpId...)).Scan(&id) if err != nil { return nil, err } strategyId = removeSingleStrategyId(strategyId, id, &isStrategyIdFirstQ) if len(strategyId) == 0 { return nil, nil } } if len(strategyId) > 0 { cond.Where[_dao.Strategyid.String()] = utils.RemoveRep(strategyId) } return cond, nil } // GetLinkEvent // @Description: 分页查询 // @receiver l // @param where 查询条件 // @param page // @param limit // @return []*model.LinkEvent // @return error func (l *LinkEventLogic) GetLinkEvent(req *dto.GetLinkEventReq) (*dto.GetLinkEventResp, error) { _dao := l.SvcCtx.LinkEvent _app := l.SvcCtx.SysApp _relationObj := l.SvcCtx.LinkRelationObj _mpInfo := l.SvcCtx.DevMpinfo _cpInfo := l.SvcCtx.DevCpinfo _devInfo := l.SvcCtx.DevDevinfo _linkStrategy := l.SvcCtx.LinkStrategy var linkEventList []*model.LinkEvent cond, err := l.getLinkEventCond(req) if cond == nil && err == nil { return &dto.GetLinkEventResp{}, err } if err != nil { return nil, err } err = _dao.Base.Find(cond, &linkEventList) if err != nil { return nil, err } resp := &dto.GetLinkEventResp{} err = _dao.Base.Count(cond, &resp.Total) if err != nil { return nil, err } for _, v := range linkEventList { tmp := &dto.LinkEventDto{} tmp.LinkEvent = *v tmpDo := _mpInfo.Where(_mpInfo.Columns(_mpInfo.Mpid).In( _relationObj.Select(_relationObj.Linkfromid).Where(_relationObj.Strategyid.Eq(v.Strategyid)), )) tmpDo.Select(_mpInfo.Positionname).Scan(&tmp.SrcPositionName) //_app.Select(_app.Nickname).Where(_app.Columns(_app.Appid).In(tmpDo.Select(_mpInfo.Appid))).Scan(&tmp.SrcAppName) //if tmp.SrcAppName == nil { // tmp.SrcAppName = []string{"策略已删除"} //} _app.Select(_app.Nickname).Where(_app.Appid.Eq(v.Srcappid)).Scan(&tmp.SrcAppName) _app.Select(_app.Nickname).Where(_app.Appid.Eq(v.Dstappid)).Scan(&tmp.DstAppName) devCpinfo, err := _cpInfo.Where(_cpInfo.Cpid.Eq(v.Linktoid)).Last() if err == nil { tmp.Dst = *devCpinfo tmp.DstPositionName = devCpinfo.Positionname //_app.Select(_app.Nickname).Where(_app.Appid.Eq(devCpinfo.Appid)).Scan(&tmp.DstAppName) _devInfo.Select(_devInfo.Devicename).Where(_devInfo.Deviceid.Eq(devCpinfo.Deviceid)).Scan(&tmp.DstDeviceName) } else if err.Error() == "record not found" { tmp.DstAppName = "测点已删除" } linkStrategy, err := _linkStrategy.Where(_linkStrategy.Strategyid.Eq(v.Strategyid)).Last() if err == nil { tmp.LinkStrategy = *linkStrategy } resp.List = append(resp.List, tmp) } return resp, err } // GetLinkEventCount // @Description:查询满足条件的总数 // @receiver l // @param where 查询条件 // @return int64 // @return error func (l *LinkEventLogic) GetLinkEventCount(req *dto.GetLinkEventReq) (int64, error) { cond, err := l.getLinkEventCond(req) if err != nil { return 0, err } var count int64 err = l.SvcCtx.LinkEvent.Base.Count(cond, &count) if err != nil { return 0, err } return count, err }