Procházet zdrojové kódy

dashboard: 首页饼图

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

+ 1 - 1
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20240607071100-7ceeb27fa509
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20240611054034-685974cf4140
 	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

+ 6 - 0
go.sum

@@ -140,6 +140,12 @@ gitee.com/xuyiping_admin/go_proto v0.0.0-20240607064246-3237fc0007bc h1:9bSZpry6
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240607064246-3237fc0007bc/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240607071100-7ceeb27fa509 h1:e7+0jjy1IcTM7CiyxyXREcp75yLF+60Vs3w7sEoTsT0=
 gitee.com/xuyiping_admin/go_proto v0.0.0-20240607071100-7ceeb27fa509/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240607081232-f45f3bb44464 h1:ZeAaJI/3yti7v/as/df6TnJa1WiGvSvBU7R/fZj9BJ4=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240607081232-f45f3bb44464/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240607081535-3e16d4bd0c36 h1:9xGMQvqm7y7a9ZWX+lVMMx8pFk0wKLnVXw3BdWYuBEQ=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240607081535-3e16d4bd0c36/go.mod h1:BKrFW6YLDectlQcQk3FYKBeXvjEiodAKJ5rq7O/QiPE=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240611054034-685974cf4140 h1:gZVMEIgfekdsKky/9JaSNgrbzXX/6tRf24PnaNl7mZM=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20240611054034-685974cf4140/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=

+ 18 - 0
http/handler/dashboard/bar.go

@@ -0,0 +1,18 @@
+package dashboard
+
+import (
+	"kpt-pasture/http/middleware"
+
+	"gitee.com/xuyiping_admin/pkg/apierr"
+	"gitee.com/xuyiping_admin/pkg/ginutil"
+	"github.com/gin-gonic/gin"
+)
+
+func Bar(c *gin.Context) {
+	res, err := middleware.Dependency(c).StoreEventHub.OpsService.Bar(c)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}

+ 17 - 0
http/route/dashboard.go

@@ -0,0 +1,17 @@
+package route
+
+import (
+	"github.com/gin-gonic/gin"
+	"kpt-pasture/http/handler/dashboard"
+)
+
+func DashboardApi(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
+	return func(s *gin.Engine) {
+		for _, opt := range opts {
+			opt(s)
+		}
+		// CowAPI API 组  牛只信息
+		eventRoute := authRouteGroup(s, "/api/v1/dashboard/")
+		eventRoute.GET("/bar", dashboard.Bar)
+	}
+}

+ 1 - 0
http/route/route.go

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

+ 17 - 0
model/cow.go

@@ -1,6 +1,7 @@
 package model
 
 import (
+	"fmt"
 	"math"
 	"time"
 
@@ -98,6 +99,22 @@ func NewCalfCow(motherId, fatherId int64, calf *EventCalvingCalf) *Cow {
 	}
 }
 
+type BarCowStruct struct {
+	Number int32                  `json:"number"`
+	TypeId pasturePb.CowType_Kind `json:"type_id"`
+}
+
+type BarCowStructSlice []*BarCowStruct
+
+func (b BarCowStructSlice) ToPB(cowTypeMap map[pasturePb.CowType_Kind]string, count int32) []*pasturePb.BarCowStruct {
+	var pb []*pasturePb.BarCowStruct
+	for _, v := range b {
+		name := fmt.Sprintf("%s", cowTypeMap[v.TypeId])
+		pb = append(pb, &pasturePb.BarCowStruct{Name: name, Value: v.Number})
+	}
+	return pb
+}
+
 // GetDayAge 日龄
 func (c *Cow) GetDayAge() int32 {
 	return int32(math.Floor(float64(time.Now().Unix()-c.BirthAt) / 86400))

+ 1 - 1
model/event_seme_time.go

@@ -27,7 +27,7 @@ func NewEventSemeTime(currentUser *SystemUser, req *pasturePb.SemeTimeRequest) *
 		Name:           req.Form.Name,
 		WeekType:       req.Form.WeekType,
 		CowType:        req.Form.CowType,
-		IsShow:         pasturePb.IsShow_No,
+		IsShow:         pasturePb.IsShow_Ok,
 		IsDelete:       pasturePb.IsShow_Ok,
 		PostpartumDays: req.Form.PostpartumDays,
 		Remarks:        req.Form.Remarks,

+ 9 - 0
module/backend/config_data.go

@@ -645,6 +645,14 @@ func (s *StoreEntry) CowTypeMap() map[pasturePb.CowType_Kind]string {
 	return res
 }
 
+func (s *StoreEntry) CowStatusMap() map[pasturePb.CowStatus_Kind]string {
+	res := make(map[pasturePb.CowStatus_Kind]string)
+	for _, v := range s.CowStatusEnumList() {
+		res[pasturePb.CowStatus_Kind(v.Value)] = v.Label
+	}
+	return res
+}
+
 func (s *StoreEntry) BarnTypeOptions(ctx context.Context) (*pasturePb.ConfigOptionsListResponse, error) {
 	return &pasturePb.ConfigOptionsListResponse{
 		Code:    http.StatusOK,
@@ -652,6 +660,7 @@ func (s *StoreEntry) BarnTypeOptions(ctx context.Context) (*pasturePb.ConfigOpti
 		Data:    s.BarnTypeEnumList(),
 	}, nil
 }
+
 func (s *StoreEntry) BarnListOptions(ctx context.Context, penType int) (*pasturePb.ConfigOptionsListResponse, error) {
 	penList := make([]*model.Pen, 0)
 	pref := s.DB.Table(new(model.Pen).TableName()).

+ 35 - 0
module/backend/dashboard.go

@@ -0,0 +1,35 @@
+package backend
+
+import (
+	"context"
+	"kpt-pasture/model"
+	"net/http"
+
+	"gitee.com/xuyiping_admin/pkg/xerr"
+
+	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+)
+
+func (s *StoreEntry) Bar(ctx context.Context) (*pasturePb.BarCowStructResponse, error) {
+	barCowStructList := make([]*model.BarCowStruct, 0)
+	var count int32 = 0
+	if err := s.DB.Model(new(model.Cow)).Select("COUNT(*) AS number ,type_id").
+		Where("is_remove = ?", pasturePb.IsShow_Ok).
+		Group("type_id").
+		Find(&barCowStructList).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	cowTypeMap := s.CowTypeMap()
+
+	for _, v := range barCowStructList {
+		count += v.Number
+	}
+	return &pasturePb.BarCowStructResponse{
+		Code:    http.StatusOK,
+		Message: "ok",
+		Data: &pasturePb.BarCowStructData{
+			List:  model.BarCowStructSlice(barCowStructList).ToPB(cowTypeMap, count),
+			Total: count,
+		},
+	}, nil
+}

+ 5 - 0
module/backend/interface.go

@@ -47,6 +47,7 @@ type KptService interface {
 	CowService           // 牛只相关
 	GoodsService         // 牧场物品相关
 	AnalyseService       // 分析相关
+	DashboardService     // 牧场统计相关
 }
 
 //go:generate mockgen -destination mock/SystemService.go -package kptservicemock kpt-pasture/module/backend SystemService
@@ -162,3 +163,7 @@ type GoodsService interface {
 type AnalyseService interface {
 	GrowthCurve(ctx context.Context, req *pasturePb.SearchGrowthCurvesRequest) (*pasturePb.GrowthCurvesResponse, error)
 }
+
+type DashboardService interface {
+	Bar(ctx context.Context) (*pasturePb.BarCowStructResponse, error)
+}