Bladeren bron

更新历史记录实现

sun-pc-linux 7 maanden geleden
bovenliggende
commit
dc528ac791

+ 0 - 2
boxcost/api/aadiffupdatetest.go

@@ -37,7 +37,6 @@ func DiffUpdatePlanTest(c *gin.Context, apictx *ApiSession) (interface{}, error)
 
 	result, err1 := repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), "product-plan_copy1", plan.Id.Hex(), &plan, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: plan.Id.Hex(),
 	})
 
@@ -82,7 +81,6 @@ func DiffUpdateProduceTest(c *gin.Context, apictx *ApiSession) (interface{}, err
 
 	result, err1 := repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), "bill-produce_copy1", produce.Id.Hex(), &produce, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: produce.Id.Hex(),
 	})
 

+ 10 - 7
boxcost/api/bill-produce.go

@@ -81,13 +81,12 @@ func ProduceReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		UpdateTime: time.Now(),
 		SignUsers:  signs,
 	}
-	desc := fmt.Sprintf("【%s】审核了订单", user.Name)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &produce, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: _id,
-		Desc:     desc,
+		Type:     "reviewed",
 	})
 
 }
@@ -205,12 +204,16 @@ func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	}
 	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
 	user, _ := getUserById(apictx, userId)
-	desc := fmt.Sprintf("【%s】更新了订单", user.Name)
+	logType := "update"
 	// 计算结算价格
 	if bill.Status == "complete" {
 		bill.CompleteTime = time.Now()
-		desc = fmt.Sprintf("【%s】完成了订单", user.Name)
+		logType = "complete"
 	}
+	if bill.Status == "deprecated" {
+		logType = "deprecated"
+	}
+
 	if bill.Remark == "" {
 		bill.Remark = " "
 	}
@@ -240,9 +243,9 @@ func UpdateProduceBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, bill.Id.Hex(), &bill, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: bill.Id.Hex(),
-		Desc:     desc,
+		Type:     logType,
 	})
 }
 

+ 10 - 7
boxcost/api/bill-product.go

@@ -82,13 +82,12 @@ func ProductReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		SignUsers:  signs,
 	}
 
-	desc := fmt.Sprintf("【%s】审核了订单", user.Name)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &product)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &product, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: _id,
-		Desc:     desc,
+		Type:     "reviewed",
 	})
 
 }
@@ -207,10 +206,14 @@ func UpdateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	// 计算结算价格
 	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
 	user, _ := getUserById(apictx, userId)
-	desc := fmt.Sprintf("【%s】更新了订单", user.Name)
+	logType := "update"
+	// 计算结算价格
 	if bill.Status == "complete" {
 		bill.CompleteTime = time.Now()
-		desc = fmt.Sprintf("【%s】完成了订单", user.Name)
+		logType = "complete"
+	}
+	if bill.Status == "deprecated" {
+		logType = "deprecated"
 	}
 
 	if bill.Remark == "" {
@@ -242,9 +245,9 @@ func UpdateProductBill(c *gin.Context, apictx *ApiSession) (interface{}, error)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, bill.Id.Hex(), &bill, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: bill.Id.Hex(),
-		Desc:     desc,
+		Type:     logType,
 	})
 
 }

+ 12 - 10
boxcost/api/bill.go

@@ -71,13 +71,12 @@ func BillRecord(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	update := bson.M{"isRecord": req.Record}
 	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
 	user, _ := getUserById(apictx, userId)
-	desc := fmt.Sprintf("【%s】对订单进行了对账", user.Name)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), collection, req.Id.Hex(), &update)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), collection, req.Id.Hex(), &update, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: req.Id.Hex(),
-		Desc:     desc,
+		Type:     "recorded",
 	})
 
 }
@@ -125,13 +124,12 @@ func PurchaseReview(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		SignUsers:  signs,
 	}
 
