prescription.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package model
  2. import (
  3. "encoding/json"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  6. "go.uber.org/zap"
  7. )
  8. type Prescription struct {
  9. Id int64 `json:"id"`
  10. Name string `json:"name"`
  11. ApplicableDisease string `json:"applicable_disease"`
  12. UseDays int32 `json:"use_days"`
  13. UseCount int32 `json:"use_count"`
  14. MeatExpiredDays int32 `json:"meat_expired_days"`
  15. MilkExpiredDays int32 `json:"milk_expired_days"`
  16. IsShow pasturePb.IsShow_Kind `json:"is_show"`
  17. Remarks string `json:"remarks"`
  18. OperationId int64 `json:"operation_id"`
  19. OperationName string `json:"operation_name"`
  20. CreatedAt int64 `json:"created_at"`
  21. UpdatedAt int64 `json:"updated_at"`
  22. }
  23. func (p *Prescription) TableName() string {
  24. return "prescription"
  25. }
  26. type ApplicableDisease struct {
  27. DiseaseId int64 `json:"disease_id"`
  28. DiseaseName string `json:"disease_name"`
  29. }
  30. func NewPrescription(req *pasturePb.PrescriptionRequest, applicableDisease string, useDays,
  31. meatExpiredDays, milkExpiredDays int32, systemUser *SystemUser) *Prescription {
  32. return &Prescription{
  33. Name: req.Name,
  34. ApplicableDisease: applicableDisease,
  35. UseDays: useDays,
  36. UseCount: 0,
  37. MeatExpiredDays: meatExpiredDays,
  38. MilkExpiredDays: milkExpiredDays,
  39. IsShow: req.IsShow,
  40. Remarks: req.Remarks,
  41. OperationId: systemUser.Id,
  42. OperationName: systemUser.Name,
  43. }
  44. }
  45. type PrescriptionSlice []*Prescription
  46. func (p PrescriptionSlice) ToPB(prescriptionDrugsList []*PrescriptionDrugs) []*pasturePb.SearchPrescriptionList {
  47. res := make([]*pasturePb.SearchPrescriptionList, len(p))
  48. for i, v := range p {
  49. drugsList := make([]*pasturePb.PrescriptionDrugsList, 0)
  50. for _, d := range prescriptionDrugsList {
  51. if d.PrescriptionId != v.Id {
  52. continue
  53. }
  54. drugsList = append(drugsList, &pasturePb.PrescriptionDrugsList{
  55. Id: int32(d.Id),
  56. DrugsId: int32(d.DrugsId),
  57. DrugsName: d.DrugsName,
  58. Unit: d.Unit,
  59. UnitName: d.UnitName,
  60. Dosages: d.Dosages,
  61. UseDays: d.UseDays,
  62. Specs: d.Specs,
  63. })
  64. }
  65. applicableDiseaseList := make([]*ApplicableDisease, 0)
  66. if v.ApplicableDisease != "" {
  67. err := json.Unmarshal([]byte(v.ApplicableDisease), &applicableDiseaseList)
  68. if err != nil {
  69. zaplog.Error("PrescriptionSliceToPB", zap.Any("err", err))
  70. }
  71. }
  72. applicableDiseaseNames := make([]string, 0)
  73. applicableDiseaseIds := make([]int32, 0)
  74. for _, a := range applicableDiseaseList {
  75. applicableDiseaseNames = append(applicableDiseaseNames, a.DiseaseName)
  76. applicableDiseaseIds = append(applicableDiseaseIds, int32(a.DiseaseId))
  77. }
  78. res[i] = &pasturePb.SearchPrescriptionList{
  79. Id: int32(v.Id),
  80. Name: v.Name,
  81. ApplicableDisease: v.ApplicableDisease,
  82. ApplicableDiseaseIds: applicableDiseaseIds,
  83. ApplicableDiseaseNames: applicableDiseaseNames,
  84. UseDays: v.UseDays,
  85. UseCount: v.UseCount,
  86. MeatExpiredDays: v.MeatExpiredDays,
  87. MilkExpiredDays: v.MilkExpiredDays,
  88. IsShow: v.IsShow,
  89. Remarks: v.Remarks,
  90. OperationId: int32(v.OperationId),
  91. OperationName: v.OperationName,
  92. CreatedAt: int32(v.CreatedAt),
  93. UpdatedAt: int32(v.UpdatedAt),
  94. DrugsList: drugsList,
  95. }
  96. }
  97. return res
  98. }