Browse Source

feed: 畜牧分类和饲料分类下发牧场

Yi 1 year ago
parent
commit
553e80c4b1
3 changed files with 124 additions and 2 deletions
  1. 2 0
      model/group_pasture.go
  2. 11 0
      model/pasture_data.go
  3. 111 2
      module/backend/pasture_service.go

+ 2 - 0
model/group_pasture.go

@@ -53,6 +53,8 @@ const (
 	DashboardExecTimeUrl          = "pasture/dashboard/process_analysis"
 	DashboardSprinkleFeedTimeUrl  = "pasture/dashboard/sprinkle_statistics"
 	PastureAccountDistributionURl = "pasture/account/distribute"
+	ForageCategoryDistributionURl = "pasture/forage_category/distribute"
+	CattleCategoryDistributionURl = "pasture/cattle_category/distribute"
 )
 
 var (

+ 11 - 0
model/pasture_data.go

@@ -50,3 +50,14 @@ type AccountDistribution struct {
 	Phone     string `json:"phone"`
 	PastureId int32  `json:"pasture_id"`
 }
+
+type CategoryRequest struct {
+	PastureId  int32  `json:"pasture_id"`
+	ParentId   int32  `json:"parent_id"`
+	ParentName string `json:"parent_name"`
+	Name       string `json:"name"`
+	Id         int32  `json:"id"`
+	Number     string `json:"number"`
+	IsShow     int32  `json:"is_show"`
+	GroupId    int32  `json:"group_id"`
+}

+ 111 - 2
module/backend/pasture_service.go

@@ -13,6 +13,7 @@ import (
 	operationPb "kpt-tmr-group/proto/go/backend/operation"
 	"net/http"
 	"strconv"
+	"sync"
 
 	"go.uber.org/zap"
 
@@ -61,7 +62,6 @@ func (s *StoreEntry) PastureAccountDistribution(ctx context.Context, groupPastur
 			return xerr.Customf("%s", res.Msg)
 		}
 	}
-
 	return nil
 }
 
@@ -210,6 +210,19 @@ func (s *StoreEntry) AddCattleCategory(ctx context.Context, req *operationPb.Add
 	if err := s.DB.Create(cattleCategory).Error; err != nil {
 		return xerr.WithStack(err)
 	}
+	defer func() {
+		body := &model.CategoryRequest{
+			ParentId:   int32(req.ParentId),
+			ParentName: req.ParentName,
+			Name:       req.Name,
+			Id:         int32(req.Id),
+			Number:     req.Number,
+			IsShow:     int32(req.IsShow),
+			GroupId:    int32(cattleCategory.Id),
+		}
+		s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body)
+	}()
+
 	return nil
 }
 
@@ -234,6 +247,19 @@ func (s *StoreEntry) EditCattleCategory(ctx context.Context, req *operationPb.Ad
 		Updates(updateData).Error; err != nil {
 		return xerr.WithStack(err)
 	}
+
+	defer func() {
+		body := &model.CategoryRequest{
+			ParentId:   int32(req.ParentId),
+			ParentName: req.ParentName,
+			Name:       req.Name,
+			Id:         int32(req.Id),
+			Number:     req.Number,
+			IsShow:     int32(req.IsShow),
+			GroupId:    int32(req.Id),
+		}
+		s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body)
+	}()
 	return nil
 }
 
@@ -250,6 +276,18 @@ func (s *StoreEntry) IsShowCattleCategory(ctx context.Context, req *operationPb.
 	if err := s.DB.Model(new(model.CattleCategory)).Where("id = ?", req.CattleCategoryId).Update("is_show", req.IsShow).Error; err != nil {
 		return xerr.WithStack(err)
 	}
+	defer func() {
+		body := &model.CategoryRequest{
+			ParentId:   int32(cattleCategory.ParentId),
+			ParentName: cattleCategory.ParentName,
+			Name:       cattleCategory.Name,
+			Id:         int32(req.CattleCategoryId),
+			Number:     cattleCategory.Number,
+			IsShow:     int32(req.IsShow),
+			GroupId:    int32(cattleCategory.Id),
+		}
+		s.CategoryDistribution(ctx, model.CattleCategoryDistributionURl, body)
+	}()
 	return nil
 }
 
