Procházet zdrojové kódy

analysis: 生长曲线统计

Yi před 9 měsíci
rodič
revize
82111fdf92

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240605085152-de3ddf049865
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240606095857-7c3f8fb582a1
 	gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/getsentry/sentry-go v0.23.0

+ 2 - 0
go.sum

@@ -134,6 +134,8 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240605030356-5e185b3955e4 h1:0hQ8qWbS
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240605030356-5e185b3955e4/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240605085152-de3ddf049865 h1:icm4K9AW11Yg8CaWGBRpM5VmCM2DnrjUJyBUDG7Dlno=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240605085152-de3ddf049865/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240606095857-7c3f8fb582a1 h1:pSTADLNa+ylTtxq+xhOg+4oQmu6YVVjur1q/KvE7Z9Q=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240606095857-7c3f8fb582a1/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015 h1:dfb5dRd57L2HKjdwLT93UFmPYFPOmEl56gtZmqcNnaE=
 gitee.com/xuyiping_admin/pkg v0.0.0-20231218082641-aac597b8a015/go.mod h1:Fk4GYI/v0IK3XFrm1Gn+VkgCz5Y7mfswD5hsTJYOG6A=
 github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=

+ 27 - 0
http/handler/analysis/analysis.go

@@ -0,0 +1,27 @@
+package analysis
+
+import (
+	"kpt-pasture/http/middleware"
+	"net/http"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	"gitee.com/xuyiping_admin/pkg/ginutil"
+
+	"gitee.com/xuyiping_admin/pkg/apierr"
+	"github.com/gin-gonic/gin"
+)
+
+func GrowthCurve(c *gin.Context) {
+	var req pasturePb.SearchGrowthCurvesRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.GrowthCurve(c, &req)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	c.JSON(http.StatusOK, res)
+}

+ 18 - 0
http/route/analysis_api.go

@@ -0,0 +1,18 @@
+package route
+
+import (
+	"kpt-pasture/http/handler/analysis"
+
+	"github.com/gin-gonic/gin"
+)
+
+func AnalysisAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
+	return func(s *gin.Engine) {
+		for _, opt := range opts {
+			opt(s)
+		}
+		// pasture API 组  牧场管理
+		pastureRoute := authRouteGroup(s, "/api/v1/analysis/")
+		pastureRoute.GET("/growth/curve", analysis.GrowthCurve)
+	}
+}

+ 0 - 0
http/route/cow.go → http/route/cow_api.go


+ 0 - 0
http/route/goods.go → http/route/goods_api.go


+ 1 - 0
http/route/route.go

@@ -12,6 +12,7 @@ func HTTPServerRoute(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		EventAPI(opts...),
 		CowAPI(opts...),
 		GoodsManageAPI(opts...),
+		AnalysisAPI(opts...),
 	}
 
 	return func(s *gin.Engine) {

+ 0 - 0
model/barn.go → model/pen.go


+ 63 - 0
module/backend/analysis.go

@@ -0,0 +1,63 @@
+package backend
+
+import (
+	"context"
+	"kpt-pasture/model"
+	"net/http"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
+
+// GrowthCurve 生长曲线 获取图表数据
+func (s *StoreEntry) GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error) {
+	// 查询数据
+	cowList := make([]*model.Cow, 0)
+	pref := s.DB.Model(new(model.Cow)).Where("is_remove = ?", pasturePb.IsShow_Ok)
+
+	if req.GetCowId() != "" {
+		pref.Where("id IN ?", req.GetCowId())
+	}
+
+	if req.BirthStartDate > 0 && req.BirthEndDate > 0 && req.BirthStartDate <= req.BirthEndDate {
+		pref.Where("birth_at BETWEEN ? AND ?", req.GetBirthStartDate(), req.GetBirthEndDate())
+	}
+
+	if err := pref.Find(&cowList).Error; err != nil {
+		return nil, err
+	}
+	// 计算图表数据
+	chartsList := &pasturePb.Charts{
+		CowId:  make([]int32, 0),
+		Weight: make([]float32, 0),
+		DayAge: make([]int32, 0),
+	}
+	cowData := make([]*pasturePb.CowList, 0)
+	for _, cow := range cowList {
+		currentWeight := float32(cow.CurrentWeight) / 100
+		cowData = append(cowData, &pasturePb.CowList{
+			CowId:                  int32(cow.Id),
+			EarNumber:              cow.EarNumber,
+			DayAge:                 cow.GetDayAge(),
+			PenName:                "",
+			CurrentWeight:          currentWeight,
+			BirthAt:                int32(cow.BirthAt),
+			BirthWeight:            float32(cow.BirthWeight) / 100,
+			LastWeightAt:           int32(cow.LastWeightAt),
+			DailyWeightGain:        0,
+			AverageDailyWeightGain: 0,
+		})
+		chartsList.CowId = append(chartsList.CowId, int32(cow.Id))
+		chartsList.Weight = append(chartsList.Weight, currentWeight)
+		chartsList.DayAge = append(chartsList.DayAge, cow.GetDayAge())
+	}
+	// 返回数据
+	return &pasturePb.GrowthCurvesResponse{
+		Code:    http.StatusOK,
+		Message: "success",
+		Data: &pasturePb.GrowthCurveData{
+			Table:  cowData,
+			Charts: chartsList,
+		},
+	}, nil
+
+}

+ 5 - 0
module/backend/interface.go

@@ -46,6 +46,7 @@ type KptService interface {
 	EventService         // 事件相关
 	CowService           // 牛只相关
 	GoodsService         // 牧场物品相关
+	AnalyseService       // 分析相关
 }
 
 //go:generate mockgen -destination mock/SystemService.go -package kptservicemock kpt-pasture/module/backend SystemService
@@ -157,3 +158,7 @@ type GoodsService interface {
 	DrugsList(ctx context.Context, req *pasturePb.SearchDrugsRequest, pagination *pasturePb.PaginationModel) (*pasturePb.SearchDrugsResponse, error)
 	DrugsCreate(ctx context.Context, req *pasturePb.SearchDrugsList) error
 }
+
+type AnalyseService interface {
+	GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error)
+}