event_health.go 18 KB

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