utils.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package main
  2. import (
  3. "encoding/json"
  4. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  5. )
  6. type LogEntry struct {
  7. ProjectID string
  8. InfoID string
  9. OriginalBid float64
  10. UpdatedBid float64
  11. }
  12. // ProcessBids 处理中标金额
  13. func ProcessBids(slice []interface{}, pid string, budget float64, bidamount float64) ([]interface{}, float64, string, float64, string, []LogEntry) {
  14. var maxBid, minBid float64
  15. var maxInfoID, minInfoID string
  16. found := false
  17. var logs []LogEntry
  18. //newList := make([]interface{}, 0)
  19. // 深拷贝传入的slice
  20. copySlice := deepCopySlice(slice)
  21. // Find the max and min bidamounts
  22. for _, item := range copySlice {
  23. if m, ok := item.(map[string]interface{}); ok {
  24. if bid, ok := m["bidamount"]; ok {
  25. bidFloat := util.Float64All(bid)
  26. if !found {
  27. maxBid = bidFloat
  28. minBid = bidFloat
  29. maxInfoID = m["infoid"].(string)
  30. minInfoID = m["infoid"].(string)
  31. found = true
  32. } else {
  33. if bidFloat > maxBid {
  34. maxBid = bidFloat
  35. maxInfoID = m["infoid"].(string)
  36. }
  37. if bidFloat < minBid {
  38. minBid = bidFloat
  39. minInfoID = m["infoid"].(string)
  40. }
  41. }
  42. }
  43. }
  44. }
  45. // 最大金额是最小金额的 1万倍时,才处理数据
  46. if !found || minBid == 0 || maxBid < 1000000000 || budget == 0 || bidamount == 0 || maxBid == minBid || maxBid <= bidamount {
  47. return []interface{}{}, 0, "", 0, "", logs
  48. }
  49. // 处理数据,把过大的金额数值,更新为小的
  50. if maxBid/budget >= 10000 || (maxBid/minBid == 10000 && maxBid/budget > 1000) {
  51. for _, item := range copySlice {
  52. if m, ok := item.(map[string]interface{}); ok {
  53. if bid, ok := m["bidamount"]; ok && util.Float64All(bid) == maxBid {
  54. logs = append(logs, LogEntry{
  55. InfoID: m["infoid"].(string),
  56. ProjectID: pid,
  57. OriginalBid: maxBid,
  58. UpdatedBid: minBid,
  59. })
  60. m["bidamount"] = minBid
  61. }
  62. }
  63. }
  64. return copySlice, maxBid, maxInfoID, minBid, minInfoID, logs
  65. }
  66. //if maxBid < 1000000000 {
  67. // // Update the max bid with min bid value
  68. // for _, item := range slice {
  69. // if m, ok := item.(map[string]interface{}); ok {
  70. // if bid, ok := m["bidamount"]; ok && convertToFloat(bid) == maxBid {
  71. // logs = append(logs, LogEntry{
  72. // InfoID: m["infoid"].(string),
  73. // OriginalBid: maxBid,
  74. // UpdatedBid: minBid,
  75. // })
  76. // m["bidamount"] = minBid
  77. // }
  78. // }
  79. // }
  80. //} else {
  81. // // Update the min bid with max bid value
  82. // for _, item := range slice {
  83. // if m, ok := item.(map[string]interface{}); ok {
  84. // if bid, ok := m["bidamount"]; ok && convertToFloat(bid) == minBid {
  85. // logs = append(logs, LogEntry{
  86. // InfoID: m["infoid"].(string),
  87. // OriginalBid: minBid,
  88. // UpdatedBid: maxBid,
  89. // })
  90. // m["bidamount"] = maxBid
  91. // }
  92. // }
  93. // }
  94. //}
  95. return []interface{}{}, 0, "", 0, "", logs
  96. }
  97. // getBudget 获取预算,循环数据,获取最大值
  98. func getBudget(list []interface{}) float64 {
  99. budget := float64(0)
  100. for _, item := range list {
  101. if m, ok := item.(map[string]interface{}); ok {
  102. if bid, ok := m["budget"]; ok {
  103. bidFloat := util.Float64All(bid)
  104. if bidFloat > budget {
  105. budget = bidFloat
  106. }
  107. }
  108. }
  109. }
  110. return budget
  111. }
  112. func deepCopySlice(src []interface{}) []interface{} {
  113. var dst []interface{}
  114. // 使用JSON序列化和反序列化实现深拷贝
  115. bytes, err := json.Marshal(src)
  116. if err != nil {
  117. panic(err)
  118. }
  119. err = json.Unmarshal(bytes, &dst)
  120. if err != nil {
  121. panic(err)
  122. }
  123. return dst
  124. }