protocol.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "gorm.io/gorm"
  7. "log/slog"
  8. )
  9. import (
  10. "google.golang.org/grpc/codes"
  11. "google.golang.org/grpc/status"
  12. "google.golang.org/protobuf/types/known/structpb"
  13. "gorm.io/datatypes"
  14. )
  15. import (
  16. "protocol-server/common"
  17. "protocol-server/models"
  18. pb "protocol-server/rpc_idl/dr_protocol_pb"
  19. )
  20. var nilStruct *structpb.Struct
  21. func init() {
  22. var err error
  23. nilStruct, err = structpb.NewStruct(map[string]any{})
  24. if err != nil {
  25. panic(err)
  26. }
  27. }
  28. func Json2Struct(o *datatypes.JSON) *structpb.Struct {
  29. var tempMap map[string]interface{}
  30. err := json.Unmarshal(*o, &tempMap)
  31. if err != nil {
  32. slog.Error("Error unmarshalling datatypes.JSON to map", "err", err)
  33. return nilStruct
  34. }
  35. protoStruct, err := structpb.NewStruct(tempMap)
  36. if err != nil {
  37. slog.Error("Error creating proto struct", "err", err)
  38. return nilStruct
  39. }
  40. return protoStruct
  41. }
  42. type ProtocolServer struct {
  43. pb.UnimplementedProtocolServer
  44. }
  45. func (s *ProtocolServer) GetPatientType(ctx context.Context, in *pb.PatientTypeRequest) (*pb.PatientTypeReply, error) {
  46. product, _, _ := common.GetHeader(ctx)
  47. res := pb.PatientTypeReply{}
  48. err := models.DB.Model(&models.PatientType{}).Scopes(
  49. models.String("product = ?", product.ToString()),
  50. models.Bool("is_enabled = ?", in.IsEnabled),
  51. ).Order("sort").Find(&res.PatientTypeList).Error
  52. if err != nil {
  53. return nil, status.Errorf(codes.Internal, "patient_type find err %v", err)
  54. }
  55. for _, pt := range res.GetPatientTypeList() {
  56. pt.PatientTypeLocal = pt.PatientTypeName
  57. }
  58. return &res, nil
  59. }
  60. func (s *ProtocolServer) GetBodyPart(ctx context.Context, in *pb.BodyPartRequest) (*pb.BodyPartReply, error) {
  61. product, _, _ := common.GetHeader(ctx)
  62. res := pb.BodyPartReply{}
  63. err := models.DB.Model(&models.BodyPart{}).Scopes(
  64. models.String("patient_type = ?", in.GetPatientType()),
  65. models.String("modality = ?", in.GetModality()),
  66. models.String("product = ?", product.ToString()),
  67. models.Bool("is_enabled = ?", in.IsEnabled),
  68. ).Order("sort").Find(&res.BodyPartList).Error
  69. if err != nil {
  70. return nil, status.Errorf(codes.Internal, "body_part find err %v", err)
  71. }
  72. for _, pt := range res.GetBodyPartList() {
  73. pt.BodyPartLocal = pt.BodyPartName
  74. }
  75. return &res, nil
  76. }
  77. func (s *ProtocolServer) GetProcedure(ctx context.Context, in *pb.ProcedureRequest) (*pb.ProcedureReply, error) {
  78. product, _, _ := common.GetHeader(ctx)
  79. res := pb.ProcedureReply{}
  80. err := models.DB.Model(&models.Procedure{}).Scopes(
  81. models.String("patient_type = ?", in.GetPatientType()),
  82. models.String("body_part_id = ?", in.GetBodyPartId()),
  83. models.String("product = ?", product.ToString()),
  84. models.Bool("is_enabled = ?", in.IsEnabled),
  85. models.Bool("is_pre_install = ?", in.IsPreInstall),
  86. ).Order("sort").Find(&res.ProcedureList).Error
  87. if err != nil {
  88. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  89. }
  90. for _, pt := range res.GetProcedureList() {
  91. pt.ProcedureNameLocal = pt.ProcedureName
  92. pt.ProcedureDescriptionLocal = pt.ProcedureDescription
  93. }
  94. return &res, nil
  95. }
  96. func view2pb(view models.View) *pb.View {
  97. return &pb.View{
  98. InternalId: view.InternalID,
  99. ViewId: view.ViewID,
  100. ViewName: view.ViewName,
  101. ViewNameLocal: view.ViewNameLocal,
  102. ViewOtherName: view.ViewOtherName,
  103. ViewDescription: view.ViewDescription,
  104. ViewPosition: view.ViewPosition,
  105. Application: view.Application,
  106. AnatomicRegion: view.AnatomicRegion,
  107. PatientType: view.PatientType,
  108. BodyPartId: view.BodyPartID,
  109. ViewIconName: view.ViewIconName,
  110. ViewBigIconName: view.ViewBigIconName,
  111. ViewCoachName: view.ViewCoachName,
  112. Modality: view.Modality,
  113. ConfigObject: Json2Struct(&view.ConfigObject),
  114. TechTemplate: view.TechTemplate,
  115. ImgProcTemplate: view.ImgProcTemplate,
  116. Sort: view.Sort,
  117. IsEnabled: view.IsEnabled,
  118. Product: view.Product,
  119. IsPreInstall: view.IsPreInstall,
  120. }
  121. }
  122. func (s *ProtocolServer) GetView(ctx context.Context, in *pb.ViewRequest) (*pb.ViewReply, error) {
  123. product, _, _ := common.GetHeader(ctx)
  124. procedure := models.Procedure{}
  125. res := pb.ViewReply{}
  126. if in.GetProcedureId() == "" {
  127. err := models.DB.Model(&models.View{}).Scopes(
  128. models.String("patient_type = ?", in.GetPatientType()),
  129. models.String("body_part_id = ?", in.GetBodyPartId()),
  130. models.String("product = ?", product.ToString()),
  131. models.Bool("is_enabled = ?", in.IsEnabled),
  132. models.Bool("is_pre_install = ?", in.IsPreInstall),
  133. ).Order("sort").Find(&procedure.Views).Error
  134. if err != nil {
  135. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  136. }
  137. } else {
  138. err := models.DB.Model(&models.Procedure{}).Preload("Views").Scopes(
  139. models.String("procedure_id = ?", in.GetProcedureId()),
  140. models.String("product = ?", product.ToString()),
  141. models.Bool("is_enabled = ?", in.IsEnabled),
  142. models.Bool("is_pre_install = ?", in.IsPreInstall),
  143. ).Order("sort").First(&procedure).Error
  144. if err != nil {
  145. if errors.Is(err, gorm.ErrRecordNotFound) {
  146. return nil, status.Errorf(codes.NotFound, "procedure %s not found", in.GetProcedureId())
  147. }
  148. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  149. }
  150. }
  151. for _, view := range procedure.Views {
  152. res.ViewList = append(res.ViewList, view2pb(view))
  153. }
  154. return &res, nil
  155. }