-	desc := fmt.Sprintf("【%s】审核了订单", user.Name)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &purchase)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &purchase, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: _id,
-		Desc:     desc,
+		Type:     "reviewed",
 	})
 
 }
@@ -344,10 +342,14 @@ func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	}
 	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
 	user, _ := getUserById(apictx, userId)
-	desc := fmt.Sprintf("【%s】更新了订单", user.Name)
+	logType := "update"
+	// 计算结算价格
 	if bill.Status == "complete" {
 		bill.CompleteTime = time.Now()
-		desc = fmt.Sprintf("【%s】完成了订单", user.Name)
+		logType = "complete"
+	}
+	if bill.Status == "deprecated" {
+		logType = "deprecated"
 	}
 	if bill.Remark == "" {
 		bill.Remark = " "
@@ -377,9 +379,9 @@ func UpdateBill(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, bill.Id.Hex(), &bill, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: bill.Id.Hex(),
-		Desc:     desc,
+		Type:     logType,
 	})
 }
 

+ 0 - 1
boxcost/api/craft.go

@@ -120,7 +120,6 @@ func UpdateCraft(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionCraft, craft.Id.Hex(), &craft)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionCraft, craft.Id.Hex(), &craft, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: craft.Id.Hex(),
 	})
 }

+ 0 - 50
boxcost/api/diff-logs.go

@@ -1,50 +0,0 @@
-package api
-
-import (
-	"box-cost/db/repo"
-	"errors"
-
-	"github.com/gin-gonic/gin"
-	"go.mongodb.org/mongo-driver/bson"
-)
-
-func BillHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
-	// id := c.Query("id")
-	// CollectionMaterial      = "material"
-	// CollectionCraft         = "craft"
-	// CollectionProduct       = "product"
-	// CollectionSupplier      = "supplier"
-	// CollectionSupplierPrice = "supplier-price"
-	// CollectionPack          = "pack"
-	// CollectionProductPlan   = "product-plan" // 计划
-	// CollectionBillPurchase  = "bill-purchase" // 材料采购单
-	// CollectionBillProduce   = "bill-produce"  // 工艺采购单
-	// CollectionBillProduct   = "bill-product" // 成品采购单
-
-	// CollectionSupplierMatprice     = "supplier-mats"
-	// CollectionSupplierCraftprice   = "supplier-crafts"
-	// CollectionSupplierProductprice = "supplier-product" // 成品采购
-	// CollectionIncrement            = "increment"
-	// CollectionSignature            = "signature"
-	// CollectionUsers                = "users"
-	// // 更改日志记录
-	// CollectionLogs        = "logs"
-	// CollectionRequestLogs = "request-logs"
-	// CollectionPlanTrack   = "plan-track"
-
-	// ?query={"targetId":"","collection":""}
-	page, size, query := UtilQueryPageSize(c)
-	if _, ok := query["targetId"]; !ok {
-		return errors.New("目标id不能为空"), nil
-	}
-	if _, ok := query["collection"]; !ok {
-		return errors.New("目标类型不能为空"), nil
-	}
-	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
-		CollectName: repo.CollectionBillHistory,
-		Query:       query,
-		Page:        page,
-		Size:        size,
-		Sort:        bson.M{"createTime": -1},
-	})
-}

+ 99 - 0
boxcost/api/history.go

