Browse Source

feed: 配方修改记录

Yi 1 year ago
parent
commit
d0dc80518c

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

@@ -121,6 +121,33 @@ func AddFeedByFeedFormula(c *gin.Context) {
 	})
 }
 
+// EditFeedByFeedFormula 配方饲料编辑
+func EditFeedByFeedFormula(c *gin.Context) {
+	var req operationPb.AddFeedFormulaDetail
+	if err := c.BindJSON(&req); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := valid.ValidateStruct(&req,
+		valid.Field(&req.FeedFormulaId, valid.Required, valid.Min(1)),
+		valid.Field(&req.Id, valid.Required, valid.Min(1)),
+	); err != nil {
+		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
+		return
+	}
+
+	if err := middleware.BackendOperation(c).OpsService.EditFeedByFeedFormula(c, &req); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, &operationPb.CommonOK{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.Success{Success: true},
+	})
+}
+
 // DetailBySort 配方详情排序
 func DetailBySort(c *gin.Context) {
 	var req operationPb.GroupAddFeedFormulaDetail

+ 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/feed/add", feed.AddFeedByFeedFormula)
+		opsRoute.POST("/feed_formula/feed/edit", feed.EditFeedByFeedFormula)
 		opsRoute.POST("/feed_formula/feed/sort", feed.DetailBySort)
 		opsRoute.POST("/feed_formula/feed/delete", feed.DeleteFeedDetail)
 		opsRoute.POST("/feed_formula/feed/search", feed.SearchFeedDetail)

+ 16 - 0
model/feed_formula_edit_record.go

@@ -21,3 +21,19 @@ type FeedFormulaEditRecord struct {
 func (f *FeedFormulaEditRecord) TableName() string {
 	return "feed_formula_edit_record"
 }
+
+func NewFeedFormulaEditRecord(groupId, feedFormulaId, pastureId int64, pastureName, operationName, forageName, fieldName,
+	beforeValue, afterValue string, status operationPb.FeedFormulaEditRecordType_Kind) *FeedFormulaEditRecord {
+	return &FeedFormulaEditRecord{
+		GroupId:       groupId,
+		FeedFormulaId: feedFormulaId,
+		PastureId:     pastureId,
+		PastureName:   pastureName,
+		OperationName: operationName,
+		ForageName:    forageName,
+		FieldName:     fieldName,
+		Status:        status,
+		BeforeValue:   beforeValue,
+		AfterValue:    afterValue,
+	}
+}

+ 114 - 0
module/backend/feed_service.go

@@ -30,6 +30,13 @@ var PastureDataLogType = map[string]int32{
 	"FeedFormula_IsModify":          2,
 	"FeedFormula_Cancel_Distribute": 3,
 }
+var EditRecodeMap = map[string]string{
+	"forage_name": "饲料名称",
+	"weight":      "重量",
+	"stir_delay":  "搅拌延迟",
+	"allow_error": "允许误差",
+	"sort":        "排序",
+}
 
 // CreateFeedFormula 添加数据
 func (s *StoreEntry) CreateFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error {
@@ -109,6 +116,113 @@ func (s *StoreEntry) AddFeedByFeedFormula(ctx context.Context, req *operationPb.
 	return nil
 }
 
+// EditFeedByFeedFormula 配方饲料编辑
+func (s *StoreEntry) EditFeedByFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaDetail) error {
+	feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}
+	if err := s.DB.Model(new(model.FeedFormula)).First(feedFormulaData).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	feedFormulaDetail := &model.FeedFormulaDetail{Id: int64(req.Id)}
+	if err := s.DB.Model(new(model.FeedFormulaDetail)).
+		Where("is_show = ?", operationPb.IsShow_OK).
+		First(feedFormulaDetail).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	// 添加修改记录
+	defer s.EditFeedFormulaDetailAddRecode(ctx, req, feedFormulaDetail)
+
+	// 更新数据
+	updateData := &model.FeedFormulaDetail{
+		ForageId:        int64(req.ForageId),
+		ForageName:      req.ForageName,
+		ForageGroupName: req.ForageGroupName,
+		Weight:          int32(req.Weight * 100),
+		StirDelay:       req.StirDelay,
+		AllowError:      req.AllowError,
+		Sort:            req.Sort,
+	}
+	if err := s.DB.Model(new(model.FeedFormulaDetail)).Where("id = ?", req.Id).Updates(updateData).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+
+	return nil
+}
+
+// EditFeedFormulaDetailAddRecode 更新饲料配方修改记录
+func (s *StoreEntry) EditFeedFormulaDetailAddRecode(ctx context.Context, req *operationPb.AddFeedFormulaDetail, feedFormulaDetail *model.FeedFormulaDetail) {
+	editRecordList := make([]*model.FeedFormulaEditRecord, 0)
+
+	editRecordData := &model.FeedFormulaEditRecord{
+		FeedFormulaId: int64(req.FeedFormulaId),
+		PastureName:   "集团",
+		ForageName:    req.ForageName,
+		Status:        operationPb.FeedFormulaEditRecordType_UPDATE,
+	}
+
+	if operationName, err := s.GetCurrentUserName(ctx); err != nil {
+		zaplog.Error("EditFeedByFeedFormula", zap.Any("GetCurrentUserName", err))
+	} else {
+		editRecordData.OperationName = operationName
+	}
+
+	lastGroupIdData := &model.FeedFormulaEditRecord{}
+	if err := s.DB.Model(new(model.FeedFormulaEditRecord)).
+		Where("is_show = ?", operationPb.IsShow_OK).
+		Order("group_id desc").
+		First(&lastGroupIdData).Error; err != nil {
+		zaplog.Error("EditFeedByFeedFormula", zap.Any("lastGroupIdData", err))
+	} else {
+		editRecordData.GroupId = lastGroupIdData.GroupId + 1
+		editRecordData.BeforeValue = lastGroupIdData.BeforeValue
+	}
+
+	if feedFormulaDetail.ForageName != req.ForageName {
+		editRecordData.FieldName = EditRecodeMap["forage_name"]
+		editRecordData.BeforeValue = lastGroupIdData.ForageName
+		editRecordData.AfterValue = req.ForageName
+		editRecordList = append(editRecordList, editRecordData)
+	}
+
+	if feedFormulaDetail.Weight != int32(req.Weight*100) {
+		editRecordData.FieldName = EditRecodeMap["weight"]
+		editRecordData.AfterValue = fmt.Sprintf("%d", int32(req.Weight*100))
+		editRecordList = append(editRecordList, editRecordData)
+	}
+
+	if feedFormulaDetail.AllowError != req.AllowError {
+		editRecordData.FieldName = EditRecodeMap["allow_error"]
+		editRecordData.AfterValue = fmt.Sprintf("%d", req.AllowError)
+		editRecordList = append(editRecordList, editRecordData)
+	}
+
+	if feedFormulaDetail.StirDelay != req.StirDelay {
+		editRecordData.FieldName = EditRecodeMap["stir_delay"]
+		editRecordData.AfterValue = fmt.Sprintf("%d", req.StirDelay)
+		editRecordList = append(editRecordList, editRecordData)
+	}
+
+	if feedFormulaDetail.Sort != req.Sort {
+		editRecordData.FieldName = EditRecodeMap["sort"]
+		editRecordData.AfterValue = fmt.Sprintf("%d", req.Sort)
+		editRecordList = append(editRecordList, editRecordData)
+	}
+
+	if err := s.CreateFeedFormulaEditRecord(ctx, editRecordList); err != nil {
+		zaplog.Error("EditFeedByFeedFormula", zap.Any("CreateFeedFormulaEditRecord", err))
+	}
+}
+
+// CreateFeedFormulaEditRecord 创建配方修改记录
+func (s *StoreEntry) CreateFeedFormulaEditRecord(ctx context.Context, req []*model.FeedFormulaEditRecord) error {
+	if req == nil {
+		return nil
+	}
+	if err := s.DB.Model(new(model.FeedFormulaDetail)).Save(req).Error; err != nil {
+		return xerr.WithStack(err)
+	}
+	return nil
+}
+
 // FeedFormulaDetailBySort 配方饲料排序
 func (s *StoreEntry) FeedFormulaDetailBySort(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error {
 	feedFormulaData := &model.FeedFormula{Id: int64(req.FeedFormulaId)}

+ 1 - 0
module/backend/interface.go

@@ -90,6 +90,7 @@ type PastureService interface {
 	CreateFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error
 	EditFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error
 	AddFeedByFeedFormula(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error
+	EditFeedByFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaDetail) error
 	FeedFormulaDetailBySort(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error
 	DeleteFeedFormulaDetail(ctx context.Context, req *operationPb.GroupAddFeedFormulaDetail) error
 	SearchFeedFormulaDetail(ctx context.Context, req *operationPb.AddFeedFormulaDetail) (*operationPb.FeedFormulaDetailResponse, error)

+ 6 - 4
module/backend/system_service.go

@@ -19,6 +19,8 @@ import (
 	"gorm.io/gorm"
 )
 
+const CurrentUserName = "userName"
+
 // Auth 用户登录
 func (s *StoreEntry) Auth(ctx context.Context, auth *operationPb.UserAuthData) (*operationPb.SystemToken, error) {
 	systemUser := &model.SystemUser{}
@@ -49,8 +51,8 @@ func (s *StoreEntry) Auth(ctx context.Context, auth *operationPb.UserAuthData) (
 	}, nil
 }
 
-func (s *StoreEntry) GetUserName(ctx context.Context) (string, error) {
-	userNameInter := ctx.Value("userName")
+func (s *StoreEntry) GetCurrentUserName(ctx context.Context) (string, error) {
+	userNameInter := ctx.Value(CurrentUserName)
 	if userNameInter == nil {
 		return "", xerr.Customf("cannot userName")
 	}
@@ -65,7 +67,7 @@ func (s *StoreEntry) GetUserName(ctx context.Context) (string, error) {
 // GetUserInfo 获取用户信息
 func (s *StoreEntry) GetUserInfo(ctx context.Context, token string) (*operationPb.UserAuth, error) {
 	systemUser := &model.SystemUser{}
-	userName, err := s.GetUserName(ctx)
+	userName, err := s.GetCurrentUserName(ctx)
 	if err != nil {
 		return nil, xerr.WithStack(err)
 	}
@@ -262,7 +264,7 @@ func (s *StoreEntry) IsShowSystemUser(ctx context.Context, req *operationPb.IsSh
 // GetSystemUserPermissions 返回系统用户相关菜单权限
 func (s *StoreEntry) GetSystemUserPermissions(ctx context.Context, token string) (*operationPb.SystemUserMenuPermissions, error) {
 	// 解析token
-	userName, err := s.GetUserName(ctx)
+	userName, err := s.GetCurrentUserName(ctx)
 	if err != nil {
 		return nil, xerr.WithStack(err)
 	}