event_health.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. package backend
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "kpt-pasture/model"
  7. "net/http"
  8. "strings"
  9. "time"
  10. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  11. "go.uber.org/zap"
  12. "gorm.io/gorm"
  13. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  14. "gitee.com/xuyiping_admin/pkg/xerr"
  15. )
  16. // CowDiseaseCreate 牛只发病提交
  17. func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest, source string) error {
  18. userModel, err := s.GetUserModel(ctx)
  19. if err != nil {
  20. return xerr.WithStack(err)
  21. }
  22. // 牛只信息
  23. cow, err := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, req.EarNumber)
  24. if err != nil {
  25. return xerr.Customf("牛只信息错误: %d", req.CowId)
  26. }
  27. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  28. if err != nil {
  29. return xerr.Customf("请检查操作人信息")
  30. }
  31. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, req.DiseaseId)
  32. if err != nil {
  33. return xerr.WithStack(err)
  34. }
  35. newEventCowDisease := model.NewEventCowDisease(userModel.AppPasture.Id, cow, disease, req, operationUser, userModel.SystemUser)
  36. defer func() {
  37. // 更新牛只健康状态
  38. if newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Disease || newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Treatment {
  39. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Disease)
  40. if err = s.DB.Model(new(model.Cow)).
  41. Select("health_status").
  42. Where("id = ?", req.CowId).
  43. Updates(cow).Error; err != nil {
  44. zaplog.Error("CowDiseaseCreate", zap.Any("EventHealthStatusUpdate", err))
  45. }
  46. }
  47. if req.PenId > 0 {
  48. penMap := s.PenMap(ctx, userModel.AppPasture.Id)
  49. penData, ok := penMap[req.PenId]
  50. if !ok {
  51. return
  52. }
  53. cow.EventPenUpdate(penData)
  54. if err = s.DB.Model(new(model.Cow)).
  55. Select("pen_id", "pen_name").
  56. Where("id = ?", cow.Id).
  57. Updates(cow).Error; err != nil {
  58. zaplog.Error("CowDiseaseCreate", zap.Any("EventPenUpdate", err))
  59. }
  60. }
  61. }()
  62. // PC端直接跳过诊断过程
  63. if source == model.SourcePC {
  64. newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
  65. newEventCowDisease.DiagnoseOperationId = int32(operationUser.Id)
  66. newEventCowDisease.DiagnoseOperationName = operationUser.Name
  67. newEventCowDisease.Source = model.SourcePC
  68. newEventCowDisease.DiagnoseId = req.DiseaseId
  69. newEventCowDisease.DiagnoseName = disease.Name
  70. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Disease
  71. }
  72. if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
  73. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
  74. newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
  75. newEventCowDisease.FirstTreatmentAt = int64(req.DiseaseAt)
  76. newEventCowDisease.LastTreatmentAt = int64(req.DiseaseAt)
  77. newEventCowDisease.DiagnoseId = int32(disease.Id)
  78. newEventCowDisease.DiagnoseName = disease.Name
  79. }
  80. newEventCowTreatment := &model.EventCowTreatment{}
  81. newCowTreatmentRequest := &pasturePb.CowTreatmentRequest{}
  82. var isCreatePrescription bool
  83. diseaseTypeMap := s.DiseaseTypeMap()
  84. prescription := &model.Prescription{}
  85. if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
  86. isCreatePrescription = true
  87. if req.PrescriptionId > 0 {
  88. prescription, err = s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
  89. if err != nil {
  90. return xerr.WithStack(err)
  91. }
  92. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
  93. if err != nil {
  94. return xerr.WithStack(err)
  95. }
  96. req.PrescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
  97. }
  98. }
  99. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  100. lastPrescriptionName := ""
  101. // 已有的处方使用次数+1
  102. if req.PrescriptionId > 0 {
  103. prescription.EventUseCountUpdate()
  104. if err = tx.Model(new(model.Prescription)).
  105. Select("use_count").
  106. Where("id = ?", prescription.Id).
  107. Updates(prescription).Error; err != nil {
  108. return xerr.WithStack(err)
  109. }
  110. lastPrescriptionName = prescription.Name
  111. }
  112. // 新的临时处方
  113. if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
  114. newPrescriptionRequest := &pasturePb.PrescriptionRequest{
  115. Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Format("20060102"), operationUser.Name),
  116. ApplicableDiseaseIds: []int32{req.DiseaseId},
  117. IsShow: pasturePb.IsShow_Ok,
  118. }
  119. newPrescription := model.NewPrescription(
  120. userModel.AppPasture.Id,
  121. newPrescriptionRequest,
  122. fmt.Sprintf("%d", disease.Id),
  123. 1,
  124. 0,
  125. 0,
  126. userModel.SystemUser,
  127. )
  128. newPrescription.UseCount += 1
  129. if err = tx.Model(new(model.Prescription)).Create(newPrescription).Error; err != nil {
  130. return xerr.WithStack(err)
  131. }
  132. prescription = newPrescription
  133. lastPrescriptionName = newPrescription.Name
  134. newPrescriptionDrugs := model.NewPrescriptionDrugs(userModel.AppPasture.Id, prescription.Id, req.PrescriptionDetail)
  135. if err = tx.Model(new(model.PrescriptionDrugs)).Create(newPrescriptionDrugs).Error; err != nil {
  136. return xerr.WithStack(err)
  137. }
  138. // 记录事件日志
  139. cowLogs := s.SubmitEventLog(ctx, userModel.AppPasture.Id, cow, pasturePb.EventType_Disease, newEventCowDisease)
  140. if err = tx.Table(cowLogs.TableName()).Create(cowLogs).Error; err != nil {
  141. return xerr.WithStack(err)
  142. }
  143. }
  144. // 创建 CowDisease
  145. newEventCowDisease.LastPrescriptionName = lastPrescriptionName
  146. if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
  147. return xerr.WithStack(err)
  148. }
  149. // 创建治疗记录
  150. if isCreatePrescription {
  151. newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
  152. CowId: req.CowId,
  153. PrescriptionId: prescription.Id,
  154. DiseaseId: req.DiseaseId,
  155. DiseaseName: disease.Name,
  156. DiseaseType: disease.DiseaseType,
  157. PrescriptionDetail: req.PrescriptionDetail,
  158. TreatmentResult: pasturePb.TreatmentResult_GoOn,
  159. Remarks: req.Remarks,
  160. TreatmentAt: req.DiseaseAt,
  161. }
  162. newEventCowTreatment = model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, userModel.SystemUser)
  163. newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
  164. // 创建治疗记录
  165. if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
  166. return xerr.WithStack(err)
  167. }
  168. }
  169. return nil
  170. }); err != nil {
  171. return xerr.WithStack(err)
  172. }
  173. return nil
  174. }
  175. // CowDiseaseList 发病牛只清单
  176. func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
  177. userModel, err := s.GetUserModel(ctx)
  178. if err != nil {
  179. return nil, xerr.WithStack(err)
  180. }
  181. cowDiseaseList := make([]*model.EventCowDisease, 0)
  182. var count int64 = 0
  183. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
  184. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  185. Select("a.*,b.pen_name").
  186. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  187. Where("a.health_status != ?", pasturePb.HealthStatus_Curable)
  188. if len(req.CowIds) > 0 {
  189. pref.Where("a.cow_id IN ?", req.CowIds)
  190. }
  191. if req.EarNumber != "" {
  192. pref.Where("a.ear_number = ?", req.EarNumber)
  193. }
  194. if req.DiseaseId > 0 {
  195. pref.Where("a.disease_id = ?", req.DiseaseId)
  196. }
  197. if req.PenId > 0 {
  198. pref.Where("b.pen_id = ?", req.PenId)
  199. }
  200. if req.HealthStatus > 0 {
  201. pref.Where("a.health_status = ?", req.HealthStatus)
  202. }
  203. if req.DiseasedStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseEndAt >= req.DiseasedStartAt {
  204. pref.Where("a.disease_at BETWEEN ? AND ?", req.DiseasedStartAt, req.DiseaseEndAt)
  205. }
  206. if err = pref.Order("a.id DESC").
  207. Count(&count).Limit(int(pagination.PageSize)).
  208. Offset(int(pagination.PageOffset)).
  209. Find(&cowDiseaseList).Error; err != nil {
  210. return nil, xerr.WithStack(err)
  211. }
  212. healthStatusMap := s.HealthStatusMap()
  213. return &pasturePb.EventCowDiseaseResponse{
  214. Code: http.StatusOK,
  215. Msg: "ok",
  216. Data: &pasturePb.EventCowDiseaseData{
  217. List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
  218. Total: int32(count),
  219. PageSize: pagination.PageSize,
  220. Page: pagination.Page,
  221. },
  222. }, 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. // 牛只疾病信息
  301. eventCowDisease := &model.EventCowDisease{}
  302. if err = s.DB.Where("cow_id = ?", req.CowId).
  303. Where("pasture_id = ?", userModel.AppPasture.Id).
  304. Where("id = ?", req.Id).
  305. First(eventCowDisease).Error; err != nil {
  306. return xerr.WithStack(err)
  307. }
  308. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
  309. eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
  310. return xerr.Custom("异常牛只数据")
  311. }
  312. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  313. if err != nil {
  314. return xerr.WithStack(err)
  315. }
  316. // 操作人信息
  317. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  318. if err != nil {
  319. return xerr.Customf("操作人数据异常: %d", req.OperationId)
  320. }
  321. // 处方信息
  322. prescription, err := s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
  323. if err != nil {
  324. return xerr.WithStack(err)
  325. }
  326. // 疾病信息
  327. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, eventCowDisease.DiseaseId)
  328. if err != nil {
  329. return xerr.WithStack(err)
  330. }
  331. req.DiseaseName = disease.Name
  332. req.DiseaseType = disease.DiseaseType
  333. // 处方信息
  334. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
  335. if err != nil {
  336. return xerr.WithStack(err)
  337. }
  338. // 处方详情
  339. unitMap := s.UnitMap()
  340. prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, len(prescriptionDrugs))
  341. for i, v := range prescriptionDrugs {
  342. prescriptionDetail[i] = &pasturePb.PrescriptionDrugsList{
  343. DrugsId: int32(v.DrugsId),
  344. DrugsName: v.DrugsName,
  345. Unit: v.Unit,
  346. UnitName: unitMap[v.Unit],
  347. Dosages: v.Dosages,
  348. }
  349. }
  350. req.PrescriptionDetail = prescriptionDetail
  351. healthStatus := pasturePb.HealthStatus_Treatment
  352. if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
  353. healthStatus = pasturePb.HealthStatus_Curable
  354. }
  355. if req.TreatmentResult == pasturePb.TreatmentResult_Out {
  356. healthStatus = pasturePb.HealthStatus_Out
  357. }
  358. if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
  359. healthStatus = pasturePb.HealthStatus_Dead
  360. }
  361. diseaseTypeMap := s.DiseaseTypeMap()
  362. newEventCowTreatment := model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, req, diseaseTypeMap, operationUser, userModel.SystemUser)
  363. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  364. if err = tx.Create(newEventCowTreatment).Error; err != nil {
  365. return xerr.WithStack(err)
  366. }
  367. eventCowDisease.EventTreatmentUpdate(healthStatus, int64(req.TreatmentAt), prescription.Name)
  368. if err = tx.Model(eventCowDisease).
  369. Select("health_status", "first_treatment_at", "last_treatment_at", "last_prescription_name").
  370. Where("id = ?", req.Id).
  371. Where("cow_id = ?", req.CowId).
  372. Updates(eventCowDisease).Error; err != nil {
  373. return xerr.WithStack(err)
  374. }
  375. cow.EventHealthStatusUpdate(healthStatus)
  376. if err = tx.Model(new(model.Cow)).
  377. Select("health_status").
  378. Where("id = ?", req.CowId).
  379. Updates(cow).Error; err != nil {
  380. return xerr.WithStack(err)
  381. }
  382. prescription.EventUseCountUpdate()
  383. if err = tx.Model(prescription).
  384. Select("use_count").
  385. Where("id = ?", prescription.Id).
  386. Updates(prescription).Error; err != nil {
  387. return xerr.WithStack(err)
  388. }
  389. return nil
  390. }); err != nil {
  391. return xerr.WithStack(err)
  392. }
  393. return nil
  394. }
  395. func (s *StoreEntry) DiseaseSuggestPrescription(ctx context.Context, diseaseId int64) (*pasturePb.ConfigOptionsListResponse, error) {
  396. userModel, err := s.GetUserModel(ctx)
  397. if err != nil {
  398. return nil, xerr.WithStack(err)
  399. }
  400. res := make([]*pasturePb.ConfigOptionsList, 0)
  401. prescriptionList := make([]*model.Prescription, 0)
  402. if err = s.DB.Model(new(model.Prescription)).
  403. Where("is_show = ?", pasturePb.IsShow_Ok).
  404. Where("pasture_id = ?", userModel.AppPasture.Id).
  405. Find(&prescriptionList).Error; err != nil {
  406. return nil, xerr.WithStack(err)
  407. }
  408. for _, v := range prescriptionList {
  409. disabled := false
  410. if strings.Contains(v.ApplicableDisease, fmt.Sprintf("%d", diseaseId)) {
  411. disabled = true
  412. }
  413. res = append(res, &pasturePb.ConfigOptionsList{
  414. Value: v.Id,
  415. Label: v.Name,
  416. Disabled: disabled,
  417. })
  418. }
  419. return &pasturePb.ConfigOptionsListResponse{
  420. Code: http.StatusOK,
  421. Msg: "ok",
  422. Data: res,
  423. }, nil
  424. }
  425. // CowDiseaseTreatmentDetail 发病牛只治疗详情列表
  426. func (s *StoreEntry) CowDiseaseTreatmentDetail(ctx context.Context, req *pasturePb.EventCowTreatmentDetailRequest,
  427. pagination *pasturePb.PaginationModel) (*pasturePb.EventCowTreatmentDetailResponse, error) {
  428. userModel, err := s.GetUserModel(ctx)
  429. if err != nil {
  430. return nil, xerr.WithStack(err)
  431. }
  432. eventCowDisease := &model.EventCowDisease{}
  433. var count int64 = 0
  434. pref := s.DB.Model(new(model.EventCowDisease)).
  435. Where("cow_id = ?", req.CowId).
  436. Where("pasture_id = ?", userModel.AppPasture.Id).
  437. Where("id = ?", req.Id)
  438. if req.DiseaseId > 0 {
  439. pref.Where("disease_id = ?", req.DiseaseId)
  440. }
  441. if req.DiseaseStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseStartAt <= req.DiseaseEndAt {
  442. pref.Where("disease_at BETWEEN ? AND ?", req.DiseaseStartAt, req.DiseaseEndAt)
  443. }
  444. if err = pref.Count(&count).
  445. Limit(int(pagination.PageSize)).
  446. Offset(int(pagination.PageOffset)).
  447. Order("id desc").
  448. First(&eventCowDisease).Error; err != nil {
  449. if errors.Is(err, gorm.ErrRecordNotFound) {
  450. return &pasturePb.EventCowTreatmentDetailResponse{
  451. Code: http.StatusOK,
  452. Msg: "ok",
  453. Data: &pasturePb.EventCowTreatmentDetail{
  454. List: make([]*pasturePb.EventCowTreatment, 0),
  455. Total: 0,
  456. PageSize: pagination.PageSize,
  457. Page: pagination.Page,
  458. },
  459. }, nil
  460. } else {
  461. return nil, xerr.WithStack(err)
  462. }
  463. }
  464. eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
  465. if err = s.DB.Model(new(model.EventCowTreatment)).
  466. Where("cow_disease_id = ?", req.Id).
  467. Where("cow_id = ?", req.CowId).
  468. Order("id desc").
  469. Find(&eventCowTreatmentList).Error; err != nil {
  470. return nil, xerr.WithStack(err)
  471. }
  472. return &pasturePb.EventCowTreatmentDetailResponse{
  473. Code: http.StatusOK,
  474. Msg: "ok",
  475. Data: &pasturePb.EventCowTreatmentDetail{
  476. List: model.EventCowTreatmentSlice(eventCowTreatmentList).ToPB(eventCowDisease),
  477. Total: int32(count),
  478. PageSize: pagination.Page,
  479. Page: pagination.PageSize,
  480. },
  481. }, nil
  482. }
  483. func (s *StoreEntry) CowDiseaseCurable(ctx context.Context, req *pasturePb.EventCowCurableRequest) error {
  484. userModel, err := s.GetUserModel(ctx)
  485. if err != nil {
  486. return xerr.WithStack(err)
  487. }
  488. eventCowDiseaseList := make([]*model.EventCowDisease, 0)
  489. if err = s.DB.Where("id IN ?", req.Ids).
  490. Where("health_status = ?", pasturePb.HealthStatus_Treatment).
  491. Where("pasture_id = ?", userModel.AppPasture.Id).
  492. Find(&eventCowDiseaseList).Error; err != nil {
  493. zaplog.Error("GetEventCowDiseaseList", zap.Any("err", err), zap.Any("req", req))
  494. return xerr.Custom("异常数据")
  495. }
  496. if len(eventCowDiseaseList) == 0 {
  497. return nil
  498. }
  499. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  500. if err != nil {
  501. return xerr.Customf("该用户不存在: %d", req.OperationId)
  502. }
  503. eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
  504. for _, v := range eventCowDiseaseList {
  505. newEventCowTreatment := model.NewEventCowCurableTreatment(userModel.AppPasture.Id, userModel.SystemUser, operationUser, v, req.Remarks, int64(req.CurableAt))
  506. eventCowTreatmentList = append(eventCowTreatmentList, newEventCowTreatment)
  507. }
  508. if len(eventCowTreatmentList) <= 0 {
  509. return nil
  510. }
  511. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  512. for _, eventCowDisease := range eventCowDiseaseList {
  513. eventCowDisease.EventCurableUpdate(int64(req.CurableAt))
  514. if err = tx.Model(eventCowDisease).
  515. Select("health_status", "diagnosed_result", "curable_at").
  516. Where("id = ?", eventCowDisease.Id).
  517. Where("health_status = ?", pasturePb.HealthStatus_Treatment).
  518. Updates(eventCowDisease).Error; err != nil {
  519. return xerr.WithStack(err)
  520. }
  521. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventCowDisease.CowId)
  522. if err != nil {
  523. return xerr.WithStack(err)
  524. }
  525. // 更新牛只健康状态
  526. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Curable)
  527. if err = tx.Model(cow).
  528. Select("health_status").
  529. Where("id = ?", eventCowDisease.CowId).
  530. Updates(cow).Error; err != nil {
  531. return xerr.WithStack(err)
  532. }
  533. // 更新发病事件日志
  534. cowLogs := &model.EventCowLog{CowId: cow.Id}
  535. curableAtFormat := ""
  536. if eventCowDisease.CurableAt > 0 {
  537. curableAtFormat = time.Unix(eventCowDisease.CurableAt, 0).Format(model.LayoutDate2)
  538. }
  539. curableAtParse := time.Unix(eventCowDisease.CurableAt, 0)
  540. diseaseAtParse := time.Unix(eventCowDisease.DiseaseAt, 0)
  541. curableDays := int64(curableAtParse.Sub(diseaseAtParse).Hours() / 24)
  542. if err = tx.Table(cowLogs.TableName()).
  543. Where("event_type = ?", pasturePb.EventType_Disease).
  544. Where("event_at = ?", eventCowDisease.DiseaseAt).
  545. Update("event_description", fmt.Sprintf("疾病名称: %s; 状态: 已治愈; 治愈时间: %s; 治疗天数: %d;", eventCowDisease.DiseaseName, curableAtFormat, curableDays)).
  546. Error; err != nil {
  547. return xerr.WithStack(err)
  548. }
  549. }
  550. if err = tx.Model(new(model.EventCowTreatment)).Create(eventCowTreatmentList).Error; err != nil {
  551. return xerr.WithStack(err)
  552. }
  553. return nil
  554. }); err != nil {
  555. return xerr.WithStack(err)
  556. }
  557. return nil
  558. }