Sfoglia il codice sorgente

GetProcedure GetView

shao 1 giorno fa
parent
commit
d1c73553f4
4 ha cambiato i file con 81 aggiunte e 20 eliminazioni
  1. 8 2
      common/utils.go
  2. 4 4
      models/model.go
  3. 1 1
      rpc_idl
  4. 68 13
      service/protocol.go

+ 8 - 2
common/utils.go

@@ -6,6 +6,7 @@ import (
 	"crypto/cipher"
 	"crypto/md5"
 	"encoding/hex"
+	"fmt"
 	"log/slog"
 	"time"
 )
@@ -41,12 +42,17 @@ func MD5(v []byte) string {
 	return hex.EncodeToString(re)
 }
 
-func GetHeader(ctx context.Context) (Product, Source, Lang) {
+func GetHeader(ctx context.Context) (Product, Source, Lang, error) {
 	m, ok := md.FromIncomingContext(ctx)
 	if ok {
 		slog.Debug("metadata.FromIncomingContext", "md", m)
+	} else {
+		return "", "", "", fmt.Errorf("metadata.FromIncomingContext<UNK>")
 	}
-	return Product(m.Get("product")[0]), Source(m.Get("source")[0]), Lang(m.Get("language")[0])
+	if (len(m.Get("product")) == 0) || (len(m.Get("source")) == 0) || (len(m.Get("language")) == 0) {
+		return "", "", "", fmt.Errorf("metadata missing param")
+	}
+	return Product(m.Get("product")[0]), Source(m.Get("source")[0]), Lang(m.Get("language")[0]), nil
 }
 
 var dbPwKey = []byte("X3O6wVF&6*&lSVk0*504V~q7>\"k]6S'*") // 32 bytes for AES-256

+ 4 - 4
models/model.go

@@ -11,7 +11,7 @@ type PatientType struct {
 	PatientTypeName        string
 	PatientTypeNameLocal   string
 	PatientTypeDescription string
-	Sort                   int32
+	Sort                   int
 	IsEnabled              bool
 	Product                string
 	IsPreInstall           bool
@@ -29,7 +29,7 @@ type BodyPart struct {
 	BodyPartDescription string
 	PatientType         string
 	Modality            string
-	Sort                int32
+	Sort                int
 	IsEnabled           bool
 	Product             string
 	IsPreInstall        bool
@@ -57,7 +57,7 @@ type Procedure struct {
 	ProcedureCategory         string //程序类别
 	Modality                  string //设备类型
 	Views                     []View `gorm:"many2many:p_procedure_views;foreignKey:InternalID;joinForeignKey:ProcedureID;References:InternalID;joinReferences:ViewID"`
-	Sort                      int32
+	Sort                      int
 	IsEnabled                 bool
 	Product                   string
 	IsPreInstall              bool
@@ -87,7 +87,7 @@ type View struct {
 	ConfigObject    datatypes.JSON
 	TechTemplate    string //曝光参数
 	ImgProcTemplate string //图像处理
-	Sort            int32
+	Sort            int
 	IsEnabled       bool
 	Product         string
 	IsPreInstall    bool

+ 1 - 1
rpc_idl

@@ -1 +1 @@
-Subproject commit c7adaf6cbec4584dc9793b9ef81b8982ca12f8ce
+Subproject commit 0fc0d7bbb6b2119ee2abbcaf0dce8ee080937619

+ 68 - 13
service/protocol.go

@@ -4,7 +4,6 @@ import (
 	"context"
 	"encoding/json"
 	"errors"
-	"gorm.io/gorm"
 	"log/slog"
 )
 
@@ -13,6 +12,7 @@ import (
 	"google.golang.org/grpc/status"
 	"google.golang.org/protobuf/types/known/structpb"
 	"gorm.io/datatypes"
+	"gorm.io/gorm"
 )
 
 import (
@@ -50,10 +50,13 @@ type ProtocolServer struct {
 	pb.UnimplementedProtocolServer
 }
 
-func (s *ProtocolServer) GetPatientType(ctx context.Context, in *pb.PatientTypeRequest) (*pb.PatientTypeReply, error) {
-	product, _, _ := common.GetHeader(ctx)
+func (s *ProtocolServer) GetPatientTypeList(ctx context.Context, in *pb.PatientTypeRequest) (*pb.PatientTypeReply, error) {
 	res := pb.PatientTypeReply{}
-	err := models.DB.Model(&models.PatientType{}).Scopes(
+	product, _, _, err := common.GetHeader(ctx)
+	if err != nil {
+		return &res, err
+	}
+	err = models.DB.Model(&models.PatientType{}).Scopes(
 		models.String("product = ?", product.ToString()),
 		models.Bool("is_enabled = ?", in.IsEnabled),
 	).Order("sort").Find(&res.PatientTypeList).Error
@@ -65,10 +68,13 @@ func (s *ProtocolServer) GetPatientType(ctx context.Context, in *pb.PatientTypeR
 	}
 	return &res, nil
 }
-func (s *ProtocolServer) GetBodyPart(ctx context.Context, in *pb.BodyPartRequest) (*pb.BodyPartReply, error) {
-	product, _, _ := common.GetHeader(ctx)
+func (s *ProtocolServer) GetBodyPartList(ctx context.Context, in *pb.BodyPartRequest) (*pb.BodyPartReply, error) {
 	res := pb.BodyPartReply{}
-	err := models.DB.Model(&models.BodyPart{}).Scopes(
+	product, _, _, err := common.GetHeader(ctx)
+	if err != nil {
+		return &res, err
+	}
+	err = models.DB.Model(&models.BodyPart{}).Scopes(
 		models.String("patient_type = ?", in.GetPatientType()),
 		models.String("modality = ?", in.GetModality()),
 		models.String("product = ?", product.ToString()),
@@ -83,10 +89,13 @@ func (s *ProtocolServer) GetBodyPart(ctx context.Context, in *pb.BodyPartRequest
 	return &res, nil
 }
 
-func (s *ProtocolServer) GetProcedure(ctx context.Context, in *pb.ProcedureRequest) (*pb.ProcedureReply, error) {
-	product, _, _ := common.GetHeader(ctx)
+func (s *ProtocolServer) GetProcedureList(ctx context.Context, in *pb.ProcedureRequest) (*pb.ProcedureReply, error) {
 	res := pb.ProcedureReply{}
-	err := models.DB.Model(&models.Procedure{}).Scopes(
+	product, _, _, err := common.GetHeader(ctx)
+	if err != nil {
+		return &res, err
+	}
+	err = models.DB.Model(&models.Procedure{}).Scopes(
 		models.String("patient_type = ?", in.GetPatientType()),
 		models.String("body_part_id = ?", in.GetBodyPartId()),
 		models.String("product = ?", product.ToString()),
@@ -103,6 +112,28 @@ func (s *ProtocolServer) GetProcedure(ctx context.Context, in *pb.ProcedureReque
 	return &res, nil
 }
 
+func (s *ProtocolServer) GetProcedure(ctx context.Context, in *pb.IDRequest) (*pb.Procedure, error) {
+	slog.Info("[rpc]GetProcedure...", "instance_id", *in.InstanceId)
+	res := pb.Procedure{}
+	product, _, _, err := common.GetHeader(ctx)
+	if err != nil {
+		return &res, err
+	}
+	err = models.DB.Model(&models.Procedure{}).Scopes(
+		models.String("internal_id = ?", in.GetInstanceId()),
+		models.String("product = ?", product.ToString()),
+	).Order("sort").Find(&res).Error
+	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return nil, status.Errorf(codes.NotFound, "procedure %s not found", in.GetInstanceId())
+		}
+		return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
+	}
+	res.ProcedureNameLocal = res.ProcedureName
+	res.ProcedureDescriptionLocal = res.ProcedureDescription
+	return &res, nil
+}
+
 func view2pb(view models.View) *pb.View {
 	return &pb.View{
 		InternalId:      view.InternalID,
@@ -123,17 +154,20 @@ func view2pb(view models.View) *pb.View {
 		ConfigObject:    Json2Struct(&view.ConfigObject),
 		TechTemplate:    view.TechTemplate,
 		ImgProcTemplate: view.ImgProcTemplate,
-		Sort:            view.Sort,
+		Sort:            int32(view.Sort),
 		IsEnabled:       view.IsEnabled,
 		Product:         view.Product,
 		IsPreInstall:    view.IsPreInstall,
 	}
 }
 
-func (s *ProtocolServer) GetView(ctx context.Context, in *pb.ViewRequest) (*pb.ViewReply, error) {
-	product, _, _ := common.GetHeader(ctx)
+func (s *ProtocolServer) GetViewList(ctx context.Context, in *pb.ViewRequest) (*pb.ViewReply, error) {
 	procedure := models.Procedure{}
 	res := pb.ViewReply{}
+	product, _, _, err := common.GetHeader(ctx)
+	if err != nil {
+		return &res, err
+	}
 	if in.GetProcedureId() == "" {
 		err := models.DB.Model(&models.View{}).Scopes(
 			models.String("patient_type = ?", in.GetPatientType()),
@@ -164,3 +198,24 @@ func (s *ProtocolServer) GetView(ctx context.Context, in *pb.ViewRequest) (*pb.V
 	}
 	return &res, nil
 }
+
+func (s *ProtocolServer) GetView(ctx context.Context, in *pb.IDRequest) (*pb.View, error) {
+	slog.Info("[rpc]GetView...", "instance_id", *in.InstanceId)
+	product, _, _, err := common.GetHeader(ctx)
+	if err != nil {
+		slog.Info("[rpc]GetView err:", err)
+		return &pb.View{}, err
+	}
+	var view models.View
+	err = models.DB.Scopes(
+		models.String("internal_id = ?", in.GetInstanceId()),
+		models.String("product = ?", product.ToString()),
+	).Order("sort").First(&view).Error
+	if err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			return nil, status.Errorf(codes.NotFound, "procedure %s not found", in.GetInstanceId())
+		}
+		return nil, status.Errorf(codes.Internal, "view find err %v", err)
+	}
+	return view2pb(view), nil
+}