| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 | package backendimport (	"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}
 |