main.go 32 KB

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