event_health.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "time"
  8. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  9. "go.uber.org/zap"
  10. "gorm.io/gorm"
  11. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  12. "gitee.com/xuyiping_admin/pkg/xerr"
  13. )
  14. // CowDiseaseList 发病牛只清单
  15. func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
  16. userModel, err := s.GetUserModel(ctx)
  17. if err != nil {
  18. return nil, xerr.WithStack(err)
  19. }
  20. cowDiseaseList := make([]*model.EventCowDisease, 0)
  21. var count int64 = 0
  22. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
  23. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  24. Select("a.*,b.pen_name").
  25. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  26. Where("a.health_status != ?", pasturePb.HealthStatus_Curable).
  27. Where("b.admission_status != ?", pasturePb.AdmissionStatus_Admission)
  28. if len(req.CowIds) > 0 {
  29. pref.Where("a.cow_id IN ?", req.CowIds)
  30. }
  31. if req.EarNumber != "" {
  32. pref.Where("a.ear_number = ?", req.EarNumber)
  33. }
  34. if req.DiseaseId > 0 {
  35. pref.Where("a.disease_id = ?", req.DiseaseId)
  36. }
  37. if req.PenId > 0 {
  38. pref.Where("b.pen_id = ?", req.PenId)
  39. }
  40. if req.HealthStatus > 0 {
  41. pref.Where("a.health_status = ?", req.HealthStatus)
  42. }
  43. if req.DiseasedStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseEndAt >= req.DiseasedStartAt {
  44. pref.Where("a.disease_at BETWEEN ? AND ?", req.DiseasedStartAt, req.DiseaseEndAt)
  45. }
  46. if err = pref.Order("a.id DESC").
  47. Count(&count).Limit(int(pagination.PageSize)).
  48. Offset(int(pagination.PageOffset)).
  49. Find(&cowDiseaseList).Error; err != nil {
  50. return nil, xerr.WithStack(err)
  51. }
  52. healthStatusMap := s.HealthStatusMap()
  53. return &pasturePb.EventCowDiseaseResponse{
  54. Code: http.StatusOK,
  55. Msg: "ok",
  56. Data: &pasturePb.EventCowDiseaseData{
  57. List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
  58. Total: int32(count),
  59. PageSize: pagination.PageSize,
  60. Page: pagination.Page,
  61. },
  62. }, nil
  63. }
  64. // CowDiseaseCreate 牛只发病提交
  65. func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest, source string) error {
  66. userModel, err := s.GetUserModel(ctx)
  67. if err != nil {
  68. return xerr.WithStack(err)
  69. }
  70. pastureId := userModel.AppPasture.Id
  71. // 牛只信息
  72. cow, err := s.GetCowInfoByEarNumber(ctx, pastureId, req.EarNumber)
  73. if err != nil {
  74. return xerr.Customf("牛只信息错误: %d", req.CowId)
  75. }
  76. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  77. if err != nil {
  78. return xerr.Customf("请检查操作人信息")
  79. }
  80. disease, err := s.GetDiseaseById(ctx, pastureId, req.DiseaseId)
  81. if err != nil {
  82. return xerr.WithStack(err)
  83. }
  84. // 牛只疾病信息
  85. newEventCowDisease := model.NewEventCowDisease(pastureId, cow, disease, req, operationUser, userModel.SystemUser)
  86. defer func() {
  87. // 更新牛只健康状态
  88. if newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Disease || newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Treatment {
  89. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Disease)
  90. if err = s.DB.Model(new(model.Cow)).
  91. Select("health_status").
  92. Where("id = ?", req.CowId).
  93. Updates(cow).Error; err != nil {
  94. zaplog.Error("CowDiseaseCreate", zap.Any("EventHealthStatusUpdate", err))
  95. }
  96. }
  97. // 更新栏舍信息
  98. if req.PenId > 0 {
  99. penMap := s.PenMap(ctx, userModel.AppPasture.Id)
  100. penData, ok := penMap[req.PenId]
  101. if !ok {
  102. return
  103. }
  104. cow.EventPenUpdate(penData)
  105. if err = s.DB.Model(new(model.Cow)).
  106. Select("pen_id", "pen_name").
  107. Where("id = ?", cow.Id).
  108. Updates(cow).Error; err != nil {
  109. zaplog.Error("CowDiseaseCreate", zap.Any("EventPenUpdate", err))
  110. }
  111. }
  112. }()
  113. // PC端h和脖环揭发直接跳过诊断过程
  114. if source == model.SourcePC || req.ExposeDiseaseType == pasturePb.ExposeDiseaseType_Neck_Ring {
  115. newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
  116. newEventCowDisease.DiagnoseOperationId = int32(operationUser.Id)
  117. newEventCowDisease.DiagnoseOperationName = operationUser.Name
  118. newEventCowDisease.Source = model.SourcePC
  119. newEventCowDisease.DiagnoseId = req.DiseaseId
  120. newEventCowDisease.DiagnoseName = disease.Name
  121. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Disease
  122. }
  123. if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
  124. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
  125. newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
  126. newEventCowDisease.FirstTreatmentAt = int64(req.DiseaseAt)
  127. newEventCowDisease.LastTreatmentAt = int64(req.DiseaseAt)
  128. newEventCowDisease.DiagnoseId = int32(disease.Id)
  129. newEventCowDisease.DiagnoseName = disease.Name
  130. }
  131. prescription := &model.Prescription{}
  132. prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, 0)
  133. // 获取处方信息
  134. if req.PrescriptionId > 0 && len(req.PrescriptionDetail) <= 0 {
  135. prescription, err = s.GetPrescriptionById(ctx, pastureId, req.PrescriptionId)
  136. if err != nil {
  137. return xerr.WithStack(err)
  138. }
  139. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, pastureId, prescription.Id)
  140. if err != nil {
  141. return xerr.WithStack(err)
  142. }
  143. prescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
  144. }
  145. // 创建新的处方
  146. if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
  147. newPrescriptionRequest := &pasturePb.PrescriptionRequest{
  148. Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Local().Format("20060102"), operationUser.Name),
  149. ApplicableDiseaseIds: []int32{req.DiseaseId},
  150. IsShow: pasturePb.IsShow_Ok,
  151. }
  152. prescription = model.NewPrescription(
  153. pastureId, newPrescriptionRequest, fmt.Sprintf("%d", disease.Id),
  154. 1, 0, 0, userModel.SystemUser,
  155. )
  156. if err = s.DB.Model(new(model.Prescription)).
  157. Create(prescription).Error; err != nil {
  158. zaplog.Error("CowDiseaseCreate", zap.Any("err", err), zap.Any("prescription", prescription))
  159. return xerr.Customf("创建处方错误: %s", err.Error())
  160. }
  161. newPrescriptionDrugs := model.NewPrescriptionDrugs(pastureId, prescription.Id, req.PrescriptionDetail)
  162. if err = s.DB.Model(new(model.PrescriptionDrugs)).
  163. Create(newPrescriptionDrugs).Error; err != nil {
  164. return xerr.WithStack(err)
  165. }
  166. prescriptionDetail = model.PrescriptionDrugsSlice(newPrescriptionDrugs).ToPB()
  167. }
  168. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  169. lastPrescriptionName := ""
  170. // 1. 更新处方使用次数
  171. if prescription.Id > 0 {
  172. prescription.EventUseCountUpdate() // 处方使用次数+1
  173. if err = tx.Model(new(model.Prescription)).
  174. Select("use_count").
  175. Where("id = ?", prescription.Id).
  176. Updates(prescription).Error; err != nil {
  177. return xerr.WithStack(err)
  178. }
  179. lastPrescriptionName = prescription.Name
  180. // 记录事件日志
  181. cowLogs := s.SubmitEventLog(ctx, pastureId, cow, pasturePb.EventType_Disease, newEventCowDisease)
  182. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  183. return xerr.WithStack(err)
  184. }
  185. }
  186. // 2. 创建牛只疾病信息
  187. newEventCowDisease.LastPrescriptionName = lastPrescriptionName
  188. if err = tx.Model(new(model.EventCowDisease)).
  189. Create(newEventCowDisease).Error; err != nil {
  190. return xerr.WithStack(err)
  191. }
  192. // 3. 创建治疗记录
  193. if len(prescriptionDetail) > 0 {
  194. diseaseTypeMap := s.DiseaseTypeMap()
  195. newCowTreatmentRequest := &pasturePb.CowTreatmentRequest{
  196. CowId: req.CowId,
  197. PrescriptionId: prescription.Id,
  198. DiseaseId: int32(disease.Id),
  199. DiseaseName: disease.Name,
  200. DiseaseType: disease.DiseaseType,
  201. PrescriptionDetail: prescriptionDetail,
  202. TreatmentResult: pasturePb.TreatmentResult_GoOn,
  203. Remarks: req.Remarks,
  204. TreatmentAt: req.DiseaseAt,
  205. }
  206. newEventCowTreatment := model.NewEventCowTreatment(pastureId, prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, userModel.SystemUser)
  207. newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
  208. if err = tx.Model(new(model.EventCowTreatment)).
  209. Create(newEventCowTreatment).Error; err != nil {
  210. return xerr.WithStack(err)
  211. }
  212. }
  213. // 4. 如果是脖环揭发
  214. if req.ExposeDiseaseType == pasturePb.ExposeDiseaseType_Neck_Ring {
  215. if err = s.NeckRingUpdateHealth(ctx, pastureId, cow.Id, operationUser); err != nil {
  216. return xerr.WithStack(err)
  217. }
  218. }
  219. return nil
  220. }); err != nil {
  221. return xerr.WithStack(err)
  222. }
  223. return nil
  224. }
  225. // CowDiseaseDiagnose 发病牛只诊断
  226. func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
  227. userModel, err := s.GetUserModel(ctx)
  228. if err != nil {
  229. return xerr.WithStack(err)
  230. }
  231. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  232. if err != nil {
  233. return xerr.Customf("错误的牛只信息: %d", req.CowId)
  234. }
  235. eventCowDisease := &model.EventCowDisease{}
  236. if err = s.DB.Model(new(model.EventCowDisease)).
  237. Where("cow_id = ?", req.CowId).
  238. Where("id = ?", req.Id).
  239. Where("health_status = ?", pasturePb.HealthStatus_Health).
  240. Where("pasture_id = ?", userModel.AppPasture.Id).
  241. First(eventCowDisease).Error; err != nil {
  242. zaplog.Error("CowDiseaseDiagnose", zap.Any("req", req), zap.Any("userModel", userModel))
  243. return xerr.Custom("异常牛只数据")
  244. }
  245. if eventCowDisease == nil || eventCowDisease.Id <= 0 {
  246. return xerr.Custom("异常牛只数据")
  247. }
  248. if req.DiagnosedResult == pasturePb.IsShow_No {
  249. // 未发病更新
  250. eventCowDisease.EventUnDiseaseUpdate(userModel.SystemUser, req.Remarks)
  251. if err = s.DB.Model(eventCowDisease).
  252. Select("diagnosed_result", "diagnose_operation_id", "diagnose_operation_name", "remarks").
  253. Where("id = ?", req.Id).
  254. Updates(eventCowDisease).Error; err != nil {
  255. return xerr.WithStack(err)
  256. }
  257. return nil
  258. }
  259. // 已发病
  260. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, req.DiseaseId)
  261. if err != nil {
  262. return xerr.WithStack(err)
  263. }
  264. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  265. if err != nil {
  266. return xerr.WithStack(err)
  267. }
  268. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  269. eventCowDisease.EventDiseaseUpdate(disease, systemUser, req.Temperature)
  270. if err = tx.Model(eventCowDisease).
  271. Select("health_status", "diagnosed_result", "diagnose_id", "diagnose_name", "temperature", "diagnose_operation_id", "diagnose_operation_name", "diagnosed_at").
  272. Where("id = ?", req.Id).
  273. Where("cow_id = ?", req.CowId).
  274. Updates(eventCowDisease).Error; err != nil {
  275. return xerr.WithStack(err)
  276. }
  277. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Disease)
  278. if err = tx.Model(cow).
  279. Select("health_status").
  280. Where("id = ?", cow.Id).
  281. Updates(cow).Error; err != nil {
  282. return xerr.WithStack(err)
  283. }
  284. // 记录事件日志
  285. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Disease, eventCowDisease)
  286. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  287. return xerr.WithStack(err)
  288. }
  289. return nil
  290. }); err != nil {
  291. return xerr.WithStack(err)
  292. }
  293. return nil
  294. }
  295. // CowDiseaseTreatment 发病牛只治疗
  296. func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.CowTreatmentRequest) error {
  297. userModel, err := s.GetUserModel(ctx)
  298. if err != nil {
  299. return xerr.WithStack(err)
  300. }
  301. pastureId := userModel.AppPasture.Id
  302. cow, err := s.GetCowInfoByCowId(ctx, pastureId, int64(req.CowId))
  303. if err != nil {
  304. return xerr.WithStack(err)
  305. }
  306. // 操作人信息
  307. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  308. if err != nil {
  309. return xerr.Customf("操作人数据异常: %d", req.OperationId)
  310. }
  311. // 处方信息
  312. prescription, err := s.GetPrescriptionById(ctx, pastureId, req.PrescriptionId)
  313. if err != nil {
  314. return xerr.WithStack(err)
  315. }
  316. // 疾病信息
  317. disease, err := s.GetDiseaseById(ctx, pastureId, req.DiseaseId)
  318. if err != nil {
  319. return xerr.WithStack(err)
  320. }
  321. req.DiseaseName = disease.Name
  322. req.DiseaseType = disease.DiseaseType
  323. // 处方信息
  324. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, pastureId, prescription.Id)
  325. if err != nil {
  326. return xerr.WithStack(err)
  327. }
  328. // 牛只疾病信息
  329. eventCowDisease := &model.EventCowDisease{}
  330. if err = s.DB.Where("cow_id = ?", req.CowId).
  331. Where("pasture_id = ?", userModel.AppPasture.Id).
  332. Where("id = ?", req.Id).
  333. First(eventCowDisease).Error; err != nil {
  334. return xerr.WithStack(err)
  335. }
  336. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
  337. eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
  338. return xerr.Custom("异常牛只数据")
  339. }
  340. // 处方详情
  341. unitMap := s.UnitMap()
  342. prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, len(prescriptionDrugs))
  343. for i, v := range prescriptionDrugs {
  344. prescriptionDetail[i] = &pasturePb.PrescriptionDrugsList{
  345. DrugsId: int32(v.DrugsId),
  346. DrugsName: v.DrugsName,
  347. Unit: v.Unit,
  348. UnitName: unitMap[v.Unit],
  349. Dosages: v.Dosages,
  350. }
  351. }
  352. req.PrescriptionDetail = prescriptionDetail
  353. healthStatus := pasturePb.HealthStatus_Treatment
  354. if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
  355. healthStatus = pasturePb.HealthStatus_Curable
  356. }
  357. if req.TreatmentResult == pasturePb.TreatmentResult_Out {
  358. healthStatus = pasturePb.HealthStatus_Out
  359. }
  360. if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
  361. healthStatus = pasturePb.HealthStatus_Dead
  362. }
  363. diseaseTypeMap := s.DiseaseTypeMap()
  364. newEventCowTreatment := model.NewEventCowTreatment(pastureId, prescription, req, diseaseTypeMap, operationUser, userModel.SystemUser)
  365. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  366. if err = tx.Create(newEventCowTreatment).Error; err != nil {
  367. return xerr.WithStack(err)
  368. }
  369. eventCowDisease.EventTreatmentUpdate(healthStatus, int64(req.TreatmentAt), prescription.Name)
  370. if err = tx.Model(eventCowDisease).
  371. Select("health_status", "first_treatment_at", "last_treatment_at", "last_prescription_name").
  372. Where("id = ?", req.Id).
  373. Where("cow_id = ?", req.CowId).
  374. Updates(eventCowDisease).Error; err != nil {
  375. return xerr.WithStack(err)
  376. }
  377. cow.EventHealthStatusUpdate(healthStatus)
  378. if err = tx.Model(new(model.Cow)).
  379. Select("health_status").
  380. Where("id = ?", req.CowId).
  381. Updates(cow).Error; err != nil {
  382. return xerr.WithStack(err)
  383. }
  384. prescription.EventUseCountUpdate()
  385. if err = tx.Model(prescription).
  386. Select("use_count").
  387. Where("id = ?", prescription.Id).
  388. Updates(prescription).Error; err != nil {
  389. return xerr.WithStack(err)
  390. }
  391. return nil
  392. }); err != nil {
  393. return xerr.WithStack(err)
  394. }
  395. return nil
  396. }