main.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. package main
  2. import (
  3. "fmt"
  4. "go.mongodb.org/mongo-driver/bson"
  5. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  6. es "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  7. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  8. "log"
  9. "strings"
  10. "time"
  11. )
  12. var (
  13. //更新es
  14. updateEsPool = make(chan []map[string]interface{}, 5000)
  15. updateEsSp = make(chan bool, 3) //保存协程
  16. // 更新mongo
  17. updatePool = make(chan []map[string]interface{}, 5000)
  18. updateSp = make(chan bool, 5)
  19. saveSize = 50
  20. //更新es
  21. Es *es.Elastic
  22. esIndex = "qyxy"
  23. Mgo *mongodb.MongodbSim
  24. MgoQY *mongodb.MongodbSim
  25. )
  26. func initEs() {
  27. Es = &es.Elastic{
  28. //S_esurl: "http://127.0.0.1:19908",
  29. S_esurl: "http://172.17.4.184:19908",
  30. I_size: 10,
  31. Username: "jybid",
  32. Password: "Top2023_JEB01i@31",
  33. }
  34. Es.InitElasticSize()
  35. }
  36. // initMgo initMgo
  37. func initMgo() {
  38. //181 凭安库
  39. //MgoQY = &mongodb.MongodbSim{
  40. // MongodbAddr: "172.17.4.181:27001",
  41. // //MongodbAddr: "127.0.0.1:27001",
  42. // DbName: "mixdata",
  43. // Size: 10,
  44. // UserName: "",
  45. // Password: "",
  46. // //Direct: true,
  47. //}
  48. //MgoQY.InitPool()
  49. //qyxy_std
  50. Mgo = &mongodb.MongodbSim{
  51. MongodbAddr: "172.31.31.202:27081,172.20.45.128:27080",
  52. //MongodbAddr: "127.0.0.1:27083",
  53. Size: 10,
  54. DbName: "mixdata",
  55. UserName: "SJZY_RWbid_ES",
  56. Password: "SJZY@B4i4D5e6S",
  57. //Direct: true,
  58. }
  59. Mgo.InitPool()
  60. }
  61. func main() {
  62. go updateEsMethod()
  63. go updateMethod()
  64. initEs()
  65. initMgo()
  66. readXlsx()
  67. //dealCapitalData()
  68. //dealCompanyTypeInt()
  69. dealCompanyType()
  70. select {}
  71. }
  72. // dealCapitalData 处理数据 注册资金
  73. func dealCapitalData() {
  74. defer util.Catch()
  75. sess := MgoQY.GetMgoConn()
  76. defer MgoQY.DestoryMongoConn(sess)
  77. it := sess.DB("mixdata").C("company_base").Find(nil).Select(nil).Sort("_id").Iter()
  78. fmt.Println("taskRun 开始")
  79. count := 0
  80. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  81. if count%1000 == 0 {
  82. log.Println("current:", count, tmp["company_name"])
  83. }
  84. where := map[string]interface{}{
  85. "company_name": tmp["company_name"],
  86. }
  87. update := make(map[string]interface{})
  88. std, _ := Mgo.FindOne("qyxy_std", where)
  89. companyType := util.ObjToString(tmp["company_type"])
  90. if companyType == "事业单位" {
  91. special, _ := MgoQY.FindOne("special_enterprise", where)
  92. //1.事业单位数据,注册资金错误
  93. if util.ObjToString((*special)["capital"]) != "" {
  94. text := util.ObjToString((*special)["capital"])
  95. capital := ObjToMoney(text)
  96. capital = capital / 10000
  97. update["capital"] = capital
  98. }
  99. } else {
  100. //2.企业的capital =nil,需要更新为0
  101. if util.ObjToString(tmp["capital"]) == "" {
  102. update["capital"] = float64(0)
  103. }
  104. }
  105. if _, ok := (*std)["capital"]; ok {
  106. if len(update) > 0 {
  107. //Mgo.Update(GF.Mongo.Coll, where, map[string]interface{}{"$set": update}, true, false)
  108. Mgo.Update("qyxy_std", where, map[string]interface{}{"$set": update}, true, false)
  109. ////更新es
  110. //err := Es.UpdateDocument(esIndex, util.ObjToString(tmp["company_id"]), update)
  111. //if err != nil {
  112. // log.Println("err", err, update, where)
  113. //}
  114. updateEsPool <- []map[string]interface{}{
  115. {"_id": util.ObjToString(tmp["company_id"])},
  116. update,
  117. }
  118. }
  119. }
  120. }
  121. log.Println("数据迭代结束")
  122. }
  123. // dealCompanyTypeInt 修复company_type_int
  124. func dealCompanyTypeInt() {
  125. defer util.Catch()
  126. sess := Mgo.GetMgoConn()
  127. defer Mgo.DestoryMongoConn(sess)
  128. // 构建查询条件
  129. where := bson.M{
  130. "$or": []bson.M{
  131. {"company_type": "其他"}, // company_type 等于 "其他"
  132. {"company_type": bson.M{"$exists": false}}, // company_type 不存在
  133. },
  134. }
  135. it := sess.DB("mixdata").C("qyxy_std").Find(where).Select(nil).Iter()
  136. fmt.Println("taskRun 开始")
  137. count := 0
  138. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  139. if count%1000 == 0 {
  140. log.Println("current:", count, tmp["company_name"], tmp["_id"])
  141. }
  142. company_name := util.ObjToString(tmp["company_name"])
  143. if strings.HasSuffix(company_name, "公司") || strings.HasSuffix(company_name, "集团") {
  144. update := make(map[string]interface{})
  145. update["company_type_int"] = 12
  146. updateEsPool <- []map[string]interface{}{
  147. {"_id": util.ObjToString(tmp["_id"])},
  148. update,
  149. }
  150. }
  151. }
  152. log.Println("数据处理完毕,总数是", count)
  153. }
  154. func dealCompanyType() {
  155. defer util.Catch()
  156. sess := Mgo.GetMgoConn()
  157. defer Mgo.DestoryMongoConn(sess)
  158. it := sess.DB("mixdata").C("qyxy_std").Find(nil).Select(nil).Iter()
  159. fmt.Println("dealCompanyType 开始处理 企业数据")
  160. count := 0
  161. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  162. if count%1000 == 0 {
  163. log.Println("current:", count, tmp["company_name"], tmp["_id"])
  164. }
  165. company_name := util.ObjToString(tmp["company_name"])
  166. company_type := util.ObjToString(tmp["company_type"])
  167. if company_name == "" || company_type == "" {
  168. continue
  169. }
  170. ss := getCompanyType(company_name, company_type)
  171. if ss != "" {
  172. update := make(map[string]interface{})
  173. update["ownership_type"] = ss
  174. updateEsPool <- []map[string]interface{}{
  175. {"_id": util.ObjToString(tmp["_id"])},
  176. update,
  177. }
  178. //更新MongoDB
  179. updatePool <- []map[string]interface{}{
  180. {"_id": tmp["_id"]},
  181. {"$set": update},
  182. }
  183. }
  184. }
  185. log.Println("企业数据 处理完毕,总数是", count)
  186. }
  187. // updateEsMethod 更新es
  188. func updateEsMethod() {
  189. arru := make([][]map[string]interface{}, 200) //200条一组更新es
  190. indexu := 0
  191. for {
  192. select {
  193. case v := <-updateEsPool:
  194. arru[indexu] = v
  195. indexu++
  196. if indexu == 200 {
  197. updateEsSp <- true
  198. go func(arru [][]map[string]interface{}) {
  199. defer func() {
  200. <-updateEsSp
  201. }()
  202. Es.UpdateBulk(esIndex, arru...)
  203. }(arru)
  204. arru = make([][]map[string]interface{}, 200)
  205. indexu = 0
  206. }
  207. case <-time.After(1000 * time.Millisecond):
  208. if indexu > 0 {
  209. updateEsSp <- true
  210. go func(arru [][]map[string]interface{}) {
  211. defer func() {
  212. <-updateEsSp
  213. }()
  214. Es.UpdateBulk(esIndex, arru...)
  215. }(arru[:indexu])
  216. arru = make([][]map[string]interface{}, 200)
  217. indexu = 0
  218. }
  219. }
  220. }
  221. }
  222. // updateMethod 更新MongoDB
  223. func updateMethod() {
  224. arru := make([][]map[string]interface{}, saveSize)
  225. indexu := 0
  226. for {
  227. select {
  228. case v := <-updatePool:
  229. arru[indexu] = v
  230. indexu++
  231. if indexu == saveSize {
  232. updateSp <- true
  233. go func(arru [][]map[string]interface{}) {
  234. defer func() {
  235. <-updateSp
  236. }()
  237. Mgo.UpdateBulk("qyxy_std", arru...)
  238. }(arru)
  239. arru = make([][]map[string]interface{}, saveSize)
  240. indexu = 0
  241. }
  242. case <-time.After(1000 * time.Millisecond):
  243. if indexu > 0 {
  244. updateSp <- true
  245. go func(arru [][]map[string]interface{}) {
  246. defer func() {
  247. <-updateSp
  248. }()
  249. Mgo.UpdateBulk("qyxy_std", arru...)
  250. }(arru[:indexu])
  251. arru = make([][]map[string]interface{}, saveSize)
  252. indexu = 0
  253. }
  254. }
  255. }
  256. }