cow_indicators_base.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(new(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. func (e *Entry) FindPastureAdultCow(pastureList []*model.AppPastureList) map[int64]string {
  28. res := make(map[int64]string)
  29. for _, pasture := range pastureList {
  30. var count int64
  31. if err := e.DB.Model(new(model.Cow)).
  32. Where("pasture_id = ?", pasture.Id).
  33. Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
  34. Where("lact > ?", 0).
  35. Count(&count).Error; err != nil {
  36. zaplog.Error("FindPastureAdultCow", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  37. }
  38. res[pasture.Id] = fmt.Sprintf("%d", count)
  39. }
  40. return res
  41. }
  42. // FindCalvingInterval 产犊间隔
  43. func (e *Entry) FindCalvingInterval(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  44. res := make(map[int64]string)
  45. for _, pasture := range pastureList {
  46. eventCalvingList := make([]*model.EventCalving, 0)
  47. if err := e.DB.Model(new(model.EventCalving)).
  48. Where("pasture_id = ?", pasture.Id).
  49. Where("status = ?", pasturePb.IsShow_Ok).
  50. Where("reality_day BETWEEN ? AND ?", startTime, endTime).
  51. Find(&eventCalvingList).Error; err != nil {
  52. zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  53. }
  54. cowInterval := make(map[int64]int64)
  55. allInterval := int64(0)
  56. for _, v := range eventCalvingList {
  57. preEventCalving := &model.EventCalving{}
  58. if err := e.DB.Model(new(model.EventCalving)).
  59. Where("pasture_id = ?", pasture.Id).
  60. Where("cow_id = ?", v.CowId).
  61. Where("status = ?", pasturePb.IsShow_Ok).
  62. Where("reality_day < ?", v.RealityDay).
  63. Order("reality_day DESC").
  64. First(&preEventCalving).Error; err != nil {
  65. if errors.Is(err, gorm.ErrRecordNotFound) {
  66. continue
  67. } else {
  68. zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  69. }
  70. }
  71. if preEventCalving.RealityDay > 0 {
  72. interval := v.RealityDay - preEventCalving.RealityDay
  73. cowInterval[v.CowId] = interval
  74. allInterval += interval
  75. }
  76. }
  77. if len(cowInterval) > 0 {
  78. res[pasture.Id] = fmt.Sprintf("%d", int32(allInterval/int64(len(cowInterval))))
  79. }
  80. }
  81. return res
  82. }
  83. // FindOutputNumber 本月销售牛只
  84. func (e *Entry) FindOutputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  85. res := make(map[int64]string)
  86. for _, pasture := range pastureList {
  87. var count int64
  88. if err := e.DB.Model(new(model.EventSale)).
  89. Where("pasture_id = ?", pasture.Id).
  90. Where("sale_at BETWEEN ? AND ?", startTime, endTime).
  91. Count(&count).Error; err != nil {
  92. zaplog.Error("FindOutputNumber", 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) FindInputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  99. res := make(map[int64]string)
  100. for _, pasture := range pastureList {
  101. var count int64
  102. if err := e.DB.Model(new(model.EventEnter)).
  103. Where("pasture_id = ?", pasture.Id).
  104. Where("cow_source = ?", pasturePb.CowSource_Buy).
  105. Where("enter_at BETWEEN ? AND ?", startTime, endTime).
  106. Count(&count).Error; err != nil {
  107. zaplog.Error("FindInputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  108. }
  109. res[pasture.Id] = fmt.Sprintf("%d", count)
  110. }
  111. return res
  112. }
  113. func (e *Entry) FindSalesVolume(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
  114. res := make(map[int64]string)
  115. for _, pasture := range pastureList {
  116. saleAllAmount := struct {
  117. SaleAllAmount int64 `json:"sale_all_amount"`
  118. }{
  119. SaleAllAmount: 0,
  120. }
  121. if err := e.DB.Model(new(model.EventSale)).
  122. Select("SUM(sale_all_amount) as sale_all_amount").
  123. Where("pasture_id = ?", pasture.Id).
  124. Where("sale_at BETWEEN ? AND ?", startTime, endTime).
  125. First(&saleAllAmount).Error; err != nil {
  126. zaplog.Error("FindSalesVolume", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
  127. }
  128. res[pasture.Id] = fmt.Sprintf("%d", saleAllAmount.SaleAllAmount)
  129. }
  130. return res
  131. }
  132. func (e *Entry) UpdatePastureIndicators(pastureId int64, indicatorsDetails *model.IndicatorsDetails, dateTime int64, value string) {
  133. date := time.Unix(dateTime, 0).Format(model.LayoutMonth)
  134. where := &model.IndicatorsData{
  135. PastureId: pastureId,
  136. Date: date,
  137. Kind: indicatorsDetails.Kind,
  138. }
  139. data := &model.IndicatorsData{
  140. PastureId: pastureId,
  141. CategoryType: indicatorsDetails.CategoryType,
  142. CategoryName: indicatorsDetails.CategoryName,
  143. Date: date,
  144. Kind: indicatorsDetails.Kind,
  145. Value: value,
  146. }
  147. var existData model.IndicatorsData
  148. if err := e.DB.Model(new(model.IndicatorsData)).
  149. Where(where).First(&existData).Error; err != nil {
  150. if errors.Is(err, gorm.ErrRecordNotFound) {
  151. if err = e.DB.Model(new(model.IndicatorsData)).Create(data).Error; err != nil {
  152. zaplog.Error("UpdatePastureIndicators", zap.Any("Create", err))
  153. }
  154. } else {
  155. zaplog.Error("UpdatePastureIndicators", zap.Any("Find", err))
  156. return
  157. }
  158. }
  159. if err := e.DB.Model(new(model.IndicatorsData)).
  160. Where("id = ?", existData.Id).
  161. Update("value", value).Error; err != nil {
  162. zaplog.Error("UpdatePastureIndicators", zap.Any("Update", err))
  163. }
  164. }