Browse Source

pasture: 配方数据同步

Yi 1 year ago
parent
commit
df57313156

+ 6 - 1
http/handle/group/feed_formula.go

@@ -28,7 +28,12 @@ func DistributeFeedFormula(c *gin.Context) {
 		return
 	}
 
-	if len(req.Body) <= 0 {
+	if len(req.FeedFormula) <= 0 {
+		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
+		return
+	}
+
+	if len(req.FeedFormulaDetail) <= 0 {
 		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
 		return
 	}

+ 5 - 3
migration/v0001_feedtemplet.sql

@@ -1,6 +1,8 @@
 ALTER TABLE `feedtemplet`
-    ADD COLUMN `is_modify` tinyint(1) unsigned NOT NULL DEFAULT '0' COMMENT '是否可修改 0 无效 1 是 2 否';
+    ADD COLUMN `is_modify` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否可修改 0 无效 1 是 2 否';
 
+ALTER TABLE `ftdetail`
+    ADD COLUMN `is_modify` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否可修改 0 无效 1 是 2 否';
 
 ## SELECT * FROM apisql WHERE sqlname = 'getFTList' AND ENABLE >0
 ## 需要更新的语句如下
@@ -62,10 +64,10 @@ WHEN 'accuracy' THEN ?
 WHEN 'sprinkleFeedTimeAllow' THEN ?
 WHEN 'domain' THEN ?
 END
-WHERE pastureid= ?
+WHERE pastureid= ?;
 
 ## 需要更新的语句如下params
-isGetLastPlan,isGetNextPlan,isEnableSupplyFeed,isEnableRemainFeed,reportDigit,isLockCount,remainOpt,waterOpt,overweightWarnRate,overweightBanRate,repertoryWarn,isfeedstorage,times,isEnableContract,remainOptDis,remainOptRate,isSmallMaterial,decimalPlaces,isDataSync,anyCar,decimalRate,accuracy,sprinkleFeedTimeAllow,domain,pastureid
+# isGetLastPlan,isGetNextPlan,isEnableSupplyFeed,isEnableRemainFeed,reportDigit,isLockCount,remainOpt,waterOpt,overweightWarnRate,overweightBanRate,repertoryWarn,isfeedstorage,times,isEnableContract,remainOptDis,remainOptRate,isSmallMaterial,decimalPlaces,isDataSync,anyCar,decimalRate,accuracy,sprinkleFeedTimeAllow,domain,pastureid
 
 
 ##  setp 1 需要增加的语句

+ 1 - 0
models/feed_template_detail.go

@@ -17,6 +17,7 @@ type FeedTemplateDetail struct {
 	AutoSecondName string  `xorm:"autosecondname" json:"auto_second_name"`
 	SplitFtPreId   int64   `xorm:"splitftpreid" json:"split_ft_pre_id"`
 	Deviation      int32   `xorm:"deviation" json:"deviation"`
+	IsModify       int32   `xorm:"is_modify" json:"is_modify"`
 }
 
 func (f *FeedTemplateDetail) TableName() string {

+ 24 - 2
models/group_data.go

@@ -5,8 +5,9 @@ import (
 )
 
 type PastureBodyRequest struct {
-	PastureId int64          `json:"pasture_id"`
-	Body      []*FeedFormula `json:"body"`
+	PastureId         int64                `json:"pasture_id"`
+	FeedFormula       []*FeedFormula       `json:"feed_formula"`
+	FeedFormulaDetail []*FeedFormulaDetail `json:"feed_formula_detail"`
 }
 
 type CancelDistributeFeedFormulaRequest struct {
@@ -36,6 +37,27 @@ type FeedFormula struct {
 	UpdatedAt          int64  `json:"updated_at"`
 }
 
+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"`
+	ForageId            int64  `json:"forage_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"`
+	Prefit              int64  `json:"prefit"`
+	IsShow              int32  `json:"is_show"`
+	IsModify            int32  `json:"is_modify"`
+	IsLockCowCountRatio int32  `json:"is_lock_cow_count_ratio"`
+	Sort                int32  `json:"sort"`
+	CreatedAt           int64  `json:"created_at"`
+	UpdatedAt           int64  `json:"updated_at"`
+}
+
 type PastureFeedFormulaIsModifyRequest struct {
 	PastureId     int64 `json:"pasture_id"`
 	FeedFormulaId int64 `json:"feed_formula_id"`

+ 37 - 7
module/group.go

@@ -15,11 +15,44 @@ const (
 )
 
 // DistributeFeedFormula 集团饲料配方下发
-func DistributeFeedFormula(res []*models.FeedTemplate) error {
-	if _, err := restful.Engine.Table(new(models.FeedTemplate)).Insert(&res); err != nil {
-		return err
+func DistributeFeedFormula(pastureId int64, feedTemplateList []*models.FeedTemplate, feedFormulaDetailList []*models.FeedFormulaDetail) error {
+
+	tx := restful.Engine.NewSession()
+	defer tx.Close()
+	for _, feedTemplate := range feedTemplateList {
+		if _, err := tx.Table(new(models.FeedTemplate)).Insert(feedTemplate); err != nil {
+			tx.Rollback()
+			return err
+		}
+		feedFormulaDetail := make([]*models.FeedTemplateDetail, 0)
+		for _, f := range feedFormulaDetailList {
+			feedFormulaDetail = append(feedFormulaDetail, &models.FeedTemplateDetail{
+				PastureId:      pastureId,
+				FtId:           feedTemplate.Id,
+				FId:            f.ForageId,
+				FName:          f.ForageName,
+				FWeight:        float64(f.Weight / 100),
+				IsLockCount:    f.IsLockCowCountRatio,
+				Sort:           f.Sort,
+				FeedGroup:      f.ForageGroupName,
+				PreFtId:        f.Prefit,
+				AutoSecond:     f.StirDelay,
+				AutoSecondName: "",
+				SplitFtPreId:   0,
+				Deviation:      f.AllowError,
+				IsModify:       f.IsModify,
+			})
+		}
+		if len(feedFormulaDetail) <= 0 {
+			continue
+		}
+		if _, err := tx.Table(new(models.FeedTemplateDetail)).Insert(feedFormulaDetail); err != nil {
+			tx.Rollback()
+			return err
+		}
 	}
-	return nil
+
+	return tx.Commit()
 }
 
 // CancelDistributeFeedFormula 集团饲料配方取消下发
@@ -250,7 +283,6 @@ func CattleCategoryDistribute(req *models.CowClass) error {
 }
 
 func ForageCategoryDistribute(req *models.FeedClass) error {
-
 	has, err := restful.Engine.Table(new(models.FeedClass).TableName()).Exist(&models.FeedClass{GroupId: req.GroupId})
 	if err != nil {
 		return err
@@ -354,12 +386,10 @@ func FeedTemplateDetailList(req *models.FeedListRequest) ([]*models.FeedTemplate
 	total, err = newSession.Table(new(models.FeedTemplateDetail).TableName()).
 		Where("pastureid = ?", req.PastureId).Count(&res)
 	if err != nil {
-		fmt.Println("=========FeedTemplateDetailList==========", err)
 		return nil, 0, err
 	}
 
 	if err = newSession.Table(new(models.FeedTemplateDetail).TableName()).Limit(int(req.PageSize), int(req.Page-1)*int(req.PageSize)).Find(&res); err != nil {
-		fmt.Println("=========newSession==========", err)
 		return nil, 0, err
 	}
 	return res, total, nil

+ 2 - 6
service/group/group.go

@@ -14,12 +14,8 @@ import (
 
 // DistributeFeedFormulaService 饲料配方下发牧场端
 func DistributeFeedFormulaService(req *models.PastureBodyRequest) error {
-	if len(req.Body) <= 0 {
-		return nil
-	}
-
 	feedTemplateList := make([]*models.FeedTemplate, 0)
-	for _, b := range req.Body {
+	for _, b := range req.FeedFormula {
 		feedTemplateList = append(feedTemplateList, &models.FeedTemplate{
 			PastureId:    req.PastureId,
 			TCode:        b.EncodeNumber,
@@ -40,7 +36,7 @@ func DistributeFeedFormulaService(req *models.PastureBodyRequest) error {
 			IsModify:     b.IsModify,
 		})
 	}
-	return module.DistributeFeedFormula(feedTemplateList)
+	return module.DistributeFeedFormula(req.PastureId, feedTemplateList, req.FeedFormulaDetail)
 }
 
 // CancelDistributeFeedFormulaService 取消饲料配方下发牧场端