main.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package main
  2. import (
  3. "fmt"
  4. "github.com/gin-gonic/gin"
  5. "github.com/xuri/excelize/v2"
  6. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "log"
  9. "net/http"
  10. )
  11. type EnvRequest struct {
  12. Names []string `json:"names"`
  13. }
  14. type EnvResponse struct {
  15. Code int `json:"code"`
  16. Msg string `json:"msg"`
  17. Data interface{} `json:"data"`
  18. }
  19. func main() {
  20. err := InitConfig()
  21. if err != nil {
  22. log.Println("配置文件 读取失败", err)
  23. return
  24. }
  25. readXlsx() //98家 央企名单
  26. InitMgo()
  27. //getCompanyTypes()
  28. updateXlsx()
  29. log.Println("数据处理完毕!")
  30. }
  31. // updateXlsx 更新xlsx 文件标签
  32. func updateXlsx() {
  33. filePath := GF.Env.File
  34. if filePath == "" {
  35. log.Println("文件路径为空")
  36. return
  37. }
  38. // 1. 读取 Excel(获取 A 列数据)
  39. f, err := excelize.OpenFile(filePath)
  40. if err != nil {
  41. log.Fatal("❌ 无法打开 Excel 文件:", err)
  42. }
  43. defer f.Close()
  44. //读取央企
  45. rows, err := f.GetRows(GF.Env.Sheet)
  46. if err != nil {
  47. log.Fatal("❌ 无法读取 Sheet1:", err)
  48. }
  49. for i := 1; i < len(rows); i++ {
  50. //project_name := rows[i][0] //项目名称
  51. name := rows[i][1] //企业名称
  52. res1 := getCompanyType(name)
  53. res2 := getMarketType(name)
  54. res3 := getTop(name)
  55. if res1 != "" {
  56. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), res1)
  57. }
  58. if res2 != "" {
  59. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("E%v", i+1), res2)
  60. }
  61. if res3 != "" {
  62. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("F%v", i+1), res3)
  63. }
  64. }
  65. f.Save()
  66. }
  67. // updateBidding 更新企业类型信息等
  68. func updateBidding() {
  69. sess := MgoB.GetMgoConn()
  70. defer MgoB.DestoryMongoConn(sess)
  71. log.Println("开始处理:", GF.MongoB.DB, GF.MongoB.Coll)
  72. queryMgo := sess.DB(GF.MongoB.DB).C(GF.MongoB.Coll).Find(nil).Select(nil).Iter()
  73. count := 0
  74. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  75. if count%1000 == 0 {
  76. log.Println("current:", count, tmp["_id"], tmp["title"])
  77. }
  78. //中标单位
  79. name := util.ObjToString(tmp["s_winner"])
  80. if name == "" {
  81. continue
  82. }
  83. update := make(map[string]interface{}, 0)
  84. res1 := getCompanyType(name)
  85. res2 := getMarketType(name)
  86. res3 := getTop(name)
  87. if res1 != "" {
  88. update["company_type"] = res1
  89. }
  90. if res2 != "" {
  91. update["market_type"] = res2
  92. }
  93. if res3 != "" {
  94. update["top_name"] = res3
  95. }
  96. projectscope := util.ObjToString(tmp["projectscope"])
  97. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  98. if projectscope == "" {
  99. project := getProject(biddingID)
  100. projectscope = util.ObjToString(project["projectscope"])
  101. if projectscope != "" {
  102. update["projectscope"] = projectscope
  103. }
  104. }
  105. // 需要更新
  106. if len(update) > 0 {
  107. MgoB.UpdateById(GF.MongoB.Coll, biddingID, map[string]interface{}{"$set": update})
  108. }
  109. }
  110. log.Println(GF.MongoB.DB, GF.MongoB.Coll, "数据处理完毕")
  111. }
  112. // getCompanyTypes 获取公司类型
  113. func getCompanyTypes() {
  114. r := gin.Default()
  115. //
  116. r.POST("/types", func(c *gin.Context) {
  117. var req EnvRequest
  118. if err := c.ShouldBindJSON(&req); err != nil {
  119. respon := EnvResponse{
  120. Code: 400,
  121. Msg: "请求参数无效",
  122. }
  123. c.JSON(http.StatusBadRequest, respon)
  124. return
  125. }
  126. //
  127. results := make([]map[string]interface{}, 0)
  128. for _, v := range req.Names {
  129. res1 := getCompanyType(v)
  130. res2 := getMarketType(v)
  131. res3 := getTop(v)
  132. dd := map[string]interface{}{
  133. "company_type": res1,
  134. "market_type": res2,
  135. "top_name": res3,
  136. "company_name": v,
  137. }
  138. results = append(results, dd)
  139. }
  140. respon := EnvResponse{
  141. Code: 200,
  142. Msg: "请求成功",
  143. Data: results,
  144. }
  145. c.JSON(http.StatusBadRequest, respon)
  146. return
  147. })
  148. // 启动服务
  149. r.Run(":8080")
  150. }