|
@@ -2,6 +2,7 @@ package backend
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"kpt-pasture/util"
|
|
|
"net/http"
|
|
@@ -33,7 +34,8 @@ func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckR
|
|
|
if err != nil {
|
|
|
return nil, xerr.Customf("系统错误!")
|
|
|
}
|
|
|
- if err = pref.Order("a.level DESC").
|
|
|
+ if err = pref.Group("a.cow_id").
|
|
|
+ Order("a.level,a.id DESC").
|
|
|
Count(&count).
|
|
|
Find(&neckRingEstrusList).Error; err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
@@ -46,10 +48,33 @@ func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckR
|
|
|
"middle": 0,
|
|
|
"behind": 0,
|
|
|
}
|
|
|
+
|
|
|
+ cowIds := make([]int64, 0)
|
|
|
+ for _, v := range neckRingEstrusList {
|
|
|
+ cowIds = append(cowIds, v.CowId)
|
|
|
+ }
|
|
|
+
|
|
|
+ cowList := make([]*model.Cow, 0)
|
|
|
+ if err = s.DB.Model(new(model.Cow)).
|
|
|
+ Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
+ Where("admission_status = ?", pasturePb.AdmissionStatus_Admission).
|
|
|
+ Where("id IN ?", cowIds).
|
|
|
+ Find(&cowList).Error; err != nil {
|
|
|
+ zaplog.Error("NeckRingWarning", zap.Any("err", err))
|
|
|
+ }
|
|
|
+ cowByIdMap := map[int64]*model.Cow{}
|
|
|
+ for _, v := range cowList {
|
|
|
+ cowByIdMap[v.Id] = v
|
|
|
+ }
|
|
|
+
|
|
|
for _, v := range neckRingEstrusList {
|
|
|
estrusWarningLevelItems[int32(v.Level)] += 1
|
|
|
countEstrusWarning += 1
|
|
|
- cowInfo, _ := s.GetCowInfoByEarNumber(ctx, userModel.AppPasture.Id, v.EarNumber)
|
|
|
+ cowInfo, ok := cowByIdMap[v.CowId]
|
|
|
+ if !ok {
|
|
|
+ zaplog.Info("NeckRingWarning", zap.Any("v", v), zap.Any("cowByIdMap", cowByIdMap))
|
|
|
+ continue
|
|
|
+ }
|
|
|
pzHour := v.CalculatePzHour(cowInfo.Lact)
|
|
|
optimumMatingStartTime := pzHour.Add(-4 * time.Hour)
|
|
|
optimumMatingEndTime := pzHour.Add(4 * time.Hour)
|
|
@@ -72,7 +97,8 @@ func (s *StoreEntry) NeckRingWarning(ctx context.Context) (*pasturePb.IndexNeckR
|
|
|
return nil, xerr.Customf("系统错误!")
|
|
|
}
|
|
|
|
|
|
- if err = pref.Group("cow_id").Count(&abortionCount).Error; err != nil {
|
|
|
+ if err = pref.Group("cow_id").
|
|
|
+ Count(&abortionCount).Error; err != nil {
|
|
|
return nil, xerr.WithStack(err)
|
|
|
}
|
|
|
|
|
@@ -111,73 +137,87 @@ func (s *StoreEntry) FocusIndicatorsList(ctx context.Context, dimension string)
|
|
|
userFocusIndicators = model.DefaultFocusIndicators
|
|
|
}
|
|
|
userFocusIndicatorsList := strings.Split(userFocusIndicators, ",")
|
|
|
- indicatorsDataList := make([]*model.IndicatorsData, 0)
|
|
|
- pref := s.DB.Model(new(model.IndicatorsData)).
|
|
|
- Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
- Where("kind in (?)", userFocusIndicatorsList)
|
|
|
-
|
|
|
- /*if dimension == "Year" {
|
|
|
- pref.Where("date = ?", time.Now().Local().Format(model.LayoutMonth))
|
|
|
- }*/
|
|
|
-
|
|
|
- nowTime := time.Now().Local()
|
|
|
- if dimension == "Month" {
|
|
|
- pref.Where("date = ?", nowTime.Format(model.LayoutMonth))
|
|
|
+ // 获取用户关注的指标
|
|
|
+ userFocusIndicatorsDetailsList := make([]*model.IndicatorsDetails, 0)
|
|
|
+ if err = s.DB.Model(new(model.IndicatorsDetails)).
|
|
|
+ Where("kind in (?)", userFocusIndicatorsList).
|
|
|
+ Find(&userFocusIndicatorsDetailsList).Error; err != nil {
|
|
|
+ zaplog.Error("FocusIndicators", zap.Any("err", err))
|
|
|
}
|
|
|
|
|
|
- if err = pref.Find(&indicatorsDataList).Error; err != nil {
|
|
|
+ nowTime := time.Now().Local()
|
|
|
+ currentMonth := nowTime.Format(model.LayoutMonth)
|
|
|
+ lastMonth := nowTime.AddDate(0, -1, 0).Format(model.LayoutMonth)
|
|
|
+ lastYear := nowTime.AddDate(-1, 0, 0).Format(model.LayoutYear)
|
|
|
+ indicatorsDataList := make([]*model.IndicatorsData, 0)
|
|
|
+ if err = s.DB.Model(new(model.IndicatorsData)).
|
|
|
+ Where("pasture_id = ?", userModel.AppPasture.Id).
|
|
|
+ Where("kind in (?)", userFocusIndicatorsList).
|
|
|
+ Where("date IN ?", []string{currentMonth, lastMonth, lastYear}).
|
|
|
+ Order("kind,date").
|
|
|
+ Find(&indicatorsDataList).Error; err != nil {
|
|
|
zaplog.Error("FocusIndicators", zap.Any("err", err))
|
|
|
}
|
|
|
|
|
|
- indicatorsDetailsMap, _, err := s.GetIndicatorsDetailsMap(ctx)
|
|
|
- if err != nil {
|
|
|
- return nil, xerr.WithStack(err)
|
|
|
- }
|
|
|
focusIndicatorsList := make([]*pasturePb.FocusIndicators, 0)
|
|
|
+ indicatorsDataByKindMap := map[string][]*model.IndicatorsData{}
|
|
|
for _, v := range indicatorsDataList {
|
|
|
- indicatorsDetails, ok := indicatorsDetailsMap[v.Kind]
|
|
|
- if !ok {
|
|
|
- continue
|
|
|
+ if indicatorsDataByKindMap[v.Kind] == nil {
|
|
|
+ indicatorsDataByKindMap[v.Kind] = make([]*model.IndicatorsData, 0)
|
|
|
}
|
|
|
- onYear, onMonth := "", ""
|
|
|
+ indicatorsDataByKindMap[v.Kind] = append(indicatorsDataByKindMap[v.Kind], v)
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, indicatorsDetails := range userFocusIndicatorsDetailsList {
|
|
|
+ indicatorsList := indicatorsDataByKindMap[indicatorsDetails.Kind]
|
|
|
+ onYear, onMonth, currValue, onValue := "", "", float64(0), ""
|
|
|
isUp := pasturePb.IsShow_Ok
|
|
|
+ for _, v := range indicatorsList {
|
|
|
+ if v.Date == currentMonth {
|
|
|
+ currValue, _ = strconv.ParseFloat(v.Value, 64)
|
|
|
+ }
|
|
|
+ if v.Date == lastYear {
|
|
|
+ onYear = v.Value
|
|
|
+ }
|
|
|
+ if v.Date == lastMonth {
|
|
|
+ onMonth = v.Value
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ oldValue := float64(0)
|
|
|
if dimension == "Year" {
|
|
|
- return nil, xerr.Custom("暂不支持该维度")
|
|
|
+ oldValue, _ = strconv.ParseFloat(onYear, 64)
|
|
|
}
|
|
|
|
|
|
if dimension == "Month" {
|
|
|
- lastMonth := nowTime.AddDate(0, -1, 0).Format(model.LayoutMonth)
|
|
|
- oldIndicators, _ := s.GetIndicatorsDataByDate(userModel.AppPasture.Id, v.Kind, lastMonth)
|
|
|
- if oldIndicators != nil {
|
|
|
- if oldIndicators.Value != "" && oldIndicators.Value != "0" {
|
|
|
- oldValue, _ := strconv.ParseFloat(oldIndicators.Value, 64)
|
|
|
- currValue, _ := strconv.ParseFloat(v.Value, 64)
|
|
|
- onMonthValue := float64(0)
|
|
|
- if oldValue > 0 {
|
|
|
- onMonthValue = (oldValue - currValue) / oldValue * 100
|
|
|
- }
|
|
|
- omv := util.RoundToTwoDecimals(onMonthValue)
|
|
|
- if omv < 0 {
|
|
|
- isUp = pasturePb.IsShow_No
|
|
|
- }
|
|
|
- onMonth = strconv.FormatFloat(omv, 'f', 2, 64) + "%"
|
|
|
- }
|
|
|
+ oldValue, _ = strconv.ParseFloat(onMonth, 64)
|
|
|
+ }
|
|
|
+
|
|
|
+ if currValue > 0 && oldValue > 0 {
|
|
|
+ b := float64(0)
|
|
|
+ if oldValue > 0 {
|
|
|
+ b = (oldValue - currValue) / oldValue * 100
|
|
|
+ }
|
|
|
+ omv := util.RoundToTwoDecimals(b)
|
|
|
+ if omv < 0 {
|
|
|
+ isUp = pasturePb.IsShow_No
|
|
|
}
|
|
|
+ onValue = strconv.FormatFloat(omv, 'f', 2, 64) + "%"
|
|
|
}
|
|
|
+
|
|
|
focusIndicatorsList = append(focusIndicatorsList, &pasturePb.FocusIndicators{
|
|
|
Kind: indicatorsDetails.Kind,
|
|
|
Name: indicatorsDetails.Name,
|
|
|
- Value: v.Value,
|
|
|
+ Value: fmt.Sprintf("%f", currValue),
|
|
|
Describe: indicatorsDetails.Zh,
|
|
|
UnitName: indicatorsDetails.Unit,
|
|
|
- OnMonth: onMonth,
|
|
|
- OnYear: onYear,
|
|
|
IsUp: isUp,
|
|
|
+ OnYear: onValue,
|
|
|
+ OnMonth: onValue,
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- indicatorsDetailsList, _ := s.FindIndicatorsDetailsList(ctx)
|
|
|
-
|
|
|
+ indicatorsDetailsList, _ := s.FindIndicatorsDetailsList()
|
|
|
return &pasturePb.IndexFocusIndicatorsResponse{
|
|
|
Code: http.StatusOK,
|
|
|
Msg: "ok",
|