|
@@ -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
|
|
|
+}
|