@@ -0,0 +1,99 @@
+package api
+
+import (
+	"box-cost/db/model"
+	"box-cost/db/repo"
+	"box-cost/log"
+	"errors"
+
+	"github.com/gin-gonic/gin"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+)
+
+var HistoryProject = []string{
+	"_id",
+	"path",
+	"collection",
+	"type",
+	"userInfo",
+	"createTime",
+}
+
+func BillHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	// ?query={"targetId":"","collection":""}
+	page, size, query := UtilQueryPageSize(c)
+	if _, ok := query["targetId"]; !ok {
+		return errors.New("目标id不能为空"), nil
+	}
+	if _, ok := query["collection"]; !ok {
+		return errors.New("目标类型不能为空"), nil
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionBillHistory,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Project:     HistoryProject,
+		Sort:        bson.M{"createTime": -1},
+	})
+}
+
+func GetBillHistory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	hid := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(hid)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var history model.History
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionBillHistory,
+		Query:       repo.Map{"_id": id},
+	}, &history)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return history, nil
+
+}
+
+func PlanHistoryList(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	// ?query={"targetId":"","collection":""}
+	page, size, query := UtilQueryPageSize(c)
+	if _, ok := query["targetId"]; !ok {
+		return errors.New("目标id不能为空"), nil
+	}
+	if _, ok := query["collection"]; !ok {
+		return errors.New("目标类型不能为空"), nil
+	}
+	return repo.RepoPageSearch(apictx.CreateRepoCtx(), &repo.PageSearchOptions{
+		CollectName: repo.CollectionPlanHistory,
+		Query:       query,
+		Page:        page,
+		Size:        size,
+		Project:     HistoryProject,
+		Sort:        bson.M{"createTime": -1},
+	})
+}
+
+func GetPlanHistory(c *gin.Context, apictx *ApiSession) (interface{}, error) {
+	hid := c.Param("id")
+	id, err := primitive.ObjectIDFromHex(hid)
+	if err != nil {
+		return nil, errors.New("非法id")
+	}
+	var history model.History
+	found, err := repo.RepoSeachDoc(apictx.CreateRepoCtx(), &repo.DocSearchOptions{
+		CollectName: repo.CollectionPlanHistory,
+		Query:       repo.Map{"_id": id},
+	}, &history)
+	if !found || err != nil {
+		log.Info(err)
+		return nil, errors.New("数据未找到")
+	}
+
+	return history, nil
+
+}

+ 0 - 1
boxcost/api/material.go

@@ -114,7 +114,6 @@ func UpdateMaterial(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionMaterial, mat.Id.Hex(), &mat)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionMaterial, mat.Id.Hex(), &mat, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: mat.Id.Hex(),
 	})
 }

+ 0 - 1
boxcost/api/pack.go

@@ -111,7 +111,6 @@ func UpdatePack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionPack, pack.Id.Hex(), &pack)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionPack, pack.Id.Hex(), &pack, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: pack.Id.Hex(),
 	})
 }

+ 0 - 1
boxcost/api/plan-process-track.go

@@ -137,7 +137,6 @@ func UpdatePlanTrack(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	pt.UpdateTime = time.Now()
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionPlanTrack, pt.Id.Hex(), &pt, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: pt.Id.Hex(),
 	})
 }

+ 15 - 9
boxcost/api/plan.go

@@ -105,7 +105,6 @@ func PlanAllocBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	if len(typeBillIds) < 1 {
 		return nil, errors.New("未找到单据信息")
 	}
-	desc := fmt.Sprintf("【%s】发送了订单", user.Name)
 
 	var wg sync.WaitGroup
 	for _, tId := range typeBillIds {
@@ -119,9 +118,9 @@ func PlanAllocBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			// _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId.Hex(), &model.PurchaseBill{IsSend: true, SendTime: time.Now()})
 			_, err = repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId.Hex(), &model.PurchaseBill{IsSend: true, SendTime: time.Now()}, &repo.RecordLogReq{
 				Path:     c.Request.URL.Path,
-				UserId:   apictx.User.ID,
+				UserInfo: user,
 				TargetId: billId.Hex(),
-				Desc:     desc,
+				Type:     "send",
 			})
 		}
 		// 工艺
@@ -130,9 +129,9 @@ func PlanAllocBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			// _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId.Hex(), &model.ProduceBill{IsSend: true, SendTime: time.Now()})
 			_, err = repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId.Hex(), &model.ProduceBill{IsSend: true, SendTime: time.Now()}, &repo.RecordLogReq{
 				Path:     c.Request.URL.Path,
-				UserId:   apictx.User.ID,
+				UserInfo: user,
 				TargetId: billId.Hex(),
-				Desc:     desc,
+				Type:     "send",
 			})
 		}
 		// 成品采购
