Stock.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package dto
  2. import (
  3. "gogs.baozhida.cn/zoie/ERP_libs/lib"
  4. "sort"
  5. )
  6. // 入库产品mingxi
  7. type StockProduct struct {
  8. T_product_id int
  9. T_num int
  10. T_relation_sn []string
  11. }
  12. type StockProductRelationSn struct {
  13. T_product_id int
  14. T_delete_relation_sn []string
  15. T_add_relation_sn []string
  16. }
  17. // 比较两个StockProduct列表是否相等
  18. func StockProductListsEqual(a, b []StockProduct) (isEqual bool, needDelete, needAdd, needEdit map[int]StockProduct, snDiff map[int]StockProductRelationSn) {
  19. needDelete = make(map[int]StockProduct)
  20. needAdd = make(map[int]StockProduct)
  21. needEdit = make(map[int]StockProduct)
  22. snDiff = make(map[int]StockProductRelationSn)
  23. isEqual = false
  24. // 原始数据
  25. aMap := make(map[int]StockProduct)
  26. aProductList := []int{}
  27. for _, item := range a {
  28. sort.Strings(item.T_relation_sn)
  29. aMap[item.T_product_id] = item
  30. aProductList = append(aProductList, item.T_product_id)
  31. }
  32. // 修改提交的数据
  33. bMap := make(map[int]StockProduct)
  34. bProductList := []int{}
  35. for _, item := range b {
  36. sort.Strings(item.T_relation_sn)
  37. bMap[item.T_product_id] = item
  38. bProductList = append(bProductList, item.T_product_id)
  39. }
  40. // 查询需要删除的产品 和需要添加的产品
  41. _, commonIds, needDeleteIds, needAddIds := lib.IntListCompare(aProductList, bProductList)
  42. for _, product_id := range commonIds {
  43. equal, _, T_delete_relation_sn, T_add_relation_sn := lib.StringListCompare(aMap[product_id].T_relation_sn, bMap[product_id].T_relation_sn)
  44. snDiff[product_id] = StockProductRelationSn{
  45. T_product_id: product_id,
  46. T_delete_relation_sn: T_delete_relation_sn,
  47. T_add_relation_sn: T_add_relation_sn,
  48. }
  49. if bMap[product_id].T_num != aMap[product_id].T_num && len(bMap[product_id].T_relation_sn) == 0 {
  50. needEdit[product_id] = bMap[product_id]
  51. }
  52. if !equal {
  53. isEqual = false
  54. }
  55. }
  56. if len(needDelete) == 0 && len(needAdd) == 0 && len(needEdit) == 0 && isEqual {
  57. isEqual = true
  58. }
  59. for _, id := range needDeleteIds {
  60. needDelete[id] = aMap[id]
  61. }
  62. for _, id := range needAddIds {
  63. needAdd[id] = bMap[id]
  64. }
  65. return isEqual, needDelete, needAdd, needEdit, snDiff
  66. }