Stock_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. package search
  2. import (
  3. "fmt"
  4. "reflect"
  5. "sort"
  6. "testing"
  7. )
  8. // 对比两个列表数据是否相同
  9. func StringListCompare(list1, list2 []string) (isEqual bool, common, onlyList1, onlyList2 []string) {
  10. if len(list1) != len(list2) {
  11. isEqual = false
  12. }
  13. // 创建map来存储list1列表中的元素
  14. AMap := make(map[string]int)
  15. for _, item := range list1 {
  16. AMap[item]++
  17. }
  18. // 检查B列表中的元素是否在AMap中
  19. for _, item := range list2 {
  20. if count, exists := AMap[item]; !exists || count == 0 {
  21. isEqual = false
  22. }
  23. AMap[item]--
  24. }
  25. if !isEqual {
  26. // 创建一个map来存储A列表中的元素
  27. list1Map := make(map[string]struct{})
  28. for _, a := range list1 {
  29. list1Map[a] = struct{}{}
  30. }
  31. // 查找list2列表中存在但list1列表中不存在的元素
  32. for _, v := range list2 {
  33. if _, exists := list1Map[v]; !exists {
  34. onlyList2 = append(onlyList2, v)
  35. } else {
  36. common = append(common, v)
  37. }
  38. }
  39. // 创建一个map来存储B列表中的元素
  40. list2Map := make(map[string]struct{})
  41. for _, b := range list2 {
  42. list2Map[b] = struct{}{}
  43. }
  44. // 查找list1列表中存在但list2列表中不存在的元素
  45. for _, a := range list1 {
  46. if _, exists := list2Map[a]; !exists {
  47. onlyList1 = append(onlyList1, a)
  48. }
  49. }
  50. }
  51. isEqual = true
  52. return
  53. }
  54. func IntListCompare(list1, list2 []int) (isEqual bool, common, onlyList1, onlyList2 []int) {
  55. if len(list1) != len(list2) {
  56. isEqual = false
  57. }
  58. // 创建map来存储list1列表中的元素
  59. AMap := make(map[int]int)
  60. for _, item := range list1 {
  61. AMap[item]++
  62. }
  63. // 检查B列表中的元素是否在AMap中
  64. for _, item := range list2 {
  65. if count, exists := AMap[item]; !exists || count == 0 {
  66. isEqual = false
  67. }
  68. AMap[item]--
  69. }
  70. if !isEqual {
  71. // 创建一个map来存储A列表中的元素
  72. list1Map := make(map[int]struct{})
  73. for _, a := range list1 {
  74. list1Map[a] = struct{}{}
  75. }
  76. // 查找list2列表中存在但list1列表中不存在的元素
  77. for _, v := range list2 {
  78. if _, exists := list1Map[v]; !exists {
  79. onlyList2 = append(onlyList2, v)
  80. } else {
  81. common = append(common, v)
  82. }
  83. }
  84. // 创建一个map来存储B列表中的元素
  85. list2Map := make(map[int]struct{})
  86. for _, b := range list2 {
  87. list2Map[b] = struct{}{}
  88. }
  89. // 查找list1列表中存在但list2列表中不存在的元素
  90. for _, a := range list1 {
  91. if _, exists := list2Map[a]; !exists {
  92. onlyList1 = append(onlyList1, a)
  93. }
  94. }
  95. }
  96. isEqual = true
  97. return
  98. }
  99. type StockProduct struct {
  100. T_product_id int
  101. T_num int
  102. T_relation_sn []string
  103. }
  104. type StockProductRelationSn struct {
  105. T_product_id int
  106. T_delete_relation_sn []string
  107. T_add_relation_sn []string
  108. }
  109. // 比较两个StockProduct列表是否相等
  110. func StockProductListsEqual(a, b []StockProduct) (isEqual bool, cDiff map[int]StockProductRelationSn) {
  111. cDiff = make(map[int]StockProductRelationSn)
  112. isEqual = true
  113. // 创建一个map来存储a列表中的StockProduct
  114. aMap := make(map[int]StockProduct)
  115. aProductList := []int{}
  116. for _, item := range a {
  117. sort.Strings(item.T_relation_sn)
  118. aMap[item.T_product_id] = item
  119. aProductList = append(aProductList, item.T_product_id)
  120. }
  121. bMap := make(map[int]StockProduct)
  122. bProductList := []int{}
  123. for _, item := range b {
  124. sort.Strings(item.T_relation_sn)
  125. bMap[item.T_product_id] = item
  126. bProductList = append(bProductList, item.T_product_id)
  127. }
  128. // 查找两个元素相同
  129. _, common, needDelete, needAdd := IntListCompare(aProductList, bProductList)
  130. fmt.Println("needDelete:", needDelete)
  131. fmt.Println("needAdd:", needAdd)
  132. for _, product_id := range common {
  133. //_, _, aDiff[product_id], bDiff[product_id] = StringListCompare(aMap[product_id].T_relation_sn, bMap[product_id].T_relation_sn)
  134. equal, _, T_delete_relation_sn, T_add_relation_sn := StringListCompare(aMap[product_id].T_relation_sn, bMap[product_id].T_relation_sn)
  135. cDiff[product_id] = StockProductRelationSn{
  136. T_product_id: product_id,
  137. T_delete_relation_sn: T_delete_relation_sn,
  138. T_add_relation_sn: T_add_relation_sn,
  139. }
  140. fmt.Println("equal:", equal)
  141. if !equal {
  142. isEqual = false
  143. }
  144. }
  145. if len(needDelete) == 0 && len(needAdd) == 0 && isEqual {
  146. isEqual = true
  147. } else {
  148. isEqual = false
  149. }
  150. return isEqual, cDiff
  151. }
  152. // 比较两个StockProduct是否相等
  153. func stockProductEqual(a, b StockProduct) bool {
  154. 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)
  155. }
  156. func TestStockProductListsEqual(t *testing.T) {
  157. // 定义两个StockProduct列表
  158. A := []StockProduct{
  159. {T_product_id: 1, T_num: 10, T_relation_sn: []string{"20220101", "20230202"}},
  160. {T_product_id: 2, T_num: 20, T_relation_sn: []string{"20220303", "20230404"}},
  161. }
  162. B := []StockProduct{
  163. //{T_product_id: 1, T_num: 10, T_relation_sn: []string{"20220101", "20230202"}},
  164. //{T_product_id: 2, T_num: 20, T_relation_sn: []string{"20220303", "20230404"}},
  165. }
  166. //B := []StockProduct{
  167. // {T_product_id: 1, T_num: 10, T_relation_sn: []string{"20220101", "202302"}},
  168. // {T_product_id: 2, T_num: 20, T_relation_sn: []string{"20230404", "20220303"}},
  169. // {T_product_id: 3, T_num: 20, T_relation_sn: []string{"20240404", "20240303"}},
  170. //}
  171. //查找两个StockProduct列表中T_relation_sn不相同的元素
  172. //aDiff, bDiff := StockProductListsEqual(A, B)
  173. ////打印结果
  174. //fmt.Println("A列表中T_relation_sn不相同的元素:", aDiff)
  175. //fmt.Println("B列表中T_relation_sn不相同的元素:", bDiff)
  176. isEqual, aDiff := StockProductListsEqual(A, B)
  177. //打印结果
  178. fmt.Println("是否相等:", isEqual)
  179. fmt.Println("A列表中T_relation_sn不相同的元素:", aDiff)
  180. //_, c, d, a := StringListCompare([]string{"20220101", "20230202"}, []string{"20230202", "20220103"})
  181. //fmt.Println("共同元素:", c)
  182. //fmt.Println("仅A列表中:", d)
  183. //fmt.Println("仅B列表中:", a)
  184. }