animeic 2 жил өмнө
parent
commit
616a53e9bb

+ 6 - 1
boxcost/api/bill-produce.go

@@ -120,8 +120,13 @@ func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		return nil, errors.New("id的为空")
 	}
 
+	billType, err := SearchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
+	if err != nil {
+		return nil, err
+	}
+
 	// 如果更改类型
-	if len(bill.Type) > 0 {
+	if billType != bill.Type {
 		bill.SerialNumber, err = incrementer(apictx, bill.Type)
 		if err != nil {
 			return nil, err

+ 8 - 1
boxcost/api/bill.go

@@ -126,13 +126,20 @@ func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if bill.Id.Hex() == "" {
 		return nil, errors.New("id的为空")
 	}
-	if len(bill.Type) > 0 {
+	billType, err := SearchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
+	if err != nil {
+		return nil, err
+	}
+
+	// 如果更改类型
+	if billType != bill.Type {
 		bill.SerialNumber, err = incrementer(apictx, bill.Type)
 		if err != nil {
 			return nil, err
 		}
 
 	}
+
 	bill.UpdateTime = time.Now()
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
 }

+ 7 - 1
boxcost/api/test_print.go

@@ -1,10 +1,16 @@
 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) {
-	return incrementer(apictx, "纸张类")
+
+	id, _ := primitive.ObjectIDFromHex("638edc9ac3242a12b462efce")
+	return SearchBillTypeById(apictx, repo.CollectionBillProduce, id)
+	// return incrementer(apictx, "纸张类")
 
 }

+ 14 - 0
boxcost/api/utils.go

@@ -6,6 +6,7 @@ import (
 	"fmt"
 
 	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
 func incrementer(ctx *ApiSession, typeName string) (serial string, err error) {
@@ -34,3 +35,16 @@ func incrementer(ctx *ApiSession, typeName string) (serial string, err error) {
 	return fmt.Sprintf("%s-%05d", cate.LetterName, index), nil
 
 }
+
+func SearchBillTypeById(ctx *ApiSession, collectName string, id primitive.ObjectID) (string, error) {
+	found, curbill := repo.RepoSeachDocMap(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: collectName,
+		Project:     []string{"type"},
+		Query:       repo.Map{"_id": id},
+		Sort:        bson.M{"_id": -1},
+	})
+	if !found {
+		return "", fmt.Errorf("未找到该分类")
+	}
+	return curbill["type"].(string), nil
+}