|
@@ -2,6 +2,7 @@ package backend
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
+ "fmt"
|
|
|
"kpt-pasture/model"
|
|
|
"net/http"
|
|
|
"strings"
|
|
@@ -148,3 +149,92 @@ func (s *StoreEntry) WeightRange(ctx context.Context, req *pasturePb.WeightRange
|
|
|
},
|
|
|
}, nil
|
|
|
}
|
|
|
+
|
|
|
+func (s *StoreEntry) MatingTimely(ctx context.Context, req *pasturePb.MatingTimelyRequest) (*pasturePb.MatingTimelyResponse, error) {
|
|
|
+ matingTimelyChart := make([]*model.MatingTimelyChart, 0)
|
|
|
+ sql := `SELECT calving_age,cow_type, DATE_FORMAT(FROM_UNIXTIME(reality_day), '%Y-%m-%d') AS reality_day, lact_group
|
|
|
+ FROM (
|
|
|
+ SELECT calving_age, cow_type,reality_day, '0' AS lact_group
|
|
|
+ FROM event_mating
|
|
|
+ WHERE lact = 0 AND status = 1
|
|
|
+ UNION ALL
|
|
|
+ SELECT calving_age,cow_type, reality_day, '1' AS lact_group
|
|
|
+ FROM event_mating
|
|
|
+ WHERE lact = 1 AND status = 1
|
|
|
+ UNION ALL
|
|
|
+ SELECT calving_age,cow_type, reality_day, '2' AS lact_group
|
|
|
+ FROM event_mating
|
|
|
+ WHERE lact = 2 AND status = 1
|
|
|
+ UNION ALL
|
|
|
+ SELECT calving_age, cow_type, reality_day, '3+' AS lact_group
|
|
|
+ FROM event_mating
|
|
|
+ WHERE lact >= 3 AND status = 1
|
|
|
+ ) AS subquery WHERE 1 = 1 `
|
|
|
+
|
|
|
+ whereSql := ""
|
|
|
+ if req.CowType > 0 {
|
|
|
+ whereSql += fmt.Sprintf("AND cow_type = %d ", req.CowType)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.StartDayAt > 0 && req.EndDayAt > 0 {
|
|
|
+ whereSql += fmt.Sprintf("AND reality_day BETWEEN %d AND %d", req.StartDayAt, req.EndDayAt)
|
|
|
+ }
|
|
|
+ if err := s.DB.Raw(sql).Find(&matingTimelyChart).Error; err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ chart := &pasturePb.MatingTimelyChart{
|
|
|
+ Lact0: make([]int32, 0),
|
|
|
+ Lact1: make([]int32, 0),
|
|
|
+ Lact2: make([]int32, 0),
|
|
|
+ Lact3: make([]int32, 0),
|
|
|
+ }
|
|
|
+ if len(matingTimelyChart) == 0 {
|
|
|
+ return &pasturePb.MatingTimelyResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.MatingTimelyData{
|
|
|
+ CowList: make([]*pasturePb.CowList, 0),
|
|
|
+ Chart: chart,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, v := range matingTimelyChart {
|
|
|
+ switch v.LactGroup {
|
|
|
+ case "0":
|
|
|
+ chart.Lact0 = append(chart.Lact0, v.CalvingAge)
|
|
|
+ case "1":
|
|
|
+ chart.Lact1 = append(chart.Lact1, v.CalvingAge)
|
|
|
+ case "2":
|
|
|
+ chart.Lact2 = append(chart.Lact2, v.CalvingAge)
|
|
|
+ case "3+":
|
|
|
+ chart.Lact3 = append(chart.Lact3, v.CalvingAge)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 牛只详情列表
|
|
|
+ eventMatingList := make([]*model.EventMating, 0)
|
|
|
+ pref := s.DB.Model(new(model.EventMating)).
|
|
|
+ Where("status = ?", pasturePb.IsShow_Ok)
|
|
|
+ if req.CowType > 0 {
|
|
|
+ pref.Where("cow_type = ?", req.CowType)
|
|
|
+ }
|
|
|
+
|
|
|
+ if req.StartDayAt > 0 && req.EndDayAt > 0 {
|
|
|
+ pref.Where("reality_day BETWEEN ? AND ?", req.StartDayAt, req.EndDayAt)
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := pref.Find(&eventMatingList).Error; err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ return &pasturePb.MatingTimelyResponse{
|
|
|
+ Code: http.StatusOK,
|
|
|
+ Message: "ok",
|
|
|
+ Data: &pasturePb.MatingTimelyData{
|
|
|
+ CowList: model.EventMatingSlice(eventMatingList).ToPB2(),
|
|
|
+ Chart: chart,
|
|
|
+ },
|
|
|
+ }, nil
|
|
|
+}
|