package search import ( "fmt" "reflect" "sort" "testing" ) // 对比两个列表数据是否相同 func StringListCompare(list1, list2 []string) (isEqual bool, common, onlyList1, onlyList2 []string) { if len(list1) != len(list2) { isEqual = false } // 创建map来存储list1列表中的元素 AMap := make(map[string]int) for _, item := range list1 { AMap[item]++ } // 检查B列表中的元素是否在AMap中 for _, item := range list2 { if count, exists := AMap[item]; !exists || count == 0 { isEqual = false } AMap[item]-- } if !isEqual { // 创建一个map来存储A列表中的元素 list1Map := make(map[string]struct{}) for _, a := range list1 { list1Map[a] = struct{}{} } // 查找list2列表中存在但list1列表中不存在的元素 for _, v := range list2 { if _, exists := list1Map[v]; !exists { onlyList2 = append(onlyList2, v) } else { common = append(common, v) } } // 创建一个map来存储B列表中的元素 list2Map := make(map[string]struct{}) for _, b := range list2 { list2Map[b] = struct{}{} } // 查找list1列表中存在但list2列表中不存在的元素 for _, a := range list1 { if _, exists := list2Map[a]; !exists { onlyList1 = append(onlyList1, a) } } } isEqual = true return } func IntListCompare(list1, list2 []int) (isEqual bool, common, onlyList1, onlyList2 []int) { if len(list1) != len(list2) { isEqual = false } // 创建map来存储list1列表中的元素 AMap := make(map[int]int) for _, item := range list1 { AMap[item]++ } // 检查B列表中的元素是否在AMap中 for _, item := range list2 { if count, exists := AMap[item]; !exists || count == 0 { isEqual = false } AMap[item]-- } if !isEqual { // 创建一个map来存储A列表中的元素 list1Map := make(map[int]struct{}) for _, a := range list1 { list1Map[a] = struct{}{} } // 查找list2列表中存在但list1列表中不存在的元素 for _, v := range list2 { if _, exists := list1Map[v]; !exists { onlyList2 = append(onlyList2, v) } else { common = append(common, v) } } // 创建一个map来存储B列表中的元素 list2Map := make(map[int]struct{}) for _, b := range list2 { list2Map[b] = struct{}{} } // 查找list1列表中存在但list2列表中不存在的元素 for _, a := range list1 { if _, exists := list2Map[a]; !exists { onlyList1 = append(onlyList1, a) } } } isEqual = true return } type StockProduct struct { T_product_id int T_num int T_relation_sn []string } type StockProductRelationSn struct { T_product_id int T_delete_relation_sn []string T_add_relation_sn []string } // 比较两个StockProduct列表是否相等 func StockProductListsEqual(a, b []StockProduct) (isEqual bool, cDiff map[int]StockProductRelationSn) { cDiff = make(map[int]StockProductRelationSn) isEqual = true // 创建一个map来存储a列表中的StockProduct aMap := make(map[int]StockProduct) aProductList := []int{} for _, item := range a { sort.Strings(item.T_relation_sn) aMap[item.T_product_id] = item aProductList = append(aProductList, item.T_product_id) } bMap := make(map[int]StockProduct) bProductList := []int{} for _, item := range b { sort.Strings(item.T_relation_sn) bMap[item.T_product_id] = item bProductList = append(bProductList, item.T_product_id) } // 查找两个元素相同 _, common, needDelete, needAdd := IntListCompare(aProductList, bProductList) fmt.Println("needDelete:", needDelete) fmt.Println("needAdd:", needAdd) for _, product_id := range common { //_, _, aDiff[product_id], bDiff[product_id] = StringListCompare(aMap[product_id].T_relation_sn, bMap[product_id].T_relation_sn) equal, _, T_delete_relation_sn, T_add_relation_sn := StringListCompare(aMap[product_id].T_relation_sn, bMap[product_id].T_relation_sn) cDiff[product_id] = StockProductRelationSn{ T_product_id: product_id, T_delete_relation_sn: T_delete_relation_sn, T_add_relation_sn: T_add_relation_sn, } fmt.Println("equal:", equal) if !equal { isEqual = false } } if len(needDelete) == 0 && len(needAdd) == 0 && isEqual { isEqual = true } else { isEqual = false } return isEqual, cDiff } // 比较两个StockProduct是否相等 func stockProductEqual(a, b StockProduct) bool { return a.T_product_id == b.T_product_id && a.T_num == b.T_num && reflect.DeepEqual(a.T_relation_sn, b.T_relation_sn) } func TestStockProductListsEqual(t *testing.T) { // 定义两个StockProduct列表 A := []StockProduct{ {T_product_id: 1, T_num: 10, T_relation_sn: []string{"20220101", "20230202"}}, {T_product_id: 2, T_num: 20, T_relation_sn: []string{"20220303", "20230404"}}, } B := []StockProduct{ //{T_product_id: 1, T_num: 10, T_relation_sn: []string{"20220101", "20230202"}}, //{T_product_id: 2, T_num: 20, T_relation_sn: []string{"20220303", "20230404"}}, } //B := []StockProduct{ // {T_product_id: 1, T_num: 10, T_relation_sn: []string{"20220101", "202302"}}, // {T_product_id: 2, T_num: 20, T_relation_sn: []string{"20230404", "20220303"}}, // {T_product_id: 3, T_num: 20, T_relation_sn: []string{"20240404", "20240303"}}, //} //查找两个StockProduct列表中T_relation_sn不相同的元素 //aDiff, bDiff := StockProductListsEqual(A, B) ////打印结果 //fmt.Println("A列表中T_relation_sn不相同的元素:", aDiff) //fmt.Println("B列表中T_relation_sn不相同的元素:", bDiff) isEqual, aDiff := StockProductListsEqual(A, B) //打印结果 fmt.Println("是否相等:", isEqual) fmt.Println("A列表中T_relation_sn不相同的元素:", aDiff) //_, c, d, a := StringListCompare([]string{"20220101", "20230202"}, []string{"20230202", "20220103"}) //fmt.Println("共同元素:", c) //fmt.Println("仅A列表中:", d) //fmt.Println("仅B列表中:", a) }