فهرست منبع

feed: 配方模板更新

Yi 1 سال پیش
والد
کامیت
de0d0dcb9f

+ 2 - 1
go.mod

@@ -3,7 +3,7 @@ module tmr-watch
 go 1.17
 
 require (
-	gitee.com/xuyiping_admin/go_proto v0.0.0-20230807090033-3a36b1b84a99
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20230802054950-77dee3d12065
 	gitee.com/xuyiping_admin/pkg v0.0.0-20230802061948-5b07cd8c16e6
 	github.com/360EntSecGroup-Skylar/excelize v1.4.1
 	github.com/Anderson-Lu/gofasion v0.0.0-20190530065914-6a05b679ee48
@@ -39,6 +39,7 @@ require (
 )
 
 require (
+	gitee.com/xuyiping_admin/go_proto v0.0.0-20230811090104-41d5081a918d // indirect
 	github.com/Chronokeeper/anyxml v0.0.0-20160530174208-54457d8e98c6 // indirect
 	github.com/CloudyKit/fastprinter v0.0.0-20200109182630-33d98a066a53 // indirect
 	github.com/CloudyKit/jet v2.1.2+incompatible // indirect

+ 3 - 2
go.sum

@@ -3,8 +3,8 @@ cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMT
 cloud.google.com/go v0.37.4/go.mod h1:NHPJ89PdicEuT9hdPXMROBD91xc5uRDxsMtSB16k7hw=
 gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a h1:lSA0F4e9A2NcQSqGqTOXqu2aRi/XEQxDCBwM8yJtE6s=
 gitea.com/xorm/sqlfiddle v0.0.0-20180821085327-62ce714f951a/go.mod h1:EXuID2Zs0pAQhH8yz+DNjUbjppKQzKFAn28TMYPB6IU=
-gitee.com/xuyiping_admin/go_proto v0.0.0-20230807090033-3a36b1b84a99 h1:SIxSfg7Fon7aJNngCYGpd4CEiXxONETc8BnBjbBjF4A=
-gitee.com/xuyiping_admin/go_proto v0.0.0-20230807090033-3a36b1b84a99/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230811090104-41d5081a918d h1:jNBG7Y91dojnjVGK+vT010fJ3JwtqddKXQI30807bT8=
+gitee.com/xuyiping_admin/go_proto v0.0.0-20230811090104-41d5081a918d/go.mod h1:x47UOU+lOkZnrtAENAsOGd7mZ5I8D2JRkMKMqLLRlVw=
 gitee.com/xuyiping_admin/pkg v0.0.0-20230802061948-5b07cd8c16e6 h1:K4RrKDO1SG1NOCoqNO0jnlGKmY7AfOmuqzDIw94KCTk=
 gitee.com/xuyiping_admin/pkg v0.0.0-20230802061948-5b07cd8c16e6/go.mod h1:h3kdB9CyxmW6duxLLA6fJezVCHa0g5+erAQ60iv+S58=
 github.com/360EntSecGroup-Skylar/excelize v1.4.1 h1:l55mJb6rkkaUzOpSsgEeKYtS6/0gHwBYyfo5Jcjv/Ks=
@@ -138,6 +138,7 @@ github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm
 github.com/gin-gonic/gin v1.3.0/go.mod h1:7cKuhb5qV2ggCFctp2fJQ+ErvciLZrIeoOSOm6mUr7Y=
 github.com/gin-gonic/gin v1.4.0/go.mod h1:OW2EZn3DO8Ln9oIKOvM++LBO+5UPHJJDH72/q/3rZdM=
 github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
+github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
 github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
 github.com/glendc/gopher-json v0.0.0-20170414221815-dc4743023d0c/go.mod h1:Gja1A+xZ9BoviGJNA2E9vFkPjjsl+CoJxSXiQM1UXtw=
 github.com/go-errors/errors v1.4.2 h1:J6MZopCL4uSllY1OfXM374weqZFFItUbrImctkmUxIA=

+ 22 - 2
http/handle/api/new_api.go

@@ -2,17 +2,19 @@ package api
 
 import (
 	"net/http"
+	"strconv"
 	"tmr-watch/service/group"
 
-	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
+	feedPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/pasture"
 	"gitee.com/xuyiping_admin/pkg/apierr"
 	"gitee.com/xuyiping_admin/pkg/ginutil"
 	"gitee.com/xuyiping_admin/pkg/valid"
 	"github.com/gin-gonic/gin"
 )
 
+// SearchFeetTemplateList 配方列表
 func SearchFeetTemplateList(c *gin.Context) {
-	var req operationPb.SearchFeedFormulaRequest
+	var req feedPb.SearchFeedTemplateRequest
 	if err := ginutil.BindProto(c, &req); err != nil {
 		apierr.AbortBadRequest(c, http.StatusBadRequest, err)
 		return
@@ -30,5 +32,23 @@ func SearchFeetTemplateList(c *gin.Context) {
 		return
 	}
 	ginutil.JSONResp(c, list)
+}
+
+// GetFeetTemplateVersion 获取配方最新详情
+func GetFeetTemplateVersion(c *gin.Context) {
+	feetTemplateIdStr := c.Param("feet_template_id")
+	feetTemplateId, _ := strconv.Atoi(feetTemplateIdStr)
+
+	if err := valid.Validate(feetTemplateId, valid.Required, valid.Min(1)); err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
 
+	// SearchFeedTemplateList
+	list, err := group.GetFeetTemplateVersion(c, feetTemplateId)
+	if err != nil {
+		apierr.ClassifiedAbort(c, err)
+		return
+	}
+	ginutil.JSONResp(c, list)
 }

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

@@ -387,5 +387,4 @@ func FeedFormulaVersion(c *gin.Context) {
 		return
 	}
 	appG.Response(http.StatusOK, e.SUCCESS, map[string]bool{"success": true})
-
 }

+ 1 - 0
http/routers/new_api.go

@@ -13,5 +13,6 @@ func NewAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		}
 		newApiGroup := s.Group("/new_api")
 		newApiGroup.POST("feet_template/list", api.SearchFeetTemplateList)
+		newApiGroup.POST("feet_template/version/:feet_template_id", api.GetFeetTemplateVersion)
 	}
 }

