event_cow_treatment.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package model
  2. import (
  3. "encoding/json"
  4. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  5. "go.uber.org/zap"
  6. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  7. )
  8. type EventCowTreatment struct {
  9. Id int64 `json:"id"`
  10. PastureId int64 `json:"pastureId"`
  11. CowId int64 `json:"cowId"`
  12. CowDiseaseId int64 `json:"cowDiseaseId"`
  13. DiseaseId int64 `json:"diseaseId"`
  14. DiseaseName string `json:"diseaseName"`
  15. DiseaseType int32 `json:"diseaseType"`
  16. DiseaseTypeName string `json:"diseaseTypeName"`
  17. PrescriptionId int32 `json:"prescriptionId"`
  18. PrescriptionName string `json:"prescriptionName"`
  19. PrescriptionDetail string `json:"prescriptionDetail"`
  20. TreatmentResult pasturePb.TreatmentResult_Kind `json:"treatmentResult"`
  21. OperationId int64 `json:"operationId"`
  22. OperationName string `json:"operationName"`
  23. MessageId int64 `json:"messageId"`
  24. MessageName string `json:"messageName"`
  25. Remarks string `json:"remarks"`
  26. TreatmentAt int64 `json:"treatmentAt"`
  27. CreatedAt int64 `json:"createdAt"`
  28. UpdatedAt int64 `json:"updatedAt"`
  29. }
  30. func (e *EventCowTreatment) TableName() string {
  31. return "event_cow_treatment"
  32. }
  33. func NewEventCowTreatment(
  34. pastureId int64,
  35. prescription *Prescription,
  36. req *pasturePb.CowTreatmentRequest,
  37. diseaseTypeMap map[int32]string,
  38. operation, currentUser *SystemUser,
  39. ) *EventCowTreatment {
  40. b, _ := json.Marshal(req.PrescriptionDetail)
  41. return &EventCowTreatment{
  42. PastureId: pastureId,
  43. CowId: int64(req.CowId),
  44. CowDiseaseId: int64(req.Id),
  45. DiseaseId: int64(req.DiseaseId),
  46. DiseaseName: req.DiseaseName,
  47. DiseaseType: req.DiseaseType,
  48. DiseaseTypeName: diseaseTypeMap[req.DiseaseType],
  49. PrescriptionId: req.PrescriptionId,
  50. PrescriptionName: prescription.Name,
  51. PrescriptionDetail: string(b),
  52. TreatmentResult: req.TreatmentResult,
  53. OperationId: operation.Id,
  54. OperationName: operation.Name,
  55. MessageId: currentUser.Id,
  56. MessageName: currentUser.Name,
  57. Remarks: req.Remarks,
  58. }
  59. }
  60. func NewEventCowCurableTreatment(pastureId int64, currUser, operationUser *SystemUser, cowDisease *EventCowDisease, remarks string, curableAt int64) *EventCowTreatment {
  61. return &EventCowTreatment{
  62. PastureId: pastureId,
  63. CowId: cowDisease.CowId,
  64. CowDiseaseId: cowDisease.Id,
  65. DiseaseId: int64(cowDisease.DiseaseId),
  66. DiseaseName: cowDisease.DiseaseName,
  67. TreatmentResult: pasturePb.TreatmentResult_Curable,
  68. OperationId: operationUser.Id,
  69. OperationName: operationUser.Name,
  70. Remarks: remarks,
  71. TreatmentAt: curableAt,
  72. }
  73. }
  74. type EventCowTreatmentSlice []*EventCowTreatment
  75. func (e EventCowTreatmentSlice) ToPB(eventCowDiseaseList []*EventCowDisease) []*pasturePb.EventCowTreatment {
  76. res := make([]*pasturePb.EventCowTreatment, len(e))
  77. for i, v := range e {
  78. prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, 0)
  79. if v.PrescriptionDetail != "" {
  80. err := json.Unmarshal([]byte(v.PrescriptionDetail), &prescriptionDetail)
  81. if err != nil {
  82. zaplog.Error("EventCowTreatmentSlice", zap.Any("err", err))
  83. }
  84. }
  85. eventCowTreatment := &pasturePb.EventCowTreatment{
  86. CowId: int32(v.CowId),
  87. CowDiseaseId: int32(v.CowDiseaseId),
  88. PrescriptionId: v.PrescriptionId,
  89. PrescriptionName: v.PrescriptionName,
  90. PrescriptionDetail: prescriptionDetail,
  91. TreatmentResult: 0,
  92. OperationId: int32(v.OperationId),
  93. OperationName: v.OperationName,
  94. Remarks: v.Remarks,
  95. CreatedAt: int32(v.CreatedAt),
  96. UpdatedAt: int32(v.UpdatedAt),
  97. DiseaseId: int32(v.DiseaseId),
  98. Id: int32(v.Id),
  99. }
  100. for _, d := range eventCowDiseaseList {
  101. if v.CowDiseaseId != d.Id {
  102. continue
  103. }
  104. eventCowTreatment.DiseaseName = d.DiseaseName
  105. eventCowTreatment.DiseaseAt = int32(d.DiseaseAt)
  106. }
  107. res[i] = eventCowTreatment
  108. }
  109. return res
  110. }