Jelajahi Sumber

crontab: milkDaily update

ping 1 bulan lalu
induk
melakukan
2a37715fd1

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250418054121-409ac98e3475
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250421082915-83b5518d3a0c
 	gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3

+ 4 - 0
go.sum

@@ -42,6 +42,10 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20250416102510-604a13fe9d52 h1:aCqqT2Y/
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250416102510-604a13fe9d52/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250418054121-409ac98e3475 h1:zGCW7bHN9o67VxxK3eZr0k0h5HB+twDjx4cIc9q3dEI=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20250418054121-409ac98e3475/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250420125055-0d4da4a5a6fe h1:FSRq0AItdCCIisvxG0Ki+l7PxxaPFJSkOXN49x3LpnQ=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250420125055-0d4da4a5a6fe/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250421082915-83b5518d3a0c h1:CuiDP9SfrSEiJ/wG0iz7wgIt9n38SeckUXscY6X5zBU=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20250421082915-83b5518d3a0c/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b h1:w05MxH7yqveRlaRbxHhbif5YjPrJFodRPfOjYhXn7Zk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20241108060137-caea58c59f5b/go.mod h1:8tF25X6pE9WkFCczlNAC0K2mrjwKvhhp02I7o0HtDxY=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 1 - 0
model/event_pregnant_check.go

