event_cow_disease.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package model
  2. import (
  3. "time"
  4. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  5. )
  6. type EventCowDisease struct {
  7. Id int64 `json:"id"`
  8. PastureId int64 `json:"pastureId"`
  9. CowId int64 `json:"cowId"`
  10. EarNumber string `json:"earNumber"`
  11. CowType pasturePb.CowType_Kind `json:"cowType"`
  12. Lact int32 `json:"lact"`
  13. DayAge int32 `json:"dayAge"`
  14. PenId int32 `json:"penId"`
  15. PenName string `json:"penName"`
  16. DiseaseId int32 `json:"diseaseId"`
  17. DiseaseName string `json:"diseaseName"`
  18. DiseaseType int32 `json:"diseaseType"`
  19. DiseaseTypeName string `json:"diseaseTypeName"`
  20. DiagnoseId int32 `json:"diagnoseId"`
  21. DiagnoseName string `json:"diagnoseName"`
  22. HealthStatus pasturePb.HealthStatus_Kind `json:"healthStatus"`
  23. DiagnosedResult pasturePb.IsShow_Kind `json:"diagnosedResult"`
  24. DiagnosedAt int64 `json:"diagnosedAt"`
  25. DiseaseAt int64 `json:"diseaseAt"`
  26. FirstTreatmentAt int64 `json:"firstTreatmentAt"`
  27. LastTreatmentAt int64 `json:"lastTreatmentAt"`
  28. LastPrescriptionName string `json:"LastPrescriptionName"`
  29. Temperature int32 `json:"temperature"`
  30. ExposeDiseaseType pasturePb.ExposeDiseaseType_Kind `json:"exposeDiseaseType"`
  31. Remarks string `json:"remarks"`
  32. OperationId int32 `json:"operationId"`
  33. OperationName string `json:"operationName"`
  34. MessageId int32 `json:"messageId"`
  35. MessageName string `json:"messageName"`
  36. DiagnoseOperationId int32 `json:"diagnoseOperationId"`
  37. DiagnoseOperationName string `json:"diagnoseOperationName"`
  38. CurableAt int64 `json:"curableAt"`
  39. Source string `json:"source"`
  40. CreatedAt int64 `json:"createdAt"`
  41. UpdatedAt int64 `json:"updatedAt"`
  42. }
  43. func (e *EventCowDisease) TableName() string {
  44. return "event_cow_disease"
  45. }
  46. // EventUnDiseaseUpdate 诊断未生病
  47. func (e *EventCowDisease) EventUnDiseaseUpdate(operation *SystemUser, remarks string) {
  48. e.DiagnosedResult = pasturePb.IsShow_No
  49. e.DiagnoseOperationId = int32(operation.Id)
  50. e.DiagnoseOperationName = operation.Name
  51. e.Remarks = remarks
  52. }
  53. // EventDiseaseUpdate 诊断有生病
  54. func (e *EventCowDisease) EventDiseaseUpdate(disease *Disease, operationUser *SystemUser, temp float32) {
  55. e.HealthStatus = pasturePb.HealthStatus_Disease
  56. e.DiagnosedResult = pasturePb.IsShow_Ok
  57. e.DiagnoseId = int32(disease.Id)
  58. e.DiagnoseName = disease.Name
  59. e.Temperature = int32(temp * 100)
  60. e.DiagnoseOperationId = int32(operationUser.Id)
  61. e.DiagnoseOperationName = operationUser.Name
  62. e.DiseaseAt = time.Now().Local().Unix()
  63. }
  64. // EventTreatmentUpdate 治疗更新
  65. func (e *EventCowDisease) EventTreatmentUpdate(healthStatus pasturePb.HealthStatus_Kind, treatmentAt int64, prescriptionName string) {
  66. if e.FirstTreatmentAt <= 0 {
  67. e.FirstTreatmentAt = treatmentAt
  68. }
  69. e.LastTreatmentAt = treatmentAt
  70. e.LastPrescriptionName = prescriptionName
  71. e.HealthStatus = healthStatus
  72. }
  73. // EventCurableUpdate 治愈
  74. func (e *EventCowDisease) EventCurableUpdate(cureAt int64) {
  75. e.HealthStatus = pasturePb.HealthStatus_Curable
  76. e.CurableAt = cureAt
  77. e.DiagnosedResult = pasturePb.IsShow_Ok
  78. }
  79. func NewEventCowDisease(pastureId int64, cow *Cow, disease *Disease, req *pasturePb.EventCowDiseaseRequest, operation, currUser *SystemUser) *EventCowDisease {
  80. return &EventCowDisease{
  81. PastureId: pastureId,
  82. CowId: cow.Id,
  83. EarNumber: cow.EarNumber,
  84. CowType: cow.CowType,
  85. Lact: cow.Lact,
  86. DayAge: cow.DayAge,
  87. DiseaseId: int32(disease.Id),
  88. DiseaseName: disease.Name,
  89. DiseaseType: disease.DiseaseType,
  90. DiseaseTypeName: disease.DiseaseTypeName,
  91. PenId: cow.PenId,
  92. PenName: cow.PenName,
  93. HealthStatus: pasturePb.HealthStatus_Health,
  94. DiseaseAt: int64(req.DiseaseAt),
  95. Temperature: int32(req.Temperature * 10),
  96. OperationId: int32(operation.Id),
  97. OperationName: operation.Name,
  98. MessageId: int32(currUser.Id),
  99. MessageName: currUser.Name,
  100. Remarks: req.Remarks,
  101. DiagnosedResult: pasturePb.IsShow_No,
  102. Source: SourceApp,
  103. DiagnosedAt: int64(req.DiseaseAt),
  104. ExposeDiseaseType: req.ExposeDiseaseType,
  105. }
  106. }
  107. type EventCowDiseaseSlice []*EventCowDisease
  108. func (e EventCowDiseaseSlice) ToPB(healthStatusMap map[pasturePb.HealthStatus_Kind]string) []*pasturePb.EventCowDisease {
  109. res := make([]*pasturePb.EventCowDisease, len(e))
  110. for i, v := range e {
  111. lastTreatedTimeFormat := ""
  112. if v.LastTreatmentAt > 0 {
  113. lastTreatedTimeFormat = time.Unix(v.LastTreatmentAt, 0).Local().Format(LayoutDate2)
  114. }
  115. onsetDays := int32(0)
  116. if v.FirstTreatmentAt > 0 {
  117. firstTime := time.Unix(v.FirstTreatmentAt, 0).Local()
  118. diff := time.Now().Local().Sub(firstTime)
  119. onsetDays = int32(diff.Hours() / 24)
  120. }
  121. res[i] = &pasturePb.EventCowDisease{
  122. Id: int32(v.Id),
  123. CowId: int32(v.CowId),
  124. EarNumber: v.EarNumber,
  125. Lact: v.Lact,
  126. DayAge: v.DayAge,
  127. DiseaseId: v.DiseaseId,
  128. DiseaseName: v.DiseaseName,
  129. DiagnoseId: v.DiagnoseId,
  130. DiagnoseName: v.DiagnoseName,
  131. PenId: v.PenId,
  132. PenName: v.PenName,
  133. HealthStatus: v.HealthStatus,
  134. HealthStatusName: healthStatusMap[v.HealthStatus],
  135. Temperature: float32(v.HealthStatus / 10),
  136. DiseaseAt: int32(v.DiseaseAt),
  137. Remarks: v.Remarks,
  138. OperationId: v.OperationId,
  139. OperationName: v.OperationName,
  140. LastPrescriptionName: v.LastPrescriptionName,
  141. LastTreatedTimeFormat: lastTreatedTimeFormat,
  142. OnsetDays: onsetDays,
  143. ExposeDiseaseType: v.ExposeDiseaseType,
  144. }
  145. }
  146. return res
  147. }