@@ -141,9 +140,9 @@ func PlanAllocBatch(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 			// _, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()})
 			_, err = repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()}, &repo.RecordLogReq{
 				Path:     c.Request.URL.Path,
-				UserId:   apictx.User.ID,
+				UserInfo: user,
 				TargetId: billId.Hex(),
-				Desc:     desc,
+				Type:     "send",
 			})
 		}
 
@@ -191,11 +190,14 @@ func updateStageCount(c *gin.Context, planId primitive.ObjectID, idCounts map[st
 
 	}
 	plan.UpdateTime = time.Now()
+	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
+	user, _ := getUserById(apictx, userId)
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId.Hex(), &plan)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionProductPlan, planId.Hex(), &plan, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: planId.Hex(),
+		Type:     "supplierSyncConfirmCount",
 	})
 }
 
@@ -1359,11 +1361,15 @@ func UpdateProductPlan(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		return nil, errors.New("id的为空")
 	}
 	plan.UpdateTime = time.Now()
+	userId, _ := primitive.ObjectIDFromHex(apictx.User.ID)
+	user, _ := getUserById(apictx, userId)
+
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionProductPlan, plan.Id.Hex(), &plan, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
+		UserInfo: user,
 		TargetId: plan.Id.Hex(),
+		Type:     "update",
 	})
 }
 

+ 3 - 0
boxcost/api/router.go

@@ -22,6 +22,9 @@ func RegRouters(svc *Service) {
 	boxcost.POSTJWT("/diffUpdateProduceTest", DiffUpdateProduceTest)
 	// boxcost.GET("/printDiff", PrintDiff)
 	boxcost.GETJWT("/bill/history/list", BillHistoryList)
+	boxcost.GETJWT("/plan/history/list", PlanHistoryList)
+	boxcost.GETJWT("/bill/history/detail/:id", GetBillHistory)
+	boxcost.GETJWT("/plan/history/detail/:id", GetPlanHistory)
 	boxcost.POSTJWT("/download/plan/track", DownloadPlanTrack)
 
 	PlanTrack(boxcost)

+ 0 - 1
boxcost/api/service.go

@@ -88,7 +88,6 @@ func CreateCRUD(router *GinRouter, prefix string, option *CRUDOption) {
 			// out, err := repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), option.Collection, objId, m)
 			out, err := repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), option.Collection, objId, m, &repo.RecordLogReq{
 				Path:     c.Request.URL.Path,
-				UserId:   apictx.User.ID,
 				TargetId: objId,
 			})
 

+ 0 - 1
boxcost/api/signature.go

@@ -112,7 +112,6 @@ func SignatureUpdate(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSignature, signature.Id.Hex(), &signature)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionSignature, signature.Id.Hex(), &signature, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: signature.Id.Hex(),
 	})
 }

+ 0 - 1
boxcost/api/supplier-price.go

@@ -340,7 +340,6 @@ func UpdateSupplierPrice(c *gin.Context, apictx *ApiSession) (interface{}, error
 	// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice)
 	return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionSupplierPrice, supplierprice.Id.Hex(), &supplierprice, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   apictx.User.ID,
 		TargetId: supplierprice.Id.Hex(),
 	})
 }

+ 12 - 14
boxcost/api/supplier.go

@@ -167,7 +167,6 @@ func SupplierBillAlloc(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		return nil, errors.New("订单类型错误")
 	}
 
