main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. "strings"
  11. )
  12. type EnvRequest struct {
  13. Names []string `json:"names"`
  14. }
  15. type EnvResponse struct {
  16. Code int `json:"code"`
  17. Msg string `json:"msg"`
  18. Data interface{} `json:"data"`
  19. }
  20. func main() {
  21. err := InitConfig()
  22. if err != nil {
  23. log.Println("配置文件 读取失败", err)
  24. return
  25. }
  26. readXlsx() //98家 央企名单
  27. readXlsx2() //企业经营主体代码表
  28. InitMgo()
  29. //getCompanyTypes() //http 提供接口的服务形式
  30. updateXlsx() // 根据提供的Excel文件更新对应标签
  31. //matchCompanyCode() //测试;匹配现有企业库的类型
  32. log.Println("数据处理完毕!")
  33. }
  34. // updateXlsx 更新xlsx 文件标签
  35. func updateXlsx() {
  36. filePath := GF.Env.File
  37. if filePath == "" {
  38. log.Println("文件路径为空")
  39. return
  40. }
  41. // 1. 读取 Excel(获取 A 列数据)
  42. f, err := excelize.OpenFile(filePath)
  43. if err != nil {
  44. log.Fatal("❌ 无法打开 Excel 文件:", err)
  45. }
  46. defer f.Close()
  47. //读取央企
  48. rows, err := f.GetRows(GF.Env.Sheet)
  49. if err != nil {
  50. log.Fatal("❌ 无法读取 Sheet1:", err)
  51. }
  52. for i := 1; i < len(rows); i++ {
  53. //project_name := rows[i][0] //项目名称
  54. name := rows[i][0] //企业名称
  55. name = strings.TrimSpace(name)
  56. res1 := getCompanyType(name)
  57. if res1 == "" {
  58. if strings.Contains(name, "(") && strings.Contains(name, ")") {
  59. name = strings.ReplaceAll(name, ")", ")")
  60. name = strings.ReplaceAll(name, "(", "(")
  61. res1 = getCompanyType(name)
  62. }
  63. }
  64. if res1 != "" {
  65. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), res1)
  66. }
  67. res2 := getMarketType(name)
  68. if res2 != "" {
  69. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("E%v", i+1), res2)
  70. }
  71. res3 := getTop(name, true)
  72. if res3 != "" {
  73. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("F%v", i+1), res3)
  74. }
  75. tab1, tab2 := getCodeType(name)
  76. if res1 == "" {
  77. if tab1 == "私企" || tab1 == "个体" {
  78. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), "民企")
  79. }
  80. }
  81. //if tab1 != "" {
  82. // f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("G%v", i+1), tab1)
  83. //}
  84. //if tab2 != "" {
  85. // f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("H%v", i+1), tab2)
  86. //}
  87. if i%100 == 0 {
  88. log.Println(name, res1, tab1, tab2)
  89. }
  90. }
  91. f.Save()
  92. }
  93. // updateBidding 更新企业类型信息等
  94. func updateBidding() {
  95. sess := MgoB.GetMgoConn()
  96. defer MgoB.DestoryMongoConn(sess)
  97. log.Println("开始处理:", GF.MongoB.DB, GF.MongoB.Coll)
  98. queryMgo := sess.DB(GF.MongoB.DB).C(GF.MongoB.Coll).Find(nil).Select(nil).Iter()
  99. count := 0
  100. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  101. if count%1000 == 0 {
  102. log.Println("current:", count, tmp["_id"], tmp["title"])
  103. }
  104. //中标单位
  105. name := util.ObjToString(tmp["s_winner"])
  106. if name == "" {
  107. continue
  108. }
  109. update := make(map[string]interface{}, 0)
  110. res1 := getCompanyType(name)
  111. res2 := getMarketType(name)
  112. res3 := getTop(name, true)
  113. if res1 != "" {
  114. update["company_type"] = res1
  115. }
  116. if res2 != "" {
  117. update["market_type"] = res2
  118. }
  119. if res3 != "" {
  120. update["top_name"] = res3
  121. }
  122. projectscope := util.ObjToString(tmp["projectscope"])
  123. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  124. if projectscope == "" {
  125. project := getProject(biddingID)
  126. projectscope = util.ObjToString(project["projectscope"])
  127. if projectscope != "" {
  128. update["projectscope"] = projectscope
  129. }
  130. }
  131. // 需要更新
  132. if len(update) > 0 {
  133. MgoB.UpdateById(GF.MongoB.Coll, biddingID, map[string]interface{}{"$set": update})
  134. }
  135. }
  136. log.Println(GF.MongoB.DB, GF.MongoB.Coll, "数据处理完毕")
  137. }
  138. // getCompanyTypes 获取公司类型
  139. func getCompanyTypes() {
  140. r := gin.Default()
  141. //
  142. r.POST("/types", func(c *gin.Context) {
  143. var req EnvRequest
  144. if err := c.ShouldBindJSON(&req); err != nil {
  145. respon := EnvResponse{
  146. Code: 400,
  147. Msg: "请求参数无效",
  148. }
  149. c.JSON(http.StatusBadRequest, respon)
  150. return
  151. }
  152. //
  153. results := make([]map[string]interface{}, 0)
  154. for _, v := range req.Names {
  155. res1 := getCompanyType(v)
  156. res2 := getMarketType(v)
  157. res3 := getTop(v, false)
  158. dd := map[string]interface{}{
  159. "company_type": res1,
  160. "market_type": res2,
  161. "top_name": res3,
  162. "company_name": v,
  163. }
  164. results = append(results, dd)
  165. }
  166. respon := EnvResponse{
  167. Code: 200,
  168. Msg: "请求成功",
  169. Data: results,
  170. }
  171. c.JSON(http.StatusBadRequest, respon)
  172. return
  173. })
  174. // 启动服务
  175. r.Run(":8080")
  176. }