@@ -181,6 +181,7 @@ func (e EventPregnantCheckSlice) ToPB3(
 		res[i] = &pasturePb.PregnancyReportTable{
 			Id:                      int32(v.Id),
 			CowId:                   int32(v.CowId),
+			EarNumber:               v.EarNumber,
 			Lact:                    int32(v.Lact),
 			PregnancyCheckName:      pregnancyCheckName,
 			PregnancyCheckAtFormat:  time.Unix(v.RealityDay, 0).Local().Format(LayoutDate2),

+ 3 - 3
model/event_sale.go

@@ -13,7 +13,7 @@ type EventSale struct {
 	DealerName       string                    `json:"dealerName"`
 	SalePrice        float64                   `json:"salePrice"`
 	SaleAllWeight    int32                     `json:"saleAllWeight"`
-	SaleAllAmount    float64                   `json:"saleAllAmount"`
+	SaleAllAmount    int64                     `json:"saleAllAmount"`
 	SaleCowCount     int32                     `json:"saleCowCount"`
 	SaleKind         pasturePb.SalesType_Kind  `json:"saleKind"`
 	OutReasonsKind   pasturePb.OutReasons_Kind `json:"outReasonsKind"`
@@ -41,7 +41,7 @@ func NewEventSale(pastureId int64, dealerInfo *SaleDealer, req *pasturePb.EventC
 		DealerName:       dealerInfo.Name,
 		SalePrice:        float64(req.SalePrice),
 		SaleAllWeight:    int32(req.SaleAllWeight * 1000),
-		SaleAllAmount:    float64(req.SaleAllPrice),
+		SaleAllAmount:    int64(req.SaleAllPrice * 100),
 		SaleCowCount:     int32(len(req.EarNumbers)),
 		SaleKind:         req.SalesType,
 		OutReasonsKind:   req.OutReasonsKind,
@@ -106,7 +106,7 @@ func (e EventSaleSlice) ToPB(eventSaleCarMap map[int64][]*EventSaleCar, eventSal
 			OutReasonsKind:   v.OutReasonsKind,
 			OutReasonsName:   v.OutReasonsName,
 			SaleAllWeight:    float32(v.SaleAllWeight) / 1000,
-			SaleAllPrice:     float32(v.SaleAllAmount),
+			SaleAllPrice:     float32(v.SaleAllAmount) / 100,
 			SalePrice:        float32(v.SalePrice),
 			OperationId:      int32(v.OperationId),
 			OperationName:    v.OperationName,

+ 3 - 3
module/backend/analysis_other.go

@@ -306,12 +306,12 @@ func (s *StoreEntry) SaleCowReport(ctx context.Context, req *pasturePb.SaleCowRe
 		Where("sale_at BETWEEN ? AND ?", startDayTimeUnix, endDayTimeUnix).
 		Where("pasture_id = ?", userModel.AppPasture.Id)
 	if req.AnalysisMethod == pasturePb.SaleCowAnalysisMethod_Months {
-		pref.Select(`SUM(sale_cow_count) AS sale_all_count,SUM(sale_all_amount) AS sale_all_amount,SUM(sale_cow_count) AS sale_all_count,
+		pref.Select(`ROUND(SUM(sale_all_amount) /100,2) AS sale_all_amount,SUM(sale_cow_count) AS sale_all_count,
 		SUM(sale_all_weight) AS sale_all_weight,DATE_FORMAT(FROM_UNIXTIME(sale_at), '%Y-%m') AS statistic_method`)
 	}
 
 	if req.AnalysisMethod == pasturePb.SaleCowAnalysisMethod_Dealer {
-		pref.Select(`SUM(sale_cow_count) AS sale_all_count,SUM(sale_all_amount) AS sale_all_amount,SUM(sale_cow_count) AS sale_all_count,
+		pref.Select(`ROUND(SUM(sale_all_amount) /100,2) AS sale_all_amount,SUM(sale_cow_count) AS sale_all_count,
 		SUM(sale_all_weight) AS sale_all_weight,dealer_name as statistic_method`)
 	}
 
@@ -333,7 +333,7 @@ func (s *StoreEntry) SaleCowReport(ctx context.Context, req *pasturePb.SaleCowRe
 			v.SaleAvgWeight = float32(util.RoundToTwoDecimals(float64(v.SaleAllWeight / float32(v.SaleAllCount))))
 		}
 		chart.Headers = append(chart.Headers, v.StatisticMethod)
-		chart.SaleMount = append(chart.SaleMount, v.SaleAllMount)
+		chart.SaleMount = append(chart.SaleMount, v.SaleAllAmount)
 		chart.SaleWeight = append(chart.SaleWeight, v.SaleAllWeight)
 		chart.SaleCount = append(chart.SaleCount, v.SaleAllCount)
 		chart.AverageSaleWeight = append(chart.AverageSaleWeight, v.SaleAvgWeight)

+ 1 - 3
module/crontab/estrus_warning.go

@@ -45,20 +45,18 @@ func (e *Entry) UpdateNeckRingWarning(pastureId int64) (err error) {
 		Where("a.pasture_id = ?", pastureId).
 		Where("a.active_time >= ?", fmt.Sprintf("%s 00:00:00", startTime.Format(model.LayoutDate2))).
 		Where("a.active_level >= ?", pasturePb.EstrusLevel_Low).
-		Where("a.check_result IN (?)", []pasturePb.CheckResult_Kind{pasturePb.CheckResult_Pending, pasturePb.CheckResult_Correct}).
+		Where("a.check_result IN (?)", []pasturePb.CheckResult_Kind{pasturePb.CheckResult_Pending}).
 		Where("a.is_show = ?", pasturePb.IsShow_Ok).
 		Where("a.is_peak >= ?", pasturePb.IsShow_Ok).
 		Where("b.admission_status = ?", pasturePb.AdmissionStatus_Admission).
 		Find(&neckRingEstrusList).Error; err != nil {
 		return xerr.WithStack(err)
 	}
-	zaplog.Info("UpdateNeckRingWarning", zap.Any("neckRingEstrusList", neckRingEstrusList))
 	if len(neckRingEstrusList) == 0 {
 		return nil
 	}
 
 	neckRingEstrusWarningList := e.GroupAndProcessData(neckRingEstrusList)
-	zaplog.Info("UpdateNeckRingWarning", zap.Any("neckRingEstrusWarningList", neckRingEstrusWarningList))
 
 	if len(neckRingEstrusWarningList) > 0 {
 		if err = e.DB.Create(neckRingEstrusWarningList).Error; err != nil {

+ 10 - 3
module/crontab/milk_daily.go

@@ -99,9 +99,16 @@ func (e *Entry) processCowsWithLact(recordDate time.Time) error {
 		milkDailyList = append(milkDailyList, milkDaily)
 	}
 	if len(milkDailyList) > 0 {
-		if err := e.DB.Model(new(model.MilkDaily)).
-			Create(&milkDailyList).Error; err != nil {
-			return err
+		// 分批次插入数据
+		for i := 0; i < len(milkDailyList); i += 100 {
+			end := i + 100
+			if end > len(milkDailyList) {
+				end = len(milkDailyList)
+			}
+			if err := e.DB.Model(new(model.MilkDaily)).
+				Create(milkDailyList[i:end]).Error; err != nil {
+				return err
+			}
 		}
 	}
 

+ 1 - 1
module/crontab/model.go

@@ -13,7 +13,7 @@ type WeekHabit struct {
 type ActivityVolume struct {
 	NeckRingNumber string
 	AvgFilter      float64
-	StdFilter      int32
+	StdFilter      float64
 	Nb             int32
 }
 

+ 10 - 6
module/crontab/neck_ring_calculate.go

@@ -100,7 +100,7 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 		Where("heat_date >= ?", time.Now().Local().AddDate(0, 0, -7).Format(model.LayoutDate2)).
 		Where("pasture_id = ?", pastureId).
 		Where("is_show = ?", pasturePb.IsShow_No).
-		Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
+		//Where(e.DB.Where("high >= ?", xToDay.High).Or("rumina >= ?", xToDay.Rumina)).
 		Order("heat_date,neck_ring_number,frameid").
 		Limit(int(limit)).
 		Find(&newNeckActiveHabitList).Error; err != nil {
@@ -109,6 +109,9 @@ func (e *Entry) FirstFilterUpdate(pastureId int64, xToDay *XToday) (processIds [
 
 	// 活动量滤波
 	for _, v := range newNeckActiveHabitList {
+		if !(v.High >= xToDay.High || v.Rumina >= xToDay.Rumina) {
+			continue
+		}
 		// 4小时数据不全的不参与滤波
 		activeTime, _ := util.TimeParseLocal(model.LayoutTime, v.ActiveTime)
 		if v.RecordCount != model.DefaultRecordCount && time.Now().Local().Sub(activeTime).Hours() <= 4 {
@@ -320,13 +323,14 @@ func (e *Entry) FilterCorrectAndScoreUpdate(pastureId int64, processIds []int64,
 			Where("active_time <= ?", beginDayDate.Add(-12*time.Hour).Format(model.LayoutTime)).
 			Where("change_filter > ?", MinChangeFilter).
 			Where("neck_ring_number = ?", v.NeckRingNumber).
-			Having("nb > ?", DefaultNb).
+			Having("nb >= ?", DefaultNb).
 			First(&activityVolume).Error; err != nil {
 			zaplog.Error("ActivityVolumeChanges-0", zap.Any("error", err), zap.Any("xToday", xToday))
 			continue
 		}
 		if activityVolume != nil && activityVolume.NeckRingNumber != "" {
-			filterCorrect := model.DefaultFilterCorrect - int(math.Floor(activityVolume.AvgFilter/3+float64(activityVolume.StdFilter)/2))
+			//filterCorrect := model.DefaultFilterCorrect - int(math.Floor(activityVolume.AvgFilter/3+float64(activityVolume.StdFilter)/2))
+			filterCorrect := model.DefaultFilterCorrect - int(math.Round(activityVolume.AvgFilter/3+float64(int(math.Round(activityVolume.StdFilter))/2)))
 			// 活动量校正系数
 			if err := e.DB.Model(new(model.NeckActiveHabit)).
 				Where("id = ?", v.Id).
@@ -382,11 +386,10 @@ func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) {
 	if err := e.DB.Table(fmt.Sprintf("%s as h", new(model.NeckActiveHabit).TableName())).
 		Select(`h.neck_ring_number,h.heat_date, h.frameid, c.pen_id, c.pen_name, COUNT(*) as nb,
 		ROUND(AVG(h.change_high)) as change_high, ROUND(AVG(h.change_filter)) as change_filter`).
-		Joins("JOIN cow as c ON h.neck_ring_number = c.neck_ring_number").
+		Joins("JOIN cow as c ON h.cow_id = c.id").
 		Where("h.pasture_id = ?", pastureId).
 		Where("h.heat_date >= ?", oneDayAgo).
-		Where("h.is_show = ?", pasturePb.IsShow_Ok).
-		Where("h.cow_id >= ?", 0).
+		Where("h.cow_id > ?", 0).
 		Where("c.pen_id > ?", 0).
 		Group("h.heat_date, h.frameid, c.pen_id").
 		Order("h.heat_date, h.frameid, c.pen_id").
@@ -402,6 +405,7 @@ func (e *Entry) UpdateChangeAdJust(pastureId int64, xToday *XToday) {
 			Where("pasture_id = ?", pastureId).
 			Where("neck_ring_number = ?", v.NeckRingNumber).
 			Where("heat_date = ?", v.HeatDate).
+			Where("pen_id = ?", v.PenId).
 			Where("frameid = ?", v.FrameId).
 			Update("change_adjust", v.ChangeFilter).Error; err != nil {
 			zaplog.Error("UpdateChangeAdJust-1", zap.Any("error", err), zap.Any("xToday", xToday))

+ 5 - 5
module/crontab/pen_behavior_day.go

@@ -89,11 +89,11 @@ func (e *Entry) updateMilkDaily(pastureId int64, targetDate string) error {
 func (e *Entry) insertBarBehaviorDay(pastureId int64, targetDate string) error {
 	penBehaviorList := make([]*model.PenBehaviorDay, 0)
 	if err := e.DB.Model(new(model.MilkDaily)).
-		Select(`heat_date, pasture_id, pen_id, COUNT(1) as cow_count, ROUND(AVG(d.day_yield), 1) as day_milk,
-		ROUND(AVG(d.week_avg), 1) as week_milk,ROUND(AVG(d.day_high), 0) as day_high,
-		ROUND(AVG(d.day_rumina), 0) as day_rumina,ROUND(AVG(d.day_intake), 0) as day_intake,
-		ROUND(AVG(d.day_inactive), 0) as day_inactive,ROUND(AVG(d.day_gasp), 0) as day_gasp,
-		ROUND(AVG(d.day_active), 0) as day_active,STD(d.day_rumina) as rumina_std`).
+		Select(`heat_date, pasture_id, pen_id, COUNT(1) as cow_count, ROUND(AVG(day_yield), 1) as day_milk,
+		ROUND(AVG(week_avg), 1) as week_milk,ROUND(AVG(day_high), 0) as day_high,
+		ROUND(AVG(day_rumina), 0) as day_rumina,ROUND(AVG(day_intake), 0) as day_intake,
+		ROUND(AVG(day_inactive), 0) as day_inactive,ROUND(AVG(day_gasp), 0) as day_gasp,
+		ROUND(AVG(day_active), 0) as day_active,STD(day_rumina) as rumina_std`).
 		Where("pasture_id = ?", pastureId).
 		Where("heat_date = ?", targetDate).
 		Where("day_high > ?", MaxDayHigh).