Browse Source

Merge branch 'feature/bug-list' of xuyiping/kpt-tmr-group into develop

xuyiping 1 year ago
parent
commit
21423549dc

+ 18 - 0
http/debug/debug.go

@@ -51,3 +51,21 @@ func FeedSync(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func FeedFormulaDetailListSync(c *gin.Context) {
+	var req operationPb.FeedFormulaSyncRequest
+	if err := ginutil.BindProto(c, &req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.FeedFormulaDetailListSyncData(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}

+ 21 - 0
http/handler/feed/feed_formula.go

@@ -109,6 +109,27 @@ func SearchFeedFormulaList(c *gin.Context) {
 	ginutil.JSONResp(c, res)
 }
 
+func SearchFeedFormulaByForageList(c *gin.Context) {
+	req := &operationPb.SearchFeedFormulaRequest{}
+	if err := ginutil.BindProto(c, req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	req.Pagination = &operationPb.PaginationModel{
+		Page:       int32(c.GetInt(middleware.Page)),
+		PageSize:   int32(c.GetInt(middleware.PageSize)),
+		PageOffset: int32(c.GetInt(middleware.PageOffset)),
+	}
+
+	res, err := middleware.BackendOperation(c).OpsService.SearchFeedFormulaList(c, req)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, res)
+}
+
 func DeleteFeedFormula(c *gin.Context) {
 	feedFormulaIdStr := c.Param("feed_formula_id")
 	feedFormulaId, _ := strconv.Atoi(feedFormulaIdStr)

+ 3 - 2
http/route/api_debug_route.go

@@ -19,8 +19,9 @@ func DebugAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		debugRoute := authRouteGroup(s, "/api/v1/kpt/debug/")
 		// kpt debug api
 		debugRoute.GET("hello", debug.HelloOk)
-		debugRoute.POST("pasture/feed_formula/sync", debug.FeedFormulaSync) // 同步牧场端饲料配方
-		debugRoute.POST("pasture/feed/sync", debug.FeedSync)                // 同步牧场端饲料
+		debugRoute.POST("pasture/feed_formula/sync", debug.FeedFormulaSync)                       // 同步牧场端饲料配方
+		debugRoute.POST("pasture/feed/sync", debug.FeedSync)                                      // 同步牧场端饲料
+		debugRoute.POST("pasture/feed_formula/detail_list/sync", debug.FeedFormulaDetailListSync) // 同步牧场端饲料配方详情
 	}
 
 }

+ 1 - 0
http/route/ops_api.go

@@ -55,6 +55,7 @@ func OpsAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		opsRoute.POST("/feed_formula/add", feed.AddFeedFormula)
 		opsRoute.POST("/feed_formula/edit", feed.EditFeedFormula)
 		opsRoute.POST("/feed_formula/list", feed.SearchFeedFormulaList)
+		opsRoute.POST("/feed_formula/forage_list", feed.SearchFeedFormulaByForageList)
 		opsRoute.DELETE("/feed_formula/delete/:feed_formula_id", feed.DeleteFeedFormula)
 		opsRoute.POST("/feed_formula/is_modify_show", feed.IsShowModifyFeedFormula)
 		opsRoute.POST("/feed_formula/excel_export", feed.ExcelExportFeedFormula)

+ 1 - 1
model/feed_formula.go

@@ -46,7 +46,7 @@ func NewFeedFormula(req *operationPb.AddFeedFormulaRequest) *FeedFormula {
 		Remarks:            req.Remarks,
 		Version:            0,
 		PastureId:          0,
-		PastureName:        "",
+		PastureName:        "集团",
 		IsShow:             req.IsShow,
 		IsDelete:           operationPb.IsShow_OK,
 		IsModify:           operationPb.IsShow_OK,

+ 25 - 0
model/feed_formula_detail.go

@@ -0,0 +1,25 @@
+package model
+
+import operationPb "kpt-tmr-group/proto/go/backend/operation"
+
+type FeedFormulaDetail struct {
+	Id              int64                   `json:"id"`
+	PastureId       int64                   `json:"pasture_id"`
+	PastureName     string                  `json:"pasture_name"`
+	PastureDataId   int64                   `json:"pasture_data_id"`
+	FeedFormulaId   int64                   `json:"feed_formula_id"`
+	ForgeId         int64                   `json:"forge_id"`
+	ForageName      string                  `json:"forage_name"`
+	ForageGroupName string                  `json:"forage_group_name"`
+	Weight          int32                   `json:"weight"`
+	StirDelay       int32                   `json:"stir_delay"`
+	AllowError      int32                   `json:"allow_error"`
+	IsShow          operationPb.IsShow_Kind `json:"is_show"`
+	Sort            int32                   `json:"sort"`
+	CreatedAt       int64                   `json:"created_at"`
+	UpdatedAt       int64                   `json:"updated_at"`
+}
+
+func (f *FeedTemplateDetail) TableName() string {
+	return "feed_formula_detail"
+}

+ 1 - 0
model/forage.go

@@ -46,6 +46,7 @@ func (c *Forage) TableName() string {
 func NewForage(req *operationPb.AddForageRequest) *Forage {
 	return &Forage{
 		Name:               req.Name,
+		PastureName:        "集团",
 		CategoryId:         int64(req.CategoryId),
 		CategoryName:       req.CategoryName,
 		UniqueEncode:       req.UniqueEncode,

+ 32 - 31
model/group_pasture.go

@@ -16,37 +16,6 @@ import (
 	"go.uber.org/zap"
 )
 
-type GroupPasture struct {
-	Id             int64                   `json:"id,omitempty"`
-	Name           string                  `json:"name,omitempty"`
-	PastureId      int64                   `json:"pasture_id"`
-	Account        string                  `json:"account,omitempty"`
-	Password       string                  `json:"password"`
-	ManagerUser    string                  `json:"manager_user"`
-	ManagerPhone   string                  `json:"manager_phone"`
-	Domain         string                  `json:"domain"`
-	Extranet       string                  `json:"extranet"`
-	Intranet       string                  `json:"intranet"`
-	IsShow         operationPb.IsShow_Kind `json:"is_show,omitempty"`
-	IsDelete       operationPb.IsShow_Kind `json:"is_delete,omitempty"`
-	IsDistribution operationPb.IsShow_Kind `json:"is_distribution"`
-	Address        string                  `json:"address"`
-	CreatedAt      int64                   `json:"created_at,omitempty"`
-	UpdatedAt      int64                   `json:"updated_at,omitempty"`
-}
-
-func (g *GroupPasture) TableName() string {
-	return "group_pasture"
-}
-
-// GetPastureId 获取牧场id
-func (g *GroupPasture) GetPastureId() int64 {
-	if g.PastureId > 0 {
-		return g.PastureId
-	}
-	return g.Id
-}
-
 const (
 	InitManagerPassword = "123456"
 	UrlDataByName       = "authdata/GetDataByName"
@@ -60,6 +29,7 @@ const (
 	FeedFormulaCancelDistributeUrl = "pasture/feed_formula/cancel/distribute"
 	FeedFormulaIsModifyUrl         = "pasture/feed_formula/is_modify"
 	FeedFormulaAsyncUrl            = "pasture/feed_formula/async"
+	FeedFormulaDetailListAsyncUrl  = "pasture/feed_formula/detail_list"
 	FeedAsyncUrl                   = "pasture/feed/async"
 	DashboardAccuracyUrl           = "pasture/dashboard/accuracy_data"
 	DashboardExecTimeUrl           = "pasture/dashboard/process_analysis"
@@ -89,6 +59,37 @@ var (
 	}
 )
 
+type GroupPasture struct {
+	Id             int64                   `json:"id,omitempty"`
+	Name           string                  `json:"name,omitempty"`
+	PastureId      int64                   `json:"pasture_id"`
+	Account        string                  `json:"account,omitempty"`
+	Password       string                  `json:"password"`
+	ManagerUser    string                  `json:"manager_user"`
+	ManagerPhone   string                  `json:"manager_phone"`
+	Domain         string                  `json:"domain"`
+	Extranet       string                  `json:"extranet"`
+	Intranet       string                  `json:"intranet"`
+	IsShow         operationPb.IsShow_Kind `json:"is_show,omitempty"`
+	IsDelete       operationPb.IsShow_Kind `json:"is_delete,omitempty"`
+	IsDistribution operationPb.IsShow_Kind `json:"is_distribution"`
+	Address        string                  `json:"address"`
+	CreatedAt      int64                   `json:"created_at,omitempty"`
+	UpdatedAt      int64                   `json:"updated_at,omitempty"`
+}
+
+func (g *GroupPasture) TableName() string {
+	return "group_pasture"
+}
+
+// GetPastureId 获取牧场id
+func (g *GroupPasture) GetPastureId() int64 {
+	if g.PastureId > 0 {
+		return g.PastureId
+	}
+	return g.Id
+}
+
 type PastureTokenRequest struct {
 	UserName string `json:"username"`
 	Password string `json:"password"`

+ 32 - 0
model/pasture_data.go

@@ -169,3 +169,35 @@ type Feed struct {
 	Backup2        string  `xorm:"backup2" json:"backup2"`
 	Backup3        string  `xorm:"backup3" json:"backup3"`
 }
+
+type FeedFormulaDetailListResponse struct {
+	Code int32                  `json:"code"`
+	Msg  string                 `json:"msg"`
+	Data *FeedFormulaDetailData `json:"data"`
+}
+
+type FeedFormulaDetailData struct {
+	Total    int32                 `json:"total"`
+	Page     int32                 `json:"page"`
+	PageSize int32                 `json:"page_size"`
+	List     []*FeedTemplateDetail `json:"list"`
+}
+
+type FeedTemplateDetail struct {
+	Id             int64   `json:"id"`
+	PastureId      int64   `json:"pasture_id"`
+	FtId           int64   `json:"ft_id"`
+	FId            int64   `json:"f_id"`
+	FdName         string  `json:"fd_name"`
+	LWeight        float64 `json:"l_weight"`
+	FWeight        float64 `json:"f_weight"`
+	IsLockCount    int32   `json:"is_lock_count"`
+	IsFg           int32   `json:"is_fg"`
+	Sort           int32   `json:"sort"`
+	FeedGroup      string  `json:"feed_group"`
+	PreFtId        int64   `json:"pre_ft_id"`
+	AutoSecond     int32   `json:"auto_second"`
+	AutoSecondName string  `json:"auto_second_name"`
+	SplitFtPreId   int64   `json:"split_ft_pre_id"`
+	Deviation      int32   `json:"deviation"`
+}

+ 1 - 0
module/backend/interface.go

@@ -170,4 +170,5 @@ type PastureSyncService interface {
 	CategoryDeleteData(ctx context.Context, req *operationPb.CategoryDeleteRequest) error
 	FeedFormulaSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error
 	FeedSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error
+	FeedFormulaDetailListSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error
 }

+ 92 - 3
module/backend/pasture_sync_service.go

@@ -230,21 +230,32 @@ func (s *StoreEntry) FeedSyncData(ctx context.Context, req *operationPb.FeedForm
 	return nil
 }
 
+// FeedInsert 饲料数据同步 todo 后面优化成有就更新,没有则新增
 func (s *StoreEntry) FeedInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.Feed) error {
 	res := make([]*model.Forage, 0)
 	for _, data := range list {
-
 		forageSourceName := data.Source
 		forageSourceId := operationPb.ForageSource_SYSTEM_BUILT_IN
 		if forageSourceName == "用户自定义" {
 			forageSourceId = operationPb.ForageSource_USER_DEFINED
 		}
 
+		confirmStart := operationPb.IsShow_INVALID
+		if data.ConfirmStart == 0 {
+			confirmStart = operationPb.IsShow_NO
+		}
+		if data.ConfirmStart == 1 {
+			confirmStart = operationPb.IsShow_OK
+		}
+
+		if data.Id <= 0 {
+			continue
+		}
 		res = append(res, &model.Forage{
 			Name:               data.FName,
 			CategoryId:         data.FClassId,
 			CategoryName:       data.FClass,
-			MaterialType:       0,
+			MaterialType:       0, // 牧场端找不到这个字段
 			UniqueEncode:       data.FeedCode,
 			ForageSourceId:     forageSourceId,
 			ForageSourceName:   forageSourceName,
@@ -256,17 +267,95 @@ func (s *StoreEntry) FeedInsert(ctx context.Context, groupPasture *model.GroupPa
 			Price:              int64(data.UPrice * 100),
 			JumpWeight:         data.AutoZone,
 			JumpDelay:          operationPb.JumpDelaType_Kind(data.AutoSecond),
-			ConfirmStart:       operationPb.IsShow_Kind(data.ConfirmStart),
+			ConfirmStart:       confirmStart,
 			RelayLocations:     int64(data.TrgAddress),
 			Jmp:                operationPb.IsShow_Kind(data.Jmp),
 			DataSource:         operationPb.DataSource_FROM_PASTURE,
 			Sort:               int64(data.Sort),
+			PastureId:          groupPasture.PastureId,
 			PastureName:        groupPasture.Name,
 			PastureDataId:      data.Id,
 			IsShow:             operationPb.IsShow_Kind(data.Enable),
 			IsDelete:           operationPb.IsShow_OK,
 		})
 	}
+
+	if err := s.DB.Model(new(model.Forage)).Create(res).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}
+
+// FeedFormulaDetailListSyncData 饲料配方详情同步
+func (s *StoreEntry) FeedFormulaDetailListSyncData(ctx context.Context, req *operationPb.FeedFormulaSyncRequest) error {
+	groupPastureList, err := s.DataSyncGroupPastureList(ctx)
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+
+	var (
+		ok           bool
+		groupPasture *model.GroupPasture
+	)
+	for _, pastureDetail := range groupPastureList {
+		if pastureDetail.Id != int64(req.PastureId) {
+			continue
+		}
+		groupPasture = pastureDetail
+		ok = true
+	}
+
+	if ok {
+		total := 0
+		page := 1
+		pageSize := 100
+		for i := 0; i <= total; i++ {
+			body := &model.FeedFormulaListRequest{
+				PastureId: int32(groupPasture.PastureId),
+				Page:      int32(page),
+				PageSize:  int32(pageSize),
+			}
+			response := &model.FeedFormulaDetailListResponse{}
+			if _, err = s.PastureHttpClient(ctx, model.FeedFormulaDetailListAsyncUrl, int64(req.PastureId), body, response); err != nil {
+				return xerr.WithStack(err)
+			}
+
+			if response.Code != http.StatusOK {
+				return xerr.Customf("%s", response.Msg)
+			}
+			if response.Data.Total > 0 && response.Data.List != nil {
+				total = int(math.Ceil(float64(response.Data.Total) / float64(pageSize)))
+			}
+			if err = s.FeedFormulaDetailInsert(ctx, groupPasture, response.Data.List); err != nil {
+				return xerr.WithStack(err)
+			}
+		}
+
+	}
+	return nil
+}
+
+func (s *StoreEntry) FeedFormulaDetailInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.FeedTemplateDetail) error {
+	res := make([]*model.FeedFormulaDetail, 0)
+	for _, data := range list {
+		if data.Id <= 0 {
+			continue
+		}
+		res = append(res, &model.FeedFormulaDetail{
+			PastureId:       groupPasture.PastureId,
+			PastureName:     groupPasture.Name,
+			PastureDataId:   data.Id,
+			ForgeId:         data.FId,
+			FeedFormulaId:   data.FtId,
+			Weight:          int32(data.FWeight * 100),
+			ForageGroupName: data.FeedGroup,
+			StirDelay:       data.AutoSecond,
+			AllowError:      data.Deviation,
+			IsShow:          operationPb.IsShow_OK,
+			Sort:            data.Sort,
+		})
+	}
+
 	if err := s.DB.Model(new(model.Forage)).Create(res).Error; err != nil {
 		return xerr.WithStack(err)
 	}