123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178 |
- package main
- import (
- "context"
- "fmt"
- "github.com/xuri/excelize/v2"
- "go.mongodb.org/mongo-driver/bson"
- "go.mongodb.org/mongo-driver/mongo"
- "go.mongodb.org/mongo-driver/mongo/options"
- "log"
- "strings"
- )
- // MongoDB 配置
- const (
- mongoHost = "172.31.31.202:27081,172.20.45.128:27080" // MongoDB 地址(可修改)
- mongoUser = "SJZY_RWbid_ES" // MongoDB 用户名(替换为你的用户名)
- mongoPassword = "SJZY@B4i4D5e6S" // MongoDB 密码(替换为你的密码)
- mongoDatabase = "qfw" // 目标数据库(替换为你的数据库名称)
- mongoCollection = "bidding" // MongoDB 表(集合)
- authSource = "admin" // 认证数据库(通常是 admin)
- )
- // 2024 年的 UNIX 时间戳范围
- const (
- startOf2024 = 1704067200 // 2024-01-01 00:00:00
- endOf2024 = 1735689599 // 2024-12-31 23:59:59
- //endOf2024 = 1704384000 // 2024-02-01 23:59:59
- )
- // 连接 MongoDB(带用户名密码)
- func connectMongoDB() (*mongo.Client, error) {
- credential := options.Credential{
- Username: mongoUser,
- Password: mongoPassword,
- AuthSource: authSource,
- }
- clientOptions := options.Client().ApplyURI("mongodb://" + mongoHost).SetAuth(credential)
- client, err := mongo.Connect(context.TODO(), clientOptions)
- if err != nil {
- return nil, err
- }
- // 测试连接
- err = client.Ping(context.TODO(), nil)
- if err != nil {
- return nil, err
- }
- fmt.Println("✅ MongoDB 连接成功!")
- return client, nil
- }
- // 计数结果结构体
- type CountResult struct {
- toptypeCount int
- subtypeCount int
- }
- func processExcel() {
- filePath := "银行业金融机构法人名单.xlsx"
- // 1. 读取 Excel(获取 A 列数据)
- f, err := excelize.OpenFile(filePath)
- if err != nil {
- log.Fatal("❌ 无法打开 Excel 文件:", err)
- }
- defer f.Close()
- // 2. 解析机构名称(A 列)到 map
- buyerMap := make(map[string]int) // 用于快速匹配
- rowIndexMap := make(map[string]int) // 记录 buyerName 对应的 Excel 行号
- rows, err := f.GetRows("Sheet3")
- if err != nil {
- log.Fatal("❌ 无法读取 Sheet3:", err)
- }
- for i, row := range rows {
- if i == 0 || len(row) == 0 {
- continue // 跳过表头或空行
- }
- buyerName := row[0]
- if buyerName != "" {
- buyerMap[buyerName] = i + 1 // 记录 buyerName 在 Excel 的行号(从 1 开始)
- rowIndexMap[buyerName] = i + 1
- }
- }
- // 3. 连接 MongoDB
- client, err := connectMongoDB()
- if err != nil {
- log.Fatal("❌ MongoDB 连接失败:", err)
- }
- defer client.Disconnect(context.TODO())
- collection := client.Database(mongoDatabase).Collection(mongoCollection)
- // 4. **遍历 MongoDB 数据(逐条处理)**
- countResults := processBiddingData(collection, buyerMap)
- // 5. **批量更新 Excel**
- for buyerName, result := range countResults {
- rowIdx := rowIndexMap[buyerName]
- f.SetCellInt("Sheet3", fmt.Sprintf("B%d", rowIdx), result.toptypeCount)
- f.SetCellInt("Sheet3", fmt.Sprintf("C%d", rowIdx), result.subtypeCount)
- }
- // 6. 保存 Excel
- if err := f.SaveAs(filePath); err != nil {
- log.Fatal("❌ 保存 Excel 失败:", err)
- }
- fmt.Println("✅ 统计完成,数据已更新到 Excel!")
- }
- // 逐条处理 MongoDB 数据,统计 `buyer` 匹配情况
- func processBiddingData(collection *mongo.Collection, buyerMap map[string]int) map[string]CountResult {
- countResults := make(map[string]CountResult)
- // 1. 查询符合 `publishtime` 条件的数据
- filter := bson.M{
- "publishtime": bson.M{"$gte": startOf2024, "$lte": endOf2024},
- //"extracttype": bson.M{"$ne": -1}, // 过滤掉 `extracttype = -1` 的数据
- }
- opts := options.Find().SetProjection(bson.M{"buyer": 1, "toptype": 1, "publishtime": 1, "subtype": 1, "extracttype": 1})
- cursor, err := collection.Find(context.TODO(), filter, opts)
- if err != nil {
- log.Fatal("❌ MongoDB 查询失败:", err)
- }
- defer cursor.Close(context.TODO())
- st := 0
- // 2. **逐条处理数据**
- for cursor.Next(context.TODO()) {
- var record struct {
- Buyer string `bson:"buyer"`
- Toptype string `bson:"toptype"`
- Subtype string `bson:"subtype"`
- Extracttype int `bson:"extracttype"`
- Publishtime int `bson:"publishtime"`
- }
- if err := cursor.Decode(&record); err != nil {
- log.Println("⚠ 解析 MongoDB 数据失败:", err)
- continue
- }
- if record.Extracttype < 0 {
- continue
- }
- if st%1000 == 0 {
- log.Println("stststsstststststtst", st, record.Buyer, record.Publishtime)
- }
- st++
- // 3. **在 Go 代码中匹配 `buyer`**
- for buyerName := range buyerMap {
- if strings.Contains(record.Buyer, buyerName) {
- result := countResults[buyerName]
- if record.Toptype == "招标" || record.Toptype == "结果" {
- result.toptypeCount++
- }
- if record.Subtype == "合同" {
- result.subtypeCount++
- }
- countResults[buyerName] = result
- }
- }
- }
- if err := cursor.Err(); err != nil {
- log.Fatal("❌ MongoDB 游标遍历错误:", err)
- }
- return countResults
- }
|