event_health.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  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. }
  78. newEventCowTreatment := &model.EventCowTreatment{}
  79. newCowTreatmentRequest := &pasturePb.CowTreatmentRequest{}
  80. var isCreatePrescription bool
  81. diseaseTypeMap := s.DiseaseTypeMap()
  82. prescription := &model.Prescription{}
  83. if req.PrescriptionId > 0 || len(req.PrescriptionDetail) > 0 {
  84. isCreatePrescription = true
  85. if req.PrescriptionId > 0 {
  86. prescription, err = s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
  87. if err != nil {
  88. return xerr.WithStack(err)
  89. }
  90. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
  91. if err != nil {
  92. return xerr.WithStack(err)
  93. }
  94. req.PrescriptionDetail = model.PrescriptionDrugsSlice(prescriptionDrugs).ToPB()
  95. }
  96. }
  97. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  98. lastPrescriptionName := ""
  99. // 已有的处方使用次数+1
  100. if req.PrescriptionId > 0 {
  101. prescription.EventUseCountUpdate()
  102. if err = tx.Model(new(model.Prescription)).
  103. Where("id = ?", prescription.Id).
  104. Updates(prescription).Error; err != nil {
  105. return xerr.WithStack(err)
  106. }
  107. lastPrescriptionName = prescription.Name
  108. }
  109. // 新的临时处方
  110. if req.PrescriptionId <= 0 && len(req.PrescriptionDetail) > 0 {
  111. newPrescriptionRequest := &pasturePb.PrescriptionRequest{
  112. Name: fmt.Sprintf("%s-%s-%s", disease.Name, time.Now().Format("20060102"), operationUser.Name),
  113. ApplicableDiseaseIds: []int32{req.DiseaseId},
  114. IsShow: pasturePb.IsShow_Ok,
  115. }
  116. newPrescription := model.NewPrescription(
  117. userModel.AppPasture.Id,
  118. newPrescriptionRequest,
  119. fmt.Sprintf("%d", disease.Id),
  120. 1,
  121. 0,
  122. 0,
  123. userModel.SystemUser,
  124. )
  125. newPrescription.UseCount += 1
  126. if err = tx.Model(new(model.Prescription)).Create(newPrescription).Error; err != nil {
  127. return xerr.WithStack(err)
  128. }
  129. prescription = newPrescription
  130. lastPrescriptionName = newPrescription.Name
  131. newPrescriptionDrugs := model.NewPrescriptionDrugs(userModel.AppPasture.Id, prescription.Id, req.PrescriptionDetail)
  132. if err = tx.Model(new(model.PrescriptionDrugs)).Create(newPrescriptionDrugs).Error; err != nil {
  133. return xerr.WithStack(err)
  134. }
  135. }
  136. // 创建 CowDisease
  137. newEventCowDisease.LastPrescriptionName = lastPrescriptionName
  138. if err = tx.Model(new(model.EventCowDisease)).Create(newEventCowDisease).Error; err != nil {
  139. return xerr.WithStack(err)
  140. }
  141. // 创建治疗记录
  142. if isCreatePrescription {
  143. newCowTreatmentRequest = &pasturePb.CowTreatmentRequest{
  144. CowId: req.CowId,
  145. PrescriptionId: prescription.Id,
  146. DiseaseId: req.DiseaseId,
  147. DiseaseName: disease.Name,
  148. DiseaseType: disease.DiseaseType,
  149. PrescriptionDetail: req.PrescriptionDetail,
  150. TreatmentResult: pasturePb.TreatmentResult_GoOn,
  151. Remarks: req.Remarks,
  152. TreatmentAt: req.DiseaseAt,
  153. }
  154. newEventCowTreatment = model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, newCowTreatmentRequest, diseaseTypeMap, operationUser, userModel.SystemUser)
  155. newEventCowTreatment.CowDiseaseId = newEventCowDisease.Id
  156. // 创建治疗记录
  157. if err = tx.Model(new(model.EventCowTreatment)).Create(newEventCowTreatment).Error; err != nil {
  158. return xerr.WithStack(err)
  159. }
  160. }
  161. return nil
  162. }); err != nil {
  163. return xerr.WithStack(err)
  164. }
  165. return nil
  166. }
  167. func (s *StoreEntry) EstrusCowList(ctx context.Context, req *pasturePb.EstrusItemsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventEstrusResponse, error) {
  168. userModel, err := s.GetUserModel(ctx)
  169. if err != nil {
  170. return nil, xerr.WithStack(err)
  171. }
  172. var count int64
  173. neckRingEstrusList := make([]*model.NeckRingEstrusWarning, 0)
  174. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.NeckRingEstrusWarning).TableName())).
  175. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  176. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  177. Where("a.is_show = ?", pasturePb.IsShow_Ok)
  178. if len(req.EarNumber) > 0 {
  179. pref.Where("a.ear_number = ?", req.EarNumber)
  180. }
  181. if req.Level > 0 {
  182. pref.Where("a.level = ?", req.Level)
  183. }
  184. if len(req.PenIds) > 0 {
  185. penIds := strings.Split(util.ArrayInt32ToStrings(req.PenIds, ","), ",")
  186. pref.Where("b.pen_id IN ?", penIds)
  187. }
  188. if err = pref.Order("a.level DESC").
  189. Count(&count).
  190. Limit(int(pagination.PageSize)).
  191. Offset(int(pagination.PageOffset)).
  192. Find(&neckRingEstrusList).Error; err != nil {
  193. return nil, xerr.WithStack(err)
  194. }
  195. cowMap := make(map[int64]*model.Cow)
  196. eventLogMap := make(map[int64]string)
  197. cowIds := make([]int64, 0)
  198. for _, v := range neckRingEstrusList {
  199. cowIds = append(cowIds, v.CowId)
  200. lastEventLog := s.GetCowLastEvent(userModel.AppPasture.Id, v.CowId, pasturePb.EventCategory_Breed)
  201. if lastEventLog != nil {
  202. eventLogMap[v.CowId] = lastEventLog.EventDescription
  203. }
  204. }
  205. if len(cowIds) > 0 {
  206. cowList, _ := s.GetCowInfoByCowIds(ctx, userModel.AppPasture.Id, cowIds)
  207. for _, cow := range cowList {
  208. cowMap[cow.Id] = cow
  209. }
  210. }
  211. return &pasturePb.EventEstrusResponse{
  212. Code: http.StatusOK,
  213. Msg: "ok",
  214. Data: &pasturePb.EstrusItemsData{
  215. List: model.NeckRingEstrusWarningSlice(neckRingEstrusList).ToPB(cowMap, eventLogMap),
  216. Total: int32(count),
  217. PageSize: pagination.PageSize,
  218. Page: pagination.Page,
  219. },
  220. }, nil
  221. }
  222. // CowDiseaseList 发病牛只清单
  223. func (s *StoreEntry) CowDiseaseList(ctx context.Context, req *pasturePb.SearchEventCowTreatmentRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowDiseaseResponse, error) {
  224. userModel, err := s.GetUserModel(ctx)
  225. if err != nil {
  226. return nil, xerr.WithStack(err)
  227. }
  228. cowDiseaseList := make([]*model.EventCowDisease, 0)
  229. var count int64 = 0
  230. pref := s.DB.Table(fmt.Sprintf("%s as a", new(model.EventCowDisease).TableName())).
  231. Joins(fmt.Sprintf("JOIN %s AS b on a.cow_id = b.id", new(model.Cow).TableName())).
  232. Select("a.*,b.pen_name").
  233. Where("a.pasture_id = ?", userModel.AppPasture.Id).
  234. Where("a.health_status != ?", pasturePb.HealthStatus_Curable)
  235. if len(req.CowIds) > 0 {
  236. pref.Where("a.cow_id IN ?", req.CowIds)
  237. }
  238. if req.EarNumber != "" {
  239. pref.Where("a.ear_number = ?", req.EarNumber)
  240. }
  241. if req.DiseaseId > 0 {
  242. pref.Where("a.disease_id = ?", req.DiseaseId)
  243. }
  244. if req.PenId > 0 {
  245. pref.Where("b.pen_id = ?", req.PenId)
  246. }
  247. if req.HealthStatus > 0 {
  248. pref.Where("a.health_status = ?", req.HealthStatus)
  249. }
  250. if req.DiseasedStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseEndAt >= req.DiseasedStartAt {
  251. pref.Where("a.disease_at BETWEEN ? AND ?", req.DiseasedStartAt, req.DiseaseEndAt)
  252. }
  253. if err = pref.Order("a.id DESC").
  254. Count(&count).Limit(int(pagination.PageSize)).
  255. Offset(int(pagination.PageOffset)).
  256. Find(&cowDiseaseList).Error; err != nil {
  257. return nil, xerr.WithStack(err)
  258. }
  259. healthStatusMap := s.HealthStatusMap()
  260. return &pasturePb.EventCowDiseaseResponse{
  261. Code: http.StatusOK,
  262. Msg: "ok",
  263. Data: &pasturePb.EventCowDiseaseData{
  264. List: model.EventCowDiseaseSlice(cowDiseaseList).ToPB(healthStatusMap),
  265. Total: int32(count),
  266. PageSize: pagination.PageSize,
  267. Page: pagination.Page,
  268. },
  269. }, nil
  270. }
  271. // CowDiseaseDiagnose 发病牛只诊断
  272. func (s *StoreEntry) CowDiseaseDiagnose(ctx context.Context, req *pasturePb.CowDiagnosedRequest) error {
  273. userModel, err := s.GetUserModel(ctx)
  274. if err != nil {
  275. return xerr.WithStack(err)
  276. }
  277. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  278. if err != nil {
  279. return xerr.Customf("错误的牛只信息: %d", req.CowId)
  280. }
  281. eventCowDisease := &model.EventCowDisease{}
  282. if err = s.DB.Model(new(model.EventCowDisease)).
  283. Where("cow_id = ?", req.CowId).
  284. Where("id = ?", req.Id).
  285. Where("health_status = ?", pasturePb.HealthStatus_Health).
  286. Where("pasture_id = ?", userModel.AppPasture.Id).
  287. First(eventCowDisease).Error; err != nil {
  288. zaplog.Error("CowDiseaseDiagnose", zap.Any("req", req), zap.Any("userModel", userModel))
  289. return xerr.Custom("异常牛只数据")
  290. }
  291. if eventCowDisease == nil || eventCowDisease.Id <= 0 {
  292. return xerr.Custom("异常牛只数据")
  293. }
  294. if req.DiagnosedResult == pasturePb.IsShow_No {
  295. // 未发病更新
  296. eventCowDisease.EventUnDiseaseUpdate(userModel.SystemUser, req.Remarks)
  297. if err = s.DB.Model(eventCowDisease).
  298. Select("diagnosed_result", "diagnose_operation_id", "diagnose_operation_name", "remarks").
  299. Where("id = ?", req.Id).
  300. Updates(eventCowDisease).Error; err != nil {
  301. return xerr.WithStack(err)
  302. }
  303. return nil
  304. }
  305. // 已发病
  306. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, req.DiseaseId)
  307. if err != nil {
  308. return xerr.WithStack(err)
  309. }
  310. systemUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  311. if err != nil {
  312. return xerr.WithStack(err)
  313. }
  314. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  315. eventCowDisease.EventDiseaseUpdate(disease, systemUser, req.Temperature)
  316. if err = tx.Model(eventCowDisease).
  317. Select("health_status", "diagnosed_result", "diagnose_id", "diagnose_name", "temperature", "diagnose_operation_id", "diagnose_operation_name", "diagnosed_at").
  318. Where("id = ?", req.Id).
  319. Where("cow_id = ?", req.CowId).
  320. Updates(eventCowDisease).Error; err != nil {
  321. return xerr.WithStack(err)
  322. }
  323. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Disease)
  324. if err = tx.Model(cow).
  325. Select("health_status").
  326. Where("id = ?", cow.Id).
  327. Updates(cow).Error; err != nil {
  328. return xerr.WithStack(err)
  329. }
  330. return nil
  331. }); err != nil {
  332. return xerr.WithStack(err)
  333. }
  334. return nil
  335. }
  336. // CowDiseaseTreatment 发病牛只治疗
  337. func (s *StoreEntry) CowDiseaseTreatment(ctx context.Context, req *pasturePb.CowTreatmentRequest) error {
  338. userModel, err := s.GetUserModel(ctx)
  339. if err != nil {
  340. return xerr.WithStack(err)
  341. }
  342. // 牛只疾病信息
  343. eventCowDisease := &model.EventCowDisease{}
  344. if err = s.DB.Where("cow_id = ?", req.CowId).
  345. Where("pasture_id = ?", userModel.AppPasture.Id).
  346. Where("id = ?", req.Id).
  347. First(eventCowDisease).Error; err != nil {
  348. return xerr.WithStack(err)
  349. }
  350. if eventCowDisease.HealthStatus != pasturePb.HealthStatus_Disease &&
  351. eventCowDisease.HealthStatus != pasturePb.HealthStatus_Treatment {
  352. return xerr.Custom("异常牛只数据")
  353. }
  354. cow, err := s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, int64(req.CowId))
  355. if err != nil {
  356. return xerr.WithStack(err)
  357. }
  358. // 操作人信息
  359. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  360. if err != nil {
  361. return xerr.Customf("操作人数据异常: %d", req.OperationId)
  362. }
  363. // 处方信息
  364. prescription, err := s.GetPrescriptionById(ctx, userModel.AppPasture.Id, req.PrescriptionId)
  365. if err != nil {
  366. return xerr.WithStack(err)
  367. }
  368. // 疾病信息
  369. disease, err := s.GetDiseaseById(ctx, userModel.AppPasture.Id, eventCowDisease.DiseaseId)
  370. if err != nil {
  371. return xerr.WithStack(err)
  372. }
  373. req.DiseaseName = disease.Name
  374. req.DiseaseType = disease.DiseaseType
  375. // 处方信息
  376. prescriptionDrugs, err := s.PrescriptionDrugsByPrescriptionId(ctx, userModel.AppPasture.Id, prescription.Id)
  377. if err != nil {
  378. return xerr.WithStack(err)
  379. }
  380. // 处方详情
  381. unitMap := s.UnitMap()
  382. prescriptionDetail := make([]*pasturePb.PrescriptionDrugsList, len(prescriptionDrugs))
  383. for i, v := range prescriptionDrugs {
  384. prescriptionDetail[i] = &pasturePb.PrescriptionDrugsList{
  385. DrugsId: int32(v.DrugsId),
  386. DrugsName: v.DrugsName,
  387. Unit: v.Unit,
  388. UnitName: unitMap[v.Unit],
  389. Dosages: v.Dosages,
  390. }
  391. }
  392. req.PrescriptionDetail = prescriptionDetail
  393. healthStatus := pasturePb.HealthStatus_Treatment
  394. if req.TreatmentResult == pasturePb.TreatmentResult_Curable {
  395. healthStatus = pasturePb.HealthStatus_Curable
  396. }
  397. if req.TreatmentResult == pasturePb.TreatmentResult_Out {
  398. healthStatus = pasturePb.HealthStatus_Out
  399. }
  400. if req.TreatmentResult == pasturePb.TreatmentResult_Dead {
  401. healthStatus = pasturePb.HealthStatus_Dead
  402. }
  403. diseaseTypeMap := s.DiseaseTypeMap()
  404. newEventCowTreatment := model.NewEventCowTreatment(userModel.AppPasture.Id, prescription, req, diseaseTypeMap, operationUser, userModel.SystemUser)
  405. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  406. if err = tx.Create(newEventCowTreatment).Error; err != nil {
  407. return xerr.WithStack(err)
  408. }
  409. eventCowDisease.EventTreatmentUpdate(healthStatus, int64(req.TreatmentAt), prescription.Name)
  410. if err = tx.Model(eventCowDisease).
  411. Select("health_status", "first_treatment_at", "last_treatment_at", "last_prescription_name").
  412. Where("id = ?", req.Id).
  413. Where("cow_id = ?", req.CowId).
  414. Updates(eventCowDisease).Error; err != nil {
  415. return xerr.WithStack(err)
  416. }
  417. cow.EventHealthStatusUpdate(healthStatus)
  418. if err = tx.Model(new(model.Cow)).
  419. Select("health_status").
  420. Where("id = ?", req.CowId).
  421. Updates(cow).Error; err != nil {
  422. return xerr.WithStack(err)
  423. }
  424. prescription.EventUseCountUpdate()
  425. if err = tx.Model(prescription).
  426. Select("use_count").
  427. Where("id = ?", prescription.Id).
  428. Updates(prescription).Error; err != nil {
  429. return xerr.WithStack(err)
  430. }
  431. return nil
  432. }); err != nil {
  433. return xerr.WithStack(err)
  434. }
  435. return nil
  436. }
  437. func (s *StoreEntry) DiseaseSuggestPrescription(ctx context.Context, diseaseId int64) (*pasturePb.ConfigOptionsListResponse, error) {
  438. userModel, err := s.GetUserModel(ctx)
  439. if err != nil {
  440. return nil, xerr.WithStack(err)
  441. }
  442. res := make([]*pasturePb.ConfigOptionsList, 0)
  443. prescriptionList := make([]*model.Prescription, 0)
  444. if err = s.DB.Model(new(model.Prescription)).
  445. Where("is_show = ?", pasturePb.IsShow_Ok).
  446. Where("pasture_id = ?", userModel.AppPasture.Id).
  447. Find(&prescriptionList).Error; err != nil {
  448. return nil, xerr.WithStack(err)
  449. }
  450. for _, v := range prescriptionList {
  451. disabled := false
  452. if strings.Contains(v.ApplicableDisease, fmt.Sprintf("%d", diseaseId)) {
  453. disabled = true
  454. }
  455. res = append(res, &pasturePb.ConfigOptionsList{
  456. Value: v.Id,
  457. Label: v.Name,
  458. Disabled: disabled,
  459. })
  460. }
  461. return &pasturePb.ConfigOptionsListResponse{
  462. Code: http.StatusOK,
  463. Msg: "ok",
  464. Data: res,
  465. }, nil
  466. }
  467. // CowDiseaseTreatmentDetail 发病牛只治疗详情列表
  468. func (s *StoreEntry) CowDiseaseTreatmentDetail(ctx context.Context, req *pasturePb.EventCowTreatmentDetailRequest, pagination *pasturePb.PaginationModel) (*pasturePb.EventCowTreatmentDetailResponse, error) {
  469. userModel, err := s.GetUserModel(ctx)
  470. if err != nil {
  471. return nil, xerr.WithStack(err)
  472. }
  473. eventCowDisease := &model.EventCowDisease{}
  474. var count int64 = 0
  475. pref := s.DB.Model(new(model.EventCowDisease)).
  476. Where("cow_id = ?", req.CowId).
  477. Where("pasture_id = ?", userModel.AppPasture.Id).
  478. Where("id = ?", req.Id)
  479. if req.DiseaseId > 0 {
  480. pref.Where("disease_id = ?", req.DiseaseId)
  481. }
  482. if req.DiseaseStartAt > 0 && req.DiseaseEndAt > 0 && req.DiseaseStartAt <= req.DiseaseEndAt {
  483. pref.Where("disease_at BETWEEN ? AND ?", req.DiseaseStartAt, req.DiseaseEndAt)
  484. }
  485. if err = pref.Count(&count).
  486. Limit(int(pagination.PageSize)).
  487. Offset(int(pagination.PageOffset)).
  488. Order("id desc").
  489. First(&eventCowDisease).Error; err != nil {
  490. return nil, xerr.WithStack(err)
  491. }
  492. eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
  493. if err = s.DB.Model(new(model.EventCowTreatment)).
  494. Where("cow_disease_id = ?", req.Id).
  495. Where("cow_id = ?", req.CowId).
  496. Order("id desc").
  497. Find(&eventCowTreatmentList).Error; err != nil {
  498. return nil, xerr.WithStack(err)
  499. }
  500. return &pasturePb.EventCowTreatmentDetailResponse{
  501. Code: http.StatusOK,
  502. Msg: "ok",
  503. Data: &pasturePb.EventCowTreatmentDetail{
  504. List: model.EventCowTreatmentSlice(eventCowTreatmentList).ToPB(eventCowDisease),
  505. Total: int32(count),
  506. PageSize: pagination.Page,
  507. Page: pagination.PageSize,
  508. },
  509. }, nil
  510. }
  511. func (s *StoreEntry) CowDiseaseCurable(ctx context.Context, req *pasturePb.EventCowCurableRequest) error {
  512. userModel, err := s.GetUserModel(ctx)
  513. if err != nil {
  514. return xerr.WithStack(err)
  515. }
  516. eventCowDiseaseList := make([]*model.EventCowDisease, 0)
  517. if err = s.DB.Where("id IN ?", req.Ids).
  518. Where("health_status = ?", pasturePb.HealthStatus_Treatment).
  519. Where("pasture_id = ?", userModel.AppPasture.Id).
  520. Find(&eventCowDiseaseList).Error; err != nil {
  521. zaplog.Error("GetEventCowDiseaseList", zap.Any("err", err), zap.Any("req", req))
  522. return xerr.Custom("异常数据")
  523. }
  524. if len(eventCowDiseaseList) == 0 {
  525. return nil
  526. }
  527. operationUser, err := s.GetSystemUserById(ctx, int64(req.OperationId))
  528. if err != nil {
  529. return xerr.Customf("该用户不存在: %d", req.OperationId)
  530. }
  531. eventCowTreatmentList := make([]*model.EventCowTreatment, 0)
  532. for _, v := range eventCowDiseaseList {
  533. newEventCowTreatment := model.NewEventCowCurableTreatment(userModel.AppPasture.Id, userModel.SystemUser, operationUser, v, req.Remarks, int64(req.CurableAt))
  534. eventCowTreatmentList = append(eventCowTreatmentList, newEventCowTreatment)
  535. }
  536. if len(eventCowTreatmentList) <= 0 {
  537. return nil
  538. }
  539. if err = s.DB.Transaction(func(tx *gorm.DB) error {
  540. cow := &model.Cow{}
  541. for _, eventCowDisease := range eventCowDiseaseList {
  542. eventCowDisease.EventCurableUpdate(int64(req.CurableAt))
  543. if err = tx.Model(eventCowDisease).
  544. Select("health_status", "diagnosed_result", "curable_at").
  545. Where("id = ?", eventCowDisease.Id).
  546. Where("health_status = ?", pasturePb.HealthStatus_Treatment).
  547. Updates(eventCowDisease).Error; err != nil {
  548. return xerr.WithStack(err)
  549. }
  550. cow, err = s.GetCowInfoByCowId(ctx, userModel.AppPasture.Id, eventCowDisease.CowId)
  551. if err != nil {
  552. return xerr.WithStack(err)
  553. }
  554. // 更新牛只健康状态
  555. cow.EventHealthStatusUpdate(pasturePb.HealthStatus_Curable)
  556. if err = tx.Model(cow).
  557. Select("health_status").
  558. Where("id = ?", eventCowDisease.CowId).
  559. Updates(cow).Error; err != nil {
  560. return xerr.WithStack(err)
  561. }
  562. }
  563. if err = tx.Model(new(model.EventCowTreatment)).Create(eventCowTreatmentList).Error; err != nil {
  564. return xerr.WithStack(err)
  565. }
  566. return nil
  567. }); err != nil {
  568. return xerr.WithStack(err)
  569. }
  570. return nil
  571. }