main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. // 替换 成 中文括号再去匹配
  59. if strings.Contains(name, "(") && strings.Contains(name, ")") {
  60. name = strings.ReplaceAll(name, ")", ")")
  61. name = strings.ReplaceAll(name, "(", "(")
  62. res1 = getCompanyType(name)
  63. }
  64. }
  65. if res1 != "" {
  66. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), res1)
  67. }
  68. // 上市类型
  69. res2 := getMarketType(name)
  70. if res2 != "" {
  71. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("E%v", i+1), res2)
  72. }
  73. // 获取投资比例大于50%的最上级 企业
  74. res3 := getTop(name)
  75. if res3 != "" {
  76. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("F%v", i+1), res3)
  77. }
  78. //获取
  79. tab1, tab2 := getCodeType(name)
  80. if res1 == "" {
  81. if tab1 == "私企" || tab1 == "个体" {
  82. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("D%v", i+1), "民企")
  83. }
  84. }
  85. //if tab1 != "" {
  86. // f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("G%v", i+1), tab1)
  87. //}
  88. //if tab2 != "" {
  89. // f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("H%v", i+1), tab2)
  90. //}
  91. //注册地点
  92. where := map[string]interface{}{
  93. "company_name": name,
  94. }
  95. std, _ := MgoQY.FindOne("qyxy_std", where)
  96. if util.ObjToString((*std)["company_area"]) != "" {
  97. f.SetCellValue(GF.Env.Sheet, fmt.Sprintf("G%v", i+1), (*std)["company_area"])
  98. }
  99. if i%100 == 0 {
  100. log.Println(name, res1, tab1, tab2)
  101. }
  102. }
  103. f.Save()
  104. }
  105. // updateBidding 更新企业类型信息等
  106. func updateBidding() {
  107. sess := MgoB.GetMgoConn()
  108. defer MgoB.DestoryMongoConn(sess)
  109. log.Println("开始处理:", GF.MongoB.DB, GF.MongoB.Coll)
  110. queryMgo := sess.DB(GF.MongoB.DB).C(GF.MongoB.Coll).Find(nil).Select(nil).Iter()
  111. count := 0
  112. for tmp := make(map[string]interface{}); queryMgo.Next(tmp); count++ {
  113. if count%1000 == 0 {
  114. log.Println("current:", count, tmp["_id"], tmp["title"])
  115. }
  116. //中标单位
  117. name := util.ObjToString(tmp["s_winner"])
  118. if name == "" {
  119. continue
  120. }
  121. update := make(map[string]interface{}, 0)
  122. res1 := getCompanyType(name)
  123. res2 := getMarketType(name)
  124. res3 := getTop(name)
  125. if res1 != "" {
  126. update["company_type"] = res1
  127. }
  128. if res2 != "" {
  129. update["market_type"] = res2
  130. }
  131. if res3 != "" {
  132. update["top_name"] = res3
  133. }
  134. projectscope := util.ObjToString(tmp["projectscope"])
  135. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  136. if projectscope == "" {
  137. project := getProject(biddingID)
  138. projectscope = util.ObjToString(project["projectscope"])
  139. if projectscope != "" {
  140. update["projectscope"] = projectscope
  141. }
  142. }
  143. // 需要更新
  144. if len(update) > 0 {
  145. MgoB.UpdateById(GF.MongoB.Coll, biddingID, map[string]interface{}{"$set": update})
  146. }
  147. }
  148. log.Println(GF.MongoB.DB, GF.MongoB.Coll, "数据处理完毕")
  149. }
  150. // getCompanyTypes 获取公司类型
  151. func getCompanyTypes() {
  152. r := gin.Default()
  153. //
  154. r.POST("/types", func(c *gin.Context) {
  155. var req EnvRequest
  156. if err := c.ShouldBindJSON(&req); err != nil {
  157. respon := EnvResponse{
  158. Code: 400,
  159. Msg: "请求参数无效",
  160. }
  161. c.JSON(http.StatusBadRequest, respon)
  162. return
  163. }
  164. //
  165. results := make([]map[string]interface{}, 0)
  166. for _, v := range req.Names {
  167. res1 := getCompanyType(v)
  168. res2 := getMarketType(v)
  169. res3 := getTop(v)
  170. dd := map[string]interface{}{
  171. "company_type": res1,
  172. "market_type": res2,
  173. "top_name": res3,
  174. "company_name": v,
  175. }
  176. results = append(results, dd)
  177. }
  178. respon := EnvResponse{
  179. Code: 200,
  180. Msg: "请求成功",
  181. Data: results,
  182. }
  183. c.JSON(http.StatusBadRequest, respon)
  184. return
  185. })
  186. // 启动服务
  187. r.Run(":8080")
  188. }