event_health.go 14 KB

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