event_health.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "net/http"
  6. "gorm.io/gorm"
  7. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  8. "gitee.com/xuyiping_admin/pkg/xerr"
  9. )
  10. // CowDiseaseCreate 牛只发病提交
  11. func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDisease) error {
  12. cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
  13. if err != nil {
  14. return xerr.WithStack(err)
  15. }
  16. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  17. if err != nil {
  18. return xerr.WithStack(err)
  19. }
  20. req.OperationName = systemUser.Name
  21. disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
  22. if err != nil {
  23. return xerr.WithStack(err)
  24. }
  25. req.DiseaseName = disease.Name
  26. newEventCowDisease := model.NewEventCowDisease(cow, req)
  27. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  28. if err = tx.Create(newEventCowDisease).Error; err != nil {
  29. return xerr.WithStack(err)
  30. }
  31. // 更新牛舍
  32. if req.PenId > 0 {
  33. if err = tx.Model(new(model.Cow)).
  34. Where("id = ?", req.CowId).
  35. Updates(map[string]interface{}{
  36. "pen_id": req.PenId,
  37. }).Error; err != nil {
  38. return xerr.WithStack(err)
  39. }
  40. }
  41. return nil
  42. }); err != nil {
  43. return xerr.WithStack(err)
  44. }
  45. return nil
  46. }
  47. // CowDiseaseList 发病牛只清单
  48. func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
  49. cowDiseaseList := make([]*model.EventCowDisease, 0)
  50. var count int64 = 0
  51. pref := s.DB.Model(new(model.EventCowDisease)).
  52. Where("diagnosed_result < ?", pasturePb.IsShow_No)
  53. if len(req.CowIds) > 0 {
  54. pref.Where("cow_id IN ?", req.CowIds)
  55. }
  56. if req.DiseaseId > 0 {
  57. pref.Where("disease_id = ?", req.DiseaseId)
  58. }
  59. if req.PenId > 0 {
  60. pref.Where("pen_id = ?", req.PenId)
  61. }
  62. if req.Lact > 0 {
  63. pref.Where("lact = ?", req.Lact)
  64. }
  65. if req.MinDayAge > 0 && req.MaxDayAge > 0 && req.MaxDayAge >= req.MinDayAge {
  66. pref.Where("day_age BETWEEN ? AND ?", req.MinDayAge, req.MaxDayAge)
  67. }
  68. if req.HealthStatus > 0 {
  69. pref.Where("health_status = ?", req.HealthStatus)
  70. }
  71. if err := pref.Order("id desc").
  72. Count(&count).Limit(int(pagination.PageSize)).
  73. Offset(int(pagination.PageOffset)).
  74. Find(&cowDiseaseList).Error; err != nil {
  75. return nil, xerr.WithStack(err)
  76. }
  77. healthStatusMap := s.HealthStatusMap()
  78. return &pasturePb.EventCowDiseaseResponse{
  79. Code: http.StatusOK,
  80. Message: "ok",
  81. Data: &pasturePb.EventCowDiseaseData{
  82. List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
  83. Total: int32(count),
  84. PageSize: pagination.PageSize,
  85. Page: pagination.Page,
  86. },
  87. }, nil
  88. }
  89. // CowDiseaseDiagnose 发病牛只诊断
  90. func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
  91. eventCowDisease := &model.EventCowDisease{}
  92. if err := s.DB.Where("cow_id = ?", req.CowId).
  93. Where("id = ?", req.Id).
  94. First(eventCowDisease).Error; err != nil {
  95. return xerr.WithStack(err)
  96. }
  97. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Health {
  98. return xerr.Custom("异常牛只数据")
  99. }
  100. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  101. if err != nil {
  102. return xerr.WithStack(err)
  103. }
  104. // 未发病
  105. if req.DiagnosedResult == pasturePb.IsShow_No {
  106. if err := s.DB.Model(eventCowDisease).
  107. Where("id = ?", req.Id).
  108. Where("cow_id = ?", req.CowId).
  109. Updates(map[string]interface{}{
  110. "diagnosed_result": pasturePb.IsShow_No,
  111. "diagnose_operation_id": req.OperationId,
  112. "diagnose_operation_name": systemUser.Name,
  113. "remarks": req.Remarks,
  114. }).Error; err != nil {
  115. return xerr.WithStack(err)
  116. }
  117. return nil
  118. }
  119. // 已发病
  120. disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
  121. if err != nil {
  122. return xerr.WithStack(err)
  123. }
  124. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  125. if err = tx.Model(eventCowDisease).
  126. Where("id = ?", req.Id).
  127. Where("cow_id = ?", req.CowId).
  128. Updates(map[string]interface{}{
  129. "health_status": pasturePb.HealthStatus_Disease,
  130. "diagnosed_result": pasturePb.IsShow_Ok,
  131. "diagnose_id": req.DiseaseId,
  132. "diagnose_name": disease.Name,
  133. "temperature": int32(req.Temperature * 10),
  134. "diagnose_operation_id": req.OperationId,
  135. "diagnose_operation_name": systemUser.Name,
  136. "remarks": req.Remarks,
  137. }).Error; err != nil {
  138. return xerr.WithStack(err)
  139. }
  140. if err = tx.Model(new(model.Cow)).
  141. Where("id = ?", req.CowId).
  142. Updates(map[string]interface{}{
  143. "health_status": pasturePb.HealthStatus_Disease,
  144. }).Error; err != nil {
  145. return xerr.WithStack(err)
  146. }
  147. return nil
  148. }); err != nil {
  149. return xerr.WithStack(err)
  150. }
  151. return nil
  152. }
  153. // CowDiseaseTreatment 发病牛只治疗
  154. func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.CowTreatmentRequest) error {
  155. eventCowDisease := &model.EventCowDisease{}
  156. if err := s.DB.Where("cow_id = ?", req.CowId).
  157. Where("id = ?", req.Id).
  158. First(eventCowDisease).Error; err != nil {
  159. return xerr.WithStack(err)
  160. }
  161. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
  162. eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
  163. return xerr.Custom("异常牛只数据")
  164. }
  165. systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
  166. if req.PrescriptionId > 0 {
  167. prescription := &model.Prescription{}
  168. if err := s.DB.Where("id = ?", req.PrescriptionId).
  169. Where("is_show = ?", pasturePb.IsShow_Ok).
  170. First(prescription).Error; err != nil {
  171. return xerr.WithStack(err)
  172. }
  173. prescriptionDrugs := make([]*model.PrescriptionDrugs, 0)
  174. if err := s.DB.Where("prescription_id = ?", req.PrescriptionId).Find(&prescriptionDrugs).Error; err != nil {
  175. return xerr.WithStack(err)
  176. }
  177. prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
  178. for i, v := range prescriptionDrugs {
  179. prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
  180. DrugsId: int32(v.DrugsId),
  181. DrugsName: v.DrugsName,
  182. Unit: v.Unit,
  183. UseNum: float32(v.Dosages),
  184. }
  185. }
  186. req.PrescriptionDetail = prescriptionDetail
  187. return nil
  188. }
  189. healthStatus := pasturePb.HealthStatus_Treatment
  190. if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
  191. healthStatus = pasturePb.HealthStatus_Curable
  192. }
  193. if req.TreatmentResult == pasturePb.TreatmentResult_Out {
  194. healthStatus = pasturePb.HealthStatus_Out
  195. }
  196. if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
  197. healthStatus = pasturePb.HealthStatus_Dead
  198. }
  199. newEventCowTreatment := model.NewEventCowTreatment(req, systemUser)
  200. if err := s.DB.Transaction(func(tx *gorm.DB) error {
  201. if err := tx.Create(newEventCowTreatment).Error; err != nil {
  202. return xerr.WithStack(err)
  203. }
  204. if err := tx.Model(eventCowDisease).
  205. Where("id = ?", req.Id).
  206. Where("cow_id = ?", req.CowId).
  207. Updates(map[string]interface{}{
  208. "health_status": healthStatus,
  209. }).Error; err != nil {
  210. return xerr.WithStack(err)
  211. }
  212. if err := tx.Model(new(model.Cow)).
  213. Where("id = ?", req.CowId).
  214. Updates(map[string]interface{}{
  215. "health_status": healthStatus,
  216. }).Error; err != nil {
  217. return xerr.WithStack(err)
  218. }
  219. return nil
  220. }); err != nil {
  221. return xerr.WithStack(err)
  222. }
  223. return nil
  224. }