task.go 29 KB

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