task.go 26 KB

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