|
@@ -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) {
|