sun-pc 7 months ago
parent
commit
e679e34a14
3 changed files with 42 additions and 18 deletions
  1. 5 5
      boxcost/api/aadiffupdatetest.http
  2. 8 0
      boxcost/api/index.html
  3. 29 13
      boxcost/db/repo/repo.go

+ 5 - 5
boxcost/api/aadiffupdatetest.http

@@ -9,10 +9,10 @@ POST http://{{host}}/boxcost/diffUpdatePlanTest HTTP/1.1
 Content-Type: application/json
 Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjI4MjU4OTIsImlkIjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0Iiwia2V5IjoiYm94Y29zdCIsIm5hbWUiOiLlrZnog5wiLCJvcmlnX2lhdCI6MTcyMjIyMTA5MiwicGFyZW50IjoiNjQyYTUyNGY1ZjUwYmM5MDNmOTg2Mzk0IiwicGhvbmUiOiIxMzQwODU0NzgyMyIsInJvbGUiOiIiLCJzdGF0ZSI6MSwidXNlclR5cGUiOjJ9.Cz7qI3-Vah0io5ZLIFDyVOf8qsqdmbT8WHdprrjbdXw
 
-{
-  
-}
- 
+ {
+  "_id": "64780c8de1c4729d48677847"
+ }
+
 ###
 # 工艺单日志记录测试
 # 测试数据表(collection): bill-produce_copy1
@@ -99,7 +99,7 @@ Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE3MjI4MjU4O
 	// CollectionPlanTrack   = "plan-track"
 ###
 # 打印更改差异
-GET http://{{host}}/boxcost/logs/list?query={"targetId":"64aa52935cca777202101884","collection":"product-plan_copy1"} HTTP/1.1
+GET http://{{host}}/boxcost/printDiff?query={"targetId":"63f31e901031634bc6eeb8d4","collection":"test"} HTTP/1.1
 Content-Type: application/json
 
 

File diff suppressed because it is too large
+ 8 - 0
boxcost/api/index.html


+ 29 - 13
boxcost/db/repo/repo.go

@@ -8,8 +8,6 @@ import (
 	"context"
 	"encoding/json"
 	"fmt"
-	"html"
-	"regexp"
 	"time"
 
 	"github.com/sergi/go-diff/diffmatchpatch"
@@ -302,21 +300,39 @@ func diffUpdateData(oldObj any, newObj any) (*DiffUpdateData, error) {
 
 	dmp := diffmatchpatch.New()
 	diffs := dmp.DiffMain(string(oldObjByte), string(newObjByte), false)
+	fmt.Println(diffs)
+	if len(diffs) < 1 {
+		return &DiffUpdateData{}, nil
+	}
+
+	diffData := &DiffUpdateData{}
+	for _, diff := range diffs {
+		switch diff.Type {
+		case diffmatchpatch.DiffInsert:
+			diffData.Diff += fmt.Sprintf("<ins>%s</ins>", diff.Text)
+		case diffmatchpatch.DiffDelete:
+			diffData.Diff += fmt.Sprintf("<des>%s</des>", diff.Text)
+		case diffmatchpatch.DiffEqual:
+			diffData.Diff += diff.Text
+		}
+	}
+	diffData.Changes = []string{}
+	// dmp.diff
 
 	// 返回差异数据用于日志记录
-	diff := &DiffUpdateData{}
-	if len(diffs) > 0 {
-		// return dmp.DiffPrettyText(diffs), nil
-		htmlString := html.UnescapeString(dmp.DiffPrettyHtml(diffs))
-		delInsRegex := regexp.MustCompile(`"([^"]+)":\s*"([^"]*(<del[^>]*>.*?</del>|<ins[^>]*>.*?</ins>)+[^"]*)",`)
-		delInsMatches := delInsRegex.FindAllString(htmlString, -1)
-		diff.Diff = htmlString
-		diff.Changes = delInsMatches
-		return diff, nil
+	// diff := &DiffUpdateData{}
+	// if len(diffs) > 0 {
+	// 	// return dmp.DiffPrettyText(diffs), nil
+	// 	htmlString := html.UnescapeString(dmp.DiffPrettyHtml(diffs))
+	// 	delInsRegex := regexp.MustCompile(`"([^"]+)":\s*"([^"]*(<del[^>]*>.*?</del>|<ins[^>]*>.*?</ins>)+[^"]*)",`)
+	// 	delInsMatches := delInsRegex.FindAllString(htmlString, -1)
+	// 	diff.Diff = htmlString
+	// 	diff.Changes = delInsMatches
+	// 	return diff, nil
 
-	}
+	// }
 
-	return &DiffUpdateData{}, err
+	return diffData, nil
 }
 
 func RepoUpdateSeDbDoc(ctx *RepoSession, db string, collectName string, idstr string, model interface{}) (*mongo.UpdateResult, error) {

Some files were not shown because too many files changed in this diff