cow_indicators_base.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package crontab
  2. import (
  3. "errors"
  4. "fmt"
  5. "kpt-pasture/model"
  6. "time"
  7. "gorm.io/gorm"
  8. pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
  9. "gitee.com/xuyiping_admin/pkg/logger/zaplog"
  10. "go.uber.org/zap"
  11. )
  12. // FindPastureAllCow 查询所有牧场牛只总数
  13. func (e *Entry) FindPastureAllCow(pastureList []*model.AppPastureList) map[int64]string {
  14. res := make(map[int64]string)
  15. for _, pasture := range pastureList {
  16. var count int64
  17. if err := e.DB.Model(&model.Cow{}).
  18. Where("pasture_id = ?", pasture.Id).
  19. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  20. Count(&count).Error; err != nil {
  21. zaplog.Error("FindAllCow", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  22. }
  23. res[pasture.Id] = fmt.Sprintf("%d", count)
  24. }
  25. return res
  26. }
  27. // FindCalvingInterval 产犊间隔
  28. func (e *Entry) FindCalvingInterval(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  29. res := make(map[int64]string)
  30. for _, pasture := range pastureList {
  31. eventCalvingList := make([]*model.EventCalving, 0)
  32. if err := e.DB.Model(new(model.EventCalving)).
  33. Where("pasture_id = ?", pasture.Id).
  34. Where("status = ?", pasturePb.IsShow_Ok).
  35. Where("reality_day BETWEEN ? AND ?", startTime, endTime).
  36. Find(&eventCalvingList).Error; err != nil {
  37. zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  38. }
  39. cowInterval := make(map[int64]int64)
  40. allInterval := int64(0)
  41. for _, v := range eventCalvingList {
  42. preEventCalving := &model.EventCalving{}
  43. if err := e.DB.Model(new(model.EventCalving)).
  44. Where("pasture_id = ?", pasture.Id).
  45. Where("cow_id = ?", v.CowId).
  46. Where("status = ?", pasturePb.IsShow_Ok).
  47. Where("reality_day < ?", v.RealityDay).
  48. Order("reality_day DESC").
  49. First(&preEventCalving).Error; err != nil {
  50. if errors.Is(err, gorm.ErrRecordNotFound) {
  51. continue
  52. } else {
  53. zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  54. }
  55. }
  56. if preEventCalving.RealityDay > 0 {
  57. interval := v.RealityDay - preEventCalving.RealityDay
  58. cowInterval[v.CowId] = interval
  59. allInterval += interval
  60. }
  61. }
  62. if len(cowInterval) > 0 {
  63. res[pasture.Id] = fmt.Sprintf("%d", int32(allInterval/int64(len(cowInterval))))
  64. }
  65. }
  66. return res
  67. }
  68. // FindOutputNumber 本月销售牛只
  69. func (e *Entry) FindOutputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  70. res := make(map[int64]string)
  71. for _, pasture := range pastureList {
  72. var count int64
  73. if err := e.DB.Model(new(model.EventSale)).
  74. Where("pasture_id = ?", pasture.Id).
  75. Where("sale_at BETWEEN ? AND ?", startTime, endTime).
  76. Count(&count).Error; err != nil {
  77. zaplog.Error("FindOutputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  78. }
  79. res[pasture.Id] = fmt.Sprintf("%d", count)
  80. }
  81. return res
  82. }
  83. func (e *Entry) FindInputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  84. res := make(map[int64]string)
  85. for _, pasture := range pastureList {
  86. var count int64
  87. if err := e.DB.Model(new(model.EventEnter)).
  88. Where("pasture_id = ?", pasture.Id).
  89. Where("cow_source = ?", pasturePb.CowSource_Buy).
  90. Where("enter_at BETWEEN ? AND ?", startTime, endTime).
  91. Count(&count).Error; err != nil {
  92. zaplog.Error("FindInputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  93. }
  94. res[pasture.Id] = fmt.Sprintf("%d", count)
  95. }
  96. return res
  97. }
  98. func (e *Entry) FindSalesVolume(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  99. res := make(map[int64]string)
  100. for _, pasture := range pastureList {
  101. saleAllAmount := struct {
  102. SaleAllAmount int64 `json:"sale_all_amount"`
  103. }{
  104. SaleAllAmount: 0,
  105. }
  106. if err := e.DB.Model(new(model.EventSale)).
  107. Select("SUM(sale_all_amount) as sale_all_amount").
  108. Where("pasture_id = ?", pasture.Id).
  109. Where("sale_at BETWEEN ? AND ?", startTime, endTime).
  110. First(&saleAllAmount).Error; err != nil {
  111. zaplog.Error("FindSalesVolume", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  112. }
  113. res[pasture.Id] = fmt.Sprintf("%d", saleAllAmount.SaleAllAmount)
  114. }
  115. return res
  116. }
  117. func (e *Entry) UpdatePastureIndicators(pastureId int64, indicatorsDetails *model.IndicatorsDetails, dateTime int64, value string) {
  118. date := time.Unix(dateTime, 0).Format(model.LayoutMonth)
  119. where := &model.IndicatorsData{
  120. PastureId: pastureId,
  121. Date: date,
  122. Kind: indicatorsDetails.Kind,
  123. }
  124. data := &model.IndicatorsData{
  125. PastureId: pastureId,
  126. CategoryType: indicatorsDetails.CategoryType,
  127. CategoryName: indicatorsDetails.CategoryName,
  128. Date: date,
  129. Kind: indicatorsDetails.Kind,
  130. Value: value,
  131. }
  132. var existData model.IndicatorsData
  133. if err := e.DB.Model(new(model.IndicatorsData)).
  134. Where(where).First(&existData).Error; err != nil {
  135. if errors.Is(err, gorm.ErrRecordNotFound) {
  136. if err = e.DB.Model(new(model.IndicatorsData)).Create(data).Error; err != nil {
  137. zaplog.Error("UpdatePastureIndicators", zap.Any("Create", err))
  138. }
  139. } else {
  140. zaplog.Error("UpdatePastureIndicators", zap.Any("Find", err))
  141. return
  142. }
  143. }
  144. if err := e.DB.Model(new(model.IndicatorsData)).
  145. Where("id = ?", existData.Id).
  146. Update("value", value).Error; err != nil {
  147. zaplog.Error("UpdatePastureIndicators", zap.Any("Update", err))
  148. }
  149. }