protocol.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "errors"
  6. "log/slog"
  7. )
  8. import (
  9. "google.golang.org/grpc/codes"
  10. "google.golang.org/grpc/status"
  11. "google.golang.org/protobuf/types/known/structpb"
  12. "gorm.io/datatypes"
  13. "gorm.io/gorm"
  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) GetPatientTypeList(ctx context.Context, in *pb.PatientTypeRequest) (*pb.PatientTypeReply, error) {
  46. res := pb.PatientTypeReply{}
  47. product, _, _, err := common.GetHeader(ctx)
  48. if err != nil {
  49. return &res, err
  50. }
  51. err = models.DB.Model(&models.PatientType{}).Scopes(
  52. models.String("product = ?", product.ToString()),
  53. models.Bool("is_enabled = ?", in.IsEnabled),
  54. ).Order("sort").Find(&res.PatientTypeList).Error
  55. if err != nil {
  56. return nil, status.Errorf(codes.Internal, "patient_type find err %v", err)
  57. }
  58. for _, pt := range res.GetPatientTypeList() {
  59. pt.PatientTypeLocal = pt.PatientTypeName
  60. }
  61. return &res, nil
  62. }
  63. func (s *ProtocolServer) GetBodyPartList(ctx context.Context, in *pb.BodyPartRequest) (*pb.BodyPartReply, error) {
  64. res := pb.BodyPartReply{}
  65. product, _, _, err := common.GetHeader(ctx)
  66. if err != nil {
  67. return &res, err
  68. }
  69. err = models.DB.Model(&models.BodyPart{}).Scopes(
  70. models.String("patient_type = ?", in.GetPatientType()),
  71. models.String("modality = ?", in.GetModality()),
  72. models.String("product = ?", product.ToString()),
  73. models.Bool("is_enabled = ?", in.IsEnabled),
  74. ).Order("sort").Find(&res.BodyPartList).Error
  75. if err != nil {
  76. return nil, status.Errorf(codes.Internal, "body_part find err %v", err)
  77. }
  78. for _, pt := range res.GetBodyPartList() {
  79. pt.BodyPartLocal = pt.BodyPartName
  80. }
  81. return &res, nil
  82. }
  83. func (s *ProtocolServer) GetProcedureList(ctx context.Context, in *pb.ProcedureRequest) (*pb.ProcedureReply, error) {
  84. res := pb.ProcedureReply{}
  85. product, _, _, err := common.GetHeader(ctx)
  86. if err != nil {
  87. return &res, err
  88. }
  89. err = models.DB.Model(&models.Procedure{}).Scopes(
  90. models.String("patient_type = ?", in.GetPatientType()),
  91. models.String("body_part_id = ?", in.GetBodyPartId()),
  92. models.String("product = ?", product.ToString()),
  93. models.Bool("is_enabled = ?", in.IsEnabled),
  94. models.Bool("is_pre_install = ?", in.IsPreInstall),
  95. ).Order("sort").Find(&res.ProcedureList).Error
  96. if err != nil {
  97. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  98. }
  99. for _, pt := range res.GetProcedureList() {
  100. pt.ProcedureNameLocal = pt.ProcedureName
  101. pt.ProcedureDescriptionLocal = pt.ProcedureDescription
  102. }
  103. return &res, nil
  104. }
  105. func (s *ProtocolServer) GetProcedure(ctx context.Context, in *pb.IDRequest) (*pb.Procedure, error) {
  106. slog.Info("[rpc]GetProcedure...", "instance_id", *in.InstanceId)
  107. res := pb.Procedure{}
  108. product, _, _, err := common.GetHeader(ctx)
  109. if err != nil {
  110. return &res, err
  111. }
  112. err = models.DB.Model(&models.Procedure{}).Scopes(
  113. models.String("internal_id = ?", in.GetInstanceId()),
  114. models.String("product = ?", product.ToString()),
  115. ).Order("sort").Find(&res).Error
  116. if err != nil {
  117. if errors.Is(err, gorm.ErrRecordNotFound) {
  118. return nil, status.Errorf(codes.NotFound, "procedure %s not found", in.GetInstanceId())
  119. }
  120. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  121. }
  122. res.ProcedureNameLocal = res.ProcedureName
  123. res.ProcedureDescriptionLocal = res.ProcedureDescription
  124. return &res, nil
  125. }
  126. func view2pb(view models.View) *pb.View {
  127. return &pb.View{
  128. InternalId: view.InternalID,
  129. ViewId: view.ViewID,
  130. ViewName: view.ViewName,
  131. ViewNameLocal: view.ViewNameLocal,
  132. ViewOtherName: view.ViewOtherName,
  133. ViewDescription: view.ViewDescription,
  134. ViewPosition: view.ViewPosition,
  135. Application: view.Application,
  136. AnatomicRegion: view.AnatomicRegion,
  137. PatientType: view.PatientType,
  138. BodyPartId: view.BodyPartID,
  139. ViewIconName: view.ViewIconName,
  140. ViewBigIconName: view.ViewBigIconName,
  141. ViewCoachName: view.ViewCoachName,
  142. Modality: view.Modality,
  143. ConfigObject: Json2Struct(&view.ConfigObject),
  144. TechTemplate: view.TechTemplate,
  145. ImgProcTemplate: view.ImgProcTemplate,
  146. Sort: int32(view.Sort),
  147. IsEnabled: view.IsEnabled,
  148. Product: view.Product,
  149. IsPreInstall: view.IsPreInstall,
  150. }
  151. }
  152. func (s *ProtocolServer) GetViewList(ctx context.Context, in *pb.ViewRequest) (*pb.ViewReply, error) {
  153. procedure := models.Procedure{}
  154. res := pb.ViewReply{}
  155. product, _, _, err := common.GetHeader(ctx)
  156. if err != nil {
  157. return &res, err
  158. }
  159. if in.GetProcedureId() == "" {
  160. err := models.DB.Model(&models.View{}).Scopes(
  161. models.String("patient_type = ?", in.GetPatientType()),
  162. models.String("body_part_id = ?", in.GetBodyPartId()),
  163. models.String("product = ?", product.ToString()),
  164. models.Bool("is_enabled = ?", in.IsEnabled),
  165. models.Bool("is_pre_install = ?", in.IsPreInstall),
  166. ).Order("sort").Find(&procedure.Views).Error
  167. if err != nil {
  168. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  169. }
  170. } else {
  171. err := models.DB.Model(&models.Procedure{}).Preload("Views").Scopes(
  172. models.String("procedure_id = ?", in.GetProcedureId()),
  173. models.String("product = ?", product.ToString()),
  174. models.Bool("is_enabled = ?", in.IsEnabled),
  175. models.Bool("is_pre_install = ?", in.IsPreInstall),
  176. ).Order("sort").First(&procedure).Error
  177. if err != nil {
  178. if errors.Is(err, gorm.ErrRecordNotFound) {
  179. return nil, status.Errorf(codes.NotFound, "procedure %s not found", in.GetProcedureId())
  180. }
  181. return nil, status.Errorf(codes.Internal, "procedure find err %v", err)
  182. }
  183. }
  184. for _, view := range procedure.Views {
  185. res.ViewList = append(res.ViewList, view2pb(view))
  186. }
  187. return &res, nil
  188. }
  189. func (s *ProtocolServer) GetView(ctx context.Context, in *pb.IDRequest) (*pb.View, error) {
  190. slog.Info("[rpc]GetView...", "instance_id", *in.InstanceId)
  191. product, _, _, err := common.GetHeader(ctx)
  192. if err != nil {
  193. slog.Info("[rpc]GetView err:", err)
  194. return &pb.View{}, err
  195. }
  196. var view models.View
  197. err = models.DB.Scopes(
  198. models.String("internal_id = ?", in.GetInstanceId()),
  199. models.String("product = ?", product.ToString()),
  200. ).Order("sort").First(&view).Error
  201. if err != nil {
  202. if errors.Is(err, gorm.ErrRecordNotFound) {
  203. return nil, status.Errorf(codes.NotFound, "procedure %s not found", in.GetInstanceId())
  204. }
  205. return nil, status.Errorf(codes.Internal, "view find err %v", err)
  206. }
  207. return view2pb(view), nil
  208. }