+ 21 - 2
migration/v0001_feedtemplet.sql

@@ -211,7 +211,7 @@ FROM `sysopt`
 WHERE pastureid = ?;
 
 ## 配方版本更新记录表
-CREATE TABLE `feed_detail_version_log` (
+CREATE TABLE `feed_detail_version` (
    `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键自增id',
    `feed_template_id` bigint(20) unsigned NOT NULL DEFAULT '0' COMMENT '配方id',
    `group_version` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '版本号',
@@ -220,4 +220,23 @@ CREATE TABLE `feed_detail_version_log` (
    `updated_at` bigint(20) NOT NULL DEFAULT '0' COMMENT '更新时间',
    PRIMARY KEY (`id`),
    KEY `idx_feed_template_id` (`feed_template_id`)
-) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='配方版本更新记录表'
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='配方版本更新记录表';
+
+CREATE TABLE `feed_detail_version_log` (
+   `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键自增id',
+   `feed_formula_id` int(11) unsigned NOT NULL COMMENT '配方id',
+   `forage_id` int(11) unsigned NOT NULL COMMENT '饲料id',
+   `forage_name` varchar(255) NOT NULL DEFAULT '' COMMENT '饲料名称',
+   `forage_group_name` varchar(155) NOT NULL DEFAULT '' COMMENT '饲料分组名称',
+   `weight` int(11) unsigned NOT NULL COMMENT '重量',
+   `stir_delay` int(11) unsigned NOT NULL COMMENT '搅拌延迟',
+   `allow_error` int(11) unsigned NOT NULL COMMENT '允许误差',
+   `is_modify` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否可修改 0 无效 1 是 2 否',
+   `is_lock_cow_count_ratio` tinyint(1) unsigned NOT NULL DEFAULT '2' COMMENT '是否锁定牛头数比例 1 是 2 否',
+   `sort` int(11) unsigned NOT NULL DEFAULT '0' COMMENT '排序',
+   `is_show` tinyint(1) unsigned NOT NULL DEFAULT '1' COMMENT '是否删除 0 无效 1 未删除 2 已删除',
+   `created` datetime NOT NULL COMMENT '创建时间',
+   `updated` datetime NOT NULL COMMENT '更新时间',
+   PRIMARY KEY (`id`),
+   KEY `idx_feed_formula_id` (`feed_formula_id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='集团端最新配方详情表'

+ 14 - 0
models/feed_detail_version.go

@@ -0,0 +1,14 @@
+package models
+
+type FeedDetailVersion struct {
+	Id             int64 `xorm:"id" json:"id,omitempty"`
+	FeedTemplateId int64 `xorm:"feed_template_id" json:"feed_template_id"`
+	GroupVersion   int32 `xorm:"group_version" json:"group_version"`
+	Belong         int32 `xorm:"belong" json:"belong"`
+	CreatedAt      int64 `xorm:"created_at" json:"created_at"`
+	UpdatedAt      int64 `xorm:"updated_at" json:"updated_at"`
+}
+
+func (f *FeedDetailVersion) TableName() string {
+	return "feed_detail_version"
+}

+ 19 - 6
models/feed_detail_version_log.go

@@ -1,12 +1,25 @@
 package models
 
+import (
+	"time"
+
+	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
+)
+
 type FeedDetailVersionLog struct {
-	Id             int64 `xorm:"id" json:"id,omitempty"`
-	FeedTemplateId int64 `xorm:"feed_template_id" json:"feed_template_id"`
-	GroupVersion   int32 `xorm:"group_version" json:"group_version"`
-	Belong         int32 `xorm:"belong" json:"belong"`
-	CreatedAt      int64 `xorm:"created_at" json:"created_at"`
-	UpdatedAt      int64 `xorm:"updated_at" json:"updated_at"`
+	Id                  int64                   `xorm:"id" json:"id,omitempty"`
+	FeedTemplateId      int64                   `xorm:"feed_template_id" json:"feed_template_id"`
+	ForageId            int64                   `xorm:"forage_id" json:"forage_id"`
+	ForageName          string                  `xorm:"forage_name" json:"forage_name"`
+	ForageGroupName     string                  `xorm:"forage_group_name" json:"forage_group_name"`
+	Weight              int32                   `xorm:"weight" json:"weight"`
+	StirDelay           int32                   `xorm:"stir_delay" json:"stir_delay"`
+	AllowError          int32                   `xorm:"allow_error" json:"allow_error"`
+	IsModify            operationPb.IsShow_Kind `xorm:"is_modify" json:"is_modify"`
+	IsLockCowCountRatio operationPb.IsShow_Kind `xorm:"is_lock_cow_count_ratio" json:"is_lock_cow_count_ratio"`
+	Sort                int32                   `xorm:"sort" json:"sort"`
+	Created             time.Time               `xorm:"created" json:"created"`
+	Updated             time.Time               `xorm:"updated" json:"updated"`
 }
 
 func (f *FeedDetailVersionLog) TableName() string {

+ 1 - 1
models/feed_template.go

@@ -65,7 +65,7 @@ func NewFeedTemplateByGroup(req *FeedFormula) *FeedTemplate {
 
 type FeedTemplateSlice []*FeedTemplate
 
-func (f FeedTemplateSlice) ToPB(feedDetailVersionLogs []*FeedDetailVersionLog) []*pasturePb.FeedTemplateDataList {
+func (f FeedTemplateSlice) ToPB(feedDetailVersionLogs []*FeedDetailVersion) []*pasturePb.FeedTemplateDataList {
 	res := make([]*pasturePb.FeedTemplateDataList, len(f))
 	for i, v := range f {
 

+ 4 - 3
models/group_data.go

@@ -290,7 +290,8 @@ type SearchFeedTemplateRequest struct {
 }
 
 type FeedFormulaUpdateVersionRequest struct {
-	FeedTemplateId int64 `json:"feed_template_id"` // 配方id
-	Version        int32 `json:"version"`          // 版本号
-	Belong         int32 `json:"belong"`           // 配方归属  0 无效 1 归属集团端 2 归属牧场端
+	FeedTemplateId int64                               `json:"feed_template_id"` // 配方id
+	Version        int32                               `json:"version"`          // 版本号
+	Belong         int32                               `json:"belong"`           // 配方归属  0 无效 1 归属集团端 2 归属牧场端
+	Data           []*operationPb.AddFeedFormulaDetail `json:"data"`
 }

+ 46 - 6
module/group.go

@@ -9,6 +9,8 @@ import (
 	"tmr-watch/http/handle/restful"
 	"tmr-watch/models"
 
+	operationPb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/operation"
+
 	"github.com/xormplus/xorm"
 
 	pasturePb "gitee.com/xuyiping_admin/go_proto/proto/go/backend/pasture"
@@ -503,7 +505,12 @@ func FeedFormulaVersion(req *models.FeedFormulaUpdateVersionRequest) error {
 	DbSession := restful.Engine.NewSession()
 	defer DbSession.Close()
 
-	history := &models.FeedDetailVersionLog{}
+	// 开启事务
+	if err := DbSession.Begin(); err != nil {
+		return err
+	}
+
+	history := &models.FeedDetailVersion{}
 	if _, err := DbSession.Where("feed_template_id = ?", req.FeedTemplateId).
 		And("belong = ?", req.Belong).
 		Get(history); err != nil {
@@ -513,11 +520,22 @@ func FeedFormulaVersion(req *models.FeedFormulaUpdateVersionRequest) error {
 	if history.Id > 0 {
 		history.GroupVersion = req.Version
 		history.UpdatedAt = time.Now().Unix()
-		if _, err := DbSession.Where("id = ?", history.Id).Cols("group_version", "updated_at").Update(history); err != nil {
+		if _, err := DbSession.Table(new(models.FeedDetailVersion).TableName()).
+			Where("id = ?", history.Id).Cols("group_version", "updated_at").
+			Update(history); err != nil {
+			DbSession.Rollback()
+			return err
+		}
+
+		if _, err := DbSession.Table(new(models.FeedDetailVersionLog).TableName()).
+			Where("feed_formula_id = ?", history.FeedTemplateId).
+			Update(map[string]interface{}{"is_show": operationPb.IsShow_NO}); err != nil {
+			DbSession.Rollback()
 			return err
 		}
+
 	} else {
-		feedDetailVersionLog := &models.FeedDetailVersionLog{
+		feedDetailVersionLog := &models.FeedDetailVersion{
 			FeedTemplateId: req.FeedTemplateId,
 			GroupVersion:   req.Version,
 			Belong:         req.Belong,
@@ -525,11 +543,33 @@ func FeedFormulaVersion(req *models.FeedFormulaUpdateVersionRequest) error {
 			UpdatedAt:      time.Now().Unix(),
 		}
 		if _, err := DbSession.Insert(feedDetailVersionLog); err != nil {
+			DbSession.Rollback()
 			return err
 		}
 	}
 
-	return nil
+	// FeedDetailVersionLog 更新
+	versionLogs := make([]*models.FeedDetailVersionLog, 0)
+	for _, v := range req.Data {
+		versionLogs = append(versionLogs, &models.FeedDetailVersionLog{
+			FeedTemplateId:      req.FeedTemplateId,
+			ForageId:            int64(v.ForageId),
+			ForageName:          v.ForageName,
+			ForageGroupName:     v.ForageGroupName,
+			Weight:              int32(v.Weight * 100),
+			StirDelay:           v.StirDelay,
+			AllowError:          v.AllowError,
+			IsModify:            v.IsModify,
+			IsLockCowCountRatio: operationPb.IsShow_Kind(v.IsLockCowCountRatio),
+			Sort:                v.Sort,
+		})
+	}
+	if _, err := DbSession.Table(new(models.FeedDetailVersionLog).TableName()).Insert(versionLogs); err != nil {
+		DbSession.Rollback()
+		return err
+	}
+
+	return DbSession.Commit()
 }
 
 func NewSearchFeedTemplateList(ctx context.Context, req *pasturePb.SearchFeedTemplateRequest) ([]*models.FeedTemplate, int64, error) {
@@ -576,8 +616,8 @@ func NewSearchFeedTemplateList(ctx context.Context, req *pasturePb.SearchFeedTem
 	}
 }
 
-func FeedDetailVersionLogList(ctx context.Context) ([]*models.FeedDetailVersionLog, error) {
-	res := make([]*models.FeedDetailVersionLog, 0)
+func FeedDetailVersionLogList(ctx context.Context) ([]*models.FeedDetailVersion, error) {
+	res := make([]*models.FeedDetailVersion, 0)
 	if err := restful.Engine.NewSession().Find(&res); err != nil {
 		if !errors.Is(err, xorm.ErrNotExist) {
 			return nil, err

+ 12 - 0
service/group/feed_template.go

@@ -31,3 +31,15 @@ func SearchFeedTemplateList(ctx context.Context, req *feedPb.SearchFeedTemplateR
 	}
 	return res, nil
 }
+
+func GetFeetTemplateVersion(ctx context.Context, feedTemplateId int) (*feedPb.GetFeetTemplateVersionResponse, error) {
+	res := &feedPb.GetFeetTemplateVersionResponse{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: nil,
+	}
+
+	list := make([]*feedPb.FeetTemplateVersionData, 0)
+	res.Data = list
+	return res, nil
+}