Yi 1 рік тому
батько
коміт
3e855d3278

+ 1 - 1
Dockerfile

@@ -10,6 +10,6 @@ ADD ./bin/kptTmrGroup /bin/kptTmrGroup
 COPY config/*.yaml /bin/config/
 
 EXPOSE 8000
-VOLUME /var/Logger
+VOLUME /bin/logger
 
 CMD ["/bin/kptTmrGroup","http"]

+ 11 - 1
backend/operation/feed_formula.proto

@@ -53,5 +53,15 @@ message SearchFeedFormulaListData {
 message IsShowModifyFeedFormula {
   int32 feed_formula_id = 1;
   IsShow.Kind is_show = 2;
-  int32 edit_type = 3;                  // 1 更新是否启用 2 更新 modify
+  int32 edit_type = 3;      // 1 更新是否启用 2 更新 modify
+}
+
+// 配方编码
+message UniqueID {
+  int32 code = 1;
+  string msg = 2;
+  message UniqueData {
+    string encode_number = 1;
+  }
+  UniqueData data = 3;
 }

+ 8 - 1
http/handler/feed/feed_formula.go

@@ -34,7 +34,6 @@ func AddFeedFormula(c *gin.Context) {
 		valid.Field(&req.FormulaTypeName, valid.Required),
 		valid.Field(&req.DataSourceId, valid.Required, valid.Min(1)),
 		valid.Field(&req.DataSourceName, valid.Required),
-		valid.Field(&req.Remarks, valid.Required),
 		valid.Field(&req.IsShow, valid.Required, valid.Min(1), valid.Max(2)),
 		valid.Field(&req.IsModify, valid.Required, valid.Min(1), valid.Max(2)),
 	); err != nil {
@@ -248,3 +247,11 @@ func ExcelTemplateFeedFormula(c *gin.Context) {
 		Data: &operationPb.Success{Success: true},
 	})
 }
+
+func EncodeNumber(c *gin.Context) {
+	ginutil.JSONResp(c, &operationPb.UniqueID{
+		Code: http.StatusOK,
+		Msg:  "ok",
+		Data: &operationPb.UniqueID_UniqueData{EncodeNumber: middleware.BackendOperation(c).OpsService.EncodeNumber(c)},
+	})
+}

+ 1 - 0
http/route/app_api.go

@@ -101,6 +101,7 @@ func AppAPI(opts ...func(engine *gin.Engine)) func(s *gin.Engine) {
 		opsRoute.POST("/feed_formula/excel_export", feed.ExcelExportFeedFormula)
 		opsRoute.POST("/feed_formula/excel_import", feed.ExcelImportFeedFormula)
 		opsRoute.POST("/feed_formula/excel_template", feed.ExcelTemplateFeedFormula)
+		opsRoute.GET("/feed_formula/encode_number", feed.EncodeNumber)
 
 		//统计分析 statistic analysis
 		opsRoute.POST("/feed_estimate/list", statistic.SearchFormulaEstimateList)

+ 4 - 1
model/system_role.go

@@ -19,7 +19,10 @@ func (s *SystemRole) TableName() string {
 	return "system_role"
 }
 
-const LayoutTime = "2006-01-02 15:04:05"
+const (
+	LayoutTime = "2006-01-02 15:04:05"
+	LayoutDate = "20060102"
+)
 
 func NewSystemRole(req *operationPb.AddRoleRequest) *SystemRole {
 	systemRole := &SystemRole{

+ 13 - 0
model/unique_data.go

@@ -0,0 +1,13 @@
+package model
+
+type UniqueData struct {
+	Id        int64  `json:"id"`
+	Prefix    string `json:"prefix"`
+	Data      int64  `json:"data"`
+	CreatedAt int64  `json:"created_at"`
+	UpdatedAt int64  `json:"updated_at"`
+}
+
+func (s *UniqueData) TableName() string {
+	return "unique_data"
+}

+ 36 - 0
module/backend/feed_service.go

@@ -11,6 +11,8 @@ import (
 	"kpt-tmr-group/pkg/xerr"
 	operationPb "kpt-tmr-group/proto/go/backend/operation"
 	"net/http"
+	"strconv"
+	"time"
 
 	"github.com/xuri/excelize/v2"
 	"go.uber.org/zap"
@@ -18,6 +20,8 @@ import (
 	"gorm.io/gorm"
 )
 
+const EncodeNumberPrefix = "encode_number"
+
 // CreateFeedFormula 添加数据
 func (s *StoreEntry) CreateFeedFormula(ctx context.Context, req *operationPb.AddFeedFormulaRequest) error {
 	forage := model.NewFeedFormula(req)
@@ -291,3 +295,35 @@ func (s *StoreEntry) ExcelTemplateFeedFormula(ctx context.Context) (*bytes.Buffe
 
 	return file.WriteToBuffer()
 }
+
+// EncodeNumber 配方编码
+func (s *StoreEntry) EncodeNumber(ctx context.Context) string {
+	currTime := time.Now().Format(model.LayoutDate)
+	prefix := fmt.Sprintf("%s_%s", EncodeNumberPrefix, currTime)
+	data := &model.UniqueData{}
+	if err := s.DB.Order("id desc").Where("prefix = ?", prefix).First(data).Error; err != nil {
+		if errors.Is(err, gorm.ErrRecordNotFound) {
+			ud, _ := strconv.Atoi(currTime)
+			result := ud*100 + 1
+			newData := &model.UniqueData{
+				Prefix: prefix,
+				Data:   int64(result),
+			}
+			zaplog.Info("EncodeNumber Create Info", zap.Any("data", newData))
+			if err = s.DB.Create(newData).Error; err != nil {
+				zaplog.Error("EncodeNumber Create", zap.Any("data", newData), zap.Any("Err", err))
+				return ""
+			}
+			return fmt.Sprintf("%d", newData.Data)
+		} else {
+			return ""
+		}
+	}
+
+	data.Data += 1
+	if err := s.DB.Model(new(model.UniqueData)).Where("prefix = ?", prefix).Update("data", data.Data).Error; err != nil {
+		return ""
+	} else {
+		return fmt.Sprintf("%d", data.Data)
+	}
+}

+ 1 - 0
module/backend/interface.go

@@ -94,6 +94,7 @@ type PastureService interface {
 	ExcelImportFeedFormula(ctx context.Context, req io.Reader) error
 	ExcelExportFeedFormula(ctx context.Context, req *operationPb.SearchFeedFormulaRequest) (*bytes.Buffer, error)
 	ExcelTemplateFeedFormula(ctx context.Context) (*bytes.Buffer, error)
+	EncodeNumber(ctx context.Context) string
 }
 
 type SystemService interface {

+ 14 - 0
module/backend/mock/kptservice.go

@@ -404,6 +404,20 @@ func (mr *MockKptServiceMockRecorder) EditSystemUser(arg0, arg1 interface{}) *go
 	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EditSystemUser", reflect.TypeOf((*MockKptService)(nil).EditSystemUser), arg0, arg1)
 }
 
+// EncodeNumber mocks base method.
+func (m *MockKptService) EncodeNumber(arg0 context.Context) string {
+	m.ctrl.T.Helper()
+	ret := m.ctrl.Call(m, "EncodeNumber", arg0)
+	ret0, _ := ret[0].(string)
+	return ret0
+}
+
+// EncodeNumber indicates an expected call of EncodeNumber.
+func (mr *MockKptServiceMockRecorder) EncodeNumber(arg0 interface{}) *gomock.Call {
+	mr.mock.ctrl.T.Helper()
+	return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "EncodeNumber", reflect.TypeOf((*MockKptService)(nil).EncodeNumber), arg0)
+}
+
 // ExcelExportFeedFormula mocks base method.
 func (m *MockKptService) ExcelExportFeedFormula(arg0 context.Context, arg1 *operationPb.SearchFeedFormulaRequest) (*bytes.Buffer, error) {
 	m.ctrl.T.Helper()

+ 33 - 0
pkg/stringutil/random.go

@@ -0,0 +1,33 @@
+package stringutil
+
+import (
+	"fmt"
+	"math/rand"
+	"strconv"
+	"time"
+)
+
+func init() {
+	rand.Seed(time.Now().UnixNano())
+}
+
+const chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
+
+// UniqueID returns randomly generated string with prefix
+func UniqueID(prefix string, l uint) string {
+	return fmt.Sprintf("%s%s", prefix, RandomString(l))
+}
+
+func UniqueIDBaseTime(prefix string) string {
+	timeStr := strconv.FormatUint(uint64(time.Now().UnixNano()), 36)
+	return fmt.Sprintf("%s%s%s", prefix, timeStr, RandomString(2))
+}
+
+// RandomString returns randomly generated string
+func RandomString(l uint) string {
+	s := make([]byte, l)
+	for i := 0; i < int(l); i++ {
+		s[i] = chars[rand.Intn(len(chars))]
+	}
+	return string(s)
+}

+ 171 - 23
proto/go/backend/operation/feed_formula.pb.go

@@ -488,6 +488,117 @@ func (x *IsShowModifyFeedFormula) GetEditType() int32 {
 	return 0
 }
 
+// 配方编码
+type UniqueID struct {
+	state         protoimpl.MessageState
+	sizeCache     protoimpl.SizeCache
+	unknownFields protoimpl.UnknownFields
+
+	Code int32                `protobuf:"varint,1,opt,name=code,proto3" json:"code,omitempty"`
+	Msg  string               `protobuf:"bytes,2,opt,name=msg,proto3" json:"msg,omitempty"`
+	Data *UniqueID_UniqueData `protobuf:"bytes,3,opt,name=data,proto3" json:"data,omitempty"`
+}
+
+func (x *UniqueID) Reset() {
+	*x = UniqueID{}
+	if protoimpl.UnsafeEnabled {
+		mi := &file_backend_operation_feed_formula_proto_msgTypes[5]
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		ms.StoreMessageInfo(mi)
+	}
+}
+
+func (x *UniqueID) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*UniqueID) ProtoMessage() {}
+
+func (x *UniqueID) ProtoReflect() protoreflect.Message {
+	mi := &file_backend_operation_feed_formula_proto_msgTypes[5]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use UniqueID.ProtoReflect.Descriptor instead.
+func (*UniqueID) Descriptor() ([]byte, []int) {
+	return file_backend_operation_feed_formula_proto_rawDescGZIP(), []int{5}
+}
+
+func (x *UniqueID) GetCode() int32 {
+	if x != nil {
+		return x.Code
+	}
+	return 0
+}
+
+func (x *UniqueID) GetMsg() string {
+	if x != nil {
+		return x.Msg
+	}
+	return ""
+}
+
+func (x *UniqueID) GetData() *UniqueID_UniqueData {
+	if x != nil {
+		return x.Data
+	}
+	return nil
+}
+
+type UniqueID_UniqueData struct {
+	state         protoimpl.MessageState
+	sizeCache     protoimpl.SizeCache
+	unknownFields protoimpl.UnknownFields
+
+	EncodeNumber string `protobuf:"bytes,1,opt,name=encode_number,json=encodeNumber,proto3" json:"encode_number,omitempty"`
+}
+
+func (x *UniqueID_UniqueData) Reset() {
+	*x = UniqueID_UniqueData{}
+	if protoimpl.UnsafeEnabled {
+		mi := &file_backend_operation_feed_formula_proto_msgTypes[6]
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		ms.StoreMessageInfo(mi)
+	}
+}
+
+func (x *UniqueID_UniqueData) String() string {
+	return protoimpl.X.MessageStringOf(x)
+}
+
+func (*UniqueID_UniqueData) ProtoMessage() {}
+
+func (x *UniqueID_UniqueData) ProtoReflect() protoreflect.Message {
+	mi := &file_backend_operation_feed_formula_proto_msgTypes[6]
+	if protoimpl.UnsafeEnabled && x != nil {
+		ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+		if ms.LoadMessageInfo() == nil {
+			ms.StoreMessageInfo(mi)
+		}
+		return ms
+	}
+	return mi.MessageOf(x)
+}
+
+// Deprecated: Use UniqueID_UniqueData.ProtoReflect.Descriptor instead.
+func (*UniqueID_UniqueData) Descriptor() ([]byte, []int) {
+	return file_backend_operation_feed_formula_proto_rawDescGZIP(), []int{5, 0}
+}
+
+func (x *UniqueID_UniqueData) GetEncodeNumber() string {
+	if x != nil {
+		return x.EncodeNumber
+	}
+	return ""
+}
+
 var File_backend_operation_feed_formula_proto protoreflect.FileDescriptor
 
 var file_backend_operation_feed_formula_proto_rawDesc = []byte{
@@ -593,9 +704,19 @@ var file_backend_operation_feed_formula_proto_rawDesc = []byte{
 	0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e, 0x49, 0x73, 0x53, 0x68, 0x6f, 0x77,
 	0x2e, 0x4b, 0x69, 0x6e, 0x64, 0x52, 0x06, 0x69, 0x73, 0x53, 0x68, 0x6f, 0x77, 0x12, 0x1b, 0x0a,
 	0x09, 0x65, 0x64, 0x69, 0x74, 0x5f, 0x74, 0x79, 0x70, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x05,
-	0x52, 0x08, 0x65, 0x64, 0x69, 0x74, 0x54, 0x79, 0x70, 0x65, 0x42, 0x0f, 0x5a, 0x0d, 0x2e, 0x3b,
-	0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x50, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f,
-	0x74, 0x6f, 0x33,
+	0x52, 0x08, 0x65, 0x64, 0x69, 0x74, 0x54, 0x79, 0x70, 0x65, 0x22, 0x9f, 0x01, 0x0a, 0x08, 0x55,
+	0x6e, 0x69, 0x71, 0x75, 0x65, 0x49, 0x44, 0x12, 0x12, 0x0a, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x18,
+	0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x04, 0x63, 0x6f, 0x64, 0x65, 0x12, 0x10, 0x0a, 0x03, 0x6d,
+	0x73, 0x67, 0x18, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x03, 0x6d, 0x73, 0x67, 0x12, 0x3a, 0x0a,
+	0x04, 0x64, 0x61, 0x74, 0x61, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x26, 0x2e, 0x62, 0x61,
+	0x63, 0x6b, 0x65, 0x6e, 0x64, 0x2e, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2e,
+	0x55, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x49, 0x44, 0x2e, 0x55, 0x6e, 0x69, 0x71, 0x75, 0x65, 0x44,
+	0x61, 0x74, 0x61, 0x52, 0x04, 0x64, 0x61, 0x74, 0x61, 0x1a, 0x31, 0x0a, 0x0a, 0x55, 0x6e, 0x69,
+	0x71, 0x75, 0x65, 0x44, 0x61, 0x74, 0x61, 0x12, 0x23, 0x0a, 0x0d, 0x65, 0x6e, 0x63, 0x6f, 0x64,
+	0x65, 0x5f, 0x6e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x0c,
+	0x65, 0x6e, 0x63, 0x6f, 0x64, 0x65, 0x4e, 0x75, 0x6d, 0x62, 0x65, 0x72, 0x42, 0x0f, 0x5a, 0x0d,
+	0x2e, 0x3b, 0x6f, 0x70, 0x65, 0x72, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x50, 0x62, 0x62, 0x06, 0x70,
+	0x72, 0x6f, 0x74, 0x6f, 0x33,
 }
 
 var (
@@ -610,33 +731,36 @@ func file_backend_operation_feed_formula_proto_rawDescGZIP() []byte {
 	return file_backend_operation_feed_formula_proto_rawDescData
 }
 
-var file_backend_operation_feed_formula_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
+var file_backend_operation_feed_formula_proto_msgTypes = make([]protoimpl.MessageInfo, 7)
 var file_backend_operation_feed_formula_proto_goTypes = []interface{}{
 	(*AddFeedFormulaRequest)(nil),         // 0: backend.operation.AddFeedFormulaRequest
 	(*SearchFeedFormulaRequest)(nil),      // 1: backend.operation.SearchFeedFormulaRequest
 	(*SearchFeedFormulaListResponse)(nil), // 2: backend.operation.SearchFeedFormulaListResponse
 	(*SearchFeedFormulaListData)(nil),     // 3: backend.operation.SearchFeedFormulaListData
 	(*IsShowModifyFeedFormula)(nil),       // 4: backend.operation.IsShowModifyFeedFormula
-	(CattleCategoryParent_Kind)(0),        // 5: backend.operation.CattleCategoryParent.Kind
-	(DataSource_Kind)(0),                  // 6: backend.operation.DataSource.Kind
-	(IsShow_Kind)(0),                      // 7: backend.operation.IsShow.Kind
-	(*PaginationModel)(nil),               // 8: backend.operation.PaginationModel
+	(*UniqueID)(nil),                      // 5: backend.operation.UniqueID
+	(*UniqueID_UniqueData)(nil),           // 6: backend.operation.UniqueID.UniqueData
+	(CattleCategoryParent_Kind)(0),        // 7: backend.operation.CattleCategoryParent.Kind
+	(DataSource_Kind)(0),                  // 8: backend.operation.DataSource.Kind
+	(IsShow_Kind)(0),                      // 9: backend.operation.IsShow.Kind
+	(*PaginationModel)(nil),               // 10: backend.operation.PaginationModel
 }
 var file_backend_operation_feed_formula_proto_depIdxs = []int32{
-	5, // 0: backend.operation.AddFeedFormulaRequest.cattle_category_id:type_name -> backend.operation.CattleCategoryParent.Kind
-	6, // 1: backend.operation.AddFeedFormulaRequest.data_source_id:type_name -> backend.operation.DataSource.Kind
-	7, // 2: backend.operation.AddFeedFormulaRequest.is_show:type_name -> backend.operation.IsShow.Kind
-	7, // 3: backend.operation.AddFeedFormulaRequest.is_modify:type_name -> backend.operation.IsShow.Kind
-	7, // 4: backend.operation.SearchFeedFormulaRequest.is_show:type_name -> backend.operation.IsShow.Kind
-	8, // 5: backend.operation.SearchFeedFormulaRequest.pagination:type_name -> backend.operation.PaginationModel
-	3, // 6: backend.operation.SearchFeedFormulaListResponse.data:type_name -> backend.operation.SearchFeedFormulaListData
-	0, // 7: backend.operation.SearchFeedFormulaListData.list:type_name -> backend.operation.AddFeedFormulaRequest
-	7, // 8: backend.operation.IsShowModifyFeedFormula.is_show:type_name -> backend.operation.IsShow.Kind
-	9, // [9:9] is the sub-list for method output_type
-	9, // [9:9] is the sub-list for method input_type
-	9, // [9:9] is the sub-list for extension type_name
-	9, // [9:9] is the sub-list for extension extendee
-	0, // [0:9] is the sub-list for field type_name
+	7,  // 0: backend.operation.AddFeedFormulaRequest.cattle_category_id:type_name -> backend.operation.CattleCategoryParent.Kind
+	8,  // 1: backend.operation.AddFeedFormulaRequest.data_source_id:type_name -> backend.operation.DataSource.Kind
+	9,  // 2: backend.operation.AddFeedFormulaRequest.is_show:type_name -> backend.operation.IsShow.Kind
+	9,  // 3: backend.operation.AddFeedFormulaRequest.is_modify:type_name -> backend.operation.IsShow.Kind
+	9,  // 4: backend.operation.SearchFeedFormulaRequest.is_show:type_name -> backend.operation.IsShow.Kind
+	10, // 5: backend.operation.SearchFeedFormulaRequest.pagination:type_name -> backend.operation.PaginationModel
+	3,  // 6: backend.operation.SearchFeedFormulaListResponse.data:type_name -> backend.operation.SearchFeedFormulaListData
+	0,  // 7: backend.operation.SearchFeedFormulaListData.list:type_name -> backend.operation.AddFeedFormulaRequest
+	9,  // 8: backend.operation.IsShowModifyFeedFormula.is_show:type_name -> backend.operation.IsShow.Kind
+	6,  // 9: backend.operation.UniqueID.data:type_name -> backend.operation.UniqueID.UniqueData
+	10, // [10:10] is the sub-list for method output_type
+	10, // [10:10] is the sub-list for method input_type
+	10, // [10:10] is the sub-list for extension type_name
+	10, // [10:10] is the sub-list for extension extendee
+	0,  // [0:10] is the sub-list for field type_name
 }
 
 func init() { file_backend_operation_feed_formula_proto_init() }
@@ -707,6 +831,30 @@ func file_backend_operation_feed_formula_proto_init() {
 				return nil
 			}
 		}
+		file_backend_operation_feed_formula_proto_msgTypes[5].Exporter = func(v interface{}, i int) interface{} {
+			switch v := v.(*UniqueID); i {
+			case 0:
+				return &v.state
+			case 1:
+				return &v.sizeCache
+			case 2:
+				return &v.unknownFields
+			default:
+				return nil
+			}
+		}
+		file_backend_operation_feed_formula_proto_msgTypes[6].Exporter = func(v interface{}, i int) interface{} {
+			switch v := v.(*UniqueID_UniqueData); i {
+			case 0:
+				return &v.state
+			case 1:
+				return &v.sizeCache
+			case 2:
+				return &v.unknownFields
+			default:
+				return nil
+			}
+		}
 	}
 	type x struct{}
 	out := protoimpl.TypeBuilder{
@@ -714,7 +862,7 @@ func file_backend_operation_feed_formula_proto_init() {
 			GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
 			RawDescriptor: file_backend_operation_feed_formula_proto_rawDesc,
 			NumEnums:      0,
-			NumMessages:   5,
+			NumMessages:   7,
 			NumExtensions: 0,
 			NumServices:   0,
 		},