package participantservicelogic import ( "context" "fmt" "github.com/pkg/errors" "gitlink.org.cn/jcce-pcm/pcm-coordinator/model" "gitlink.org.cn/jcce-pcm/utils/tool" "gorm.io/gorm" "time" "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/svc" "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/pcmCore" "github.com/zeromicro/go-zero/core/logx" ) type ReportAvailableLogic struct { ctx context.Context svcCtx *svc.ServiceContext logx.Logger } func NewReportAvailableLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ReportAvailableLogic { return &ReportAvailableLogic{ ctx: ctx, svcCtx: svcCtx, Logger: logx.WithContext(ctx), } } // ReportAvailable 监控数据上报 func (l *ReportAvailableLogic) ReportAvailable(in *pcmCore.ParticipantAvailReq) (*pcmCore.ParticipantResp, error) { db := l.svcCtx.DbEngin.Begin() // 执行回滚或者提交操作 defer func() { if p := recover(); p != nil { db.Rollback() logx.Error(p) } else if db.Error != nil { logx.Info("rollback") db.Rollback() } else { db = db.Commit() logx.Info("commit success") } }() //判断Participant静态信息是否存在 participantPhyInfo := &model.ScParticipantPhyInfo{} participantPhyInfo.Id = in.ParticipantId if errors.Is(db.Take(&participantPhyInfo).Error, gorm.ErrRecordNotFound) { return &pcmCore.ParticipantResp{ Code: 500, Msg: fmt.Sprintf("ParticipantInfo Does not exist, please check participantPhyId: %d", in.ParticipantId), }, nil } participantAvailInfo := &model.ScParticipantAvailInfo{} tool.Convert(in, participantAvailInfo) if in.Id == 0 { participantAvailInfo.Id = tool.GenSnowflakeID() } participantAvailInfo.CreatedTime = time.Now() //保存participant动态信息 result := db.Save(&participantAvailInfo) //保存节点信息 nodeList := make([]*model.ScNodeAvailInfo, 0) for _, info := range in.NodeAvailInfo { nodeInfo := &model.ScNodeAvailInfo{} tool.Convert(info, nodeInfo) nodeInfo.CreatedTime = time.Now() nodeInfo.ParticipantAvailId = participantAvailInfo.Id if nodeInfo.Id == 0 { nodeInfo.Id = tool.GenSnowflakeID() } nodeList = append(nodeList, nodeInfo) } result = db.Save(&nodeList) if result.Error != nil { logx.Errorf("orm err:", result.Error) return &pcmCore.ParticipantResp{ Code: 500, Msg: fmt.Sprintf("Save participantAvailInfo error %s", result.Error), }, nil } return &pcmCore.ParticipantResp{ Code: 200, Msg: "ok", }, nil }