|
@@ -5,6 +5,8 @@ import (
|
|
"kpt-pasture/util"
|
|
"kpt-pasture/util"
|
|
"time"
|
|
"time"
|
|
|
|
|
|
|
|
+ "gitee.com/xuyiping_admin/pkg/xerr"
|
|
|
|
+
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
|
|
|
|
|
|
"gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
"gitee.com/xuyiping_admin/pkg/logger/zaplog"
|
|
@@ -43,30 +45,79 @@ func (e *Entry) InsertMilkDaily() error {
|
|
|
|
|
|
func (e *Entry) ProcessMilkDaily(pastureId int64, maxDate time.Time) {
|
|
func (e *Entry) ProcessMilkDaily(pastureId int64, maxDate time.Time) {
|
|
nowTime := time.Now().Local()
|
|
nowTime := time.Now().Local()
|
|
|
|
+
|
|
// 处理每一天的数据
|
|
// 处理每一天的数据
|
|
for maxDate.Before(nowTime) {
|
|
for maxDate.Before(nowTime) {
|
|
// 处理有胎次的奶牛
|
|
// 处理有胎次的奶牛
|
|
- if err := e.processCowsWithLact(pastureId, maxDate); err != nil {
|
|
|
|
|
|
+ processCowIds := make([]int64, 0)
|
|
|
|
+ cowIds, err := e.processCowsWithLact(pastureId, maxDate)
|
|
|
|
+ if err != nil {
|
|
zaplog.Error("ProcessMilkDaily", zap.Any("processCowsWithFetal", err))
|
|
zaplog.Error("ProcessMilkDaily", zap.Any("processCowsWithFetal", err))
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ if len(cowIds) > 0 {
|
|
|
|
+ processCowIds = append(processCowIds, cowIds...)
|
|
|
|
+ }
|
|
// 处理无胎次的奶牛
|
|
// 处理无胎次的奶牛
|
|
- if err := e.processCowsWithNoLact(pastureId, maxDate); err != nil {
|
|
|
|
|
|
+ cowIds, err = e.processCowsWithNoLact(pastureId, maxDate)
|
|
|
|
+ if err != nil {
|
|
zaplog.Error("ProcessMilkDaily", zap.Any("processCowsWithoutFetal", err))
|
|
zaplog.Error("ProcessMilkDaily", zap.Any("processCowsWithoutFetal", err))
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ if len(cowIds) > 0 {
|
|
|
|
+ processCowIds = append(processCowIds, cowIds...)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if len(processCowIds) > 0 {
|
|
|
|
+ e.UpdateMilkDaily(pastureId, processCowIds, maxDate.Format(model.LayoutDate2))
|
|
|
|
+ }
|
|
// 日期加1天
|
|
// 日期加1天
|
|
maxDate = maxDate.AddDate(0, 0, 1)
|
|
maxDate = maxDate.AddDate(0, 0, 1)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-func (e *Entry) UpdateMilkDaily(pastureId int64, maxDateTime string) error {
|
|
|
|
- //yesterday := time.Now().Local().AddDate(0, 0, -1).Format(model.LayoutDate2)
|
|
|
|
|
|
+// UpdateMilkDaily
|
|
|
|
+// SELECT h.intCowId, ROUND(AVG(h.filterhigh), 0) high, ROUND(AVG( h.rumina)*12, 0) rumina, ROUND(AVG( h.intake)*12, 0) intake,
|
|
|
|
+// // ROUND(AVG( h.inactive)*12, 0) inactive, ROUND(AVG( h.act)*12, 0) act, COUNT(1) nb
|
|
|
|
+// // FROM h_activehabit h WHERE h.intPastureId=PastuId AND h.heatdate=xDate GROUP BY h.intCowId HAVING nb>=8
|
|
|
|
+func (e *Entry) UpdateMilkDaily(pastureId int64, processCowIds []int64, heatDate string) error {
|
|
|
|
|
|
|
|
+ neckActiveHabitList := make([]*model.NeckActiveHabit, 0)
|
|
|
|
+ if err := e.DB.Model(new(model.NeckActiveHabit)).
|
|
|
|
+ Select(`h.cow_id, ROUND(AVG(h.filter_high), 0) AS high, ROUND(AVG( h.rumina)*12, 0) AS rumina, ROUND(AVG( h.intake)*12, 0) AS intake,
|
|
|
|
+ ROUND(AVG( h.inactive)*12, 0) AS inactive, ROUND(AVG( h.active)*12, 0) AS active, COUNT(1) AS record_count`).
|
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
|
+ Where("cow_id IN ?", processCowIds).
|
|
|
|
+ Where("heat_date = ?", heatDate).
|
|
|
|
+ Group("cow_id").
|
|
|
|
+ Find(&neckActiveHabitList).Error; err != nil {
|
|
|
|
+ return xerr.WithStack(err)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, v := range neckActiveHabitList {
|
|
|
|
+ // todo 先不过滤
|
|
|
|
+ /*if v.RecordCount < 8 {
|
|
|
|
+ continue
|
|
|
|
+ }*/
|
|
|
|
+ if err := e.DB.Model(new(model.MilkDaily)).
|
|
|
|
+ Where("pasture_id = ?", pastureId).
|
|
|
|
+ Where("cow_id = ?", v.CowId).
|
|
|
|
+ Where("heat_date = ?", heatDate).
|
|
|
|
+ Updates(map[string]interface{}{
|
|
|
|
+ "day_high": v.High,
|
|
|
|
+ "day_rumina": v.Rumina,
|
|
|
|
+ "day_intake": v.Intake,
|
|
|
|
+ "day_inactive": v.Inactive,
|
|
|
|
+ "day_active": v.Active,
|
|
|
|
+ }).Error; err != nil {
|
|
|
|
+ zaplog.Error("UpdateMilkDaily", zap.Any("pastureId", pastureId), zap.Any("cowId", v.CowId), zap.Any("heatDate", heatDate), zap.Any("err", err))
|
|
|
|
+ continue
|
|
|
|
+ }
|
|
|
|
+ }
|
|
return nil
|
|
return nil
|
|
}
|
|
}
|
|
|
|
|
|
// 处理有胎次的奶牛
|
|
// 处理有胎次的奶牛
|
|
-func (e *Entry) processCowsWithLact(pastureId int64, recordDate time.Time) error {
|
|
|
|
|
|
+func (e *Entry) processCowsWithLact(pastureId int64, recordDate time.Time) ([]int64, error) {
|
|
// 查询有胎次且在记录日期前有记录的奶牛
|
|
// 查询有胎次且在记录日期前有记录的奶牛
|
|
cowList := make([]*model.Cow, 0)
|
|
cowList := make([]*model.Cow, 0)
|
|
if err := e.DB.Model(new(model.Cow)).
|
|
if err := e.DB.Model(new(model.Cow)).
|
|
@@ -75,11 +126,12 @@ func (e *Entry) processCowsWithLact(pastureId int64, recordDate time.Time) error
|
|
Where("lact > ?", 0).
|
|
Where("lact > ?", 0).
|
|
Where("last_calving_at <= ?", recordDate.Local().Unix()).
|
|
Where("last_calving_at <= ?", recordDate.Local().Unix()).
|
|
Find(&cowList).Error; err != nil {
|
|
Find(&cowList).Error; err != nil {
|
|
- return err
|
|
|
|
|
|
+ return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 批量插入数据
|
|
// 批量插入数据
|
|
milkDailyList := make([]*model.MilkDaily, 0)
|
|
milkDailyList := make([]*model.MilkDaily, 0)
|
|
|
|
+ cowIds := make([]int64, 0)
|
|
for _, cow := range cowList {
|
|
for _, cow := range cowList {
|
|
calvingDate := ""
|
|
calvingDate := ""
|
|
if cow.LastCalvingAt > 0 {
|
|
if cow.LastCalvingAt > 0 {
|
|
@@ -98,6 +150,7 @@ func (e *Entry) processCowsWithLact(pastureId int64, recordDate time.Time) error
|
|
BreedStatus: cow.BreedStatus,
|
|
BreedStatus: cow.BreedStatus,
|
|
}
|
|
}
|
|
milkDailyList = append(milkDailyList, milkDaily)
|
|
milkDailyList = append(milkDailyList, milkDaily)
|
|
|
|
+ cowIds = append(cowIds, cow.Id)
|
|
}
|
|
}
|
|
if len(milkDailyList) > 0 {
|
|
if len(milkDailyList) > 0 {
|
|
// 分批次插入数据
|
|
// 分批次插入数据
|
|
@@ -108,16 +161,16 @@ func (e *Entry) processCowsWithLact(pastureId int64, recordDate time.Time) error
|
|
}
|
|
}
|
|
if err := e.DB.Model(new(model.MilkDaily)).
|
|
if err := e.DB.Model(new(model.MilkDaily)).
|
|
Create(milkDailyList[i:end]).Error; err != nil {
|
|
Create(milkDailyList[i:end]).Error; err != nil {
|
|
- return err
|
|
|
|
|
|
+ return nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- return nil
|
|
|
|
|
|
+ return cowIds, nil
|
|
}
|
|
}
|
|
|
|
|
|
// 处理无胎次的奶牛
|
|
// 处理无胎次的奶牛
|
|
-func (e *Entry) processCowsWithNoLact(pastureId int64, recordDate time.Time) error {
|
|
|
|
|
|
+func (e *Entry) processCowsWithNoLact(pastureId int64, recordDate time.Time) ([]int64, error) {
|
|
// 查询无胎次且EID1>0的奶牛
|
|
// 查询无胎次且EID1>0的奶牛
|
|
cowList := make([]*model.Cow, 0)
|
|
cowList := make([]*model.Cow, 0)
|
|
err := e.DB.Model(new(model.Cow)).
|
|
err := e.DB.Model(new(model.Cow)).
|
|
@@ -129,13 +182,13 @@ func (e *Entry) processCowsWithNoLact(pastureId int64, recordDate time.Time) err
|
|
Where("neck_ring_number != ?", "").
|
|
Where("neck_ring_number != ?", "").
|
|
Find(&cowList).Error
|
|
Find(&cowList).Error
|
|
if err != nil {
|
|
if err != nil {
|
|
- return err
|
|
|
|
|
|
+ return nil, err
|
|
}
|
|
}
|
|
|
|
|
|
// 批量插入数据
|
|
// 批量插入数据
|
|
milkDailyList := make([]*model.MilkDaily, 0)
|
|
milkDailyList := make([]*model.MilkDaily, 0)
|
|
|
|
+ cowIds := make([]int64, 0)
|
|
for _, cow := range cowList {
|
|
for _, cow := range cowList {
|
|
-
|
|
|
|
birthDate, calvingDate := "", ""
|
|
birthDate, calvingDate := "", ""
|
|
if cow.BirthAt > 0 {
|
|
if cow.BirthAt > 0 {
|
|
birthDate = time.Unix(cow.BirthAt, 0).Local().Format(model.LayoutDate2)
|
|
birthDate = time.Unix(cow.BirthAt, 0).Local().Format(model.LayoutDate2)
|
|
@@ -158,14 +211,14 @@ func (e *Entry) processCowsWithNoLact(pastureId int64, recordDate time.Time) err
|
|
BreedStatus: cow.BreedStatus,
|
|
BreedStatus: cow.BreedStatus,
|
|
}
|
|
}
|
|
milkDailyList = append(milkDailyList, milkDaily)
|
|
milkDailyList = append(milkDailyList, milkDaily)
|
|
|
|
+ cowIds = append(cowIds, cow.Id)
|
|
}
|
|
}
|
|
|
|
|
|
if len(milkDailyList) > 0 {
|
|
if len(milkDailyList) > 0 {
|
|
if err = e.DB.Model(new(model.MilkDaily)).
|
|
if err = e.DB.Model(new(model.MilkDaily)).
|
|
Create(&milkDailyList).Error; err != nil {
|
|
Create(&milkDailyList).Error; err != nil {
|
|
- return err
|
|
|
|
|
|
+ return nil, err
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- return nil
|
|
|
|
|
|
+ return cowIds, nil
|
|
}
|
|
}
|