package backend import ( "context" "errors" "kpt-tmr-group/model" "math" "net/http" operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation" "gitee.com/xuyiping_admin/pkg/xerr" "gorm.io/gorm" ) const ( FeedCategory = "feed" FeedCategoryDelete = "feed_delete" CowCategory = "cow" CowCategoryDelete = "cow_delete" ) func (s *StoreEntry) CategorySyncData(ctx context.Context, req *operationPb.CategorySyncRequest) error { pastureDetail, err := s.GetGroupPastureListById(ctx, int64(req.PastureId)) if err != nil { return xerr.WithStack(err) } switch req.KeyWord { case FeedCategory: history := &model.ForageCategory{} if err = s.DB.Model(new(model.ForageCategory)). Where("pasture_id = ?", req.PastureId). Where("data_id = ?", req.Id). Where("data_source = ? ", operationPb.DataSource_FROM_PASTURE). First(history).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return xerr.WithStack(err) } } if history.IsShow == operationPb.IsShow_OK && history.Id > 0 { if err = s.DB.Model(new(model.ForageCategory)).Where("id = ?", history.Id).Updates(map[string]interface{}{ "number": req.Number, "is_show": req.IsShow, "parent_id": req.ParentId, "parent_name": req.ParentName, "name": req.Name, }).Error; err != nil { return xerr.WithStack(err) } } newFeedData := model.NewPastureForageCategory(req, pastureDetail) if err = s.DB.Model(new(model.ForageCategory)).Create(newFeedData).Error; err != nil { return xerr.WithStack(err) } case CowCategory: history := &model.CattleCategory{} if err = s.DB.Model(new(model.CattleCategory)). Where("pasture_id = ?", req.PastureId). Where("data_id = ?", req.Id). Where("data_source = ? ", operationPb.DataSource_FROM_PASTURE). First(history).Error; err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return xerr.WithStack(err) } } if history.IsShow == operationPb.IsShow_OK && history.Id > 0 { if err = s.DB.Model(new(model.CattleCategory)).Where("id = ?", history.Id).Updates(map[string]interface{}{ "number": req.Number, "is_show": req.IsShow, "parent_id": req.ParentId, "parent_name": req.ParentName, "name": req.Name, }).Error; err != nil { return xerr.WithStack(err) } } newCattleData := model.NewPastureCattleCategory(req, pastureDetail) if err = s.DB.Model(new(model.CattleCategory)).Create(newCattleData).Error; err != nil { return xerr.WithStack(err) } } return nil } func (s *StoreEntry) CategoryDeleteData(ctx context.Context, req *operationPb.CategoryDeleteRequest) error { var modelValue interface{} switch req.KeyWord { case FeedCategoryDelete: modelValue = new(model.ForageCategory) case CowCategoryDelete: modelValue = new(model.CattleCategory) } if modelValue == nil { return nil } if err := s.DB.Model(modelValue).Where("data_id = ?", req.DataId). Where("pasture_id = ?", req.PastureId).Where("data_source = ?", operationPb.DataSource_FROM_PASTURE). Updates(map[string]interface{}{ "is_delete": operationPb.IsShow_OK, }).Error; err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) FeedFormulaSyncData(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.FeedFormulaListResponse{} if err = s.PastureHttpClient(ctx, model.FeedFormulaAsyncUrl, 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.FeedFormulaInsert(ctx, groupPasture, response.Data.List); err != nil { return xerr.WithStack(err) } if total <= (pageSize * page) { break } page++ } } return nil } func (s *StoreEntry) FeedFormulaInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.FeedTemplate) error { res := make([]*model.FeedFormula, 0) for _, data := range list { res = append(res, &model.FeedFormula{ Name: data.TName, Colour: data.TColor, EncodeNumber: data.TCode, CattleCategoryId: operationPb.CattleCategoryParent_Kind(data.CCid), CattleCategoryName: data.CCName, FormulaTypeId: operationPb.FormulaType_Kind(data.FTTypeId), FormulaTypeName: data.FTType, DataSourceId: operationPb.DataSource_FROM_PASTURE, DataSourceName: "牧场同步", Remarks: data.Remark, Version: data.Version, PastureId: groupPasture.PastureId, PastureName: groupPasture.Name, PastureDataId: data.Id, IsShow: operationPb.IsShow_Kind(data.Enable), IsModify: operationPb.IsShow_Kind(data.IsModify), IsDelete: operationPb.IsShow_OK, }) } if err := s.DB.Model(new(model.FeedFormula)).Create(res).Error; err != nil { return xerr.WithStack(err) } return nil } func (s *StoreEntry) FeedSyncData(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 { if req.Page <= 0 { req.Page = 1 } if req.PageSize <= 0 { req.PageSize = 1000 } body := &model.FeedFormulaListRequest{ PastureId: int32(groupPasture.PastureId), Page: req.Page, PageSize: req.PageSize, } response := &model.FeedListResponse{} if err = s.PastureHttpClient(ctx, model.FeedAsyncUrl, int64(req.PastureId), body, response); err != nil { return xerr.WithStack(err) } if response.Code != http.StatusOK { return xerr.Customf("%s", response.Msg) } if err = s.FeedInsert(ctx, groupPasture, response.Data.List); err != nil { return xerr.WithStack(err) } } return nil } // FeedInsert 饲料数据同步 func (s *StoreEntry) FeedInsert(ctx context.Context, groupPasture *model.GroupPasture, list []*model.Feed) error { res := make([]*model.Forage, 0) for _, data := range list { if data.Id <= 0 { continue } res = append(res, model.NewForageByPastureFeed(groupPasture, data)) } 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 total <= (page * pageSize) { break } page++ 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 } isLockCowCountRatio := 0 if data.IsLockCount == 0 { isLockCowCountRatio = 2 } if data.IsLockCount == 1 { isLockCowCountRatio = 1 } res = append(res, &model.FeedFormulaDetail{ PastureId: groupPasture.PastureId, PastureName: groupPasture.Name, PastureDataId: data.Id, ForageId: data.FId, FeedFormulaId: data.FtId, Weight: int32(data.FWeight * 100), ForageGroupName: data.FeedGroup, ForageName: data.FName, StirDelay: data.AutoSecond, AllowError: data.Deviation, Prefit: data.PreFtId, IsShow: operationPb.IsShow_OK, IsLockCowCountRatio: operationPb.IsShow_Kind(isLockCowCountRatio), Sort: data.Sort, }) } if err := s.DB.Model(new(model.FeedFormulaDetail)).Create(res).Error; err != nil { return xerr.WithStack(err) } return nil }