cow_indicators_breed_more.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package crontab
  2. import (
  3. "fmt"
  4. "kpt-pasture/model"
  5. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  6. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  7. "go.uber.org/zap"
  8. )
  9. // AdultCowsPregnancyRate 成牛受胎率
  10. func (e *Entry) AdultCowsPregnancyRate(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  11. res := make(map[int64]string)
  12. for _, pasture := range pastureList {
  13. var (
  14. pregnantCount int64 // 确认怀孕的
  15. realityBreedCount int64 // 参与配种的
  16. )
  17. if err := e.DB.Model(new(model.EventMating)).
  18. Where("pasture_id = ?", pasture.Id).
  19. Where("status = ?", pasturePb.IsShow_Ok).
  20. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  21. Where("mating_result_at BETWEEN ? AND ?", startTime, endTime).
  22. Count(&pregnantCount).Error; err != nil {
  23. zaplog.Error("pregnantCount", zap.Any("err", err))
  24. }
  25. if err := e.DB.Model(new(model.EventMating)).
  26. Where("pasture_id = ?", pasture.Id).
  27. Where("status = ?", pasturePb.IsShow_Ok).
  28. Where("reality_day BETWEEN ? AND ?", startTime, endTime).
  29. Count(&realityBreedCount).Error; err != nil {
  30. zaplog.Error("realityBreedCount", zap.Any("err", err))
  31. }
  32. if realityBreedCount > 0 && pregnantCount > 0 {
  33. res[pasture.Id] = fmt.Sprintf("%.2f", float64(pregnantCount)/float64(realityBreedCount))
  34. } else {
  35. res[pasture.Id] = "0"
  36. }
  37. }
  38. return res
  39. }
  40. // LactPregnancyRate 母牛不同胎次受胎率
  41. func (e *Entry) LactPregnancyRate(pastureList []*model.AppPastureList, condition string, startTime, endTime int64) map[int64]string {
  42. res := make(map[int64]string)
  43. for _, pasture := range pastureList {
  44. var (
  45. pregnantCount int64 // 确认怀孕的
  46. realityBreedCount int64 // 参与配种的
  47. )
  48. if err := e.DB.Model(new(model.EventMating)).
  49. Where("pasture_id = ?", pasture.Id).
  50. Where("status = ?", pasturePb.IsShow_Ok).
  51. Where(condition).
  52. Where("mating_result = ?", pasturePb.MatingResult_Pregnant).
  53. Where("mating_result_at BETWEEN ? AND ?", startTime, endTime).
  54. Count(&pregnantCount).Error; err != nil {
  55. zaplog.Error("pregnantCount", zap.Any("err", err))
  56. }
  57. if err := e.DB.Model(new(model.EventMating)).
  58. Where("pasture_id = ?", pasture.Id).
  59. Where("status = ?", pasturePb.IsShow_Ok).
  60. Where(condition).
  61. Where("reality_day BETWEEN ? AND ?", startTime, endTime).
  62. Count(&realityBreedCount).Error; err != nil {
  63. zaplog.Error("realityBreedCount", zap.Any("err", err))
  64. }
  65. if realityBreedCount > 0 && pregnantCount > 0 {
  66. res[pasture.Id] = fmt.Sprintf("%.2f", float64(pregnantCount)/float64(realityBreedCount))
  67. } else {
  68. res[pasture.Id] = "0"
  69. }
  70. }
  71. return res
  72. }