event_health.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. package backend
  2. import (
  3. "context"
  4. "kpt-pasture/model"
  5. "net/http"
  6. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  7. "go.uber.org/zap"
  8. "gorm.io/gorm"
  9. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  10. "gitee.com/xuyiping_admin/pkg/xerr"
  11. )
  12. // CowDiseaseCreate 牛只发病提交
  13. func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest) error {
  14. cow, err := s.GetCowInfoByCowId(ctx, int64(req.CowId))
  15. if err != nil {
  16. return xerr.WithStack(err)
  17. }
  18. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  19. if err != nil {
  20. return xerr.WithStack(err)
  21. }
  22. disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
  23. if err != nil {
  24. return xerr.WithStack(err)
  25. }
  26. prescription := &model.Prescription{}
  27. if req.PrescriptionId > 0 {
  28. prescription, err = s.GetPrescriptionById(ctx, req.PrescriptionId)
  29. if err != nil {
  30. return xerr.WithStack(err)
  31. }
  32. }
  33. newEventCowDisease := model.NewEventCowDisease(cow, disease, req, systemUser)
  34. if prescription.Id > 0 {
  35. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
  36. }
  37. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  38. if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
  39. return xerr.WithStack(err)
  40. }
  41. // 更新牛舍
  42. if req.PenId > 0 {
  43. if err = tx.Model(new(model.Cow)).
  44. Where("id = ?", req.CowId).
  45. Updates(map[string]interface{}{
  46. "pen_id": req.PenId,
  47. }).Error; err != nil {
  48. zaplog.Error("CowDiseaseCreate", zap.Any("UpdatePen", err), zap.Any("req", req))
  49. }
  50. }
  51. // 如果有处方Id,则创建治疗记录
  52. if prescription.Id > 0 {
  53. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
  54. if err != nil {
  55. return xerr.WithStack(err)
  56. }
  57. prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
  58. for i, v := range prescriptionDrugs {
  59. prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
  60. DrugsId: int32(v.DrugsId),
  61. DrugsName: v.DrugsName,
  62. Unit: v.Unit,
  63. UseNum: float32(v.Dosages),
  64. }
  65. }
  66. newEventCowTreatment := model.NewEventCowTreatment(prescription, &pasturePb.CowTreatmentRequest{
  67. CowId: req.CowId,
  68. PrescriptionId: prescription.Id,
  69. DiseaseId: req.DiseaseId,
  70. DiseaseName: disease.Name,
  71. PrescriptionDetail: prescriptionDetail,
  72. TreatmentResult: pasturePb.TreatmentResult_GoOn,
  73. Remarks: "",
  74. TreatmentAt: req.DiseaseAt,
  75. }, systemUser)
  76. // 创建治疗记录
  77. if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
  78. return xerr.WithStack(err)
  79. }
  80. if err = tx.Model(new(model.Cow)).
  81. Where("id = ?", req.CowId).
  82. Updates(map[string]interface{}{
  83. "health_status": pasturePb.HealthStatus_Disease,
  84. }).Error; err != nil {
  85. return xerr.WithStack(err)
  86. }
  87. }
  88. return nil
  89. }); err != nil {
  90. return xerr.WithStack(err)
  91. }
  92. return nil
  93. }
  94. // CowDiseaseList 发病牛只清单
  95. func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
  96. cowDiseaseList := make([]*model.EventCowDisease, 0)
  97. var count int64 = 0
  98. pref := s.DB.Model(new(model.EventCowDisease)).
  99. Where("diagnosed_result < ?", pasturePb.IsShow_No)
  100. if len(req.CowIds) > 0 {
  101. pref.Where("cow_id IN ?", req.CowIds)
  102. }
  103. if req.DiseaseId > 0 {
  104. pref.Where("disease_id = ?", req.DiseaseId)
  105. }
  106. if req.PenId > 0 {
  107. pref.Where("pen_id = ?", req.PenId)
  108. }
  109. if req.Lact > 0 {
  110. pref.Where("lact = ?", req.Lact)
  111. }
  112. if req.MinDayAge > 0 && req.MaxDayAge > 0 && req.MaxDayAge >= req.MinDayAge {
  113. pref.Where("day_age BETWEEN ? AND ?", req.MinDayAge, req.MaxDayAge)
  114. }
  115. if req.HealthStatus > 0 {
  116. pref.Where("health_status = ?", req.HealthStatus)
  117. }
  118. if err := pref.Order("id desc").
  119. Count(&count).Limit(int(pagination.PageSize)).
  120. Offset(int(pagination.PageOffset)).
  121. Order("id desc").
  122. Find(&cowDiseaseList).Error; err != nil {
  123. return nil, xerr.WithStack(err)
  124. }
  125. healthStatusMap := s.HealthStatusMap()
  126. return &pasturePb.EventCowDiseaseResponse{
  127. Code: http.StatusOK,
  128. Message: "ok",
  129. Data: &pasturePb.EventCowDiseaseData{
  130. List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
  131. Total: int32(count),
  132. PageSize: pagination.PageSize,
  133. Page: pagination.Page,
  134. },
  135. }, nil
  136. }
  137. // CowDiseaseDiagnose 发病牛只诊断
  138. func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
  139. eventCowDisease := &model.EventCowDisease{}
  140. if err := s.DB.Where("cow_id = ?", req.CowId).
  141. Where("id = ?", req.Id).
  142. First(eventCowDisease).Error; err != nil {
  143. return xerr.WithStack(err)
  144. }
  145. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Health {
  146. return xerr.Custom("异常牛只数据")
  147. }
  148. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  149. if err != nil {
  150. return xerr.WithStack(err)
  151. }
  152. // 未发病
  153. if req.DiagnosedResult == pasturePb.IsShow_No {
  154. if err = s.DB.Model(eventCowDisease).
  155. Where("id = ?", req.Id).
  156. Where("cow_id = ?", req.CowId).
  157. Updates(map[string]interface{}{
  158. "diagnosed_result": pasturePb.IsShow_No,
  159. "diagnose_operation_id": req.OperationId,
  160. "diagnose_operation_name": systemUser.Name,
  161. "remarks": req.Remarks,
  162. }).Error; err != nil {
  163. return xerr.WithStack(err)
  164. }
  165. return nil
  166. }
  167. // 已发病
  168. disease, err := s.GetDiseaseById(ctx, req.DiseaseId)
  169. if err != nil {
  170. return xerr.WithStack(err)
  171. }
  172. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  173. if err = tx.Model(eventCowDisease).
  174. Where("id = ?", req.Id).
  175. Where("cow_id = ?", req.CowId).
  176. Updates(map[string]interface{}{
  177. "health_status": pasturePb.HealthStatus_Disease,
  178. "diagnosed_result": pasturePb.IsShow_Ok,
  179. "diagnose_id": req.DiseaseId,
  180. "diagnose_name": disease.Name,
  181. "temperature": int32(req.Temperature * 10),
  182. "diagnose_operation_id": req.OperationId,
  183. "diagnose_operation_name": systemUser.Name,
  184. "remarks": req.Remarks,
  185. }).Error; err != nil {
  186. return xerr.WithStack(err)
  187. }
  188. if err = tx.Model(new(model.Cow)).
  189. Where("id = ?", req.CowId).
  190. Updates(map[string]interface{}{
  191. "health_status": pasturePb.HealthStatus_Disease,
  192. }).Error; err != nil {
  193. return xerr.WithStack(err)
  194. }
  195. return nil
  196. }); err != nil {
  197. return xerr.WithStack(err)
  198. }
  199. return nil
  200. }
  201. // CowDiseaseTreatment 发病牛只治疗
  202. func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.CowTreatmentRequest) error {
  203. eventCowDisease := &model.EventCowDisease{}
  204. if err := s.DB.Where("cow_id = ?", req.CowId).
  205. Where("id = ?", req.Id).
  206. First(eventCowDisease).Error; err != nil {
  207. return xerr.WithStack(err)
  208. }
  209. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
  210. eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
  211. return xerr.Custom("异常牛只数据")
  212. }
  213. systemUser, _ := s.GetSystemUserById(ctx, int64(req.OperationId))
  214. prescription, err := s.GetPrescriptionById(ctx, req.PrescriptionId)
  215. if err != nil {
  216. return xerr.WithStack(err)
  217. }
  218. disease, err := s.GetDiseaseById(ctx, eventCowDisease.DiagnoseId)
  219. if err != nil {
  220. return xerr.WithStack(err)
  221. }
  222. req.DiseaseName = disease.Name
  223. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, prescription.Id)
  224. if err != nil {
  225. return xerr.WithStack(err)
  226. }
  227. prescriptionDetail := make([]*pasturePb.TreatmentDrugs, len(prescriptionDrugs))
  228. for i, v := range prescriptionDrugs {
  229. prescriptionDetail[i] = &pasturePb.TreatmentDrugs{
  230. DrugsId: int32(v.DrugsId),
  231. DrugsName: v.DrugsName,
  232. Unit: v.Unit,
  233. UseNum: float32(v.Dosages),
  234. }
  235. }
  236. req.PrescriptionDetail = prescriptionDetail
  237. healthStatus := pasturePb.HealthStatus_Treatment
  238. if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
  239. healthStatus = pasturePb.HealthStatus_Curable
  240. }
  241. if req.TreatmentResult == pasturePb.TreatmentResult_Out {
  242. healthStatus = pasturePb.HealthStatus_Out
  243. }
  244. if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
  245. healthStatus = pasturePb.HealthStatus_Dead
  246. }
  247. newEventCowTreatment := model.NewEventCowTreatment(prescription, req, systemUser)
  248. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  249. if err = tx.Create(newEventCowTreatment).Error; err != nil {
  250. return xerr.WithStack(err)
  251. }
  252. if err = tx.Model(eventCowDisease).
  253. Where("id = ?", req.Id).
  254. Where("cow_id = ?", req.CowId).
  255. Updates(map[string]interface{}{
  256. "health_status": healthStatus,
  257. }).Error; err != nil {
  258. return xerr.WithStack(err)
  259. }
  260. if err = tx.Model(new(model.Cow)).
  261. Where("id = ?", req.CowId).
  262. Updates(map[string]interface{}{
  263. "health_status": healthStatus,
  264. }).Error; err != nil {
  265. return xerr.WithStack(err)
  266. }
  267. return nil
  268. }); err != nil {
  269. return xerr.WithStack(err)
  270. }
  271. return nil
  272. }
  273. func (s *StoreEntry) CowDiseaseTreatmentDetail(ctx context.Context, req *pasturePb.EventCowTreatmentDetailRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowTreatmentDetailResponse, error) {
  274. eventCowDiseaseList := make([]*model.EventCowDisease, 0)
  275. var count int64 = 0
  276. pref := s.DB.Where("cow_id = ?", req.CowId)
  277. if req.DiseaseId > 0 {
  278. pref.Where("disease_id = ?", req.DiseaseId)
  279. }
  280. if req.DiseaseStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseStartAt <= req.DiseaseEndAt {
  281. pref.Where("disease_at BETWEEN ? AND ?", req.DiseaseStartAt, req.DiseaseEndAt)
  282. }
  283. if err := pref.Count(&count).
  284. Limit(int(pagination.PageSize)).
  285. Offset(int(pagination.PageOffset)).
  286. Order("id desc").
  287. Find(&eventCowDiseaseList).Error; err != nil {
  288. return nil, xerr.WithStack(err)
  289. }
  290. eventCowTreatmentList := make([]*pasturePb.EventCowTreatment, 0)
  291. cowDiseaseIds := make([]int64, len(eventCowDiseaseList))
  292. for i, v := range eventCowDiseaseList {
  293. cowDiseaseIds[i] = v.Id
  294. }
  295. if err := s.DB.Where("cow_disease_id IN ?", cowDiseaseIds).
  296. Where("cow_id = ?", req.CowId).
  297. Group("cow_disease_id").
  298. Order("id desc").
  299. Find(&eventCowTreatmentList).Error; err != nil {
  300. return nil, xerr.WithStack(err)
  301. }
  302. return &pasturePb.EventCowTreatmentDetailResponse{
  303. Code: http.StatusOK,
  304. Message: "ok",
  305. Data: &pasturePb.EventCowTreatmentDetail{
  306. List: eventCowTreatmentList,
  307. Total: int32(count),
  308. PageSize: pagination.Page,
  309. Page: pagination.PageSize,
  310. },
  311. }, nil
  312. }