package main import ( "fmt" "github.com/gin-gonic/gin" "github.com/xuri/excelize/v2" util "jygit.jydev.jianyu360.cn/data_processing/common_utils" "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb" "log" "net/http" "strings" ) type EnvRequest struct { Names []string `json:"names"` } type EnvResponse struct { Code int `json:"code"` Msg string `json:"msg"` Data interface{} `json:"data"` } func main() { err := InitConfig() if err != nil { log.Println("配置文件 读取失败", err) return } readXlsx() //98家 央企名单 readXlsx2() //企业经营主体代码表 InitMgo() //getCompanyTypes() //http 提供接口的服务形式 updateXlsx() // 根据提供的Excel文件更新对应标签 //matchCompanyCode() //测试;匹配现有企业库的类型 log.Println("数据处理完毕!") } // updateXlsx 更新xlsx 文件标签 func updateXlsx() { filePath := GF.Env.File if filePath == "" { log.Println("文件路径为空") return } // 1. 读取 Excel(获取 A 列数据) f, err := excelize.OpenFile(filePath) if err != nil { log.Fatal("❌ 无法打开 Excel 文件:", err) } defer f.Close() //读取央企 rows, err := f.GetRows(GF.Env.Sheet) if err != nil { log.Fatal("❌ 无法读取 Sheet1:", err) } for i := 1; i < len(rows); i++ { //project_name := rows[i][0] //项目名称 name := rows[i][0] //企业名称 name = strings.TrimSpace(name) res1 := getCompanyType(name) if res1 == "" { // 替换 成 中文括号再去匹配 if strings.Contains(name, "(") && strings.Contains(name, ")") { name = strings.ReplaceAll(name, ")", ")") name = strings.ReplaceAll(name, "(", "(") res1 = getCompanyType(name) } } if res1 != "" { f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), res1) } // 上市类型 res2 := getMarketType(name) if res2 != "" { f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("E%v", i+1), res2) } // 获取投资比例大于50%的最上级 企业 res3 := getTop(name) if res3 != "" { f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("F%v", i+1), res3) } //获取 tab1, tab2 := getCodeType(name) if res1 == "" { if tab1 == "私企" || tab1 == "个体" { f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), "民企") } } //if tab1 != "" { // f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("G%v", i+1), tab1) //} //if tab2 != "" { // f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("H%v", i+1), tab2) //} //注册地点 where := map[string]interface{}{ "company_name": name, } std, _ := MgoQY.FindOne("qyxy_std", where) if util.ObjToString((*std)["company_area"]) != "" { f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("G%v", i+1), (*std)["company_area"]) } if i%100 == 0 { log.Println(name, res1, tab1, tab2) } } f.Save() } // updateBidding 更新企业类型信息等 func updateBidding() { sess := MgoB.GetMgoConn() defer MgoB.DestoryMongoConn(sess) log.Println("开始处理:", GF.MongoB.DB, GF.MongoB.Coll) queryMgo := sess.DB(GF.MongoB.DB).C(GF.MongoB.Coll).Find(nil).Select(nil).Iter() count := 0 for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ { if count%1000 == 0 { log.Println("current:", count, tmp["_id"], tmp["title"]) } //中标单位 name := util.ObjToString(tmp["s_winner"]) if name == "" { continue } update := make(map[string]interface{}, 0) res1 := getCompanyType(name) res2 := getMarketType(name) res3 := getTop(name) if res1 != "" { update["company_type"] = res1 } if res2 != "" { update["market_type"] = res2 } if res3 != "" { update["top_name"] = res3 } projectscope := util.ObjToString(tmp["projectscope"]) biddingID := mongodb.BsonIdToSId(tmp["_id"]) if projectscope == "" { project := getProject(biddingID) projectscope = util.ObjToString(project["projectscope"]) if projectscope != "" { update["projectscope"] = projectscope } } // 需要更新 if len(update) > 0 { MgoB.UpdateById(GF.MongoB.Coll, biddingID, map[string]interface{}{"$set": update}) } } log.Println(GF.MongoB.DB, GF.MongoB.Coll, "数据处理完毕") } // getCompanyTypes 获取公司类型 func getCompanyTypes() { r := gin.Default() // r.POST("/types", func(c *gin.Context) { var req EnvRequest if err := c.ShouldBindJSON(&req); err != nil { respon := EnvResponse{ Code: 400, Msg: "请求参数无效", } c.JSON(http.StatusBadRequest, respon) return } // results := make([]map[string]interface{}, 0) for _, v := range req.Names { res1 := getCompanyType(v) res2 := getMarketType(v) res3 := getTop(v) dd := map[string]interface{}{ "company_type": res1, "market_type": res2, "top_name": res3, "company_name": v, } results = append(results, dd) } respon := EnvResponse{ Code: 200, Msg: "请求成功", Data: results, } c.JSON(http.StatusBadRequest, respon) return }) // 启动服务 r.Run(":8080") }