Explorar el Código

Merge branch 'master' of http://124.70.149.18:10880/sunsheng/box-cost

liwei hace 2 años
padre
commit
f251598514

+ 20 - 0
boxcost/api/bill-produce.go

@@ -53,6 +53,11 @@ func CreateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		return nil, errors.New("类型为空")
 	}
 
+	bill.SerialNumber, err = generateSerial(apictx, bill.Type)
+	if err != nil {
+		return nil, err
+	}
+
 	bill.Status = "created"
 	bill.CreateTime = time.Now()
 	bill.UpdateTime = time.Now()
@@ -114,6 +119,21 @@ func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	if bill.Id.Hex() == "" {
 		return nil, errors.New("id的为空")
 	}
+
+	billType, err := searchBillTypeById(apictx, repo.CollectionBillProduce, bill.Id)
+	if err != nil {
+		return nil, err
+	}
+
+	// 如果更改类型
+	if billType != bill.Type {
+		bill.SerialNumber, err = generateSerial(apictx, bill.Type)
+		if err != nil {
+			return nil, err
+		}
+
+	}
+
 	bill.UpdateTime = time.Now()
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
 }

+ 19 - 0
boxcost/api/bill.go

@@ -61,6 +61,11 @@ func CreateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("类型为空")
 	}
 
+	bill.SerialNumber, err = generateSerial(apictx, bill.Type)
+	if err != nil {
+		return nil, err
+	}
+
 	bill.Status = "created"
 	bill.CreateTime = time.Now()
 	bill.UpdateTime = time.Now()
@@ -121,6 +126,20 @@ func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if bill.Id.Hex() == "" {
 		return nil, errors.New("id的为空")
 	}
+	billType, err := searchBillTypeById(apictx, repo.CollectionBillPurchase, bill.Id)
+	if err != nil {
+		return nil, err
+	}
+
+	// 如果更改类型
+	if billType != bill.Type {
+		bill.SerialNumber, err = generateSerial(apictx, bill.Type)
+		if err != nil {
+			return nil, err
+		}
+
+	}
+
 	bill.UpdateTime = time.Now()
 	return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
 }

+ 8 - 2
boxcost/api/test_print.go

@@ -1,7 +1,13 @@
 package api
 
-import "github.com/gin-gonic/gin"
+import (
+	"github.com/gin-gonic/gin"
+)
 
 func Printr(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	return "aaa", nil
+
+	// id, _ := primitive.ObjectIDFromHex("638edc9ac3242a12b462efce")
+	// return searchBillTypeById(apictx, repo.CollectionBillProduce, id)
+	return generateSerial(apictx, "纸张类")
+
 }

+ 59 - 0
boxcost/api/utils.go

@@ -0,0 +1,59 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"fmt"
+
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+func generateSerial(ctx *ApiSession, typeName string) (serial string, err error) {
+	// 获取类型
+	cate := &model.Category{}
+	found, err := repo.RepoSeachDoc(ctx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: "cates",
+		Project:     []string{"letterName"},
+		Query:       repo.Map{"name": typeName},
+		Sort:        bson.M{"_id": -1},
+	}, cate)
+	if !found || err != nil {
+		return "", fmt.Errorf("未找到该类型")
+	}
+	// 自增器 increment index加1
+	increment := &model.Increment{}
+	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.RepoUpdateSetDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index})
+
+	// 拼接为序号
+	return fmt.Sprintf("%s-%06d", 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
+}

+ 1 - 1
boxcost/db/db.go

@@ -35,7 +35,7 @@ func (db *MongoDB) GetOrCreateDatabase(name string) *mongo.Database {
 }
 
 func NewMongoDB(bus *comm.NatsBus) *MongoDB {
-	inst, err := bus.NewMongoDBFromConfig("box-mongo")
+	inst, err := bus.NewMongoDBFromConfigDev("box-mongo")
 	if err != nil {
 		panic(err)
 	}

+ 6 - 0
boxcost/db/model/bill.go

@@ -56,6 +56,9 @@ type PurchaseBill struct {
 
 	//纸张类采购
 	Paper []*PaperBill `bson:"papers,omitempty" json:"papers"`
+
+	// 序号
+	SerialNumber string `bson:"serialNumber,omitempty" json:"serialNumber"`
 }
 
 // 工艺生产数据
@@ -114,4 +117,7 @@ type ProduceBill struct {
 
 	//纸张类采购
 	Produces []*ProduceBillData `bson:"produces,omitempty" json:"produces"`
+
+	// 序号
+	SerialNumber string `bson:"serialNumber,omitempty" json:"serialNumber"`
 }

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

@@ -0,0 +1,11 @@
+package model
+
+import (
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+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"`
+}

+ 7 - 0
boxcost/db/repo/repo.go

@@ -30,6 +30,7 @@ const (
 
 	CollectionSupplierMatprice   = "supplier-mats"
 	CollectionSupplierCraftprice = "supplier-crafts"
+	CollectionIncrement          = "increment"
 )
 
 type Map map[string]interface{}
@@ -189,6 +190,9 @@ func RepoSeachDoc(ctx *RepoSession, param *DocSearchOptions, v interface{}) (boo
 		}
 		opt.SetProjection(prj)
 	}
+	if len(param.Sort) > 0 {
+		opt.Sort = param.Sort
+	}
 	filter := bson.M{}
 	if len(param.Query) > 0 {
 		for k, v := range param.Query {
@@ -225,6 +229,9 @@ func RepoSeachDocMap(ctx *RepoSession, param *DocSearchOptions) (bool, map[strin
 		}
 		opt.SetProjection(prj)
 	}
+	if len(param.Sort) > 0 {
+		opt.Sort = param.Sort
+	}
 	filter := bson.M{}
 	if len(param.Query) > 0 {
 		for k, v := range param.Query {