task.go 29 KB

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