event_health.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "net/http"
  7. "strings"
  8. "time"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. "go.uber.org/zap"
  11. "gorm.io/gorm"
  12. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  13. "gitee.com/xuyiping_admin/pkg/xerr"
  14. )
  15. // CowDiseaseCreate 牛只发病提交
  16. func (s *StoreEntry) CowDiseaseCreate(ctx context.Context, req *pasturePb.EventCowDiseaseRequest, source string) error {
  17. userModel, err := s.GetUserModel(ctx)
  18. if err != nil {
  19. return xerr.WithStack(err)
  20. }
  21. // 牛只信息
  22. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  23. if err != nil {
  24. return xerr.Customf("牛只信息错误: %d", req.CowId)
  25. }
  26. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  27. if err != nil {
  28. return xerr.Customf("请检查操作人信息")
  29. }
  30. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, req.DiseaseId)
  31. if err != nil {
  32. return xerr.WithStack(err)
  33. }
  34. newEventCowDisease := model.NewEventCowDisease(userModel.AppPasture.Id, cow, disease, req, operationUser, userModel.SystemUser)
  35. defer func() {
  36. // 更新牛只健康状态
  37. if newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Disease || newEventCowDisease.HealthStatus == pasturePb.HealthStatus_Treatment {
  38. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Disease)
  39. s.DB.Model(new(model.Cow)).
  40. Select("health_status").
  41. Where("id = ?", req.CowId).
  42. Updates(cow)
  43. }
  44. if req.PenId > 0 {
  45. penMap := s.PenMap(ctx, userModel.AppPasture.Id)
  46. penData, ok := penMap[req.PenId]
  47. if !ok {
  48. return
  49. }
  50. cow.EventPenUpdate(penData)
  51. s.DB.Model(new(model.Cow)).Select("pen_id", "pen_name").Where("id = ?", cow.Id).Updates(cow)
  52. }
  53. }()
  54. // PC端直接跳过诊断过程
  55. if source == model.SourcePC {
  56. newEventCowDisease.DiagnosedResult = pasturePb.IsShow_Ok
  57. newEventCowDisease.DiagnoseOperationId = int32(operationUser.Id)
  58. newEventCowDisease.DiagnoseOperationName = operationUser.Name
  59. newEventCowDisease.Source = model.SourcePC
  60. newEventCowDisease.DiagnoseId = req.DiseaseId
  61. newEventCowDisease.DiagnoseName = disease.Name
  62. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Disease
  63. }
  64. if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
  65. newEventCowDisease.HealthStatus = pasturePb.HealthStatus_Treatment
  66. }
  67. var newEventCowTreatment *model.EventCowTreatment
  68. var newCowTreatmentRequest *pasturePb.CowTreatmentRequest
  69. var isCreatePrescription bool
  70. diseaseTypeMap := s.DiseaseTypeMap()
  71. prescription := &model.Prescription{}
  72. if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
  73. isCreatePrescription = true
  74. if req.PrescriptionId > 0 {
  75. prescription, err = s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
  76. if err != nil {
  77. return xerr.WithStack(err)
  78. }
  79. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
  80. if err != nil {
  81. return xerr.WithStack(err)
  82. }
  83. req.PrescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
  84. }
  85. }
  86. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  87. if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
  91. // 已有的处方使用次数+1
  92. if req.PrescriptionId > 0 {
  93. prescription.EventUseCountUpdate()
  94. if err = tx.Model(new(model.Prescription)).
  95. Where("id = ?", prescription.Id).
  96. Updates(prescription).Error; err != nil {
  97. return xerr.WithStack(err)
  98. }
  99. }
  100. // 新的临时处方
  101. if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
  102. newPrescriptionRequest := &pasturePb.PrescriptionRequest{
  103. Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Format("20060102"), operationUser.Name),
  104. ApplicableDiseaseIds: []int32{req.DiseaseId},
  105. IsShow: pasturePb.IsShow_Ok,
  106. }
  107. newPrescription := model.NewPrescription(
  108. userModel.AppPasture.Id,
  109. newPrescriptionRequest,
  110. fmt.Sprintf("%d", disease.Id),
  111. 1,
  112. 0,
  113. 0,
  114. userModel.SystemUser,
  115. )
  116. newPrescription.UseCount += 1
  117. if err = tx.Model(new(model.Prescription)).Create(newPrescription).Error; err != nil {
  118. return xerr.WithStack(err)
  119. }
  120. prescription = newPrescription
  121. newPrescriptionDrugs := model.NewPrescriptionDrugs(userModel.AppPasture.Id, prescription.Id, req.PrescriptionDetail)
  122. if err = tx.Model(new(model.PrescriptionDrugs)).Create(newPrescriptionDrugs).Error; err != nil {
  123. return xerr.WithStack(err)
  124. }
  125. newEventCowTreatment.PrescriptionId = prescription.Id
  126. newEventCowTreatment.PrescriptionName = prescription.Name
  127. }
  128. // 创建治疗记录
  129. if isCreatePrescription {
  130. newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
  131. CowId: req.CowId,
  132. PrescriptionId: prescription.Id,
  133. DiseaseId: req.DiseaseId,
  134. DiseaseName: disease.Name,
  135. DiseaseType: disease.DiseaseType,
  136. PrescriptionDetail: req.PrescriptionDetail,
  137. TreatmentResult: pasturePb.TreatmentResult_GoOn,
  138. Remarks: req.Remarks,
  139. TreatmentAt: req.DiseaseAt,
  140. }
  141. newEventCowTreatment = model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, userModel.SystemUser)
  142. // 创建治疗记录
  143. if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
  144. return xerr.WithStack(err)
  145. }
  146. }
  147. return nil
  148. }); err != nil {
  149. return xerr.WithStack(err)
  150. }
  151. return nil
  152. }
  153. // CowDiseaseList 发病牛只清单
  154. func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
  155. userModel, err := s.GetUserModel(ctx)
  156. if err != nil {
  157. return nil, xerr.WithStack(err)
  158. }
  159. cowDiseaseList := make([]*model.EventCowDisease, 0)
  160. var count int64 = 0
  161. pref := s.DB.Select("a.*,b.name").Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
  162. Joins(fmt.Sprintf("JOIN %s AS b on a.pen_id = b.id", new(model.Pen).TableName())).
  163. Where("a.diagnosed_result < ?", pasturePb.IsShow_No).Where("a.pasture_id = ?", userModel.AppPasture.Id)
  164. if len(req.CowIds) > 0 {
  165. pref.Where("a.cow_id IN ?", req.CowIds)
  166. }
  167. if req.DiseaseId > 0 {
  168. pref.Where("a.disease_id = ?", req.DiseaseId)
  169. }
  170. if req.PenId > 0 {
  171. pref.Where("a.pen_id = ?", req.PenId)
  172. }
  173. if req.Lact > 0 {
  174. pref.Where("a.lact = ?", req.Lact)
  175. }
  176. if req.MinDayAge > 0 && req.MaxDayAge > 0 && req.MaxDayAge >= req.MinDayAge {
  177. pref.Where("a.day_age BETWEEN ? AND ?", req.MinDayAge, req.MaxDayAge)
  178. }
  179. if req.HealthStatus > 0 {
  180. pref.Where("a.health_status = ?", req.HealthStatus)
  181. }
  182. if req.DiseasedStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseEndAt >= req.DiseasedStartAt {
  183. pref.Where("a.disease_at BETWEEN ? AND ?", req.DiseasedStartAt, req.DiseaseEndAt)
  184. }
  185. if err = pref.Order("a.id desc").
  186. Count(&count).Limit(int(pagination.PageSize)).
  187. Offset(int(pagination.PageOffset)).
  188. Find(&cowDiseaseList).Error; err != nil {
  189. return nil, xerr.WithStack(err)
  190. }
  191. healthStatusMap := s.HealthStatusMap()
  192. return &pasturePb.EventCowDiseaseResponse{
  193. Code: http.StatusOK,
  194. Msg: "ok",
  195. Data: &pasturePb.EventCowDiseaseData{
  196. List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
  197. Total: int32(count),
  198. PageSize: pagination.PageSize,
  199. Page: pagination.Page,
  200. },
  201. }, nil
  202. }
  203. // CowDiseaseDiagnose 发病牛只诊断
  204. func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
  205. userModel, err := s.GetUserModel(ctx)
  206. if err != nil {
  207. return xerr.WithStack(err)
  208. }
  209. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  210. if err != nil {
  211. return xerr.Customf("错误的牛只信息: %d", req.CowId)
  212. }
  213. eventCowDisease := &model.EventCowDisease{}
  214. if err = s.DB.Model(new(model.EventCowDisease)).
  215. Where("cow_id = ?", req.CowId).
  216. Where("id = ?", req.Id).
  217. Where("health_status = ?", pasturePb.HealthStatus_Health).
  218. Where("pasture_id = ?", userModel.AppPasture.Id).
  219. First(eventCowDisease).Error; err != nil {
  220. zaplog.Error("CowDiseaseDiagnose", zap.Any("req", req), zap.Any("userModel", userModel))
  221. return xerr.Custom("异常牛只数据")
  222. }
  223. if eventCowDisease == nil || eventCowDisease.Id <= 0 {
  224. return xerr.Custom("异常牛只数据")
  225. }
  226. if req.DiagnosedResult == pasturePb.IsShow_No {
  227. // 未发病更新
  228. eventCowDisease.EventUnDiseaseUpdate(userModel.SystemUser, req.Remarks)
  229. if err = s.DB.Model(eventCowDisease).
  230. Select("diagnosed_result", "diagnose_operation_id", "diagnose_operation_name", "remarks").
  231. Where("id = ?", req.Id).
  232. Updates(eventCowDisease).Error; err != nil {
  233. return xerr.WithStack(err)
  234. }
  235. return nil
  236. }
  237. // 已发病
  238. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, req.DiseaseId)
  239. if err != nil {
  240. return xerr.WithStack(err)
  241. }
  242. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  243. if err != nil {
  244. return xerr.WithStack(err)
  245. }
  246. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  247. eventCowDisease.EventDiseaseUpdate(disease, systemUser, req.Temperature)
  248. if err = tx.Model(eventCowDisease).
  249. Select("health_status", "diagnosed_result", "diagnose_id", "diagnose_name", "temperature", "diagnose_operation_id", "diagnose_operation_name", "diagnosed_at").
  250. Where("id = ?", req.Id).
  251. Where("cow_id = ?", req.CowId).
  252. Updates(eventCowDisease).Error; err != nil {
  253. return xerr.WithStack(err)
  254. }
  255. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Disease)
  256. if err = tx.Model(cow).
  257. Select("health_status").
  258. Where("id = ?", cow.Id).
  259. Updates(cow).Error; err != nil {
  260. return xerr.WithStack(err)
  261. }
  262. return nil
  263. }); err != nil {
  264. return xerr.WithStack(err)
  265. }
  266. return nil
  267. }
  268. // CowDiseaseTreatment 发病牛只治疗
  269. func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.CowTreatmentRequest) error {
  270. userModel, err := s.GetUserModel(ctx)
  271. if err != nil {
  272. return xerr.WithStack(err)
  273. }
  274. eventCowDisease := &model.EventCowDisease{}
  275. if err = s.DB.Where("cow_id = ?", req.CowId).
  276. Where("id = ?", req.Id).
  277. First(eventCowDisease).Error; err != nil {
  278. return xerr.WithStack(err)
  279. }
  280. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
  281. eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
  282. return xerr.Custom("异常牛只数据")
  283. }
  284. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  285. if err != nil {
  286. return xerr.Customf("操作人数据异常: %d", req.OperationId)
  287. }
  288. prescription, err := s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
  289. if err != nil {
  290. return xerr.WithStack(err)
  291. }
  292. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  293. if err != nil {
  294. return xerr.Customf("异常牛数据: %d", req.CowId)
  295. }
  296. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, eventCowDisease.DiagnoseId)
  297. if err != nil {
  298. return xerr.WithStack(err)
  299. }
  300. req.DiseaseName = disease.Name
  301. req.DiseaseType = disease.DiseaseType
  302. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
  303. if err != nil {
  304. return xerr.WithStack(err)
  305. }
  306. unitMap := s.UnitMap()
  307. prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, len(prescriptionDrugs))
  308. for i, v := range prescriptionDrugs {
  309. prescriptionDetail[i] = &pasturePb.PrescriptionDrugsList{
  310. DrugsId: int32(v.DrugsId),
  311. DrugsName: v.DrugsName,
  312. Unit: v.Unit,
  313. UnitName: unitMap[v.Unit],
  314. Dosages: v.Dosages,
  315. }
  316. }
  317. req.PrescriptionDetail = prescriptionDetail
  318. healthStatus := pasturePb.HealthStatus_Treatment
  319. if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
  320. healthStatus = pasturePb.HealthStatus_Curable
  321. }
  322. if req.TreatmentResult == pasturePb.TreatmentResult_Out {
  323. healthStatus = pasturePb.HealthStatus_Out
  324. }
  325. if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
  326. healthStatus = pasturePb.HealthStatus_Dead
  327. }
  328. diseaseTypeMap := s.DiseaseTypeMap()
  329. newEventCowTreatment := model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, req, diseaseTypeMap, operationUser, userModel.SystemUser)
  330. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  331. if err = tx.Create(newEventCowTreatment).Error; err != nil {
  332. return xerr.WithStack(err)
  333. }
  334. eventCowDisease.EventHealthStatusUpdate(healthStatus)
  335. if err = tx.Model(eventCowDisease).
  336. Select("health_status").
  337. Where("id = ?", req.Id).
  338. Where("cow_id = ?", req.CowId).
  339. Updates(eventCowDisease).Error; err != nil {
  340. return xerr.WithStack(err)
  341. }
  342. cow.EventHealthStatusUpdate(healthStatus)
  343. if err = tx.Model(new(model.Cow)).
  344. Select("health_status").
  345. Where("id = ?", req.CowId).
  346. Updates(cow).Error; err != nil {
  347. return xerr.WithStack(err)
  348. }
  349. prescription.EventUseCountUpdate()
  350. if err = tx.Model(prescription).
  351. Select("use_count").
  352. Where("id = ?", prescription.Id).
  353. Updates(prescription).Error; err != nil {
  354. return xerr.WithStack(err)
  355. }
  356. return nil
  357. }); err != nil {
  358. return xerr.WithStack(err)
  359. }
  360. return nil
  361. }
  362. func (s *StoreEntry) DiseaseSuggestPrescription(ctx context.Context, diseaseId int64) (*pasturePb.ConfigOptionsListResponse, error) {
  363. userModel, err := s.GetUserModel(ctx)
  364. if err != nil {
  365. return nil, xerr.WithStack(err)
  366. }
  367. res := make([]*pasturePb.ConfigOptionsList, 0)
  368. prescriptionList := make([]*model.Prescription, 0)
  369. if err = s.DB.Model(new(model.Prescription)).
  370. Where("is_show = ?", pasturePb.IsShow_Ok).
  371. Where("pasture_id = ?", userModel.AppPasture.Id).
  372. Find(&prescriptionList).Error; err != nil {
  373. return nil, xerr.WithStack(err)
  374. }
  375. for _, v := range prescriptionList {
  376. disabled := false
  377. if strings.Contains(v.ApplicableDisease, fmt.Sprintf("%d", diseaseId)) {
  378. disabled = true
  379. }
  380. res = append(res, &pasturePb.ConfigOptionsList{
  381. Value: v.Id,
  382. Label: v.Name,
  383. Disabled: disabled,
  384. })
  385. }
  386. return &pasturePb.ConfigOptionsListResponse{
  387. Code: http.StatusOK,
  388. Msg: "ok",
  389. Data: res,
  390. }, nil
  391. }
  392. // CowDiseaseTreatmentDetail 发病牛只治疗详情列表
  393. func (s *StoreEntry) CowDiseaseTreatmentDetail(
  394. ctx context.Context,
  395. req *pasturePb.EventCowTreatmentDetailRequest,
  396. pagination *pasturePb.PaginationModel,
  397. ) (*pasturePb.EventCowTreatmentDetailResponse, error) {
  398. userModel, err := s.GetUserModel(ctx)
  399. if err != nil {
  400. return nil, xerr.WithStack(err)
  401. }
  402. eventCowDiseaseList := make([]*model.EventCowDisease, 0)
  403. var count int64 = 0
  404. pref := s.DB.Model(new(model.EventCowDisease)).
  405. Where("cow_id = ?", req.CowId).Where("pasture_id = ?", userModel.AppPasture.Id)
  406. if req.DiseaseId > 0 {
  407. pref.Where("disease_id = ?", req.DiseaseId)
  408. }
  409. if req.DiseaseStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseStartAt <= req.DiseaseEndAt {
  410. pref.Where("disease_at BETWEEN ? AND ?", req.DiseaseStartAt, req.DiseaseEndAt)
  411. }
  412. if err = pref.Count(&count).
  413. Limit(int(pagination.PageSize)).
  414. Offset(int(pagination.PageOffset)).
  415. Order("id desc").
  416. Find(&eventCowDiseaseList).Error; err != nil {
  417. return nil, xerr.WithStack(err)
  418. }
  419. eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
  420. cowDiseaseIds := make([]int64, len(eventCowDiseaseList))
  421. for i, v := range eventCowDiseaseList {
  422. cowDiseaseIds[i] = v.Id
  423. }
  424. if err := s.DB.Model(new(model.EventCowTreatment)).
  425. Where("cow_disease_id IN ?", cowDiseaseIds).
  426. Where("cow_id = ?", req.CowId).
  427. Group("cow_disease_id").
  428. Order("id desc").
  429. Find(&eventCowTreatmentList).Error; err != nil {
  430. return nil, xerr.WithStack(err)
  431. }
  432. return &pasturePb.EventCowTreatmentDetailResponse{
  433. Code: http.StatusOK,
  434. Msg: "ok",
  435. Data: &pasturePb.EventCowTreatmentDetail{
  436. List: model.EventCowTreatmentSlice(eventCowTreatmentList).ToPB(eventCowDiseaseList),
  437. Total: int32(count),
  438. PageSize: pagination.Page,
  439. Page: pagination.PageSize,
  440. },
  441. }, nil
  442. }
  443. func (s *StoreEntry) CowDiseaseCurable(ctx context.Context, req *pasturePb.EventCowCurableRequest) error {
  444. userModel, err := s.GetUserModel(ctx)
  445. if err != nil {
  446. return xerr.WithStack(err)
  447. }
  448. eventCowDiseaseList := make([]*model.EventCowDisease, 0)
  449. if err = s.DB.Where("id IN ?", req.Ids).
  450. Where("health_status = ?", pasturePb.HealthStatus_Treatment).
  451. Where("pasture_id = ?", userModel.AppPasture.Id).
  452. Find(&eventCowDiseaseList).Error; err != nil {
  453. zaplog.Error("GetEventCowDiseaseList", zap.Any("err", err), zap.Any("req", req))
  454. return xerr.Custom("异常数据")
  455. }
  456. if len(eventCowDiseaseList) == 0 {
  457. return nil
  458. }
  459. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  460. if err != nil {
  461. return xerr.Customf("该用户不存在: %d", req.OperationId)
  462. }
  463. eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
  464. for _, v := range eventCowDiseaseList {
  465. newEventCowTreatment := model.NewEventCowCurableTreatment(userModel.AppPasture.Id, userModel.SystemUser, operationUser, v, req.Remarks, int64(req.CurableAt))
  466. eventCowTreatmentList = append(eventCowTreatmentList, newEventCowTreatment)
  467. }
  468. if len(eventCowTreatmentList) <= 0 {
  469. return nil
  470. }
  471. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  472. cow := &model.Cow{}
  473. for _, eventCowDisease := range eventCowDiseaseList {
  474. eventCowDisease.EventCurableUpdate(int64(req.CurableAt))
  475. if err = tx.Model(eventCowDisease).
  476. Select("health_status", "diagnosed_result", "curable_at").
  477. Where("id = ?", eventCowDisease.Id).
  478. Where("health_status = ?", pasturePb.HealthStatus_Treatment).
  479. Updates(eventCowDisease).Error; err != nil {
  480. return xerr.WithStack(err)
  481. }
  482. cow, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventCowDisease.CowId)
  483. if err != nil {
  484. return xerr.WithStack(err)
  485. }
  486. // 更新牛只健康状态
  487. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Curable)
  488. if err = tx.Model(cow).
  489. Select("health_status").
  490. Where("id = ?", eventCowDisease.CowId).
  491. Updates(cow).Error; err != nil {
  492. return xerr.WithStack(err)
  493. }
  494. }
  495. if err = tx.Model(new(model.EventCowTreatment)).Create(eventCowTreatmentList).Error; err != nil {
  496. return xerr.WithStack(err)
  497. }
  498. return nil
  499. }); err != nil {
  500. return xerr.WithStack(err)
  501. }
  502. return nil
  503. }