main.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. package main
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. es7 "github.com/olivere/elastic/v7"
  7. "github.com/wcc4869/common_utils/log"
  8. "go.uber.org/zap"
  9. "io"
  10. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  11. "jygit.jydev.jianyu360.cn/data_processing/common_utils/elastic"
  12. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  13. "reflect"
  14. "strings"
  15. "time"
  16. )
  17. var (
  18. Mgo *mongodb.MongodbSim
  19. MgoB *mongodb.MongodbSim
  20. MgoBAi *mongodb.MongodbSim
  21. MgoT *mongodb.MongodbSim //测试环境链接
  22. MgoR *mongodb.MongodbSim
  23. saveSize = 50
  24. Es *elastic.Elastic // 19908
  25. EsNew *elastic.Elastic //19905
  26. //EsT *elastic.Elastic
  27. // 更新mongo
  28. updatePool = make(chan []map[string]interface{}, 5000)
  29. updateSp = make(chan bool, 5)
  30. //更新es
  31. updateEsPool = make(chan []map[string]interface{}, 5000)
  32. updateEsSp = make(chan bool, 5) //保存协程
  33. updateProjectEsPool = make(chan []map[string]interface{}, 5000)
  34. updateProjectEsSp = make(chan bool, 5) //保存协程
  35. BiddingField = make(map[string]string, 200) //bidding_processing_field, level=1 最外层字段,
  36. BiddingLevelField = make(map[string]map[string]string) //level=2 的第二层字段
  37. )
  38. func Init() {
  39. MgoB = &mongodb.MongodbSim{
  40. MongodbAddr: "172.31.31.202:27081,172.20.45.128:27080",
  41. //MongodbAddr: "127.0.0.1:27083",
  42. DbName: "qfw",
  43. Size: 10,
  44. UserName: "SJZY_RWbid_ES",
  45. Password: "SJZY@B4i4D5e6S",
  46. //Direct: true,
  47. }
  48. MgoB.InitPool()
  49. //MgoBAi = &mongodb.MongodbSim{
  50. // //MongodbAddr: "172.17.189.140:27080",
  51. // MongodbAddr: "127.0.0.1:27083",
  52. // DbName: "qfw_ai",
  53. // Size: 10,
  54. // UserName: "SJZY_RWbid_ES",
  55. // Password: "SJZY@B4i4D5e6S",
  56. // Direct: true,
  57. //}
  58. //MgoBAi.InitPool()
  59. //mongodb 163
  60. //Mgo = &mongodb.MongodbSim{
  61. // //MongodbAddr: "172.17.189.140:27080",
  62. // MongodbAddr: "127.0.0.1:27083",
  63. // DbName: "qfw",
  64. // Size: 10,
  65. // UserName: "SJZY_RWbid_ES",
  66. // Password: "SJZY@B4i4D5e6S",
  67. // Direct: true,
  68. //}
  69. //Mgo.InitPool()
  70. //85
  71. //MgoR = &mongodb.MongodbSim{
  72. // //MongodbAddr: "127.0.0.1:27080",
  73. // MongodbAddr: "172.17.4.85:27080",
  74. // DbName: "qfw",
  75. // Size: 10,
  76. // //Direct: true,
  77. //}
  78. //MgoR.InitPool()
  79. //测试环境MongoDB
  80. //MgoT = &mongodb.MongodbSim{
  81. // //MongodbAddr: "172.17.189.140:27080",
  82. // MongodbAddr: "192.168.3.206:27002",
  83. // DbName: "qfw_data",
  84. // Size: 10,
  85. // UserName: "root",
  86. // Password: "root",
  87. // //Direct: true,
  88. //}
  89. //MgoT.InitPool()
  90. ////测试环境es
  91. //Es = &elastic.Elastic{
  92. // S_esurl: "http://192.168.3.149:9201",
  93. // //S_esurl: "http://172.17.4.184:19805",
  94. // I_size: 5,
  95. // Username: "",
  96. // Password: "",
  97. //}
  98. //Es.InitElasticSize()
  99. //es
  100. Es = &elastic.Elastic{
  101. //S_esurl: "http://127.0.0.1:19908",
  102. S_esurl: "http://172.17.4.184:19908",
  103. I_size: 5,
  104. Username: "jybid",
  105. Password: "Top2023_JEB01i@31",
  106. }
  107. Es.InitElasticSize()
  108. //es 新集群
  109. EsNew = &elastic.Elastic{
  110. //S_esurl: "http://127.0.0.1:19905",
  111. S_esurl: "http://172.17.4.184:19905",
  112. I_size: 5,
  113. Username: "jybid",
  114. Password: "Top2023_JEB01i@31",
  115. }
  116. EsNew.InitElasticSize()
  117. }
  118. func main() {
  119. Init()
  120. //InitEsBiddingField()
  121. go updateMethod() //更新mongodb
  122. go updateEsMethod() //更新es
  123. //go updateEsHrefMethod() //更新es href 字段
  124. //go updateProjectEsMethod()
  125. //taskRunProject()
  126. //taskRunBidding()
  127. //dealBidding() //正式环境bidding数据处理
  128. //dealBiddingAi() //正式环境bidding数据处理
  129. //dealBiddingTest() // 测试环境数据处理
  130. //dealBiddingEsHref() // 根据临时表,更新es href 字段
  131. dealBiddingNiJian() //更新拟建数据中buyer = owner
  132. //updateProject()
  133. log.Info("over")
  134. c := make(chan bool, 1)
  135. <-c
  136. }
  137. func InitEsBiddingField() {
  138. now := time.Now()
  139. info, _ := MgoB.Find("bidding_processing_field", `{"stype": "bidding"}`, nil, nil, false, -1, -1)
  140. if len(*info) > 0 {
  141. for _, m := range *info {
  142. if util.IntAll(m["level"]) == 1 {
  143. BiddingField[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  144. } else if util.IntAll(m["level"]) == 2 {
  145. pfield := util.ObjToString(m["pfield"])
  146. pfieldMap := BiddingLevelField[pfield]
  147. if pfieldMap == nil {
  148. pfieldMap = make(map[string]string, 0)
  149. }
  150. pfieldMap[util.ObjToString(m["field"])] = util.ObjToString(m["ftype"])
  151. BiddingLevelField[pfield] = pfieldMap
  152. }
  153. }
  154. }
  155. log.Info("InitEsBiddingField", zap.Int("BiddingField es 一级字段数量", len(BiddingField)))
  156. log.Info("InitEsBiddingField", zap.Int("BiddingLevelField es 二级字段数量", len(BiddingLevelField)))
  157. log.Info("InitEsBiddingField", zap.Any("duration", time.Since(now).Seconds()))
  158. }
  159. // taskRun 更新es 省市区三个字段
  160. func taskRunBidding() {
  161. defer util.Catch()
  162. sess := Mgo.GetMgoConn()
  163. defer Mgo.DestoryMongoConn(sess)
  164. //查询条件
  165. //q := map[string]interface{}{
  166. // //"_id": map[string]interface{}{
  167. // // //"$gt": mongodb.StringTOBsonId("5a862f0640d2d9bbe88e3cea"),
  168. // // //"$lte": mongodb.StringTOBsonId("65d73ba366cf0db42aca6e2f"),
  169. // //
  170. // // //"$gte": mongodb.StringTOBsonId("65d73ba366cf0db42aca6e2f"),
  171. // // "$lte": mongodb.StringTOBsonId("661e347d66cf0db42aa1a52f"),
  172. // //},
  173. // //"comeintime": map[string]interface{}{
  174. // // "$gt": 1669824000,
  175. // // //"$lte": 1669864950,
  176. // // "$lte": 1702265941,
  177. // //},
  178. // //"site": "国家能源e购",
  179. // "toptype": map[string]interface{}{"$exists": 0},
  180. //}
  181. //selected := map[string]interface{}{"contenthtml": 0, "detail": 0}
  182. it := sess.DB("qfw").C("zktest_bidding_0706").Find(nil).Select(nil).Sort("_id").Iter()
  183. fmt.Println("taskRun 开始")
  184. count := 0
  185. //realNum := 0
  186. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  187. if count%1000 == 0 {
  188. log.Info("current", log.Int("count", count), log.Any("_id", tmp["_id"]))
  189. }
  190. update := map[string]interface{}{}
  191. // 1.更新省市区
  192. //if area, ok := tmp["area"]; ok && area != nil {
  193. // update["area"] = area
  194. //} else {
  195. // update["area"] = ""
  196. //}
  197. //
  198. //if city, ok := tmp["city"]; ok && city != nil {
  199. // update["city"] = city
  200. //} else {
  201. // update["city"] = ""
  202. //}
  203. //
  204. //if district, ok := tmp["district"]; ok && district != nil {
  205. // update["district"] = district
  206. //} else {
  207. // update["district"] = ""
  208. //}
  209. // 2.更新中标单位
  210. biddingID := util.ObjToString(tmp["id"])
  211. //biddingID := mongodb.BsonIdToSId(tmp["_id"])
  212. update["winner"] = ""
  213. update["s_winner"] = ""
  214. if len(update) > 0 {
  215. //Mgo.UpdateById("bidding", biddingID, map[string]interface{}{"$set": update})
  216. //2.es 项目 更新字段
  217. err := Es.UpdateDocument("bidding", biddingID, update)
  218. err = EsNew.UpdateDocument("bidding", biddingID, update)
  219. if err != nil && err.Error() != "Document not updated: noop" {
  220. log.Info("bidding es update err", err, biddingID)
  221. }
  222. }
  223. }
  224. log.Info("Run Over...Count:", log.Int("count", count))
  225. }
  226. // taskRunProject 更新项目表 省市区
  227. func taskRunProject() {
  228. defer util.Catch()
  229. sess := Mgo.GetMgoConn()
  230. defer Mgo.DestoryMongoConn(sess)
  231. // 项目数据
  232. MgoP := &mongodb.MongodbSim{
  233. MongodbAddr: "172.17.4.85:27080",
  234. //MongodbAddr: "127.0.0.1:27080",
  235. Size: 10,
  236. DbName: "qfw",
  237. //Direct: true,
  238. }
  239. MgoP.InitPool()
  240. selected := map[string]interface{}{"contenthtml": 0, "detail": 0}
  241. it := sess.DB("qfw").C("zktest_0423_info_new").Find(nil).Select(selected).Sort("_id").Iter()
  242. fmt.Println("taskRun 开始")
  243. count := 0
  244. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  245. if count%10000 == 0 {
  246. log.Info("current", log.Int("count", count), log.Any("_id", tmp["_id"]))
  247. }
  248. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  249. where := map[string]interface{}{
  250. "ids": biddingID,
  251. }
  252. // 找到对应项目数据
  253. p, _ := MgoP.FindOne("projectset_20230904", where)
  254. projectId := mongodb.BsonIdToSId((*p)["_id"])
  255. //1.更新MongoDB
  256. update := map[string]interface{}{}
  257. if area, ok := tmp["area"]; ok && area != nil {
  258. update["area"] = area
  259. } else {
  260. update["area"] = ""
  261. }
  262. if city, ok := tmp["city"]; ok && city != nil {
  263. update["city"] = city
  264. } else {
  265. update["city"] = ""
  266. }
  267. if district, ok := tmp["district"]; ok && district != nil {
  268. update["district"] = district
  269. } else {
  270. update["district"] = ""
  271. }
  272. if len(update) > 0 {
  273. MgoP.UpdateById("projectset_20230904", projectId, map[string]interface{}{"$set": update})
  274. //2.es 项目 更新字段
  275. err := Es.UpdateDocument("projectset", projectId, update)
  276. if err != nil {
  277. log.Info("es update err", err, projectId)
  278. }
  279. }
  280. //2.es 项目 更新字段
  281. //if len(update) > 0 {
  282. // // 更新es
  283. // //updateEsPool <- []map[string]interface{}{
  284. // // {"_id": projectId},
  285. // // update,
  286. // //}
  287. //}
  288. }
  289. log.Info("Run Over...Count:", log.Int("count", count))
  290. }
  291. // dealData 正式环境,同步合同期限
  292. func dealData() {
  293. defer util.Catch()
  294. sess := Mgo.GetMgoConn()
  295. defer Mgo.DestoryMongoConn(sess)
  296. //where := map[string]interface{}{
  297. // "_id": mongodb.StringTOBsonId("65c5a36a66cf0db42ab9c1ef"),
  298. //}
  299. selected := map[string]interface{}{"signaturedate": 1, "contractperiod": 1, "expiredate": 1}
  300. it := sess.DB("qfw").C("zktest_quanliang_0210_fbs").Find(nil).Select(&selected).Iter()
  301. count := 0
  302. realNum := 0
  303. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  304. if count%1000 == 0 {
  305. log.Info("current", log.Int("count", count), log.Any("_id", tmp["_id"]))
  306. }
  307. idStr := mongodb.BsonIdToSId(tmp["_id"])
  308. update := make(map[string]interface{})
  309. if tmp["signaturedate"] != nil {
  310. update["signaturedate"] = tmp["signaturedate"]
  311. }
  312. if tmp["contractperiod"] != nil {
  313. update["contractperiod"] = tmp["contractperiod"]
  314. }
  315. if tmp["expiredate"] != nil {
  316. update["expiredate"] = tmp["expiredate"]
  317. }
  318. if len(update) == 0 {
  319. continue
  320. }
  321. //bidding 表
  322. if idStr > "5a862e7040d2d9bbe88e3b1f" {
  323. bidding, _ := Mgo.FindById("bidding", idStr, map[string]interface{}{"extracttype": 1})
  324. data := *bidding
  325. Mgo.UpdateById("bidding", idStr, map[string]interface{}{"$set": update})
  326. // 针对存量数据,重复数据不进索引
  327. if util.IntAll(data["extracttype"]) == -1 {
  328. tmp = make(map[string]interface{})
  329. continue
  330. }
  331. } else {
  332. //bidding_back
  333. bidding, _ := Mgo.FindById("bidding_back", idStr, map[string]interface{}{"extracttype": 1})
  334. data := *bidding
  335. Mgo.UpdateById("bidding_back", idStr, map[string]interface{}{"$set": update})
  336. // 针对存量数据,重复数据不进索引
  337. if util.IntAll(data["extracttype"]) == -1 {
  338. tmp = make(map[string]interface{})
  339. continue
  340. }
  341. }
  342. realNum++
  343. //2.es 更新字段
  344. esUpdate := update
  345. esUpdate["id"] = idStr
  346. if len(esUpdate) > 0 {
  347. // 更新es
  348. updateEsPool <- []map[string]interface{}{
  349. {"_id": mongodb.BsonIdToSId(tmp["_id"])},
  350. esUpdate,
  351. }
  352. }
  353. tmp = make(map[string]interface{})
  354. }
  355. log.Info("Run Over...Count:", log.Int("count", count), log.Int("realNum", realNum))
  356. }
  357. // dealResult 查询抽取表,更新合同周期字段;是dealData的后面遗漏数据
  358. func dealResult() {
  359. defer util.Catch()
  360. sess := MgoR.GetMgoConn()
  361. defer MgoR.DestoryMongoConn(sess)
  362. where := map[string]interface{}{
  363. "_id": map[string]interface{}{
  364. "$gte": mongodb.StringTOBsonId("5a4909cf40d2d9bbe8ab329c"),
  365. "$lte": mongodb.StringTOBsonId("5a4ad94d40d2d9bbe8ae0183"),
  366. },
  367. "subtype": "合同",
  368. }
  369. selected := map[string]interface{}{"signaturedate": 1, "contractperiod": 1, "expiredate": 1}
  370. it := sess.DB("qfw").C("result_20220219").Find(where).Select(&selected).Iter()
  371. count := 0
  372. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  373. if count%1000 == 0 {
  374. log.Info("current", log.Int("count", count), log.Any("_id", tmp["_id"]))
  375. }
  376. idStr := mongodb.BsonIdToSId(tmp["_id"])
  377. update := make(map[string]interface{})
  378. if tmp["signaturedate"] != nil {
  379. update["signaturedate"] = tmp["signaturedate"]
  380. }
  381. if tmp["contractperiod"] != nil {
  382. update["contractperiod"] = tmp["contractperiod"]
  383. }
  384. if tmp["expiredate"] != nil {
  385. update["expiredate"] = tmp["expiredate"]
  386. }
  387. if len(update) == 0 {
  388. continue
  389. }
  390. bidding, _ := Mgo.FindById("bidding", idStr, map[string]interface{}{"extracttype": 1})
  391. data := *bidding
  392. Mgo.UpdateById("bidding", idStr, map[string]interface{}{"$set": update})
  393. // 针对存量数据,重复数据不进索引
  394. if util.IntAll(data["extracttype"]) == -1 {
  395. tmp = make(map[string]interface{})
  396. continue
  397. }
  398. //2.es 更新字段
  399. esUpdate := update
  400. esUpdate["id"] = idStr
  401. if len(esUpdate) > 0 {
  402. // 更新es
  403. updateEsPool <- []map[string]interface{}{
  404. {"_id": mongodb.BsonIdToSId(tmp["_id"])},
  405. esUpdate,
  406. }
  407. }
  408. tmp = make(map[string]interface{})
  409. }
  410. log.Info("Run Over...Count:", log.Int("count", count))
  411. }
  412. // dealBidding 处理bidding数据
  413. func dealBidding() {
  414. defer util.Catch()
  415. sess := MgoB.GetMgoConn()
  416. defer MgoB.DestoryMongoConn(sess)
  417. where := map[string]interface{}{
  418. "comeintime": map[string]interface{}{
  419. "$lt": 1722009600,
  420. //"$lt": 1718812802,
  421. "$gte": 1718899200,
  422. },
  423. }
  424. //where := map[string]interface{}{
  425. // "_id": map[string]interface{}{
  426. // "$gte": mongodb.StringTOBsonId("66aa067e66cf0db42a8ea71e"),
  427. // "$lt": mongodb.StringTOBsonId("66aa067e66cf0db42a8ea720"),
  428. // },
  429. //}
  430. it := sess.DB("qfw").C("bidding").Find(where).Select(nil).Iter()
  431. fmt.Println("taskRun 开始")
  432. count := 0
  433. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  434. if count%10000 == 0 {
  435. log.Info("current", log.Int("count", count), log.Any("_id", tmp["_id"]))
  436. }
  437. update := map[string]interface{}{}
  438. esUpdate := map[string]interface{}{}
  439. // 2.更新中标单位,中标金额
  440. //if tag_topinformation, ok := tmp["tag_topinformation"]; ok && tag_topinformation != nil {
  441. // update["tag_topinformation"] = tag_topinformation
  442. //}
  443. //
  444. //if property_form, ok := tmp["property_form"]; ok && property_form != nil {
  445. // update["property_form"] = property_form
  446. //}
  447. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  448. //fmt.Println(biddingID)
  449. /**
  450. "s_subscopeclass" : "其它",
  451. "s_topscopeclass" : "其它",
  452. "subscopeclass" : [
  453. "其它"
  454. ],
  455. "topscopeclass" : [
  456. "其它"
  457. ],
  458. */
  459. // 行业分类默认值
  460. resultSubs := make([]string, 0)
  461. resultTobs := make([]string, 0)
  462. if topscopeclass, ok := tmp["topscopeclass"]; ok && topscopeclass != nil {
  463. if topps, ok2 := topscopeclass.([]interface{}); ok2 {
  464. for _, v := range topps {
  465. top := util.ObjToString(v)
  466. if top != "" {
  467. resultTobs = append(resultTobs, top)
  468. }
  469. }
  470. }
  471. //1.一级分类是空数组或者 是 其它
  472. if len(resultTobs) == 0 || resultTobs[0] == "其它" {
  473. update["topscopeclass"] = []string{"其它"}
  474. update["subscopeclass"] = []string{"其它"}
  475. update["s_topscopeclass"] = "其它"
  476. update["s_subscopeclass"] = "其它"
  477. esUpdate["s_topscopeclass"] = "其它"
  478. esUpdate["s_subscopeclass"] = "其它"
  479. esUpdate["topscopeclass"] = []string{"其它"}
  480. } else {
  481. if subs, ok3 := tmp["subscopeclass"]; ok3 {
  482. if subbs, ok4 := subs.([]interface{}); ok4 {
  483. for _, v := range subbs {
  484. sub := util.ObjToString(v)
  485. if sub != "" && sub != "其它" {
  486. resultSubs = append(resultSubs, sub)
  487. }
  488. }
  489. }
  490. }
  491. newTops, newSubs, cleanedTops := ProcessTopscopeclass(resultTobs, resultSubs)
  492. update["topscopeclass"] = newTops
  493. update["subscopeclass"] = newSubs
  494. update["s_topscopeclass"] = strings.Join(cleanedTops, ",")
  495. update["s_subscopeclass"] = strings.Join(newSubs, ",")
  496. esUpdate["s_topscopeclass"] = strings.Join(cleanedTops, ",")
  497. esUpdate["s_subscopeclass"] = strings.Join(newSubs, ",")
  498. esUpdate["topscopeclass"] = newTops
  499. }
  500. } else {
  501. update["topscopeclass"] = []string{"其它"}
  502. update["subscopeclass"] = []string{"其它"}
  503. update["s_topscopeclass"] = "其它"
  504. update["s_subscopeclass"] = "其它"
  505. esUpdate["s_topscopeclass"] = "其它"
  506. esUpdate["s_subscopeclass"] = "其它"
  507. esUpdate["topscopeclass"] = []string{"其它"}
  508. }
  509. //procurementlist 处理预计采购时间
  510. //if procurementlist, ok := tmp["procurementlist"]; ok && procurementlist != nil {
  511. // field := "procurementlist"
  512. // if tmp[field] != nil {
  513. // if field == "procurementlist" {
  514. // if tmp["procurementlist"] != nil {
  515. // var arr []interface{}
  516. // plist := tmp["procurementlist"].([]interface{})
  517. // for _, p := range plist {
  518. // p1 := p.(map[string]interface{})
  519. // p2 := make(map[string]interface{})
  520. // for k, v := range BiddingLevelField[field] {
  521. // if k == "projectname" && util.ObjToString(p1[k]) == "" {
  522. // p2[k] = util.ObjToString(tmp["projectname"])
  523. // } else if k == "buyer" && util.ObjToString(p1[k]) == "" && util.ObjToString(tmp["buyer"]) != "" {
  524. // p2[k] = util.ObjToString(tmp["buyer"])
  525. // } else if k == "expurasingtime" && util.ObjToString(p1[k]) != "" {
  526. // res := getMethod(util.ObjToString(p1[k]))
  527. // if res != 0 {
  528. // p2[k] = res
  529. // }
  530. // } else if p1[k] != nil && reflect.TypeOf(p1[k]).String() == v {
  531. // p2[k] = p1[k]
  532. // }
  533. //
  534. // }
  535. // arr = append(arr, p2)
  536. // }
  537. // if len(arr) > 0 {
  538. // esUpdate[field] = arr
  539. // }
  540. // }
  541. // }
  542. // }
  543. //}
  544. if len(update) > 0 {
  545. //fmt.Println("aaaaa", biddingID)
  546. //更新mongo
  547. //MgoT.UpdateById("bidding", biddingID, map[string]interface{}{"$set": update})
  548. //更新MongoDB
  549. updatePool <- []map[string]interface{}{
  550. {"_id": tmp["_id"]},
  551. {"$set": update},
  552. }
  553. //2.es 项目 更新字段
  554. //err := Es.UpdateDocument("bidding", biddingID, update)
  555. //if err != nil && err.Error() != "Document not updated: noop" {
  556. // log.Info("bidding es update err", err, biddingID)
  557. //}
  558. //// 更新es
  559. //updateEsPool <- []map[string]interface{}{
  560. // {"_id": biddingID},
  561. // update,
  562. //}
  563. }
  564. // 更新Es 数据
  565. if len(esUpdate) > 0 {
  566. // 更新es
  567. updateEsPool <- []map[string]interface{}{
  568. {"_id": biddingID},
  569. esUpdate,
  570. }
  571. }
  572. }
  573. log.Info("Run Over...Count:", log.Int("count", count))
  574. }
  575. // dealBiddingAi 处理qfw_ai 数据库bidding 数据
  576. func dealBiddingAi() {
  577. defer util.Catch()
  578. sess := MgoBAi.GetMgoConn()
  579. defer MgoBAi.DestoryMongoConn(sess)
  580. it := sess.DB("qfw_ai").C("zxl_20240926").Find(nil).Select(nil).Iter()
  581. fmt.Println("taskRun 开始")
  582. count := 0
  583. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  584. if count%1000 == 0 {
  585. fmt.Println("current:", count)
  586. }
  587. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  588. update := map[string]interface{}{}
  589. //if budget, ok := tmp["budget"]; ok && budget != nil {
  590. // update["budget"] = budget
  591. //}
  592. if bidamount, ok := tmp["bidamount"]; ok && bidamount != nil {
  593. update["bidamount"] = bidamount
  594. } else {
  595. update["bidamount"] = 0.0
  596. }
  597. //if projectcode, ok := tmp["projectcode"]; ok && projectcode != nil {
  598. // update["projectcode"] = projectcode
  599. //}
  600. if len(update) > 0 {
  601. MgoBAi.UpdateById("bidding", biddingID, map[string]interface{}{"$set": update})
  602. //2.es 项目 更新字段
  603. err := Es.UpdateDocument("bidding_ai", biddingID, update)
  604. if err != nil && err.Error() != "Document not updated: noop" {
  605. log.Info("bidding es update err", err, biddingID)
  606. }
  607. }
  608. }
  609. fmt.Println("over ----------- over ")
  610. }
  611. func dealBiddingByEs() {
  612. //url := "http://172.17.4.184:19908"
  613. url := "http://127.0.0.1:19908"
  614. username := "jybid"
  615. password := "Top2023_JEB01i@31"
  616. index := "bidding" //索引名称
  617. //index := "projectset" //索引名称
  618. // 创建 Elasticsearch 客户端
  619. client, err := es7.NewClient(
  620. es7.SetURL(url),
  621. es7.SetBasicAuth(username, password),
  622. es7.SetSniff(false),
  623. )
  624. if err != nil {
  625. log.Fatalf("创建 Elasticsearch 客户端失败:%s", err)
  626. }
  627. query := es7.NewBoolQuery()
  628. query.Must(es7.NewRangeQuery("comeintime").Gt(1718812800))
  629. query.MustNot(es7.NewExistsQuery("s_topscopeclass"))
  630. ctx := context.Background()
  631. //开始滚动搜索
  632. scrollID := ""
  633. scroll := "10m"
  634. searchSource := es7.NewSearchSource().
  635. Query(query).
  636. Size(10000).
  637. Sort("_doc", true) //升序排序
  638. //Sort("_doc", false) //降序排序
  639. searchService := client.Scroll(index).
  640. Size(10000).
  641. Scroll(scroll).
  642. SearchSource(searchSource)
  643. res, err := searchService.Do(ctx)
  644. if err != nil {
  645. if err == io.EOF {
  646. fmt.Println("没有数据")
  647. } else {
  648. panic(err)
  649. }
  650. }
  651. //defer client.ClearScroll().ScrollId(scrollID).Do(ctx) // 在退出时清理资源
  652. fmt.Println("总数是:", res.TotalHits())
  653. total := 0
  654. for len(res.Hits.Hits) > 0 {
  655. for _, hit := range res.Hits.Hits {
  656. var doc map[string]interface{}
  657. err := json.Unmarshal(hit.Source, &doc)
  658. if err != nil {
  659. fmt.Printf("解析文档失败:%s", err)
  660. continue
  661. }
  662. //delete(doc, "filetext")
  663. //delete(doc, "detail")
  664. //
  665. ////存入新表
  666. //err = MgoB.InsertOrUpdate("qfw", "wcc_subtype_err_0429", doc)
  667. //if err != nil {
  668. // fmt.Println("error", doc["id"])
  669. //}
  670. }
  671. total = total + len(res.Hits.Hits)
  672. scrollID = res.ScrollId
  673. res, err = client.Scroll().ScrollId(scrollID).Scroll(scroll).Do(ctx)
  674. fmt.Println("current count:", total)
  675. if err != nil {
  676. if err == io.EOF {
  677. // 滚动到最后一批数据,退出循环
  678. break
  679. }
  680. fmt.Println("滚动搜索失败:", err, res)
  681. break // 处理错误时退出循环
  682. }
  683. }
  684. // 在循环外调用 ClearScroll
  685. _, err = client.ClearScroll().ScrollId(scrollID).Do(ctx)
  686. if err != nil {
  687. fmt.Printf("清理滚动搜索失败:%s", err)
  688. }
  689. fmt.Println("结束~~~~~~~~~~~~~~~")
  690. }
  691. // dealBiddingTest 处理测试环境数据
  692. func dealBiddingTest() {
  693. defer util.Catch()
  694. sess := MgoT.GetMgoConn()
  695. defer MgoT.DestoryMongoConn(sess)
  696. it := sess.DB("qfw_data").C("bidding").Find(nil).Select(nil).Iter()
  697. fmt.Println("taskRun 开始")
  698. count := 0
  699. for tmp := make(map[string]interface{}); it.Next(&tmp); count++ {
  700. if count%10000 == 0 {
  701. log.Info("current", log.Int("count", count), log.Any("_id", tmp["_id"]))
  702. }
  703. update := map[string]interface{}{}
  704. // 2.更新中标单位,中标金额
  705. //if tag_topinformation, ok := tmp["tag_topinformation"]; ok && tag_topinformation != nil {
  706. // update["tag_topinformation"] = tag_topinformation
  707. //}
  708. //
  709. //if property_form, ok := tmp["property_form"]; ok && property_form != nil {
  710. // update["property_form"] = property_form
  711. //}
  712. biddingID := mongodb.BsonIdToSId(tmp["_id"])
  713. /**
  714. "s_subscopeclass" : "其它",
  715. "s_topscopeclass" : "其它",
  716. "subscopeclass" : [
  717. "其它"
  718. ],
  719. "topscopeclass" : [
  720. "其它"
  721. ],
  722. */
  723. // 行业分类默认值
  724. if topscopeclass, ok := tmp["topscopeclass"]; !ok && topscopeclass == nil {
  725. update["topscopeclass"] = []string{"其它"}
  726. update["s_topscopeclass"] = "其它"
  727. }
  728. if subscopeclass, ok := tmp["subscopeclass"]; !ok && subscopeclass == nil {
  729. update["subscopeclass"] = []string{"其它"}
  730. update["s_subscopeclass"] = "其它"
  731. }
  732. if util.ObjToString(tmp["s_topscopeclass"]) == "其它" {
  733. update["topscopeclass"] = []string{"其它"}
  734. update["s_topscopeclass"] = "其它"
  735. }
  736. if util.ObjToString(tmp["s_subscopeclass"]) == "其它" {
  737. update["subscopeclass"] = []string{"其它"}
  738. update["s_subscopeclass"] = "其它"
  739. }
  740. //procurementlist 处理预计采购时间
  741. if procurementlist, ok := tmp["procurementlist"]; ok && procurementlist != nil {
  742. for field, _ := range BiddingField {
  743. if tmp[field] != nil {
  744. if field == "procurementlist" {
  745. if tmp["procurementlist"] != nil {
  746. var arr []interface{}
  747. plist := tmp["procurementlist"].([]interface{})
  748. for _, p := range plist {
  749. p1 := p.(map[string]interface{})
  750. p2 := make(map[string]interface{})
  751. for k, v := range BiddingLevelField[field] {
  752. if k == "projectname" && util.ObjToString(p1[k]) == "" {
  753. p2[k] = util.ObjToString(tmp["projectname"])
  754. } else if k == "buyer" && util.ObjToString(p1[k]) == "" && util.ObjToString(tmp["buyer"]) != "" {
  755. p2[k] = util.ObjToString(tmp["buyer"])
  756. } else if k == "expurasingtime" && util.ObjToString(p1[k]) != "" {
  757. res := getMethod(util.ObjToString(p1[k]))
  758. if res != 0 {
  759. p2[k] = res
  760. }
  761. } else if p1[k] != nil && reflect.TypeOf(p1[k]).String() == v {
  762. p2[k] = p1[k]
  763. }
  764. }
  765. arr = append(arr, p2)
  766. }
  767. if len(arr) > 0 {
  768. update[field] = arr
  769. }
  770. }
  771. }
  772. }
  773. }
  774. }
  775. if len(update) > 0 {
  776. fmt.Println("aaaaa", biddingID)
  777. //更新mongo
  778. //MgoT.UpdateById("bidding", biddingID, map[string]interface{}{"$set": update})
  779. //更新MongoDB
  780. //updatePool <- []map[string]interface{}{
  781. // {"_id": tmp["_id"]},
  782. // {"$set": update},
  783. //}
  784. //2.es 项目 更新字段
  785. //err := Es.UpdateDocument("bidding", biddingID, update)
  786. //if err != nil && err.Error() != "Document not updated: noop" {
  787. // log.Info("bidding es update err", err, biddingID)
  788. //}
  789. // 更新es
  790. //updateEsPool <- []map[string]interface{}{
  791. // {"_id": biddingID},
  792. // update,
  793. //}
  794. }
  795. }
  796. log.Info("Run Over...Count:", log.Int("count", count))
  797. }
  798. // updateMethod 更新MongoDB
  799. func updateMethod() {
  800. arru := make([][]map[string]interface{}, saveSize)
  801. indexu := 0
  802. for {
  803. select {
  804. case v := <-updatePool:
  805. arru[indexu] = v
  806. indexu++
  807. if indexu == saveSize {
  808. updateSp <- true
  809. go func(arru [][]map[string]interface{}) {
  810. defer func() {
  811. <-updateSp
  812. }()
  813. MgoB.UpdateBulk("bidding", arru...)
  814. }(arru)
  815. arru = make([][]map[string]interface{}, saveSize)
  816. indexu = 0
  817. }
  818. case <-time.After(1000 * time.Millisecond):
  819. if indexu > 0 {
  820. updateSp <- true
  821. go func(arru [][]map[string]interface{}) {
  822. defer func() {
  823. <-updateSp
  824. }()
  825. MgoB.UpdateBulk("bidding", arru...)
  826. }(arru[:indexu])
  827. arru = make([][]map[string]interface{}, saveSize)
  828. indexu = 0
  829. }
  830. }
  831. }
  832. }
  833. // updateEsMethod 更新es
  834. func updateEsMethod() {
  835. arru := make([][]map[string]interface{}, 200)
  836. indexu := 0
  837. for {
  838. select {
  839. case v := <-updateEsPool:
  840. arru[indexu] = v
  841. indexu++
  842. if indexu == 200 {
  843. updateEsSp <- true
  844. go func(arru [][]map[string]interface{}) {
  845. defer func() {
  846. <-updateEsSp
  847. }()
  848. Es.UpdateBulk("bidding", arru...)
  849. EsNew.UpdateBulk("bidding", arru...)
  850. }(arru)
  851. arru = make([][]map[string]interface{}, 200)
  852. indexu = 0
  853. }
  854. case <-time.After(1000 * time.Millisecond):
  855. if indexu > 0 {
  856. updateEsSp <- true
  857. go func(arru [][]map[string]interface{}) {
  858. defer func() {
  859. <-updateEsSp
  860. }()
  861. Es.UpdateBulk("bidding", arru...)
  862. EsNew.UpdateBulk("bidding", arru...)
  863. }(arru[:indexu])
  864. arru = make([][]map[string]interface{}, 200)
  865. indexu = 0
  866. }
  867. }
  868. }
  869. }
  870. // updateEsMethod 更新es href 字段
  871. func updateEsHrefMethod() {
  872. arru := make([][]map[string]interface{}, 200)
  873. indexu := 0
  874. for {
  875. select {
  876. case v := <-updateEsPool:
  877. arru[indexu] = v
  878. indexu++
  879. if indexu == 200 {
  880. updateEsSp <- true
  881. go func(arru [][]map[string]interface{}) {
  882. defer func() {
  883. <-updateEsSp
  884. }()
  885. Es.UpdateBulk("bidding", arru...)
  886. Es.UpdateBulk("bidding_ai", arru...)
  887. Es.UpdateBulk("bidding_temporary", arru...)
  888. EsNew.UpdateBulk("bidding", arru...)
  889. EsNew.UpdateBulk("bidding_customer", arru...)
  890. EsNew.UpdateBulk("bidding_free", arru...)
  891. EsNew.UpdateBulk("bidding_year", arru...)
  892. EsNew.UpdateBulk("bidding_all", arru...)
  893. EsNew.UpdateBulk("bidding_temporary", arru...)
  894. }(arru)
  895. arru = make([][]map[string]interface{}, 200)
  896. indexu = 0
  897. }
  898. case <-time.After(1000 * time.Millisecond):
  899. if indexu > 0 {
  900. updateEsSp <- true
  901. go func(arru [][]map[string]interface{}) {
  902. defer func() {
  903. <-updateEsSp
  904. }()
  905. Es.UpdateBulk("bidding", arru...)
  906. Es.UpdateBulk("bidding_ai", arru...)
  907. Es.UpdateBulk("bidding_temporary", arru...)
  908. EsNew.UpdateBulk("bidding", arru...)
  909. EsNew.UpdateBulk("bidding_customer", arru...)
  910. EsNew.UpdateBulk("bidding_free", arru...)
  911. EsNew.UpdateBulk("bidding_year", arru...)
  912. EsNew.UpdateBulk("bidding_all", arru...)
  913. EsNew.UpdateBulk("bidding_temporary", arru...)
  914. }(arru[:indexu])
  915. arru = make([][]map[string]interface{}, 200)
  916. indexu = 0
  917. }
  918. }
  919. }
  920. }
  921. // updateProjectEsMethod 更新项目索引
  922. func updateProjectEsMethod() {
  923. arru := make([][]map[string]interface{}, 200)
  924. indexu := 0
  925. for {
  926. select {
  927. case v := <-updateProjectEsPool:
  928. arru[indexu] = v
  929. indexu++
  930. if indexu == 200 {
  931. updateProjectEsSp <- true
  932. go func(arru [][]map[string]interface{}) {
  933. defer func() {
  934. <-updateProjectEsSp
  935. }()
  936. Es.UpdateBulk("projectset", arru...)
  937. }(arru)
  938. arru = make([][]map[string]interface{}, 200)
  939. indexu = 0
  940. }
  941. case <-time.After(1000 * time.Millisecond):
  942. if indexu > 0 {
  943. updateProjectEsSp <- true
  944. go func(arru [][]map[string]interface{}) {
  945. defer func() {
  946. <-updateProjectEsSp
  947. }()
  948. Es.UpdateBulk("projectset", arru...)
  949. }(arru[:indexu])
  950. arru = make([][]map[string]interface{}, 200)
  951. indexu = 0
  952. }
  953. }
  954. }
  955. }