Browse Source

feeding:add\

baishaojie 5 days ago
parent
commit
7ff37fd4ba

+ 1 - 1
Makefile

@@ -18,4 +18,4 @@ lint:
 build:
 	rm -rf bin
 	mkdir -p bin
-	GOARCH=amd64 GOOS=linux CGO_ENABLED=0 go build -o bin/kptTmrGroup -ldflags "-X kpt.kptyun.cn:3000/kpt-event/kpt-pasture/pod.appVersion=${version}" main.go
+	GOARCH=amd64 GOOS=windows CGO_ENABLED=0 go build -o bin/kptTmrGroup -ldflags "-X kpt.kptyun.cn:3000/kpt-event/kpt-pasture/pod.appVersion=${version}" main.go

+ 21 - 20
go.mod

@@ -3,7 +3,7 @@ module kpt-pasture
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20250704015606-a7700f1dd25c
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20250704074215-792bdf94697f
 	gitee.com/xuyiping_admin/pkg v0.0.0-20250613101634-36c36a2d27d0
 	github.com/dgrijalva/jwt-go v3.2.0+incompatible
 	github.com/eclipse/paho.mqtt.golang v1.4.3
@@ -34,16 +34,8 @@ require (
 
 require (
 	github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
-	github.com/gorilla/websocket v1.5.3 // indirect
-	github.com/nyaruka/phonenumbers v1.1.7 // indirect
-	github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
-	golang.org/x/arch v0.3.0 // indirect
-)
-
-require (
 	github.com/beorn7/perks v1.0.1 // indirect
 	github.com/bradfitz/gomemcache v0.0.0-20190913173617-a41fca850d0b // indirect
-	github.com/bytedance/sonic v1.10.1 // indirect
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
 	github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
 	github.com/chenzhuoyu/iasm v0.9.0 // indirect
@@ -54,17 +46,16 @@ require (
 	github.com/gin-contrib/sse v0.1.0 // indirect
 	github.com/go-playground/locales v0.14.1 // indirect
 	github.com/go-playground/universal-translator v0.18.1 // indirect
-	github.com/go-playground/validator/v10 v10.15.5 // indirect
-	github.com/go-redis/redis/v8 v8.11.2 // indirect
+	github.com/go-redis/redis/v8 v8.11.5 // indirect
 	github.com/go-sql-driver/mysql v1.7.0 // indirect
 	github.com/goccy/go-json v0.10.2 // indirect
 	github.com/google/uuid v1.6.0 // indirect
+	github.com/gorilla/websocket v1.5.3 // indirect
 	github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
 	github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
 	github.com/hashicorp/hcl v1.0.0 // indirect
 	github.com/huandu/xstrings v1.4.0 // indirect
 	github.com/inconshreveable/mousetrap v1.1.0 // indirect
-	github.com/jinzhu/copier v0.3.5
 	github.com/jinzhu/inflection v1.0.0 // indirect
 	github.com/jinzhu/now v1.1.5 // indirect
 	github.com/json-iterator/go v1.1.12 // indirect
@@ -78,18 +69,20 @@ require (
 	github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
 	github.com/modern-go/reflect2 v1.0.2 // indirect
 	github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
-	github.com/pelletier/go-toml/v2 v2.1.0 // indirect
+	github.com/nyaruka/phonenumbers v1.1.7 // indirect
+	github.com/onsi/gomega v1.27.1 // indirect
+	github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
 	github.com/pkg/errors v0.9.1 // indirect
 	github.com/pmezard/go-difflib v1.0.0 // indirect
-	github.com/prometheus/client_model v0.3.0 // indirect
-	github.com/prometheus/common v0.42.0 // indirect; indirec
+	github.com/prometheus/client_model v0.5.0 // indirect
+	github.com/prometheus/common v0.42.0 // indirect
 	github.com/prometheus/procfs v0.10.1 // indirect
 	github.com/richardlehane/mscfb v1.0.4 // indirect
 	github.com/richardlehane/msoleps v1.0.3 // indirect
 	github.com/robfig/cron v1.2.0 // indirect
 	github.com/robfig/cron/v3 v3.0.1 // indirect
 	github.com/sirupsen/logrus v1.9.3 // indirect
-	github.com/spf13/afero v1.9.5 // indirect
+	github.com/spf13/afero v1.10.0 // indirect
 	github.com/spf13/cast v1.5.1 // indirect
 	github.com/spf13/jwalterweatherman v1.1.0 // indirect
 	github.com/spf13/pflag v1.0.5 // indirect
@@ -97,16 +90,13 @@ require (
 	github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
 	github.com/ugorji/go/codec v1.2.11 // indirect
 	github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
-	github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect
-	github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect
 	github.com/xwb1989/sqlparser v0.0.0-20180606152119-120387863bf2 // indirect
 	go.uber.org/atomic v1.9.0 // indirect
 	go.uber.org/multierr v1.8.0 // indirect
+	golang.org/x/arch v0.3.0 // indirect
 	golang.org/x/crypto v0.23.0 // indirect
-	golang.org/x/net v0.25.0 // indirect
 	golang.org/x/sys v0.20.0 // indirect
 	golang.org/x/text v0.15.0 // indirect
-	golang.org/x/time v0.3.0
 	google.golang.org/appengine v1.6.8 // indirect
 	google.golang.org/genproto/googleapis/rpc v0.0.0-20240513163218-0867130af1f8 // indirect
 	google.golang.org/grpc v1.64.0 // indirect
@@ -114,3 +104,14 @@ require (
 	gopkg.in/ini.v1 v1.67.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )
+
+require (
+	github.com/bytedance/sonic v1.10.1 // indirect
+	github.com/go-playground/validator/v10 v10.15.5 // indirect
+	github.com/jinzhu/copier v0.3.5
+	github.com/pelletier/go-toml/v2 v2.1.0 // indirect
+	github.com/xuri/efp v0.0.0-20231025114914-d1ff6096ae53 // indirect
+	github.com/xuri/nfp v0.0.0-20230919160717-d98342af3f05 // indirect
+	golang.org/x/net v0.25.0 // indirect
+	golang.org/x/time v0.5.0
+)

File diff suppressed because it is too large
+ 1317 - 14
go.sum


+ 42 - 0
http/handler/feeding/feeding.go

@@ -0,0 +1,42 @@
+package feeding
+
+import (
+	"kpt-pasture/http/middleware"
+	"net/http"
+
+	feedingPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	"gitee.com/xuyiping_admin/pkg/apierr"
+	"gitee.com/xuyiping_admin/pkg/ginutil"
+	"github.com/gin-gonic/gin"
+)
+
+func GetFeedingHomepage(c *gin.Context) {
+	var req feedingPb.FeedingHomepageRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+	res, err := middleware.BackendOperation(c).OpsService.GetFeedingHomepage(c, &req)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
+// feeding/management
+
+func GetFeedingManagement(c *gin.Context) {
+	var req feedingPb.FeedingManagementRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	res, err := middleware.BackendOperation(c).OpsService.GetFeedingManagement(c, &req)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}

+ 19 - 0
http/route/feeding.go

@@ -0,0 +1,19 @@
+package route
+
+import (
+	"kpt-pasture/http/handler/feeding"
+
+	"github.com/gin-gonic/gin"
+)
+
+func FeedingAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
+	return func(s *gin.Engine) {
+		for _, opt := range opts {
+			opt(s)
+		}
+		feedingRoute := authRouteGroup(s, "/api/v1/feeding/")
+
+		feedingRoute.GET("tmrdata", feeding.GetFeedingHomepage)
+		feedingRoute.GET("management", feeding.GetFeedingManagement)
+	}
+}

+ 1 - 0
http/route/route.go

@@ -19,6 +19,7 @@ func HTTPServerRoute(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		MilkManageAPI(opts...),
 		WarningAPI(opts...),
 		TestAPI(opts...),
+		FeedingAPI(opts...),
 	}
 
 	return func(s *gin.Engine) {

+ 7 - 1
migrator/v0001_demo.sql

@@ -3,4 +3,10 @@ CREATE TABLE IF NOT EXISTS `demo` (
    `created_at` bigint(20) unsigned NOT NULL COMMENT '创建时间',
    `updated_at` bigint(20) unsigned NOT NULL COMMENT '更新时间',
    PRIMARY KEY (`id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='demo';
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='demo';
+
+
+
+ALTER TABLE `kpt_pasture`.`app_pasture_list` 
+ADD COLUMN `pasture_id` int(11) NULL COMMENT '饲喂系统牧场id' AFTER `updated_at`,
+ADD COLUMN `pasture_url` varchar(80) NULL COMMENT '饲喂系统url' AFTER `pasture_id`;

+ 2 - 0
model/app_pasture_list.go

@@ -27,6 +27,8 @@ type AppPastureList struct {
 	CreatedName          string                         `json:"createdName"`
 	CreatedAt            int64                          `json:"createdAt"`
 	UpdatedAt            string                         `json:"updatedAt"`
+	PastureId            int64                          `json:"pastureId"`
+	PastureUrl           string                         `json:"pastureUrl"`
 }
 
 func (a *AppPastureList) TableName() string {

+ 39 - 0
model/feeding.go

@@ -0,0 +1,39 @@
+package model
+
+type FeedingHomepageResponse struct {
+	Code int32                  `json:"code"`
+	Msg  string                 `json:"msg"`
+	Data []*FeedingHomepageData `json:"data"`
+}
+
+type FeedingHomepageData struct {
+	Cost       string `json:"cost"`       // "0.00"
+	DoneTimes  string `json:"doneTimes"`  // "23"
+	DoneWeight string `json:"doneWeight"` // "0.00"
+	MonRateHL  string `json:"monRateHL"`  // "100.03%"
+	MonRateSL  string `json:"monRateSL"`  // "98.66%"
+	MonRateSLR string `json:"monRateSLR"` // "69.59%"
+	Montime    string `json:"montime"`    // "5.41"
+	PlanTimes  string `json:"planTimes"`  // "41"
+	PlanWeight string `json:"planWeight"` // "0.00"
+	Temtime    string `json:"temtime"`    // "7.04"
+	TotalCost  string `json:"totalCost"`  // "0.00"
+	YesRateHL  string `json:"yesRateHL"`  // "99.92%"
+	YesRateSL  string `json:"yesRateSL"`  // "99.92%"
+	YesRateSLR string `json:"yesRateSLR"` // "72.54%"
+}
+
+type FeedingManagementResponse struct {
+	Code int32                    `json:"code"`
+	Msg  string                   `json:"msg"`
+	Data []*FeedingManagementData `json:"data"`
+}
+
+type FeedingManagementData struct {
+	ActualWeightMinus string `json:"actualweightminus"` // "15456.23"
+	CCount            string `json:"ccount"`            // "147"
+	Cost              string `json:"cost"`              // "0.00"
+	DryWeight         string `json:"dryweight"`         // "0.00"
+	TotalCost         string `json:"totalcost"`         // "0.00"
+	TypeA             string `json:"typea"`             // "11"
+}

+ 167 - 0
module/backend/feeding.go

@@ -0,0 +1,167 @@
+package backend
+
+import (
+	"bytes"
+	"context"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"kpt-pasture/model"
+	"net/http"
+	"time"
+
+	feedingPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/cow"
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"gitee.com/xuyiping_admin/pkg/xerr"
+	"go.uber.org/zap"
+)
+
+type FeedingHttp struct {
+	authClient *http.Client
+}
+
+func NewFeedingService() *FeedingHttp {
+	return &FeedingHttp{
+		authClient: &http.Client{
+			Timeout: time.Duration(60) * time.Second,
+		},
+	}
+}
+
+func (c *FeedingHttp) doRequest(req *http.Request, response interface{}) ([]byte, error) {
+	resp, err := http.DefaultClient.Do(req)
+	if err != nil {
+		zaplog.Error("ClientService", zap.Any("authClient.Do", err))
+		return nil, xerr.WithStack(err)
+	}
+	b, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		zaplog.Error("ClientService", zap.Any("ioutil.ReadAll", err))
+		return nil, xerr.WithStack(err)
+	}
+	if resp.StatusCode != http.StatusOK {
+		if len(b) > 0 {
+			return nil, xerr.Customf("err:%v,body:%s", err, string(b))
+		} else {
+			return nil, xerr.Customf("err:%v", err)
+		}
+	}
+	fmt.Println(string(b))
+	if err = json.Unmarshal(b, response); err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	return b, nil
+}
+
+func (c *FeedingHttp) DoGet(url string, response interface{}) ([]byte, error) {
+	req, err := http.NewRequest(http.MethodGet, url, nil)
+	if err != nil {
+		zaplog.Error("ClientService", zap.Any("DoGet", err))
+		return nil, err
+	}
+	req.Header.Add("Accept", "application/json")
+	req.Header.Add("Content-Type", "application/json")
+	return c.doRequest(req, response)
+}
+
+func (c *FeedingHttp) DoPost(url string, body interface{}, response *interface{}) ([]byte, error) {
+	b, err := json.Marshal(body)
+	if err != nil {
+		zaplog.Error("ClientService", zap.Any("DoPost-Marshal", err))
+		return nil, xerr.WithStack(err)
+	}
+	req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(b))
+	if err != nil {
+		zaplog.Error("ClientService", zap.Any("NewRequest", err))
+		return nil, xerr.WithStack(err)
+	}
+	req.Header.Add("Accept", "application/json")
+	req.Header.Add("Content-Type", "application/json")
+	return c.doRequest(req, response)
+}
+
+func (s *StoreEntry) GetFeedingHomepage(ctx context.Context, req *feedingPb.FeedingHomepageRequest) (*feedingPb.FeedingHomepageResponse, error) {
+	var pasture model.AppPastureList
+	if err := s.DB.Debug().Table(new(model.AppPastureList).TableName()).Where("farm_id = ?", req.FarmId).First(&pasture).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if pasture.PastureId == 0 {
+		return nil, xerr.Customf("牧场不存在")
+	}
+
+	url := fmt.Sprintf("%s/feeding/tmrdata", pasture.PastureUrl)
+	fmt.Println(url)
+	client := NewFeedingService()
+
+	response := &model.FeedingHomepageResponse{Data: []*model.FeedingHomepageData{}}
+	_, err := client.DoGet(url, response)
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	fmt.Println(response)
+	res := &feedingPb.FeedingHomepageResponse{
+		Code: response.Code,
+		Msg:  response.Msg,
+		Data: &feedingPb.FeedingHomepageData{},
+	}
+
+	for _, v := range response.Data {
+		res.Data = &feedingPb.FeedingHomepageData{
+			Cost:       v.Cost,
+			DoneTimes:  v.DoneTimes,
+			DoneWeight: v.DoneWeight,
+			MonRateHL:  v.MonRateHL,
+			MonRateSL:  v.MonRateSL,
+			MonRateSLR: v.MonRateSLR,
+			Montime:    v.Montime,
+			PlanTimes:  v.PlanTimes,
+			PlanWeight: v.PlanWeight,
+			Temtime:    v.Temtime,
+			TotalCost:  v.TotalCost,
+			YesRateHL:  v.YesRateHL,
+			YesRateSL:  v.YesRateSL,
+			YesRateSLR: v.YesRateSLR,
+		}
+	}
+
+	return res, nil
+}
+
+func (s *StoreEntry) GetFeedingManagement(ctx context.Context, req *feedingPb.FeedingManagementRequest) (*feedingPb.FeedingManagementResponse, error) {
+	var pasture model.AppPastureList
+	if err := s.DB.Debug().Table(new(model.AppPastureList).TableName()).Where("farm_id = ?", req.FarmId).First(&pasture).Error; err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	if pasture.PastureId == 0 {
+		return nil, xerr.Customf("牧场不存在")
+	}
+
+	url := fmt.Sprintf("%s/feeding/management?typea=%s&startdate=%s&enddate=%s", pasture.PastureUrl, req.Typea, req.Startdate, req.Enddate)
+	fmt.Println(url)
+	client := NewFeedingService()
+
+	response := &model.FeedingManagementResponse{Data: []*model.FeedingManagementData{}}
+	_, err := client.DoGet(url, response)
+	if err != nil {
+		return nil, xerr.WithStack(err)
+	}
+	fmt.Println(response)
+	res := &feedingPb.FeedingManagementResponse{
+		Code: response.Code,
+		Msg:  response.Msg,
+		Data: []*feedingPb.FeedingManagementData{},
+	}
+
+	for _, v := range response.Data {
+		res.Data = append(res.Data, &feedingPb.FeedingManagementData{
+			Actualweightminus: v.ActualWeightMinus,
+			Ccount:            v.CCount,
+			Cost:              v.Cost,
+			Dryweight:         v.DryWeight,
+			Totalcost:         v.TotalCost,
+			Typea:             v.TypeA,
+		})
+	}
+
+	return res, nil
+}

+ 6 - 0
module/backend/interface.go

@@ -61,6 +61,7 @@ type KptService interface {
 	UploadService        // 上传文件相关
 	WarningService       // 预警相关
 	TestService          // 测试相关
+	FeedingService       // 饲喂相关
 }
 
 //go:generate mockgen -destination mock/SystemService.go -package kptservicemock kpt-pasture/module/backend SystemService
@@ -377,3 +378,8 @@ type TestService interface {
 	CalvingAge(ctx context.Context) error
 	SystemMenuInit(ctx context.Context) error
 }
+
+type FeedingService interface {
+	GetFeedingHomepage(ctx context.Context, req *pasturePb.FeedingHomepageRequest) (*pasturePb.FeedingHomepageResponse, error)
+	GetFeedingManagement(ctx context.Context, req *pasturePb.FeedingManagementRequest) (*pasturePb.FeedingManagementResponse, error)
+}

+ 4 - 3
service/wechat/http.go

@@ -3,12 +3,13 @@ package wechat
 import (
 	"bytes"
 	"encoding/json"
-	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
-	"gitee.com/xuyiping_admin/pkg/xerr"
 	"io/ioutil"
 	"net/http"
 	"time"
 
+	"gitee.com/xuyiping_admin/pkg/logger/zaplog"
+	"gitee.com/xuyiping_admin/pkg/xerr"
+
 	"go.uber.org/zap"
 )
 
@@ -23,7 +24,7 @@ func NewClientService(appid, secret string) *ClientService {
 		AppID:  appid,
 		Secret: secret,
 		authClient: &http.Client{
-			Timeout: time.Duration(5) * time.Second,
+			Timeout: time.Duration(60) * time.Second,
 		},
 	}
 }

Some files were not shown because too many files changed in this diff