♻️ refactor RegisterParticipant impl

Signed-off-by: devad <cossjie@foxmail.com>

Former-commit-id: 9ff3815f5643a7b10638528127129988c58c9772
This commit is contained in:
devad 2023-07-26 18:28:36 +08:00
parent 3d8ee4c04a
commit ad3a651558
2 changed files with 8 additions and 3 deletions

View File

@ -2,8 +2,10 @@ package participantservicelogic
import ( import (
"context" "context"
"github.com/pkg/errors"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/model" "gitlink.org.cn/jcce-pcm/pcm-coordinator/model"
"gitlink.org.cn/jcce-pcm/utils/tool" "gitlink.org.cn/jcce-pcm/utils/tool"
"gorm.io/gorm"
"time" "time"
"gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/svc" "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/internal/svc"
@ -60,7 +62,9 @@ func (l *RegisterParticipantLogic) RegisterParticipant(in *pcmCore.ParticipantPh
tool.Convert(info, nodeInfo) tool.Convert(info, nodeInfo)
nodeInfo.CreatedTime = time.Now() nodeInfo.CreatedTime = time.Now()
nodeInfo.ParticipantId = participantInfo.Id nodeInfo.ParticipantId = participantInfo.Id
if nodeInfo.Id == 0 { //查询节点name与ParticipantId是否存在
nodeErr := db.Where(&model.ScNodePhyInfo{NodeName: nodeInfo.NodeName, ParticipantId: in.ParticipantId}).Take(nodeInfo)
if errors.Is(nodeErr.Error, gorm.ErrRecordNotFound) {
nodeInfo.Id = tool.GenSnowflakeID() nodeInfo.Id = tool.GenSnowflakeID()
} }
nodeList = append(nodeList, nodeInfo) nodeList = append(nodeList, nodeInfo)
@ -73,7 +77,9 @@ func (l *RegisterParticipantLogic) RegisterParticipant(in *pcmCore.ParticipantPh
tool.Convert(label, labelInfo) tool.Convert(label, labelInfo)
labelInfo.CreatedTime = time.Now() labelInfo.CreatedTime = time.Now()
labelInfo.ParticipantId = participantInfo.Id labelInfo.ParticipantId = participantInfo.Id
if labelInfo.Id == 0 { //查询标签key value与ParticipantId是否存在
labelErr := db.Where(&model.ScParticipantLabelInfo{Key: labelInfo.Key, Value: labelInfo.Value, ParticipantId: in.ParticipantId}).Take(labelInfo)
if errors.Is(labelErr.Error, gorm.ErrRecordNotFound) {
labelInfo.Id = tool.GenSnowflakeID() labelInfo.Id = tool.GenSnowflakeID()
} }
labelList = append(labelList, labelInfo) labelList = append(labelList, labelInfo)

View File

@ -49,7 +49,6 @@ func (l *ReportAvailableLogic) ReportAvailable(in *pcmCore.ParticipantAvailReq)
participantPhyInfo := &model.ScParticipantPhyInfo{} participantPhyInfo := &model.ScParticipantPhyInfo{}
participantPhyInfo.Id = in.ParticipantId participantPhyInfo.Id = in.ParticipantId
if errors.Is(db.Take(&participantPhyInfo).Error, gorm.ErrRecordNotFound) { if errors.Is(db.Take(&participantPhyInfo).Error, gorm.ErrRecordNotFound) {
fmt.Println("sdsdfsdf ")
return &pcmCore.ParticipantResp{ return &pcmCore.ParticipantResp{
Code: 500, Code: 500,
Msg: fmt.Sprintf("ParticipantInfo Does not exist, please check participantPhyId: %d", in.ParticipantId), Msg: fmt.Sprintf("ParticipantInfo Does not exist, please check participantPhyId: %d", in.ParticipantId),