@@ -310,6 +348,18 @@ func (s *StoreEntry) AddForageCategory(ctx context.Context, req *operationPb.Add
 	if err := s.DB.Create(forageCategory).Error; err != nil {
 		return xerr.WithStack(err)
 	}
+	defer func() {
+		body := &model.CategoryRequest{
+			ParentId:   int32(req.ParentId),
+			ParentName: req.ParentName,
+			Name:       req.Name,
+			Id:         int32(req.Id),
+			Number:     req.Number,
+			IsShow:     int32(req.IsShow),
+			GroupId:    int32(forageCategory.Id),
+		}
+		s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body)
+	}()
 	return nil
 }
 
@@ -328,12 +378,24 @@ func (s *StoreEntry) EditForageCategory(ctx context.Context, req *operationPb.Ad
 		Number:     req.Number,
 		ParentId:   req.ParentId,
 	}
-
 	if err := s.DB.Model(new(model.ForageCategory)).Omit("is_show", "is_delete").
 		Where("id = ?", req.Id).
 		Updates(updateData).Error; err != nil {
 		return xerr.WithStack(err)
 	}
+	defer func() {
+		body := &model.CategoryRequest{
+			ParentId:   int32(req.ParentId),
+			ParentName: req.ParentName,
+			Name:       req.Name,
+			Id:         int32(req.Id),
+			Number:     req.Number,
+			IsShow:     int32(req.IsShow),
+			GroupId:    int32(req.Id),
+		}
+		s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body)
+	}()
+
 	return nil
 }
 
@@ -350,6 +412,18 @@ func (s *StoreEntry) IsShowForageCategory(ctx context.Context, req *operationPb.
 	if err := s.DB.Model(new(model.ForageCategory)).Where("id = ?", req.ForageCategoryId).Update("is_show", req.IsShow).Error; err != nil {
 		return xerr.WithStack(err)
 	}
+	defer func() {
+		body := &model.CategoryRequest{
+			ParentId:   int32(forageCategory.ParentId),
+			ParentName: forageCategory.ParentName,
+			Name:       forageCategory.Name,
+			Id:         req.ForageCategoryId,
+			Number:     forageCategory.Number,
+			IsShow:     int32(req.IsShow),
+			GroupId:    int32(forageCategory.Id),
+		}
+		s.CategoryDistribution(ctx, model.ForageCategoryDistributionURl, body)
+	}()
 	return nil
 }
 
@@ -408,6 +482,41 @@ func (s *StoreEntry) SearchForageCategoryList(ctx context.Context, req *operatio
 	}, nil
 }
 
+// CategoryDistribution 饲料分类和畜牧分类下发
+func (s *StoreEntry) CategoryDistribution(ctx context.Context, url string, req *model.CategoryRequest) error {
+	groupList, err := s.SearchGroupPastureList(ctx, &operationPb.SearchPastureRequest{})
+	if err != nil {
+		return xerr.WithStack(err)
+	}
+	wg := sync.WaitGroup{}
+	wg.Add(len(groupList.Data.List))
+	for _, v := range groupList.Data.List {
+		go func(data *operationPb.AddPastureRequest) {
+			res := &model.PastureCommonResponse{}
+			req.Id = data.Id
+			if err = s.PastureHttpClient(ctx, url, int64(data.Id), req, res); err != nil {
+				zaplog.Error("CategoryDistribution",
+					zap.Any("url", url),
+					zap.Any("err", err),
+					zap.Any("body", req),
+					zap.Any("res", res),
+				)
+			}
+
+			if res.Code != http.StatusOK {
+				zaplog.Error("CategoryDistribution-http",
+					zap.Any("url", url),
+					zap.Any("body", req),
+					zap.Any("res", res),
+				)
+			}
+			wg.Done()
+		}(v)
+	}
+	wg.Wait()
+	return nil
+}
+
 // CreateForage 创建饲料
 func (s *StoreEntry) CreateForage(ctx context.Context, req *operationPb.AddForageRequest) error {
 	forage := model.NewForage(req)