event_cow_treatment.go 4.9 KB

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