package main import ( "encoding/json" util "jygit.jydev.jianyu360.cn/data_processing/common_utils" ) type LogEntry struct { ProjectID string InfoID string OriginalBid float64 UpdatedBid float64 } // ProcessBids 处理中标金额 func ProcessBids(slice []interface{}, pid string, budget float64, bidamount float64) ([]interface{}, float64, string, float64, string, []LogEntry) { var maxBid, minBid float64 var maxInfoID, minInfoID string found := false var logs []LogEntry //newList := make([]interface{}, 0) // 深拷贝传入的slice copySlice := deepCopySlice(slice) // Find the max and min bidamounts for _, item := range copySlice { if m, ok := item.(map[string]interface{}); ok { if bid, ok := m["bidamount"]; ok { bidFloat := util.Float64All(bid) if !found { maxBid = bidFloat minBid = bidFloat maxInfoID = m["infoid"].(string) minInfoID = m["infoid"].(string) found = true } else { if bidFloat > maxBid { maxBid = bidFloat maxInfoID = m["infoid"].(string) } if bidFloat < minBid { minBid = bidFloat minInfoID = m["infoid"].(string) } } } } } // 最大金额是最小金额的 1万倍时,才处理数据 if !found || minBid == 0 || maxBid < 1000000000 || budget == 0 || bidamount == 0 || maxBid == minBid || maxBid <= bidamount { return []interface{}{}, 0, "", 0, "", logs } // 处理数据,把过大的金额数值,更新为小的 if maxBid/budget >= 10000 || (maxBid/minBid == 10000 && maxBid/budget > 1000) { for _, item := range copySlice { if m, ok := item.(map[string]interface{}); ok { if bid, ok := m["bidamount"]; ok && util.Float64All(bid) == maxBid { logs = append(logs, LogEntry{ InfoID: m["infoid"].(string), ProjectID: pid, OriginalBid: maxBid, UpdatedBid: minBid, }) m["bidamount"] = minBid } } } return copySlice, maxBid, maxInfoID, minBid, minInfoID, logs } //if maxBid < 1000000000 { // // Update the max bid with min bid value // for _, item := range slice { // if m, ok := item.(map[string]interface{}); ok { // if bid, ok := m["bidamount"]; ok && convertToFloat(bid) == maxBid { // logs = append(logs, LogEntry{ // InfoID: m["infoid"].(string), // OriginalBid: maxBid, // UpdatedBid: minBid, // }) // m["bidamount"] = minBid // } // } // } //} else { // // Update the min bid with max bid value // for _, item := range slice { // if m, ok := item.(map[string]interface{}); ok { // if bid, ok := m["bidamount"]; ok && convertToFloat(bid) == minBid { // logs = append(logs, LogEntry{ // InfoID: m["infoid"].(string), // OriginalBid: minBid, // UpdatedBid: maxBid, // }) // m["bidamount"] = maxBid // } // } // } //} return []interface{}{}, 0, "", 0, "", logs } // getBudget 获取预算,循环数据,获取最大值 func getBudget(list []interface{}) float64 { budget := float64(0) for _, item := range list { if m, ok := item.(map[string]interface{}); ok { if bid, ok := m["budget"]; ok { bidFloat := util.Float64All(bid) if bidFloat > budget { budget = bidFloat } } } } return budget } func deepCopySlice(src []interface{}) []interface{} { var dst []interface{} // 使用JSON序列化和反序列化实现深拷贝 bytes, err := json.Marshal(src) if err != nil { panic(err) } err = json.Unmarshal(bytes, &dst) if err != nil { panic(err) } return dst }