task.go 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033
  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 == "zc_cgxx" {
  360. update["public_type"] = "用户发布"
  361. update["extracttype"] = -1
  362. update["dataprocess"] = 7
  363. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "user"})
  364. log.Println("阳光采购用户发布", tid)
  365. } else {
  366. if util.IntAll(compare["repeat"]) != 1 {
  367. for _, v := range ygsiteArr {
  368. spidercodes := util.ObjToString(v["spidercode"])
  369. sites := util.ObjToString(v["site"])
  370. datatype := util.ObjToString(v["datatype"])
  371. if datatype == "1" && spidercodes == spidercode {
  372. update["infoattribute"] = "zc_cgxx"
  373. update["public_type"] = "平台发布"
  374. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "spidercode"})
  375. log.Println("阳光采购平台发布爬虫", tid)
  376. } else if datatype == "2" && site == sites {
  377. update["infoattribute"] = "zc_cgxx"
  378. update["public_type"] = "平台发布"
  379. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "site"})
  380. log.Println("阳光采购平台发布站点", tid)
  381. } else if datatype == "3" && spidercodes == spidercode {
  382. update["infoattribute"] = "zc_cgxx"
  383. update["public_type"] = "平台发布"
  384. update["is_yg_new"] = 1
  385. update["extracttype"] = -1
  386. update["dataprocess"] = 7
  387. MgoE.Save("bidding_yg", map[string]interface{}{"id": tid, "source": "is_yg_new"})
  388. log.Println("阳光采购平台新采集爬虫", tid)
  389. }
  390. }
  391. }
  392. }
  393. // 6.10 剑鱼发布信息分类处理, 写在这里是为了修改抽取表
  394. typeFunc(tmp, update, extractMap)
  395. if len(extractMap) > 0 {
  396. if extractMap["toptype"] != nil && extractMap["subtype"] == nil {
  397. extUpdate = append(extUpdate, []map[string]interface{}{
  398. {"_id": tmp["_id"]},
  399. {"$set": extractMap, "$unset": map[string]interface{}{"subtype": ""}},
  400. })
  401. } else {
  402. extUpdate = append(extUpdate, []map[string]interface{}{
  403. {"_id": tmp["_id"]},
  404. {"$set": extractMap},
  405. })
  406. }
  407. if len(extUpdate) >= MgoBulkSize {
  408. tmps := extUpdate
  409. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, tmps...)
  410. extUpdate = [][]map[string]interface{}{}
  411. }
  412. }
  413. // 附件有效字段
  414. if i := validFile(tmp); i != 0 {
  415. if i == -1 {
  416. tmp["isValidFile"] = false
  417. update["isValidFile"] = false
  418. } else {
  419. tmp["isValidFile"] = true
  420. update["isValidFile"] = true
  421. }
  422. }
  423. // 2024-02-21 徐志恒 情报标签字段
  424. toptype := util.ObjToString(tmp["toptype"])
  425. subtype := util.ObjToString(tmp["subtype"])
  426. buyerclass := util.ObjToString(update["buyerclass"])
  427. if buyerclass != "" {
  428. update["buyer_type"] = getStr(buyerclass)
  429. }
  430. s_topscopeclass := util.ObjToString(update["s_topscopeclass"])
  431. if (tmp["tag_topinformation"] != nil && (subtype == "合同" || subtype == "中标" || subtype == "成交" || subtype == "采购意向" || toptype == "招标")) || (tmp["tag_topinformation"] == nil && toptype == "拟建" && strings.Contains(s_topscopeclass, "建筑工程")) {
  432. update["tag_set"] = getTagSet(tmp, compare)
  433. }
  434. if len(update) > 0 {
  435. log.Println("保存bidding区域 省", update["area"], " 市 ", update["city"], " 区 ", update["district"], " buyerclass ", update["buyerclass"], update["buyer_type"], " id ", tid)
  436. if len(del) > 0 {
  437. bidUpdate = append(bidUpdate, []map[string]interface{}{{
  438. "_id": tmp["_id"],
  439. },
  440. {"$set": update, "$unset": del},
  441. })
  442. } else {
  443. bidUpdate = append(bidUpdate, []map[string]interface{}{{
  444. "_id": tmp["_id"],
  445. },
  446. {"$set": update},
  447. })
  448. }
  449. if len(bidUpdate) >= MgoBulkSize {
  450. tmps := bidUpdate
  451. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, tmps...)
  452. bidUpdate = [][]map[string]interface{}{}
  453. }
  454. }
  455. if n%500 == 0 {
  456. log.Println("biddingTask current ", n)
  457. }
  458. tmp = make(map[string]interface{})
  459. }
  460. //SaveEsLock.Lock()
  461. if len(bidUpdate) > 0 {
  462. tmps := bidUpdate
  463. MgoB.UpdateBulk(config.Conf.DB.MongoB.Coll, tmps...)
  464. bidUpdate = [][]map[string]interface{}{}
  465. }
  466. if len(extUpdate) > 0 {
  467. tmps := extUpdate
  468. MgoE.UpdateBulk(config.Conf.DB.MongoE.Coll, tmps...)
  469. extUpdate = [][]map[string]interface{}{}
  470. }
  471. //SaveEsLock.Unlock()
  472. return syncNo
  473. }
  474. // @Description subscopeclass、topscopeclass、package
  475. // 20230523 多包处理 subpackage = 1
  476. // @Author J 2022/6/7 5:54 PM
  477. func fieldFun(compare, update map[string]interface{}) {
  478. subscopeclass, _ := compare["subscopeclass"].([]interface{}) //subscopeclass
  479. if subscopeclass != nil {
  480. m1 := map[string]bool{}
  481. newclass := []string{}
  482. for _, sc := range subscopeclass {
  483. sclass, _ := sc.(string)
  484. if !m1[sclass] {
  485. m1[sclass] = true
  486. newclass = append(newclass, sclass)
  487. }
  488. }
  489. update["s_subscopeclass"] = strings.Join(newclass, ",")
  490. update["subscopeclass"] = newclass
  491. }
  492. topscopeclass, _ := compare["topscopeclass"].([]interface{}) //topscopeclass
  493. if topscopeclass != nil {
  494. m2 := map[string]bool{}
  495. newclass := []string{}
  496. for _, tc := range topscopeclass {
  497. tclass, _ := tc.(string)
  498. tclass = regLetter.ReplaceAllString(tclass, "") // 去除字母
  499. if !m2[tclass] {
  500. m2[tclass] = true
  501. newclass = append(newclass, tclass)
  502. }
  503. }
  504. update["topscopeclass"] = topscopeclass
  505. update["s_topscopeclass"] = strings.Join(newclass, ",")
  506. }
  507. if package1 := compare["package"]; package1 != nil {
  508. packageM, _ := package1.(map[string]interface{})
  509. update["package"] = packageM
  510. for _, p := range packageM {
  511. pm, _ := p.(map[string]interface{})
  512. if util.ObjToString(pm["winner"]) != "" || util.Float64All(pm["budget"]) > 0 ||
  513. util.Float64All(pm["bidamount"]) > 0 {
  514. update["multipackage"] = 1
  515. break
  516. }
  517. }
  518. } else {
  519. update["multipackage"] = 0
  520. }
  521. // subpackage
  522. if compare["package"] != nil && compare["s_winner"] != nil && compare["bidamount"] != nil {
  523. pg := compare["package"].(map[string]interface{})
  524. if len(pg) > 1 {
  525. var bmt []float64
  526. var swn []string
  527. for _, p := range pg {
  528. p1 := p.(map[string]interface{})
  529. if p1["bidamount"] != nil {
  530. bmt = append(bmt, util.Float64All(p1["bidamount"]))
  531. }
  532. if w := util.ObjToString(p1["winner"]); w != "" {
  533. swn = append(swn)
  534. }
  535. }
  536. if len(bmt) > 1 && len(swn) > 1 {
  537. sn := strings.Split(util.ObjToString(compare["s_winner"]), ",")
  538. sort.Strings(sn)
  539. sort.Strings(swn)
  540. swn1 := util.ObjArrToStringArr(Duplicate(swn)) // 去重
  541. if strings.Join(swn1, ",") == strings.Join(sn, ",") {
  542. bidamount := 0.0
  543. for _, f := range bmt {
  544. bidamount += f
  545. }
  546. if bidamount == util.Float64All(compare["bidamount"]) {
  547. update["subpackage"] = 1
  548. }
  549. }
  550. }
  551. }
  552. }
  553. }
  554. // @Description entidlist
  555. // @Author J 2022/6/7 2:36 PM
  556. func companyFun(tmp map[string]interface{}) (cid []string) {
  557. sWinnerarr := strings.Split(util.ObjToString(tmp["s_winner"]), ",")
  558. for _, w := range sWinnerarr {
  559. if w != "" {
  560. id := redis.GetStr("qyxy_id", w)
  561. if id == "" {
  562. 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)
  563. if len(*ents) > 0 {
  564. id = util.ObjToString((*ents)[0]["_id"])
  565. redis.PutCKV("qyxy_id", w, id)
  566. } else {
  567. ent, _ := MgoP.FindOne(config.Conf.DB.MongoP.Coll, map[string]interface{}{"history_name": w})
  568. if len(*ent) > 0 {
  569. id = util.ObjToString((*ent)["company_id"])
  570. redis.PutCKV("qyxy_id", w, id)
  571. }
  572. }
  573. }
  574. if id == "" {
  575. id = "-"
  576. }
  577. cid = append(cid, id)
  578. }
  579. }
  580. return cid
  581. }
  582. // @Description update 修改bidding表,extractM修改抽取表
  583. // @Author J 2022/6/10 10:29 AM
  584. func typeFunc(tmp, update, extractM map[string]interface{}) {
  585. if jyData, ok := tmp["jyfb_data"].(map[string]interface{}); ok {
  586. if t := util.ObjToString(jyData["type"]); t != "" {
  587. switch t {
  588. //case "采购信息":
  589. case "招标公告":
  590. if util.ObjToString(tmp["toptype"]) != "招标" {
  591. update["toptype"] = "招标"
  592. extractM["toptype"] = "招标"
  593. delete(update, "subtype")
  594. }
  595. case "采购意向":
  596. if util.ObjToString(tmp["toptype"]) != "采购意向" {
  597. update["toptype"] = "采购意向"
  598. update["subtype"] = "采购意向"
  599. extractM["toptype"] = "采购意向"
  600. extractM["subtype"] = "采购意向"
  601. }
  602. case "招标预告":
  603. if util.ObjToString(tmp["toptype"]) != "预告" {
  604. update["toptype"] = "预告"
  605. extractM["toptype"] = "预告"
  606. delete(update, "subtype")
  607. }
  608. case "招标结果":
  609. if util.ObjToString(tmp["toptype"]) != "结果" {
  610. update["toptype"] = "结果"
  611. extractM["toptype"] = "结果"
  612. delete(update, "subtype")
  613. }
  614. }
  615. }
  616. }
  617. }
  618. // @Description 附件有效字段(isValidFile)
  619. // @Author J 2022/7/8 14:41
  620. func validFile(tmp map[string]interface{}) int {
  621. isContinue := false
  622. if pinfo, o := tmp["projectinfo"].(map[string]interface{}); o {
  623. if atts, o1 := pinfo["attachments"].(map[string]interface{}); o1 {
  624. for _, att := range atts {
  625. if att == nil {
  626. continue
  627. }
  628. if reflect.TypeOf(att).String() == "string" {
  629. continue
  630. }
  631. att1 := att.(map[string]interface{})
  632. if fid := util.ObjToString(att1["fid"]); fid != "" {
  633. isContinue = true
  634. break
  635. }
  636. }
  637. if isContinue {
  638. if attachTxt, o := tmp["attach_text"].(map[string]interface{}); o {
  639. if len(attachTxt) > 0 {
  640. for _, at := range attachTxt {
  641. at1 := at.(map[string]interface{})
  642. if len(at1) > 0 {
  643. for k, _ := range at1 {
  644. if reflect.TypeOf(at1[k]).String() == "string" {
  645. continue
  646. }
  647. at2 := at1[k].(map[string]interface{})
  648. s := strings.ToLower(util.ObjToString(at2["file_name"]))
  649. if !strings.Contains(s, "jpg") || !strings.Contains(s, "jpeg") != strings.Contains(s, "png") ||
  650. strings.Contains(s, "pdf") {
  651. if strings.Contains(s, "swf") || strings.Contains(s, "html") {
  652. return -1
  653. } else if AnalysisFile(oss.OssGetObject(util.ObjToString(at2["attach_url"]))) {
  654. return 1
  655. }
  656. }
  657. }
  658. break
  659. } else {
  660. break
  661. }
  662. }
  663. }
  664. }
  665. flag := false
  666. for _, att := range atts {
  667. if att == nil {
  668. continue
  669. }
  670. if reflect.TypeOf(att).String() == "string" {
  671. continue
  672. }
  673. att1 := att.(map[string]interface{})
  674. if fid := util.ObjToString(att1["fid"]); fid != "" {
  675. ftype := strings.ToLower(util.ObjToString(tmp["ftype"]))
  676. if ftype != "swf" && ftype != "html" && oss.OssObjExists("jy-datafile", fid) {
  677. return 1
  678. } else {
  679. flag = true
  680. }
  681. }
  682. }
  683. if flag {
  684. return -1
  685. }
  686. }
  687. }
  688. }
  689. return 0
  690. }
  691. // @Description id不变,内容变化 重新索引数据
  692. // @Author J 2022/8/10 13:29
  693. func taskinfo(id string) {
  694. tmp, _ := MgoB.FindById("bidding", id, nil)
  695. if tmp == nil || len(*tmp) == 0 {
  696. log.Println(fmt.Sprintf("taskinfo bidding id=%s 未查询到数据", id))
  697. return
  698. }
  699. extractM, _ := MgoE.FindById(config.Conf.DB.MongoE.Coll, id, nil)
  700. if extractM == nil || len(*extractM) == 0 {
  701. extractM, _ = MgoE.FindById(config.Conf.DB.MongoE.Coll1, id, nil)
  702. if extractM == nil || len(*extractM) == 0 {
  703. log.Println(fmt.Sprintf("taskinfo extract id=%s 未查询到数据", id))
  704. return
  705. }
  706. }
  707. update := map[string]interface{}{} //要更新的mongo数据
  708. //更新bidding表字段
  709. for _, k := range config.Conf.Serve.FieldS {
  710. v1 := (*extractM)[k] //extract
  711. v2 := (*tmp)[k] //bidding
  712. if v2 == nil && v1 != nil {
  713. update[k] = v1
  714. } else if v2 != nil && v1 != nil {
  715. update[k] = v1
  716. } else if v2 != nil && v1 == nil {
  717. if k == "city" || k == "district" {
  718. update[k] = ""
  719. }
  720. }
  721. }
  722. if util.IntAll((*extractM)["repeat"]) == 1 {
  723. update["extracttype"] = -1
  724. update["dataprocess"] = 7
  725. if (*extractM)["repeat_id"] != nil {
  726. update["repeat_id"] = (*extractM)["repeat_id"]
  727. }
  728. } else {
  729. update["extracttype"] = 1
  730. update["dataprocess"] = 8
  731. }
  732. //处理分类
  733. fieldFun(*extractM, update)
  734. extractMap := make(map[string]interface{})
  735. if util.ObjToString((*tmp)["s_winner"]) != "" {
  736. cid := companyFun(*tmp)
  737. if len(cid) > 0 {
  738. update["entidlist"] = cid
  739. extractMap["entidlist"] = cid
  740. }
  741. MgoE.UpdateById(config.Conf.DB.MongoE.Coll, id, map[string]interface{}{"$set": extractMap})
  742. }
  743. // 附件有效字段
  744. if i := validFile(*tmp); i != 0 {
  745. if i == -1 {
  746. update["isValidFile"] = false
  747. } else {
  748. update["isValidFile"] = true
  749. }
  750. }
  751. if len(update) > 0 {
  752. MgoB.UpdateById(config.Conf.DB.MongoB.Coll, id, map[string]interface{}{"$set": update})
  753. }
  754. mapinfo := map[string]interface{}{
  755. "infoid": id,
  756. "stype": "index-by-id",
  757. }
  758. datas, _ := json.Marshal(mapinfo)
  759. var next = &net.UDPAddr{
  760. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  761. Port: util.IntAll(config.Conf.Udp.Next.Port),
  762. }
  763. log.Println("nsq data over es ", next, " mapinfo ", string(datas))
  764. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  765. }
  766. var DateTimeSelect = []string{"bidopentime", "bidendtime", "signaturedate", "comeintime"}
  767. // @Description 发布时间处理
  768. // @Author J 2023/5/23 14:32
  769. func methodPb(tmp map[string]interface{}) int64 {
  770. if tmp["ext_publishtime"] != nil {
  771. if newPb := util.Int64All(tmp["ext_publishtime"]); newPb < time.Now().Unix() && newPb > 1420041600 {
  772. return newPb
  773. }
  774. }
  775. for _, d := range DateTimeSelect {
  776. if tmp[d] != nil && util.Int64All(tmp[d]) < time.Now().Unix() {
  777. return util.Int64All(tmp[d])
  778. }
  779. }
  780. return 0
  781. }
  782. // Duplicate
  783. // @Description 去重
  784. // @Author J 2023/5/24 09:53
  785. func Duplicate(a interface{}) (ret []interface{}) {
  786. va := reflect.ValueOf(a)
  787. for i := 0; i < va.Len(); i++ {
  788. if i > 0 && reflect.DeepEqual(va.Index(i-1).Interface(), va.Index(i).Interface()) {
  789. continue
  790. }
  791. ret = append(ret, va.Index(i).Interface())
  792. }
  793. return ret
  794. }
  795. // @Description 获取情报标签
  796. // @Author 徐志恒 2024/2/21 09:53
  797. func getTagSet(tmp, compare map[string]interface{}) map[string]map[string]interface{} {
  798. tagSet := map[string]map[string]interface{}{}
  799. wuye := map[string]interface{}{}
  800. buyer := util.ObjToString(compare["buyer"])
  801. publishtime := util.Int64All(tmp["publishtime"])
  802. bidamount := util.Float64All(compare["bidamount"])
  803. toptype := util.ObjToString(tmp["toptype"])
  804. subtype := util.ObjToString(tmp["subtype"])
  805. if subtype == "合同" {
  806. wuye["isfirsthand"] = 62
  807. if buyer != "" {
  808. sql := `{
  809. "query": {
  810. "bool": {
  811. "must": [
  812. {
  813. "term": {
  814. "buyer": "` + buyer + `"
  815. }
  816. },
  817. {
  818. "term": {
  819. "tag_topinformation": "情报_物业"
  820. }
  821. },
  822. {
  823. "term": {
  824. "subtype": "合同"
  825. }
  826. },
  827. {
  828. "range": {
  829. "publishtime": {
  830. "lte": ` + fmt.Sprint(publishtime) + `
  831. }
  832. }
  833. }
  834. ]
  835. }
  836. },
  837. "sort": {
  838. "publishtime": "asc"
  839. },
  840. "_source": [
  841. "s_winner"
  842. ],
  843. "size": 10000
  844. }`
  845. data := Es.Get("bidding", "bidding", sql)
  846. if data != nil && len(*data) > 0 {
  847. count := 0
  848. first := util.ObjToString((*data)[0]["s_winner"])
  849. for k, v := range *data {
  850. winner := util.ObjToString(v["s_winner"])
  851. if k > 0 && first != winner {
  852. first = winner
  853. count++
  854. }
  855. }
  856. changehand := fmt.Sprintf("%.2f", float64(count)/float64(len(*data)))
  857. changehands, _ := strconv.ParseFloat(changehand, 64)
  858. wuye["changehand"] = changehands
  859. if changehands > 0.3 {
  860. wuye["changehandindex"] = 61
  861. }
  862. if len(*data) > 1 {
  863. wuye["isfirsthand"] = 0
  864. }
  865. }
  866. }
  867. wuye["period"] = getperiod(compare)
  868. } else if toptype == "招标" || toptype == "采购意向" {
  869. bidamount = util.Float64All(compare["budget"])
  870. }
  871. if tmp["projectinfo"] != nil {
  872. projectInfo := util.ObjToMap(tmp["projectinfo"])
  873. if projectInfo != nil && len(*projectInfo) > 0 {
  874. if (*projectInfo)["attachments"] != nil {
  875. wuye["isfile"] = 63
  876. }
  877. }
  878. }
  879. wuye["scale"] = getBidamountRange(bidamount)
  880. if tmp["property_form"] != nil {
  881. property_form := util.ObjArrToStringArr(tmp["property_form"].([]interface{}))
  882. wuye["property_form"] = getpropertyform(property_form)
  883. }
  884. tagSet["wuye"] = wuye
  885. return tagSet
  886. }
  887. func getBidamountRange(value float64) int {
  888. switch {
  889. case value < 500000:
  890. return 1
  891. case value >= 500000 && value < 1000000:
  892. return 2
  893. case value >= 1000000 && value < 2000000:
  894. return 3
  895. case value >= 2000000 && value < 5000000:
  896. return 4
  897. default:
  898. return 5
  899. }
  900. }
  901. func getpropertyform(value []string) string {
  902. arr := []string{}
  903. categories := map[string]string{
  904. "住宅": "21",
  905. "政府办公楼": "22",
  906. "学校": "23",
  907. "医院": "24",
  908. "产业园区": "25",
  909. "旅游景区": "26",
  910. "交通运输": "27",
  911. "商务办公楼": "28",
  912. "酒店": "29",
  913. }
  914. for _, v := range value {
  915. if categories[v] != "" {
  916. arr = append(arr, categories[v])
  917. }
  918. }
  919. return strings.Join(arr, ",")
  920. }
  921. func getperiod(data map[string]interface{}) int {
  922. res := 16
  923. signaturedate := util.Int64All(data["signaturedate"]) //合同签订日期
  924. expiredate := util.Int64All(data["expiredate"]) //合同截止日期
  925. // contractperiod := util.ObjToString(data["contractperiod"]) //合同期限
  926. project_duration := util.IntAll(data["project_duration"]) //工期时长
  927. project_timeunit := util.ObjToString(data["project_timeunit"]) //工期单位
  928. result := float64(0)
  929. if expiredate > 0 && signaturedate > 0 {
  930. result = calculateYearDifference(signaturedate, expiredate)
  931. } else if project_duration > 0 && project_timeunit != "" {
  932. if strings.Contains(project_timeunit, "年") {
  933. if project_duration == 1 {
  934. res = 12
  935. } else if project_duration == 2 {
  936. res = 13
  937. } else if project_duration == 3 {
  938. res = 14
  939. } else if project_duration == 5 {
  940. res = 15
  941. }
  942. return res
  943. } else if strings.Contains(project_timeunit, "月") {
  944. result = float64(project_duration) / 12
  945. } else if strings.Contains(project_timeunit, "周") {
  946. result = float64(project_duration) * 7 / 365
  947. } else if strings.Contains(project_timeunit, "日") || strings.Contains(project_timeunit, "天") {
  948. result = float64(project_duration) / 365
  949. }
  950. }
  951. if result == 0 {
  952. res = 16
  953. } else if result < 1 {
  954. res = 11
  955. } else if result >= 1 && result < 2 {
  956. res = 12
  957. } else if result >= 2 && result < 3 {
  958. res = 13
  959. } else if result >= 3 && result < 4 {
  960. res = 14
  961. } else if result >= 5 {
  962. res = 15
  963. }
  964. return res
  965. }
  966. func calculateYearDifference(startTime int64, endTime int64) float64 {
  967. start := time.Unix(startTime, 0)
  968. end := time.Unix(endTime, 0)
  969. duration := end.Sub(start)
  970. years := duration.Hours() / 24 / 365
  971. return years
  972. }
  973. func getStr(b string) string {
  974. if b == "" {
  975. return "其它"
  976. }
  977. a1 := "(交通|运输物流|工信|农业|住建|城管|市政|出版广电|检察院|科技|民政|生态环境|市场监管|水利|应急管理|自然资源|财政|档案|党委办|组织|发改|宣传|政府办|政务中心|人大|政协|法院|公安|国资委|海关|机关事务|纪委|军队|人社|商务|审计税务|司法|体育|统计|统战|文旅|民宗|银保监|证监|气象|社会团体|公共资源交易)"
  978. a2 := "(卫健委|医疗)"
  979. a3 := "(教育|学校)"
  980. a4 := "(人行|金融业)"
  981. a5 := "(信息技术|电信行业|农林牧渔|建筑业|传媒|制造业|住宿餐饮|采矿业|能源化工|批发零售)"
  982. if strings.Contains(a1, b) {
  983. return "政府机构"
  984. } else if strings.Contains(a2, b) {
  985. return "医疗单位"
  986. } else if strings.Contains(a3, b) {
  987. return "教育单位"
  988. } else if strings.Contains(a4, b) {
  989. return "金融企业"
  990. } else if strings.Contains(a5, b) {
  991. return "商业公司"
  992. } else {
  993. return "其它"
  994. }
  995. return "其它"
  996. }