analysis_other.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package backend
  2. import (
  3. "context"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "kpt-pasture/util"
  7. "net/http"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. "gitee.com/xuyiping_admin/pkg/xerr"
  10. )
  11. // PregnancyReport 孕检报告
  12. func (s *StoreEntry) PregnancyReport(ctx context.Context, req *pasturePb.PregnancyReportRequest, pagination *pasturePb.PaginationModel) (*pasturePb.PregnancyReportResponse, error) {
  13. startDayUnix := util.TimeParseLocalUnix(req.StartDayTime)
  14. endDayUnix := util.TimeParseLocalEndUnix(req.EndDayTime)
  15. if startDayUnix > endDayUnix || startDayUnix == 0 || endDayUnix == 0 {
  16. return nil, xerr.Custom("开始时间不能大于结束时间")
  17. }
  18. eventPregnantCheckList := make([]*model.EventPregnantCheck, 0)
  19. pref := s.DB.Model(new(model.EventPregnantCheck)).Where("status = ?", pasturePb.IsShow_Ok).Where("cow_type = ?", req.CowType)
  20. if startDayUnix > 0 && endDayUnix > 0 && endDayUnix >= startDayUnix {
  21. pref = pref.Where("create_time BETWEEN ? AND ?", startDayUnix, endDayUnix)
  22. }
  23. if req.PregnantCheckResult > 0 {
  24. pref = pref.Where("pregnant_check_result = ?", req.PregnantCheckResult)
  25. }
  26. var count int64 = 0
  27. if err := pref.Count(&count).
  28. Limit(int(pagination.PageSize)).
  29. Offset(int(pagination.PageOffset)).
  30. Order("id desc").
  31. Find(&eventPregnantCheckList).Error; err != nil {
  32. return nil, xerr.WithStack(err)
  33. }
  34. pregnantCheckResultMap := s.PregnantCheckResultMap()
  35. pregnantCheckMethodMap := s.PregnantCheckMethodMap()
  36. return &pasturePb.PregnancyReportResponse{
  37. Code: http.StatusOK,
  38. Message: "ok",
  39. Data: &pasturePb.PregnancyReportData{
  40. List: model.EventPregnantCheckSlice(eventPregnantCheckList).ToPB3(pregnantCheckResultMap, pregnantCheckMethodMap),
  41. Total: int32(count),
  42. PageSize: pagination.PageSize,
  43. Page: pagination.Page,
  44. },
  45. }, nil
  46. }
  47. // CalvingReport 产犊报告
  48. func (s *StoreEntry) CalvingReport(ctx context.Context, req *pasturePb.CalvingReportRequest) (*pasturePb.CalvingReportResponse, error) {
  49. lastDayOfMonth, err := util.GetLastDayOfMonth(req.EndDayTime)
  50. if err != nil {
  51. return nil, xerr.WithStack(err)
  52. }
  53. endDayTimeUnix := util.TimeParseLocalEndUnix(lastDayOfMonth)
  54. startDayTimeUnix := util.TimeParseLocalUnix(fmt.Sprintf("%s-01", req.StartDayTime))
  55. if startDayTimeUnix == 0 || endDayTimeUnix == 0 || endDayTimeUnix <= startDayTimeUnix {
  56. return nil, xerr.Custom("开始时间不能大于结束时间")
  57. }
  58. eventCalving1 := make([]*pasturePb.CalvingReportTable, 0)
  59. pref1 := s.DB.Model(new(model.EventCalving)).
  60. Select(fmt.Sprint(`DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m') AS statistic_method,cow_kind,`)+fmt.Sprintf(
  61. `SUM(child_number) AS totalCount,
  62. SUM(CASE WHEN child_number > 1 THEN child_number ELSE 0 END) AS twins,
  63. SUM(CASE WHEN pregnancy_age <= %d THEN 1 ELSE 0 END) AS premature_labor_count,
  64. SUM(CASE WHEN pregnancy_age >= %d THEN 1 ELSE 0 END) AS late_labor_count,
  65. count(*) as total_count`, req.PrematureLabor, req.LateLabor),
  66. ).Where("reality_day BETWEEN ? AND ? ", startDayTimeUnix, endDayTimeUnix)
  67. if req.AnalysisMethod > 0 {
  68. switch req.AnalysisMethod {
  69. case pasturePb.CalvingAnalysisMethod_months:
  70. pref1.Group("statistic_method").Order("statistic_method")
  71. case pasturePb.CalvingAnalysisMethod_cowKind:
  72. pref1.Group("cow_kind").Order("cow_kind")
  73. }
  74. }
  75. if err = pref1.Find(&eventCalving1).Error; err != nil {
  76. return nil, xerr.WithStack(err)
  77. }
  78. eventCalving2 := make([]*pasturePb.CalvingReportTable, 0)
  79. pref2 := s.DB.Model(new(model.CalvingCalf)).
  80. Select(fmt.Sprint(`DATE_FORMAT(FROM_UNIXTIME(birth_at), '%Y-%m') AS statistic_method, cow_kind,
  81. SUM(CASE WHEN sex = 1 THEN 1 ELSE 0 END) AS bulls,
  82. SUM(CASE WHEN sex = 2 THEN 1 ELSE 0 END) AS cows,
  83. SUM(CASE WHEN is_live = 1 THEN 1 ELSE 0 END) AS survive_count,
  84. SUM(CASE WHEN is_live = 2 THEN 1 ELSE 0 END) AS die_count,
  85. SUM(CASE WHEN is_live = 2 AND sex = 1 THEN 1 ELSE 0 END) AS bulls_die_count,
  86. SUM(CASE WHEN is_live = 2 AND sex = 2 THEN 1 ELSE 0 END) AS cows_die_count,
  87. SUM(CASE WHEN is_adoption = 1 THEN 1 ELSE 0 END) AS adopt_count`)).
  88. Where("birth_at BETWEEN ? AND ?", startDayTimeUnix, endDayTimeUnix)
  89. if req.AnalysisMethod > 0 {
  90. switch req.AnalysisMethod {
  91. case pasturePb.CalvingAnalysisMethod_months:
  92. pref2.Group("statistic_method").Order("statistic_method")
  93. case pasturePb.CalvingAnalysisMethod_cowKind:
  94. pref2.Group("cow_kind").Order("cow_kind")
  95. }
  96. }
  97. if err = pref2.Find(&eventCalving2).Error; err != nil {
  98. return nil, xerr.WithStack(err)
  99. }
  100. // 合并数据集
  101. for _, v1 := range eventCalving1 {
  102. for _, v2 := range eventCalving2 {
  103. if v1.StatisticMethod != v2.StatisticMethod && v1.CowKind != v2.CowKind {
  104. continue
  105. }
  106. v1.Bulls = v2.Bulls
  107. v1.Cows = v2.Cows
  108. v1.SurviveCount = v2.SurviveCount
  109. v1.DieCount = v2.DieCount
  110. v1.BullsDieCount = v2.BullsDieCount
  111. v1.CowsDieCount = v2.CowsDieCount
  112. v1.AdoptCount = v2.AdoptCount
  113. }
  114. }
  115. cowKindMap := s.CowKindMap()
  116. for _, v := range eventCalving1 {
  117. if req.AnalysisMethod == pasturePb.CalvingAnalysisMethod_cowKind {
  118. v.StatisticMethod = cowKindMap[v.CowKind]
  119. }
  120. if v.TotalCount > 0 {
  121. v.TwinsRate = float32(util.RoundToTwoDecimals(float64(v.Twins) / float64(v.TotalCount) * 100))
  122. v.BullsDieRate = float32(util.RoundToTwoDecimals(float64(v.Bulls) / float64(v.TotalCount) * 100))
  123. v.CowsRate = float32(util.RoundToTwoDecimals(float64(v.Cows) / float64(v.TotalCount) * 100))
  124. v.DieRate = float32(util.RoundToTwoDecimals(float64(v.DieCount) / float64(v.TotalCount) * 100))
  125. }
  126. if v.Bulls > 0 {
  127. v.BullsDieRate = float32(util.RoundToTwoDecimals(float64(v.BullsDieCount) / float64(v.Bulls) * 100))
  128. }
  129. if v.Cows > 0 {
  130. v.CowsDieRate = float32(util.RoundToTwoDecimals(float64(v.CowsDieCount) / float64(v.Cows) * 100))
  131. }
  132. v.NormalLaborCount = v.TotalCount - (v.PrematureLaborCount + v.LateLaborCount)
  133. if v.NormalLaborCount <= 0 {
  134. v.NormalLaborCount = 0
  135. }
  136. }
  137. return &pasturePb.CalvingReportResponse{
  138. Code: http.StatusOK,
  139. Message: "ok",
  140. Data: &pasturePb.CalvingReportData{
  141. List: eventCalving1,
  142. Total: int32(len(eventCalving1)),
  143. },
  144. }, nil
  145. }