-	desc := fmt.Sprintf("【%s】发送了订单", user.Name)
 	result := &mongo.UpdateResult{}
 	var err error
 	switch billType {
@@ -175,25 +174,25 @@ func SupplierBillAlloc(c *gin.Context, apictx *ApiSession) (interface{}, error)
 		// result, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId.Hex(), &model.PurchaseBill{IsSend: true, SendTime: time.Now()})
 		result, err = repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, billId.Hex(), &model.PurchaseBill{IsSend: true, SendTime: time.Now()}, &repo.RecordLogReq{
 			Path:     c.Request.URL.Path,
-			UserId:   apictx.User.ID,
+			UserInfo: user,
 			TargetId: billId.Hex(),
-			Desc:     desc,
+			Type:     "send",
 		})
 	case PRODUCE_BILL_TYPE:
 		// result, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId.Hex(), &model.ProduceBill{IsSend: true, SendTime: time.Now()})
 		result, err = repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, billId.Hex(), &model.ProduceBill{IsSend: true, SendTime: time.Now()}, &repo.RecordLogReq{
 			Path:     c.Request.URL.Path,
-			UserId:   apictx.User.ID,
+			UserInfo: user,
 			TargetId: billId.Hex(),
-			Desc:     desc,
+			Type:     "send",
 		})
 	case PRODUCT_BILL_TYPE:
 		// result, err = repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()})
 		result, err = repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, billId.Hex(), &model.ProductBill{IsSend: true, SendTime: time.Now()}, &repo.RecordLogReq{
 			Path:     c.Request.URL.Path,
-			UserId:   apictx.User.ID,
+			UserInfo: user,
 			TargetId: billId.Hex(),
-			Desc:     desc,
+			Type:     "send",
 		})
 	default:
 		return result, nil
@@ -252,32 +251,31 @@ func SupplierBillAck(c *gin.Context, apictx *ApiSession) (interface{}, error) {
 		return nil, errors.New("订单类型错误")
 	}
 	user, _ := getUserById(apictx, userId)
-	desc := fmt.Sprintf("【%s】接收了订单", user.Name)
 	isAck := true
 	switch billType {
 	case "purchase":
 		// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &model.PurchaseBill{IsAck: &isAck, AckTime: time.Now()})
 		return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillPurchase, _id, &model.PurchaseBill{IsAck: &isAck, AckTime: time.Now()}, &repo.RecordLogReq{
 			Path:     c.Request.URL.Path,
-			UserId:   apictx.User.ID,
+			UserInfo: user,
 			TargetId: _id,
-			Desc:     desc,
+			Type:     "take",
 		})
 	case "produce":
 		// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &model.ProduceBill{IsAck: &isAck, AckTime: time.Now()})
 		return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduce, _id, &model.ProduceBill{IsAck: &isAck, AckTime: time.Now()}, &repo.RecordLogReq{
 			Path:     c.Request.URL.Path,
-			UserId:   apictx.User.ID,
+			UserInfo: user,
 			TargetId: _id,
-			Desc:     desc,
+			Type:     "take",
 		})
 	case "product":
 		// return repo.RepoUpdateSetDoc(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &model.ProductBill{IsAck: &isAck, AckTime: time.Now()})
 		return repo.RepoUpdateSetDoc1(apictx.CreateRepoCtx(), repo.CollectionBillProduct, _id, &model.ProductBill{IsAck: &isAck, AckTime: time.Now()}, &repo.RecordLogReq{
 			Path:     c.Request.URL.Path,
-			UserId:   apictx.User.ID,
+			UserInfo: user,
 			TargetId: _id,
-			Desc:     desc,
+			Type:     "take",
 		})
 	default:
 		return nil, errors.New("更新类型错误")

+ 0 - 1
boxcost/api/utils.go

