animeic 2 years ago
parent
commit
281a1bb77f
3 changed files with 17 additions and 10 deletions
  1. 3 6
      boxcost/api/test_print.go
  2. 13 4
      boxcost/api/utils.go
  3. 1 0
      boxcost/db/model/increment.go

+ 3 - 6
boxcost/api/test_print.go

@@ -1,16 +1,13 @@
 package api
 
 import (
-	"box-cost/db/repo"
-
 	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
 func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 
-	id, _ := primitive.ObjectIDFromHex("638edc9ac3242a12b462efce")
-	return searchBillTypeById(apictx, repo.CollectionBillProduce, id)
-	// return incrementer(apictx, "纸张类")
+	// id, _ := primitive.ObjectIDFromHex("638edc9ac3242a12b462efce")
+	// return searchBillTypeById(apictx, repo.CollectionBillProduce, id)
+	return generateSerial(apictx, "纸张类")
 
 }

+ 13 - 4
boxcost/api/utils.go

@@ -19,17 +19,26 @@ func generateSerial(ctx *ApiSession, typeName string) (serial string, err error)
 		Sort:        bson.M{"_id": -1},
 	}, cate)
 	if !found || err != nil {
-		return "", fmt.Errorf("未找到该类")
+		return "", fmt.Errorf("未找到该类")
 	}
 	// 自增器 increment index加1
 	increment := &model.Increment{}
-	repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
+	found, _ = repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
 		CollectName: repo.CollectionIncrement,
+		Query:       repo.Map{"type": cate.LetterName},
 		Project:     []string{"index"},
 		Sort:        bson.M{"_id": -1},
 	}, increment)
+	if !found {
+		repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{
+			Type:  cate.LetterName,
+			Index: 1,
+		})
+		return fmt.Sprintf("%s-%06d", cate.LetterName, 1), nil
+	}
+
 	index := increment.Index + 1
-	repo.RepoAddDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, &model.Increment{Index: index})
+	repo.RepoUpdateSetDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index})
 
 	// 拼接为序号
 	return fmt.Sprintf("%s-%06d", cate.LetterName, index), nil
@@ -44,7 +53,7 @@ func searchBillTypeById(ctx *ApiSession, collectName string, id primitive.Object
 		Sort:        bson.M{"_id": -1},
 	})
 	if !found {
-		return "", fmt.Errorf("未找到该类")
+		return "", fmt.Errorf("未找到该类")
 	}
 	return curbill["type"].(string), nil
 }

+ 1 - 0
boxcost/db/model/increment.go

@@ -7,4 +7,5 @@ import (
 type Increment struct {
 	Id    primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
 	Index int32              `bson:"index,omitempty" json:"index"`
+	Type  string             `bson:"type,omitempty" json:"type"`
 }