task.go 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013
  1. package main
  2. import (
  3. "encoding/json"
  4. "field_sync/config"
  5. "field_sync/oss"
  6. "fmt"
  7. "net"
  8. "reflect"
  9. "regexp"
  10. "sort"
  11. "strconv"
  12. "strings"
  13. "time"
  14. "log"
  15. "go.mongodb.org/mongo-driver/bson"
  16. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  17. "jygit.jydev.jianyu360.cn/data_processing/common_utils/mongodb"
  18. "jygit.jydev.jianyu360.cn/data_processing/common_utils/redis"
  19. "jygit.jydev.jianyu360.cn/data_processing/common_utils/udp"
  20. )
  21. var (
  22. regLetter = regexp.MustCompile("[a-z]*")
  23. cityEndReg = regexp.MustCompile("(区|县|市)$")
  24. )
  25. func biddingTask(data []byte, mapInfo map[string]interface{}) {
  26. defer util.Catch()
  27. stype := util.ObjToString(mapInfo["stype"])
  28. q, _ := mapInfo["query"].(map[string]interface{})
  29. bkey, _ := mapInfo["bkey"].(string)
  30. if q == nil {
  31. q = map[string]interface{}{
  32. "_id": map[string]interface{}{
  33. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  34. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  35. },
  36. }
  37. }
  38. //extract库
  39. // extractConn := MgoE.GetMgoConn()
  40. // defer MgoE.DestoryMongoConn(extractConn)
  41. // extractResult := extractConn.DB(MgoE.DbName).C(config.Conf.DB.MongoE.Coll).Find(q).Select(map[string]interface{}{
  42. // "field_source": 0,
  43. // "kvtext": 0,
  44. // }).Sort("_id").Iter()
  45. // eMap := map[string]map[string]interface{}{}
  46. // extCount, repeatCount := 0, 0
  47. // for tmp := make(map[string]interface{}); extractResult.Next(tmp); extCount++ {
  48. // if util.IntAll(tmp["repeat"]) == 1 {
  49. // repeatCount++
  50. // }
  51. // tid := mongodb.BsonIdToSId(tmp["_id"])
  52. // eMap[tid] = tmp
  53. // tmp = make(map[string]interface{})
  54. // }
  55. // log.Println("抽取表 数据量", extCount, "重复数据量", repeatCount)
  56. //bidding库
  57. biddingConn := MgoB.GetMgoConn()
  58. count, _ := biddingConn.DB(MgoB.DbName).C(config.Conf.DB.MongoB.Coll).Find(&q).Count()
  59. log.Println("bidding表 同步总数:", count)
  60. c := 0
  61. // if count < 500000 {
  62. var res []map[string]interface{}
  63. result := biddingConn.DB(MgoB.DbName).C(config.Conf.DB.MongoB.Coll).Find(q).Select(map[string]interface{}{
  64. "contenthtml": 0,
  65. }).Iter()
  66. for tmp := make(map[string]interface{}); result.Next(tmp); {
  67. res = append(res, tmp)
  68. tmp = make(map[string]interface{})
  69. }
  70. MgoB.DestoryMongoConn(biddingConn)
  71. // log.Println("查询结果 bidding", count, "抽取:", extCount)
  72. c = doIndex(res, bkey, stype)
  73. // } else {
  74. // log.Println("查询结果 数据量太大,放弃", count)
  75. // MgoB.DestoryMongoConn(biddingConn)
  76. // }
  77. log.Println("bidding sync...over all", count, "extract sync ", c)
  78. NextNode(mapInfo, stype)
  79. // NextNodePro(mapInfo, stype)
  80. NextNodeTidb(mapInfo, stype)
  81. if stype == "bidding_history" {
  82. NextNodeBidData(mapInfo) // bidding-data数据
  83. NextNodeTidbQyxy(mapInfo) // tidb-企业数据
  84. NextNodeHn(mapInfo)
  85. }
  86. if stype == "bidding" {
  87. uq := map[string]interface{}{
  88. "gtid": map[string]interface{}{
  89. "$gte": util.ObjToString(mapInfo["gtid"]),
  90. },
  91. "lteid": map[string]interface{}{
  92. "$lte": util.ObjToString(mapInfo["lteid"]),
  93. },
  94. }
  95. MgoB.Update("bidding_processing_ids", uq, bson.M{"$set": bson.M{"dataprocess": 7, "updatetime": time.Now().Unix()}}, false, true)
  96. }
  97. //领域标签处理的数据 id段
  98. if stype == "bidding_history" {
  99. MgoB.Save("field_data_record", map[string]interface{}{"gtid": mapInfo["gtid"], "lteid": mapInfo["lteid"], "status": 0})
  100. }
  101. }
  102. func biddingAllTask(data []byte, mapInfo map[string]interface{}) {
  103. defer util.Catch()
  104. q, _ := mapInfo["query"].(map[string]interface{})
  105. if q == nil {
  106. q = map[string]interface{}{
  107. "_id": map[string]interface{}{
  108. "$gt": mongodb.StringTOBsonId(mapInfo["gtid"].(string)),
  109. "$lte": mongodb.StringTOBsonId(mapInfo["lteid"].(string)),
  110. },
  111. }
  112. }
  113. //extract库
  114. extractConn := MgoE.GetMgoConn()
  115. defer MgoE.DestoryMongoConn(extractConn)
  116. extractResult := extractConn.DB(MgoE.DbName).C(config.Conf.DB.MongoE.Coll).Find(q).Select(map[string]interface{}{
  117. "field_source": 0,
  118. "kvtext": 0,
  119. }).Sort("-_id").Iter()
  120. //bidding库
  121. biddingConn := MgoB.GetMgoConn()
  122. defer MgoB.DestoryMongoConn(biddingConn)
  123. count := 0
  124. var compare map[string]interface{}
  125. result := biddingConn.DB(MgoB.DbName).C(config.Conf.DB.MongoB.Coll).Find(q).Select(map[string]interface{}{
  126. "contenthtml": 0,
  127. "field_source": 0,
  128. }).Sort("-_id").Iter()
  129. for tmp := make(map[string]interface{}); result.Next(tmp); count++ {
  130. update := map[string]interface{}{}
  131. del := map[string]interface{}{} //记录extract没有值而bidding中有值的字段
  132. //对比方法----------------
  133. for {
  134. if compare == nil {
  135. compare = make(map[string]interface{})
  136. if !extractResult.Next(compare) {
  137. break
  138. }
  139. }
  140. if compare != nil {
  141. cid := mongodb.BsonIdToSId(compare["_id"])
  142. tid := mongodb.BsonIdToSId(tmp["_id"])
  143. if cid == tid {
  144. //更新bidding表;bidding表modifyinfo中的字段不更新
  145. modifyinfo := make(map[string]bool)
  146. if tmpmodifyinfo, ok := tmp["modifyinfo"].(map[string]interface{}); ok && tmpmodifyinfo != nil {
  147. for k := range tmpmodifyinfo {
  148. modifyinfo[k] = true
  149. }
  150. }
  151. for _, k := range config.Conf.Serve.FieldS {
  152. v1 := compare[k] //extract
  153. v2 := tmp[k] //bidding
  154. if v2 == nil && v1 != nil {
  155. update[k] = v1
  156. } else if v2 != nil && v1 != nil && !modifyinfo[k] {
  157. update[k] = v1
  158. } else if v2 != nil && v1 == nil && !modifyinfo[k] {
  159. if k == "s_subscopeclass" && del["subscopeclass"] == nil {
  160. continue
  161. } else if k == "s_topscopeclass" && del["topscopeclass"] == nil {
  162. continue
  163. }
  164. del[k] = 1
  165. //util.Debug("抽取结果没有值,bidding有值:field--", k, del)
  166. }
  167. }
  168. if util.IntAll(compare["repeat"]) == 1 {
  169. update["extracttype"] = -1
  170. update["dataprocess"] = 7
  171. if compare["repeat_id"] != nil {
  172. update["repeat_id"] = compare["repeat_id"]
  173. }
  174. } else {
  175. update["extracttype"] = 1
  176. update["dataprocess"] = 8
  177. }
  178. break
  179. } else {
  180. if cid < tid {
  181. compare = nil
  182. continue
  183. } else {
  184. break
  185. }
  186. }
  187. } else {
  188. break
  189. }
  190. }
  191. //------------------对比结束
  192. //处理分类
  193. if compare != nil { //extract
  194. fieldFun(compare, update)
  195. compare = nil
  196. }
  197. // entidlist
  198. extractMap := make(map[string]interface{})
  199. if update["s_winner"] != "" {
  200. cid := companyFun(update)
  201. if len(cid) > 0 {
  202. update["entidlist"] = cid
  203. extractMap["entidlist"] = cid
  204. }
  205. }
  206. if len(extractMap) > 0 {
  207. updateExtPool <- []map[string]interface{}{
  208. {"_id": tmp["_id"]},
  209. {"$set": extractMap},
  210. }
  211. }
  212. // 附件有效字段
  213. if i := validFile(tmp); i != 0 {
  214. if i == -1 {
  215. update["isValidFile"] = false
  216. } else {
  217. update["isValidFile"] = true
  218. }
  219. }
  220. if len(update) > 0 {
  221. if len(del) > 0 { //删除的字段
  222. updateBidPool <- []map[string]interface{}{{
  223. "_id": tmp["_id"],
  224. },
  225. {"$set": update, "$unset": del},
  226. }
  227. } else {
  228. updateBidPool <- []map[string]interface{}{{
  229. "_id": tmp["_id"],
  230. },
  231. {"$set": update},
  232. }
  233. }
  234. }
  235. if count%50000 == 0 {
  236. log.Println("biddingTask current", count)
  237. }
  238. tmp = make(map[string]interface{})
  239. }
  240. log.Println("biddingAll sync...over all", count)
  241. }
  242. func doIndex(infos []map[string]interface{}, bkey, stype string) int {
  243. syncNo := 0 //抽取表数据同步数量
  244. //对比两张表数据,减少查询次数
  245. var compare map[string]interface{}
  246. var bidUpdate [][]map[string]interface{}
  247. var extUpdate [][]map[string]interface{}
  248. //SaveEsLock := &sync.Mutex{}
  249. ygsiteArr := []map[string]interface{}{}
  250. sitedata, _ := MgoE.Find("bidding_yg_site", map[string]interface{}{}, nil, nil, false, -1, -1)
  251. if sitedata != nil && len(*sitedata) > 0 {
  252. ygsiteArr = *sitedata
  253. }
  254. log.Println("start ...")
  255. for n, tmp := range infos {
  256. tid := mongodb.BsonIdToSId(tmp["_id"])
  257. update := map[string]interface{}{} //要更新的mongo数据
  258. del := map[string]interface{}{}
  259. edata, _ := MgoE.FindById(config.Conf.DB.MongoE.Coll, tid, nil)
  260. //对比方法----------------
  261. if edata != nil && len(*edata) > 0 {
  262. compare = *edata
  263. if stype == "bidding" {
  264. // 增量id段 正常数据
  265. if dg := util.IntAll(compare["dataging"]); dg == 1 { //extract中dataging=1跳过
  266. tmp = make(map[string]interface{})
  267. compare = nil
  268. continue
  269. }
  270. // delete(eMap, tid)
  271. }
  272. if stype == "bidding_history" {
  273. //增量id段 历史数据
  274. if compare["history_updatetime"] == nil { //extract中history_updatetime不存在跳过
  275. tmp = make(map[string]interface{})
  276. compare = nil
  277. continue
  278. }
  279. // delete(eMap, tid)
  280. }
  281. syncNo++
  282. log.Println("抽取区域 省", compare["area"], " 市 ", compare["city"], " 区 ", compare["district"], " id ", tid)
  283. modifyinfo := make(map[string]bool)
  284. if tmp["modifyinfo"] != nil {
  285. if tmpmodifyinfo, ok := tmp["modifyinfo"].(map[string]interface{}); ok {
  286. for k := range tmpmodifyinfo {
  287. modifyinfo[k] = true
  288. }
  289. }
  290. }
  291. for _, k := range config.Conf.Serve.FieldS {
  292. v1 := compare[k] //extract
  293. v2 := tmp[k] //bidding
  294. if v2 == nil && v1 != nil {
  295. update[k] = v1
  296. } else if v2 != nil && v1 != nil && !modifyinfo[k] {
  297. update[k] = v1
  298. } else if v2 != nil && v1 == nil && !modifyinfo[k] {
  299. if k == "s_subscopeclass" && del["subscopeclass"] == nil {
  300. continue
  301. } else if k == "s_topscopeclass" && del["topscopeclass"] == nil {
  302. continue
  303. } else if k == "city" || k == "district" {
  304. update[k] = ""
  305. } else {
  306. del[k] = 1
  307. }
  308. }
  309. }
  310. // 附件重采,数据同步时不更新判重标识
  311. if util.IntAll(compare["repeat"]) == 1 {
  312. update["extracttype"] = -1
  313. update["dataprocess"] = 7
  314. if compare["repeat_id"] != nil {
  315. update["repeat_id"] = compare["repeat_id"]
  316. }
  317. } else {
  318. update["extracttype"] = 1
  319. update["dataprocess"] = 8
  320. }
  321. } else {
  322. compare = nil
  323. if util.IntAll(tmp["dataging"]) == 1 { //修改未抽取的bidding数据的dataging
  324. update["dataging"] = 0
  325. }
  326. update["dataprocess"] = 8
  327. }
  328. //下面可以多线程跑的--->
  329. //处理分类
  330. if compare != nil { //extract
  331. fieldFun(compare, update)
  332. // publishtime 20230523
  333. if util.IntAll(tmp["publishtime"]) == -1 {
  334. if pb := methodPb(compare); pb > 0 {
  335. update["publishtime"] = pb
  336. }
  337. }
  338. compare = nil
  339. }
  340. //------------------对比结束
  341. //处理key descript
  342. if bkey == "" {
  343. DealInfo(&tmp, &update)
  344. }
  345. // entidlist
  346. extractMap := make(map[string]interface{})
  347. if update["s_winner"] != "" {
  348. cid := companyFun(update)
  349. if len(cid) > 0 {
  350. tmp["entidlist"] = cid
  351. update["entidlist"] = cid
  352. extractMap["entidlist"] = cid
  353. }
  354. }
  355. //阳光采购
  356. spidercode := util.ObjToString(tmp["spidercode"])
  357. site := util.ObjToString(tmp["site"])
  358. infoAttribute := util.ObjToString(tmp["infoAttribute"])
  359. if infoAttribute != "" {
  360. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "user"})
  361. } else {
  362. for _, v := range ygsiteArr {
  363. spidercodes := util.ObjToString(v["spidercode"])
  364. sites := util.ObjToString(v["site"])
  365. datatype := util.ObjToString(v["datatype"])
  366. if datatype == "1" && spidercodes == spidercode {
  367. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "spidercode"})
  368. } else if datatype == "2" && site == sites {
  369. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "site"})
  370. }
  371. }
  372. }
  373. // 6.10 剑鱼发布信息分类处理, 写在这里是为了修改抽取表
  374. typeFunc(tmp, update, extractMap)
  375. if len(extractMap) > 0 {
  376. if extractMap["toptype"] != nil && extractMap["subtype"] == nil {
  377. extUpdate = append(extUpdate, []map[string]interface{}{
  378. {"_id": tmp["_id"]},
  379. {"$set": extractMap, "$unset": map[string]interface{}{"subtype": ""}},
  380. })
  381. } else {
  382. extUpdate = append(extUpdate, []map[string]interface{}{
  383. {"_id": tmp["_id"]},
  384. {"$set": extractMap},
  385. })
  386. }
  387. if len(extUpdate) >= MgoBulkSize {
  388. tmps := extUpdate
  389. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, tmps...)
  390. extUpdate = [][]map[string]interface{}{}
  391. }
  392. }
  393. // 附件有效字段
  394. if i := validFile(tmp); i != 0 {
  395. if i == -1 {
  396. tmp["isValidFile"] = false
  397. update["isValidFile"] = false
  398. } else {
  399. tmp["isValidFile"] = true
  400. update["isValidFile"] = true
  401. }
  402. }
  403. // 2024-02-21 徐志恒 情报标签字段
  404. toptype := util.ObjToString(tmp["toptype"])
  405. subtype := util.ObjToString(tmp["subtype"])
  406. buyerclass := util.ObjToString(update["buyerclass"])
  407. if buyerclass != "" {
  408. update["buyer_type"] = getStr(buyerclass)
  409. }
  410. s_topscopeclass := util.ObjToString(update["s_topscopeclass"])
  411. if (tmp["tag_topinformation"] != nil && (subtype == "合同" || subtype == "中标" || subtype == "成交" || subtype == "采购意向" || toptype == "招标")) || (tmp["tag_topinformation"] == nil && toptype == "拟建" && strings.Contains(s_topscopeclass, "建筑工程")) {
  412. update["tag_set"] = getTagSet(tmp, compare)
  413. }
  414. if len(update) > 0 {
  415. log.Println("保存bidding区域 省", update["area"], " 市 ", update["city"], " 区 ", update["district"], " buyerclass ", update["buyerclass"], update["buyer_type"], " id ", tid)
  416. if len(del) > 0 {
  417. bidUpdate = append(bidUpdate, []map[string]interface{}{{
  418. "_id": tmp["_id"],
  419. },
  420. {"$set": update, "$unset": del},
  421. })
  422. } else {
  423. bidUpdate = append(bidUpdate, []map[string]interface{}{{
  424. "_id": tmp["_id"],
  425. },
  426. {"$set": update},
  427. })
  428. }
  429. if len(bidUpdate) >= MgoBulkSize {
  430. tmps := bidUpdate
  431. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, tmps...)
  432. bidUpdate = [][]map[string]interface{}{}
  433. }
  434. }
  435. if n%500 == 0 {
  436. log.Println("biddingTask current ", n)
  437. }
  438. tmp = make(map[string]interface{})
  439. }
  440. //SaveEsLock.Lock()
  441. if len(bidUpdate) > 0 {
  442. tmps := bidUpdate
  443. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, tmps...)
  444. bidUpdate = [][]map[string]interface{}{}
  445. }
  446. if len(extUpdate) > 0 {
  447. tmps := extUpdate
  448. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, tmps...)
  449. extUpdate = [][]map[string]interface{}{}
  450. }
  451. //SaveEsLock.Unlock()
  452. return syncNo
  453. }
  454. // @Description subscopeclass、topscopeclass、package
  455. // 20230523 多包处理 subpackage = 1
  456. // @Author J 2022/6/7 5:54 PM
  457. func fieldFun(compare, update map[string]interface{}) {
  458. subscopeclass, _ := compare["subscopeclass"].([]interface{}) //subscopeclass
  459. if subscopeclass != nil {
  460. m1 := map[string]bool{}
  461. newclass := []string{}
  462. for _, sc := range subscopeclass {
  463. sclass, _ := sc.(string)
  464. if !m1[sclass] {
  465. m1[sclass] = true
  466. newclass = append(newclass, sclass)
  467. }
  468. }
  469. update["s_subscopeclass"] = strings.Join(newclass, ",")
  470. update["subscopeclass"] = newclass
  471. }
  472. topscopeclass, _ := compare["topscopeclass"].([]interface{}) //topscopeclass
  473. if topscopeclass != nil {
  474. m2 := map[string]bool{}
  475. newclass := []string{}
  476. for _, tc := range topscopeclass {
  477. tclass, _ := tc.(string)
  478. tclass = regLetter.ReplaceAllString(tclass, "") // 去除字母
  479. if !m2[tclass] {
  480. m2[tclass] = true
  481. newclass = append(newclass, tclass)
  482. }
  483. }
  484. update["topscopeclass"] = topscopeclass
  485. update["s_topscopeclass"] = strings.Join(newclass, ",")
  486. }
  487. if package1 := compare["package"]; package1 != nil {
  488. packageM, _ := package1.(map[string]interface{})
  489. update["package"] = packageM
  490. for _, p := range packageM {
  491. pm, _ := p.(map[string]interface{})
  492. if util.ObjToString(pm["winner"]) != "" || util.Float64All(pm["budget"]) > 0 ||
  493. util.Float64All(pm["bidamount"]) > 0 {
  494. update["multipackage"] = 1
  495. break
  496. }
  497. }
  498. } else {
  499. update["multipackage"] = 0
  500. }
  501. // subpackage
  502. if compare["package"] != nil && compare["s_winner"] != nil && compare["bidamount"] != nil {
  503. pg := compare["package"].(map[string]interface{})
  504. if len(pg) > 1 {
  505. var bmt []float64
  506. var swn []string
  507. for _, p := range pg {
  508. p1 := p.(map[string]interface{})
  509. if p1["bidamount"] != nil {
  510. bmt = append(bmt, util.Float64All(p1["bidamount"]))
  511. }
  512. if w := util.ObjToString(p1["winner"]); w != "" {
  513. swn = append(swn)
  514. }
  515. }
  516. if len(bmt) > 1 && len(swn) > 1 {
  517. sn := strings.Split(util.ObjToString(compare["s_winner"]), ",")
  518. sort.Strings(sn)
  519. sort.Strings(swn)
  520. swn1 := util.ObjArrToStringArr(Duplicate(swn)) // 去重
  521. if strings.Join(swn1, ",") == strings.Join(sn, ",") {
  522. bidamount := 0.0
  523. for _, f := range bmt {
  524. bidamount += f
  525. }
  526. if bidamount == util.Float64All(compare["bidamount"]) {
  527. update["subpackage"] = 1
  528. }
  529. }
  530. }
  531. }
  532. }
  533. }
  534. // @Description entidlist
  535. // @Author J 2022/6/7 2:36 PM
  536. func companyFun(tmp map[string]interface{}) (cid []string) {
  537. sWinnerarr := strings.Split(util.ObjToString(tmp["s_winner"]), ",")
  538. for _, w := range sWinnerarr {
  539. if w != "" {
  540. id := redis.GetStr("qyxy_id", w)
  541. if id == "" {
  542. ents, _ := MgoQ.Find(config.Conf.DB.MongoQ.Coll, map[string]interface{}{"company_name": w}, map[string]interface{}{"updatetime": -1}, map[string]interface{}{"company_name": 1}, false, -1, -1)
  543. if len(*ents) > 0 {
  544. id = util.ObjToString((*ents)[0]["_id"])
  545. redis.PutCKV("qyxy_id", w, id)
  546. } else {
  547. ent, _ := MgoP.FindOne(config.Conf.DB.MongoP.Coll, map[string]interface{}{"history_name": w})
  548. if len(*ent) > 0 {
  549. id = util.ObjToString((*ent)["company_id"])
  550. redis.PutCKV("qyxy_id", w, id)
  551. }
  552. }
  553. }
  554. if id == "" {
  555. id = "-"
  556. }
  557. cid = append(cid, id)
  558. }
  559. }
  560. return cid
  561. }
  562. // @Description update 修改bidding表,extractM修改抽取表
  563. // @Author J 2022/6/10 10:29 AM
  564. func typeFunc(tmp, update, extractM map[string]interface{}) {
  565. if jyData, ok := tmp["jyfb_data"].(map[string]interface{}); ok {
  566. if t := util.ObjToString(jyData["type"]); t != "" {
  567. switch t {
  568. //case "采购信息":
  569. case "招标公告":
  570. if util.ObjToString(tmp["toptype"]) != "招标" {
  571. update["toptype"] = "招标"
  572. extractM["toptype"] = "招标"
  573. delete(update, "subtype")
  574. }
  575. case "采购意向":
  576. if util.ObjToString(tmp["toptype"]) != "采购意向" {
  577. update["toptype"] = "采购意向"
  578. update["subtype"] = "采购意向"
  579. extractM["toptype"] = "采购意向"
  580. extractM["subtype"] = "采购意向"
  581. }
  582. case "招标预告":
  583. if util.ObjToString(tmp["toptype"]) != "预告" {
  584. update["toptype"] = "预告"
  585. extractM["toptype"] = "预告"
  586. delete(update, "subtype")
  587. }
  588. case "招标结果":
  589. if util.ObjToString(tmp["toptype"]) != "结果" {
  590. update["toptype"] = "结果"
  591. extractM["toptype"] = "结果"
  592. delete(update, "subtype")
  593. }
  594. }
  595. }
  596. }
  597. }
  598. // @Description 附件有效字段(isValidFile)
  599. // @Author J 2022/7/8 14:41
  600. func validFile(tmp map[string]interface{}) int {
  601. isContinue := false
  602. if pinfo, o := tmp["projectinfo"].(map[string]interface{}); o {
  603. if atts, o1 := pinfo["attachments"].(map[string]interface{}); o1 {
  604. for _, att := range atts {
  605. if att == nil {
  606. continue
  607. }
  608. if reflect.TypeOf(att).String() == "string" {
  609. continue
  610. }
  611. att1 := att.(map[string]interface{})
  612. if fid := util.ObjToString(att1["fid"]); fid != "" {
  613. isContinue = true
  614. break
  615. }
  616. }
  617. if isContinue {
  618. if attachTxt, o := tmp["attach_text"].(map[string]interface{}); o {
  619. if len(attachTxt) > 0 {
  620. for _, at := range attachTxt {
  621. at1 := at.(map[string]interface{})
  622. if len(at1) > 0 {
  623. for k, _ := range at1 {
  624. if reflect.TypeOf(at1[k]).String() == "string" {
  625. continue
  626. }
  627. at2 := at1[k].(map[string]interface{})
  628. s := strings.ToLower(util.ObjToString(at2["file_name"]))
  629. if !strings.Contains(s, "jpg") || !strings.Contains(s, "jpeg") != strings.Contains(s, "png") ||
  630. strings.Contains(s, "pdf") {
  631. if strings.Contains(s, "swf") || strings.Contains(s, "html") {
  632. return -1
  633. } else if AnalysisFile(oss.OssGetObject(util.ObjToString(at2["attach_url"]))) {
  634. return 1
  635. }
  636. }
  637. }
  638. break
  639. } else {
  640. break
  641. }
  642. }
  643. }
  644. }
  645. flag := false
  646. for _, att := range atts {
  647. if att == nil {
  648. continue
  649. }
  650. if reflect.TypeOf(att).String() == "string" {
  651. continue
  652. }
  653. att1 := att.(map[string]interface{})
  654. if fid := util.ObjToString(att1["fid"]); fid != "" {
  655. ftype := strings.ToLower(util.ObjToString(tmp["ftype"]))
  656. if ftype != "swf" && ftype != "html" && oss.OssObjExists("jy-datafile", fid) {
  657. return 1
  658. } else {
  659. flag = true
  660. }
  661. }
  662. }
  663. if flag {
  664. return -1
  665. }
  666. }
  667. }
  668. }
  669. return 0
  670. }
  671. // @Description id不变,内容变化 重新索引数据
  672. // @Author J 2022/8/10 13:29
  673. func taskinfo(id string) {
  674. tmp, _ := MgoB.FindById("bidding", id, nil)
  675. if tmp == nil || len(*tmp) == 0 {
  676. log.Println(fmt.Sprintf("taskinfo bidding id=%s 未查询到数据", id))
  677. return
  678. }
  679. extractM, _ := MgoE.FindById(config.Conf.DB.MongoE.Coll, id, nil)
  680. if extractM == nil || len(*extractM) == 0 {
  681. extractM, _ = MgoE.FindById(config.Conf.DB.MongoE.Coll1, id, nil)
  682. if extractM == nil || len(*extractM) == 0 {
  683. log.Println(fmt.Sprintf("taskinfo extract id=%s 未查询到数据", id))
  684. return
  685. }
  686. }
  687. update := map[string]interface{}{} //要更新的mongo数据
  688. //更新bidding表字段
  689. for _, k := range config.Conf.Serve.FieldS {
  690. v1 := (*extractM)[k] //extract
  691. v2 := (*tmp)[k] //bidding
  692. if v2 == nil && v1 != nil {
  693. update[k] = v1
  694. } else if v2 != nil && v1 != nil {
  695. update[k] = v1
  696. } else if v2 != nil && v1 == nil {
  697. if k == "city" || k == "district" {
  698. update[k] = ""
  699. }
  700. }
  701. }
  702. if util.IntAll((*extractM)["repeat"]) == 1 {
  703. update["extracttype"] = -1
  704. update["dataprocess"] = 7
  705. if (*extractM)["repeat_id"] != nil {
  706. update["repeat_id"] = (*extractM)["repeat_id"]
  707. }
  708. } else {
  709. update["extracttype"] = 1
  710. update["dataprocess"] = 8
  711. }
  712. //处理分类
  713. fieldFun(*extractM, update)
  714. extractMap := make(map[string]interface{})
  715. if util.ObjToString((*tmp)["s_winner"]) != "" {
  716. cid := companyFun(*tmp)
  717. if len(cid) > 0 {
  718. update["entidlist"] = cid
  719. extractMap["entidlist"] = cid
  720. }
  721. MgoE.UpdateById(config.Conf.DB.MongoE.Coll, id, map[string]interface{}{"$set": extractMap})
  722. }
  723. // 附件有效字段
  724. if i := validFile(*tmp); i != 0 {
  725. if i == -1 {
  726. update["isValidFile"] = false
  727. } else {
  728. update["isValidFile"] = true
  729. }
  730. }
  731. if len(update) > 0 {
  732. MgoB.UpdateById(config.Conf.DB.MongoB.Coll, id, map[string]interface{}{"$set": update})
  733. }
  734. mapinfo := map[string]interface{}{
  735. "infoid": id,
  736. "stype": "index-by-id",
  737. }
  738. datas, _ := json.Marshal(mapinfo)
  739. var next = &net.UDPAddr{
  740. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  741. Port: util.IntAll(config.Conf.Udp.Next.Port),
  742. }
  743. log.Println("nsq data over es ", next, " mapinfo ", string(datas))
  744. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  745. }
  746. var DateTimeSelect = []string{"bidopentime", "bidendtime", "signaturedate", "comeintime"}
  747. // @Description 发布时间处理
  748. // @Author J 2023/5/23 14:32
  749. func methodPb(tmp map[string]interface{}) int64 {
  750. if tmp["ext_publishtime"] != nil {
  751. if newPb := util.Int64All(tmp["ext_publishtime"]); newPb < time.Now().Unix() && newPb > 1420041600 {
  752. return newPb
  753. }
  754. }
  755. for _, d := range DateTimeSelect {
  756. if tmp[d] != nil && util.Int64All(tmp[d]) < time.Now().Unix() {
  757. return util.Int64All(tmp[d])
  758. }
  759. }
  760. return 0
  761. }
  762. // Duplicate
  763. // @Description 去重
  764. // @Author J 2023/5/24 09:53
  765. func Duplicate(a interface{}) (ret []interface{}) {
  766. va := reflect.ValueOf(a)
  767. for i := 0; i < va.Len(); i++ {
  768. if i > 0 && reflect.DeepEqual(va.Index(i-1).Interface(), va.Index(i).Interface()) {
  769. continue
  770. }
  771. ret = append(ret, va.Index(i).Interface())
  772. }
  773. return ret
  774. }
  775. // @Description 获取情报标签
  776. // @Author 徐志恒 2024/2/21 09:53
  777. func getTagSet(tmp, compare map[string]interface{}) map[string]map[string]interface{} {
  778. tagSet := map[string]map[string]interface{}{}
  779. wuye := map[string]interface{}{}
  780. buyer := util.ObjToString(compare["buyer"])
  781. publishtime := util.Int64All(tmp["publishtime"])
  782. bidamount := util.Float64All(compare["bidamount"])
  783. toptype := util.ObjToString(tmp["toptype"])
  784. subtype := util.ObjToString(tmp["subtype"])
  785. if subtype == "合同" {
  786. wuye["isfirsthand"] = 62
  787. if buyer != "" {
  788. sql := `{
  789. "query": {
  790. "bool": {
  791. "must": [
  792. {
  793. "term": {
  794. "buyer": "` + buyer + `"
  795. }
  796. },
  797. {
  798. "term": {
  799. "tag_topinformation": "情报_物业"
  800. }
  801. },
  802. {
  803. "term": {
  804. "subtype": "合同"
  805. }
  806. },
  807. {
  808. "range": {
  809. "publishtime": {
  810. "lte": ` + fmt.Sprint(publishtime) + `
  811. }
  812. }
  813. }
  814. ]
  815. }
  816. },
  817. "sort": {
  818. "publishtime": "asc"
  819. },
  820. "_source": [
  821. "s_winner"
  822. ],
  823. "size": 10000
  824. }`
  825. data := Es.Get("bidding", "bidding", sql)
  826. if data != nil && len(*data) > 0 {
  827. count := 0
  828. first := util.ObjToString((*data)[0]["s_winner"])
  829. for k, v := range *data {
  830. winner := util.ObjToString(v["s_winner"])
  831. if k > 0 && first != winner {
  832. first = winner
  833. count++
  834. }
  835. }
  836. changehand := fmt.Sprintf("%.2f", float64(count)/float64(len(*data)))
  837. changehands, _ := strconv.ParseFloat(changehand, 64)
  838. wuye["changehand"] = changehands
  839. if changehands > 0.3 {
  840. wuye["changehandindex"] = 61
  841. }
  842. if len(*data) > 1 {
  843. wuye["isfirsthand"] = 0
  844. }
  845. }
  846. }
  847. wuye["period"] = getperiod(compare)
  848. } else if toptype == "招标" || toptype == "采购意向" {
  849. bidamount = util.Float64All(compare["budget"])
  850. }
  851. if tmp["projectinfo"] != nil {
  852. projectInfo := util.ObjToMap(tmp["projectinfo"])
  853. if projectInfo != nil && len(*projectInfo) > 0 {
  854. if (*projectInfo)["attachments"] != nil {
  855. wuye["isfile"] = 63
  856. }
  857. }
  858. }
  859. wuye["scale"] = getBidamountRange(bidamount)
  860. if tmp["property_form"] != nil {
  861. property_form := util.ObjArrToStringArr(tmp["property_form"].([]interface{}))
  862. wuye["property_form"] = getpropertyform(property_form)
  863. }
  864. tagSet["wuye"] = wuye
  865. return tagSet
  866. }
  867. func getBidamountRange(value float64) int {
  868. switch {
  869. case value < 500000:
  870. return 1
  871. case value >= 500000 && value < 1000000:
  872. return 2
  873. case value >= 1000000 && value < 2000000:
  874. return 3
  875. case value >= 2000000 && value < 5000000:
  876. return 4
  877. default:
  878. return 5
  879. }
  880. }
  881. func getpropertyform(value []string) string {
  882. arr := []string{}
  883. categories := map[string]string{
  884. "住宅": "21",
  885. "政府办公楼": "22",
  886. "学校": "23",
  887. "医院": "24",
  888. "产业园区": "25",
  889. "旅游景区": "26",
  890. "交通运输": "27",
  891. "商务办公楼": "28",
  892. "酒店": "29",
  893. }
  894. for _, v := range value {
  895. if categories[v] != "" {
  896. arr = append(arr, categories[v])
  897. }
  898. }
  899. return strings.Join(arr, ",")
  900. }
  901. func getperiod(data map[string]interface{}) int {
  902. res := 16
  903. signaturedate := util.Int64All(data["signaturedate"]) //合同签订日期
  904. expiredate := util.Int64All(data["expiredate"]) //合同截止日期
  905. // contractperiod := util.ObjToString(data["contractperiod"]) //合同期限
  906. project_duration := util.IntAll(data["project_duration"]) //工期时长
  907. project_timeunit := util.ObjToString(data["project_timeunit"]) //工期单位
  908. result := float64(0)
  909. if expiredate > 0 && signaturedate > 0 {
  910. result = calculateYearDifference(signaturedate, expiredate)
  911. } else if project_duration > 0 && project_timeunit != "" {
  912. if strings.Contains(project_timeunit, "年") {
  913. if project_duration == 1 {
  914. res = 12
  915. } else if project_duration == 2 {
  916. res = 13
  917. } else if project_duration == 3 {
  918. res = 14
  919. } else if project_duration == 5 {
  920. res = 15
  921. }
  922. return res
  923. } else if strings.Contains(project_timeunit, "月") {
  924. result = float64(project_duration) / 12
  925. } else if strings.Contains(project_timeunit, "周") {
  926. result = float64(project_duration) * 7 / 365
  927. } else if strings.Contains(project_timeunit, "日") || strings.Contains(project_timeunit, "天") {
  928. result = float64(project_duration) / 365
  929. }
  930. }
  931. if result == 0 {
  932. res = 16
  933. } else if result < 1 {
  934. res = 11
  935. } else if result >= 1 && result < 2 {
  936. res = 12
  937. } else if result >= 2 && result < 3 {
  938. res = 13
  939. } else if result >= 3 && result < 4 {
  940. res = 14
  941. } else if result >= 5 {
  942. res = 15
  943. }
  944. return res
  945. }
  946. func calculateYearDifference(startTime int64, endTime int64) float64 {
  947. start := time.Unix(startTime, 0)
  948. end := time.Unix(endTime, 0)
  949. duration := end.Sub(start)
  950. years := duration.Hours() / 24 / 365
  951. return years
  952. }
  953. func getStr(b string) string {
  954. if b == "" {
  955. return "其它"
  956. }
  957. a1 := "(交通|运输物流|工信|农业|住建|城管|市政|出版广电|检察院|科技|民政|生态环境|市场监管|水利|应急管理|自然资源|财政|档案|党委办|组织|发改|宣传|政府办|政务中心|人大|政协|法院|公安|国资委|海关|机关事务|纪委|军队|人社|商务|审计税务|司法|体育|统计|统战|文旅|民宗|银保监|证监|气象|社会团体|公共资源交易)"
  958. a2 := "(卫健委|医疗)"
  959. a3 := "(教育|学校)"
  960. a4 := "(人行|金融业)"
  961. a5 := "(信息技术|电信行业|农林牧渔|建筑业|传媒|制造业|住宿餐饮|采矿业|能源化工|批发零售)"
  962. if strings.Contains(a1, b) {
  963. return "政府机构"
  964. } else if strings.Contains(a2, b) {
  965. return "医疗单位"
  966. } else if strings.Contains(a3, b) {
  967. return "教育单位"
  968. } else if strings.Contains(a4, b) {
  969. return "金融企业"
  970. } else if strings.Contains(a5, b) {
  971. return "商业公司"
  972. } else {
  973. return "其它"
  974. }
  975. return "其它"
  976. }