@@ -147,7 +147,6 @@ func generateSerial(c *gin.Context, ctx *ApiSession, typeName string) (serial st
 	// repo.RepoUpdateSetDoc(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index})
 	repo.RepoUpdateSetDoc1(ctx.CreateRepoCtx(), repo.CollectionIncrement, increment.Id.Hex(), &model.Increment{Index: index}, &repo.RecordLogReq{
 		Path:     c.Request.URL.Path,
-		UserId:   ctx.User.ID,
 		TargetId: increment.Id.Hex(),
 	})
 

+ 2 - 2
boxcost/app.yaml

@@ -29,8 +29,8 @@ debug:
   UserRole: string
 
 nats:
-  url: nats://124.71.139.24:14300
-  # url: nats://127.0.0.1:14300
+  # url: nats://124.71.139.24:14300
+  url: nats://127.0.0.1:14300
   maxReconnect: 1000
   reconnDelaySecond: 5
 

+ 7 - 6
boxcost/db/model/bill-history.go → boxcost/db/model/history.go

@@ -6,14 +6,15 @@ import (
 	"go.mongodb.org/mongo-driver/bson/primitive"
 )
 
-type BillHistory struct {
+// 历史记录,订单/计划
+type History struct {
 	Id         primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
-	UserId     string             `bson:"userId,omitempty" json:"userId"`
 	TargetId   string             `bson:"targetId,omitempty" json:"targetId"`
 	Path       string             `bson:"path,omitempty" json:"path"`
 	Collection string             `bson:"collection,omitempty" json:"collection"`
-	// 谁更新了该订单
-	Desc       string    `bson:"desc,omitempty" json:"desc"`
-	Content    string    `bson:"content,omitempty" json:"content"`
-	CreateTime time.Time `bson:"createTime,omitempty" json:"createTime"`
+	Type       string             `bson:"type,omitempty" json:"type"`
+	// 谁操作
+	Userinfo   *UserSmaple `bson:"userInfo,omitempty" json:"userInfo"`
+	Content    string      `bson:"content,omitempty" json:"content"`
+	CreateTime time.Time   `bson:"createTime,omitempty" json:"createTime"`
 }

+ 87 - 21
boxcost/db/repo/repo.go

@@ -39,10 +39,11 @@ const (
 	CollectionSignature            = "signature"
 	CollectionUsers                = "users"
 	// 更改日志记录
-	CollectionLogs        = "logs"
+	// CollectionLogs        = "logs"
 	CollectionRequestLogs = "request-logs"
 	CollectionPlanTrack   = "plan-track"
 	CollectionBillHistory = "bill-history"
+	CollectionPlanHistory = "plan-history"
 )
 
 type Map map[string]interface{}
@@ -93,6 +94,43 @@ func RepoAddDoc(ctx *RepoSession, collectName string, doc interface{}) (string,
 	return result.InsertedID.(primitive.ObjectID).Hex(), nil
 }
 
+func RepoAddDoc1(ctx *RepoSession, collectName string, doc interface{}, recordLogReq *RecordLogReq) (string, error) {
+	colls := ctx.Client.GetCollection(collectName)
+	result, err := colls.InsertOne(ctx.Ctx, doc)
+	if err != nil {
+		return "", err
+	}
+	insertId, err := result.InsertedID.(primitive.ObjectID).Hex(), nil
+	if len(insertId) > 0 {
+		_id, _ := primitive.ObjectIDFromHex(insertId)
+		if isShouldRecordHistoryByName(collectName) {
+			var newData Map
+			colls.FindOne(ctx.Ctx, bson.M{"_id": _id}).Decode(&newData)
+			newObjByte, err := json.Marshal(newData)
+			if err != nil {
+				fmt.Println("记录历史失败:", err)
+			}
+			history := &dm.History{
+				Userinfo:   recordLogReq.UserInfo,
+				TargetId:   recordLogReq.TargetId,
+				Path:       recordLogReq.Path,
+				Collection: collectName,
+				Type:       "create",
+				Content:    string(newObjByte),
+				CreateTime: time.Now(),
+			}
+			// 订单
+			if isBill(collectName) {
+				RepoAddDoc(ctx, CollectionBillHistory, history)
+				// 计划
+			} else {
+				RepoAddDoc(ctx, CollectionPlanHistory, history)
+			}
+		}
+	}
+	return insertId, nil
+}
+
 func RepoDbAddDoc(ctx *RepoSession, dbName string, collectName string, doc interface{}) (string, error) {
 	users := ctx.Client.GetDbCollection(dbName, collectName)
 	result, err := users.InsertOne(ctx.Ctx, doc)
@@ -138,9 +176,9 @@ func RepoUpdateSetDoc(ctx *RepoSession, collectName string, idstr string, model
 
 type RecordLogReq struct {
 	Path     string
-	UserId   string
+	UserInfo *dm.UserSmaple
 	TargetId string
-	Desc     string
+	Type     string
 }
 
 func RepoUpdateSetDoc1(ctx *RepoSession, collectName string, idstr string, model interface{}, recordLogReq *RecordLogReq) (*mongo.UpdateResult, error) {
@@ -149,6 +187,7 @@ func RepoUpdateSetDoc1(ctx *RepoSession, collectName string, idstr string, model
 	update := bson.M{"$set": model}
 	uid, _ := primitive.ObjectIDFromHex(idstr)
 
+	// 查询更改前的数据,用于历史记录
 	result, err := colls.UpdateByID(ctx.Ctx, uid, update)
 
 	// 更新出错或者没有更新就不进行更新日志记录
@@ -156,33 +195,60 @@ func RepoUpdateSetDoc1(ctx *RepoSession, collectName string, idstr string, model
 		return result, err
 	}
 
-	// 计划和订单日志
-	var newData Map
-	colls.FindOne(ctx.Ctx, bson.M{"_id": uid}).Decode(&newData)
-	newObjByte, err := json.Marshal(newData)
-	if err != nil {
-		fmt.Println("记录历史失败:", err)
-		return result, err
-	}
-	// 单据历史记录
-	if collectName == CollectionBillPurchase || collectName == CollectionBillProduce || collectName == CollectionBillProduct {
-		history := &dm.BillHistory{
-			UserId:     recordLogReq.UserId,
+	if isShouldRecordHistoryByName(collectName) {
+		var newData Map
+		colls.FindOne(ctx.Ctx, bson.M{"_id": uid}).Decode(&newData)
+		newObjByte, err := json.Marshal(newData)
+		if err != nil {
+			fmt.Println("记录历史失败:", err)
+		}
+		history := &dm.History{
+			Userinfo:   recordLogReq.UserInfo,
 			TargetId:   recordLogReq.TargetId,
 			Path:       recordLogReq.Path,
 			Collection: collectName,
-			Desc:       recordLogReq.Desc,
+			Type:       recordLogReq.Type,
 			Content:    string(newObjByte),
 			CreateTime: time.Now(),
 		}
-		RepoAddDoc(ctx, CollectionBillHistory, history)
+		// 订单
+		if isBill(collectName) {
+			RepoAddDoc(ctx, CollectionBillHistory, history)
+			// 计划
+		} else {
+			RepoAddDoc(ctx, CollectionPlanHistory, history)
+		}
+	}
+	return result, err
+}
 
-	} else if collectName == CollectionProductPlan {
-		// TODO 计划历史
-		fmt.Println(collectName)
+func isBill(collection string) bool {
+	bills := []string{
+		CollectionBillPurchase,
+		CollectionBillProduce,
+		CollectionBillProduct,
 	}
+	for _, bill := range bills {
+		if bill == collection {
+			return true
+		}
+	}
+	return false
+}
 
-	return result, err
+func isShouldRecordHistoryByName(collection string) bool {
+	shouldRecords := []string{
+		CollectionBillPurchase,
+		CollectionBillProduce,
+		CollectionBillProduct,
+		CollectionProductPlan,
+	}
+	for _, sr := range shouldRecords {
+		if sr == collection {
+			return true
+		}
+	}
+	return false
 }
 
 func RepoUpdateSeDbDoc(ctx *RepoSession, db string, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {