|
@@ -0,0 +1,161 @@
|
|
|
+package crontab
|
|
|
+
|
|
|
+import (
|
|
|
+ "errors"
|
|
|
+ "fmt"
|
|
|
+ "kpt-pasture/model"
|
|
|
+ "time"
|
|
|
+
|
|
|
+ "gorm.io/gorm"
|
|
|
+
|
|
|
+ pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
+
|
|
|
+ "gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
|
+ "go.uber.org/zap"
|
|
|
+)
|
|
|
+
|
|
|
+// FindPastureAllCow 查询所有牧场牛只总数
|
|
|
+func (e *Entry) FindPastureAllCow(pastureList []*model.AppPastureList) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ var count int64
|
|
|
+ if err := e.DB.Model(&model.Cow{}).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
+ Count(&count).Error; err != nil {
|
|
|
+ zaplog.Error("FindAllCow", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", count)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+// FindCalvingInterval 产犊间隔
|
|
|
+func (e *Entry) FindCalvingInterval(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ eventCalvingList := make([]*model.EventCalving, 0)
|
|
|
+ if err := e.DB.Model(new(model.EventCalving)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("status = ?", pasturePb.IsShow_Ok).
|
|
|
+ Where("reality_day BETWEEN ? AND ?", startTime, endTime).
|
|
|
+ Find(&eventCalvingList).Error; err != nil {
|
|
|
+ zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+ cowInterval := make(map[int64]int64)
|
|
|
+ allInterval := int64(0)
|
|
|
+ for _, v := range eventCalvingList {
|
|
|
+ preEventCalving := &model.EventCalving{}
|
|
|
+ if err := e.DB.Model(new(model.EventCalving)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("cow_id = ?", v.CowId).
|
|
|
+ Where("status = ?", pasturePb.IsShow_Ok).
|
|
|
+ Where("reality_day < ?", v.RealityDay).
|
|
|
+ Order("reality_day DESC").
|
|
|
+ First(&preEventCalving).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ continue
|
|
|
+ } else {
|
|
|
+ zaplog.Error("FindCalvingInterval", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if preEventCalving.RealityDay > 0 {
|
|
|
+ interval := v.RealityDay - preEventCalving.RealityDay
|
|
|
+ cowInterval[v.CowId] = interval
|
|
|
+ allInterval += interval
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if len(cowInterval) > 0 {
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", int32(allInterval/int64(len(cowInterval))))
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+// FindOutputNumber 本月销售牛只
|
|
|
+func (e *Entry) FindOutputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ var count int64
|
|
|
+ if err := e.DB.Model(new(model.EventSale)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("sale_at BETWEEN ? AND ?", startTime, endTime).
|
|
|
+ Count(&count).Error; err != nil {
|
|
|
+ zaplog.Error("FindOutputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", count)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) FindInputNumber(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ var count int64
|
|
|
+ if err := e.DB.Model(new(model.EventEnter)).
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("cow_source = ?", pasturePb.CowSource_Buy).
|
|
|
+ Where("enter_at BETWEEN ? AND ?", startTime, endTime).
|
|
|
+ Count(&count).Error; err != nil {
|
|
|
+ zaplog.Error("FindInputNumber", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", count)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) FindSalesVolume(pastureList []*model.AppPastureList, startTime, endTime int64) map[int64]string {
|
|
|
+ res := make(map[int64]string)
|
|
|
+ for _, pasture := range pastureList {
|
|
|
+ saleAllAmount := struct {
|
|
|
+ SaleAllAmount int64 `json:"sale_all_amount"`
|
|
|
+ }{
|
|
|
+ SaleAllAmount: 0,
|
|
|
+ }
|
|
|
+ if err := e.DB.Model(new(model.EventSale)).
|
|
|
+ Select("SUM(sale_all_amount) as sale_all_amount").
|
|
|
+ Where("pasture_id = ?", pasture.Id).
|
|
|
+ Where("sale_at BETWEEN ? AND ?", startTime, endTime).
|
|
|
+ First(&saleAllAmount).Error; err != nil {
|
|
|
+ zaplog.Error("FindSalesVolume", zap.Any("pasture_id", pasture.Id), zap.Any("err", err))
|
|
|
+ }
|
|
|
+ res[pasture.Id] = fmt.Sprintf("%d", saleAllAmount.SaleAllAmount)
|
|
|
+ }
|
|
|
+ return res
|
|
|
+}
|
|
|
+
|
|
|
+func (e *Entry) UpdatePastureIndicators(pastureId int64, indicatorsDetails *model.IndicatorsDetails, dateTime int64, value string) {
|
|
|
+ date := time.Unix(dateTime, 0).Format(model.LayoutDate2)
|
|
|
+ where := &model.IndicatorsData{
|
|
|
+ PastureId: pastureId,
|
|
|
+ Date: date,
|
|
|
+ Kind: indicatorsDetails.Kind,
|
|
|
+ }
|
|
|
+ data := &model.IndicatorsData{
|
|
|
+ PastureId: pastureId,
|
|
|
+ CategoryType: indicatorsDetails.CategoryType,
|
|
|
+ CategoryName: indicatorsDetails.CategoryName,
|
|
|
+ Date: date,
|
|
|
+ Kind: indicatorsDetails.Kind,
|
|
|
+ Value: value,
|
|
|
+ }
|
|
|
+
|
|
|
+ var existData model.IndicatorsData
|
|
|
+ if err := e.DB.Model(new(model.IndicatorsData)).
|
|
|
+ Where(where).First(&existData).Error; err != nil {
|
|
|
+ if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
+ if err = e.DB.Model(new(model.IndicatorsData)).Create(data).Error; err != nil {
|
|
|
+ zaplog.Error("UpdatePastureIndicators", zap.Any("Create", err))
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ zaplog.Error("UpdatePastureIndicators", zap.Any("Find", err))
|
|
|
+ return
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := e.DB.Model(new(model.IndicatorsData)).
|
|
|
+ Where("id = ?", existData.Id).
|
|
|
+ Update("value", value).Error; err != nil {
|
|
|
+ zaplog.Error("UpdatePastureIndicators", zap.Any("Update", err))
|
|
|
+ }
|
|
|
+}
|