clickhouse.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "github.com/xuri/excelize/v2"
  7. "gorm.io/driver/clickhouse"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/logger"
  10. "io"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  12. "log"
  13. "net/http"
  14. "net/url"
  15. "sync"
  16. "time"
  17. )
  18. // EntMapCode 映射关系表
  19. type EntMapCode struct {
  20. AId string `json:"a_id"`
  21. BId string `json:"b_id"`
  22. AName string `json:"a_name"`
  23. BName string `json:"b_name"`
  24. Code string `json:"code"` //关系属性代码
  25. InvestRatio string `json:"invest_ratio"` //投资比例
  26. InvestPrice string `json:"invest_price"` //投资金额
  27. CreateTime int64 `json:"create_time"`
  28. UpdateTime int64 `json:"update_time"`
  29. }
  30. func (EntMapCode) TableName() string {
  31. return "ent_map_code"
  32. }
  33. // clickhouseData 获取clickhouse 数据
  34. func clickhouseData() {
  35. host := "cc-2ze9tv451wov14w9e.clickhouse.ads.aliyuncs.com:9000"
  36. username := "biservice"
  37. password := "Bi_top95215#"
  38. Mgo := &mongodb.MongodbSim{
  39. MongodbAddr: "172.17.189.140:27080",
  40. //MongodbAddr: "127.0.0.1:27083",
  41. Size: 10,
  42. DbName: "qfw",
  43. UserName: "SJZY_RWbid_ES",
  44. Password: "SJZY@B4i4D5e6S",
  45. //Direct: true,
  46. }
  47. Mgo.InitPool()
  48. //host := "192.168.3.207:19000"
  49. //username := "jytop"
  50. //password := "pwdTopJy123"
  51. encodedPassword := url.QueryEscape(password)
  52. dn := fmt.Sprintf("clickhouse://%s:%s@%s/information?dial_timeout=10s&read_timeout=20s", username, encodedPassword, host)
  53. db, err := gorm.Open(clickhouse.Open(dn), &gorm.Config{
  54. Logger: logger.Default.LogMode(logger.Silent),
  55. })
  56. if err != nil {
  57. log.Fatal("链接数据库失败")
  58. } else {
  59. log.Println("链接成功")
  60. }
  61. f, err := excelize.OpenFile("./河南单温.xlsx")
  62. if err != nil {
  63. fmt.Println(err)
  64. return
  65. }
  66. defer func() {
  67. if err := f.Close(); err != nil {
  68. fmt.Println(err)
  69. }
  70. }()
  71. Sheet := "单位"
  72. rows, err := f.GetRows(Sheet)
  73. if err != nil {
  74. fmt.Println(err)
  75. return
  76. }
  77. defer func() {
  78. if err := f.Close(); err != nil {
  79. log.Println("f.close,", err)
  80. }
  81. }()
  82. var wg sync.WaitGroup
  83. sem := make(chan bool, 15) // 控制同时最多只有10个协程
  84. for i := 1; i < len(rows); i++ {
  85. if len(rows[i]) > 1 && rows[i][1] != "" {
  86. insert := make(map[string]interface{})
  87. company := rows[i][0]
  88. log.Println(i, "----", company)
  89. insert["bname"] = rows[i][0]
  90. insert["aname"] = rows[i][1]
  91. if len(rows[i]) > 2 {
  92. insert["type"] = rows[i][2]
  93. }
  94. Mgo.Save("wcc_henan_0603", insert)
  95. continue
  96. }
  97. //
  98. //
  99. wg.Add(1)
  100. sem <- true
  101. go func(i int) {
  102. defer wg.Done()
  103. company := rows[i][0]
  104. insert := make(map[string]interface{})
  105. log.Println(i, "----", company)
  106. insert["bname"] = rows[i][0]
  107. exist := EntMapCode{}
  108. db.Where(&EntMapCode{BName: company}).First(&exist)
  109. if exist.AName != "" {
  110. insert["aname"] = exist.AName
  111. }
  112. if exist.Code == "0101" {
  113. insert["type"] = "管辖关系"
  114. //f.SetCellValue(Sheet, fmt.Sprintf("%s%d", "C", i+1), "管辖关系")
  115. } else if exist.Code == "0102" {
  116. insert["type"] = "直属关系"
  117. //f.SetCellValue(Sheet, fmt.Sprintf("%s%d", "C", i+1), "直属关系")
  118. } else if exist.Code == "0103" {
  119. insert["type"] = "组成关系"
  120. //f.SetCellValue(Sheet, fmt.Sprintf("%s%d", "C", i+1), "组成关系")
  121. }
  122. Mgo.Save("wcc_henan_0603", insert)
  123. <-sem // 释放一个位置,允许其他协程开始执行
  124. }(i)
  125. }
  126. wg.Wait() // 等待所有协程结束
  127. err = f.Save()
  128. if err != nil {
  129. log.Println("f save err", err)
  130. }
  131. log.Println("结束")
  132. //for i := 1; i < len(rows); i++ {
  133. // company := rows[i][0]
  134. // log.Println("aaa", company)
  135. //
  136. // exist := EntMapCode{}
  137. // db.Where(&EntMapCode{BName: company}).First(&exist)
  138. // if exist.AName != "" {
  139. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "B", i+1), exist.AName)
  140. // }
  141. //
  142. // if exist.Code == "0101" {
  143. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "C", i+1), "管辖关系")
  144. // } else if exist.Code == "0102" {
  145. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "C", i+1), "直属关系")
  146. // } else if exist.Code == "0103" {
  147. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "C", i+1), "组成关系")
  148. // }
  149. //}
  150. //
  151. //f.Save()
  152. //log.Println("结束")
  153. }
  154. // getName 更新表格数据
  155. func getName() {
  156. Mgo := &mongodb.MongodbSim{
  157. MongodbAddr: "172.17.189.140:27080",
  158. //MongodbAddr: "127.0.0.1:27083",
  159. Size: 10,
  160. DbName: "qfw",
  161. UserName: "SJZY_RWbid_ES",
  162. Password: "SJZY@B4i4D5e6S",
  163. //Direct: true,
  164. }
  165. Mgo.InitPool()
  166. //181
  167. Mgo2 := &mongodb.MongodbSim{
  168. MongodbAddr: "172.17.4.181:27001",
  169. //MongodbAddr: "127.0.0.1:27001",
  170. DbName: "mixdata",
  171. Size: 10,
  172. UserName: "",
  173. Password: "",
  174. //Direct: true,
  175. }
  176. Mgo2.InitPool()
  177. f, err := excelize.OpenFile("./河南单位.xlsx")
  178. if err != nil {
  179. fmt.Println(err)
  180. return
  181. }
  182. defer func() {
  183. if err := f.Close(); err != nil {
  184. fmt.Println(err)
  185. }
  186. }()
  187. rows, err := f.GetRows("单位")
  188. if err != nil {
  189. fmt.Println(err)
  190. return
  191. }
  192. for i := 1; i < len(rows); i++ {
  193. name := rows[i][0]
  194. log.Println(i, "----", name)
  195. //res, _ := Mgo2.FindOne("special_enterprise", map[string]interface{}{"company_name": name})
  196. //if res != nil && len(*res) > 0 {
  197. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "E", i+1), "special_enterprise")
  198. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "F", i+1), (*res)["company_status"])
  199. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "G", i+1), (*res)["company_type"])
  200. //} else {
  201. // ra, _ := Mgo2.FindOne("special_gov_unit", map[string]interface{}{"company_name": name})
  202. // if ra != nil && len(*ra) > 0 {
  203. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "E", i+1), "special_gov_unit")
  204. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "G", i+1), (*ra)["company_type"])
  205. // if util.ObjToString((*ra)["company_status"]) != "" {
  206. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "F", i+1), (*ra)["company_status"])
  207. // } else {
  208. // if util.IntAll((*ra)["use_flag"]) < 5 {
  209. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "F", i+1), "正常")
  210. // } else {
  211. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "F", i+1), "废弃")
  212. // }
  213. // }
  214. // }
  215. //}
  216. // 更新D 列
  217. //rb, _ := Mgo.FindOne("wcc_zhengfujigou", map[string]interface{}{"bname": name})
  218. //if rb != nil && len(*rb) > 0 {
  219. // f.SetCellValue("单位", fmt.Sprintf("%s%d", "D", i+1), (*rb)["aname"])
  220. //}
  221. //
  222. data := map[string]interface{}{
  223. "detail": name,
  224. }
  225. res := getAreaInfo(data)
  226. if res != nil {
  227. city := res["city"]
  228. district := res["district"]
  229. f.SetCellValue("单位", fmt.Sprintf("%s%d", "B", i+1), city)
  230. f.SetCellValue("单位", fmt.Sprintf("%s%d", "C", i+1), district)
  231. }
  232. }
  233. f.Save()
  234. log.Println("结束")
  235. }
  236. // getAreaInfo 调用抽取接口,获取省市区
  237. func getAreaInfo(data map[string]interface{}) map[string]interface{} {
  238. info := map[string]interface{}{}
  239. client := &http.Client{Timeout: 2 * time.Second}
  240. jsonStr, _ := json.Marshal(data)
  241. resp, err := client.Post("http://127.0.0.1:9996/service/region", "application/json", bytes.NewBuffer(jsonStr))
  242. if err != nil {
  243. return info
  244. }
  245. res, err := io.ReadAll(resp.Body)
  246. if err != nil {
  247. return info
  248. }
  249. err = json.Unmarshal(res, &info)
  250. if err != nil {
  251. return info
  252. }
  253. return info
  254. }