task.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. package main
  2. import (
  3. util "app.yhyue.com/data_processing/common_utils"
  4. "app.yhyue.com/data_processing/common_utils/log"
  5. "app.yhyue.com/data_processing/common_utils/mongodb"
  6. "app.yhyue.com/data_processing/common_utils/redis"
  7. "app.yhyue.com/data_processing/common_utils/udp"
  8. "encoding/json"
  9. "field_sync/config"
  10. "field_sync/oss"
  11. "fmt"
  12. "go.uber.org/zap"
  13. "net"
  14. "reflect"
  15. "regexp"
  16. "strings"
  17. )
  18. var (
  19. regLetter = regexp.MustCompile("[a-z]*")
  20. )
  21. func biddingTask(data []byte, mapInfo map[string]interface{}) {
  22. defer util.Catch()
  23. stype := util.ObjToString(mapInfo["stype"])
  24. // 领域标签处理的数据 id段
  25. if stype == "bidding_history" {
  26. MgoB.Save("field_data_record", map[string]interface{}{"gtid": mapInfo["gtid"], "lteid": mapInfo["lteid"], "status": 0})
  27. }
  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.Info("抽取表", zap.Int("数据量", extCount), zap.Int("重复数据量", repeatCount))
  56. //bidding库
  57. biddingConn := MgoB.GetMgoConn()
  58. count, _ := biddingConn.DB(MgoB.DbName).C(config.Conf.DB.MongoB.Coll).Find(&q).Count()
  59. log.Info("bidding表", zap.Int64("同步总数:", count))
  60. c := 0
  61. if count < 200000 {
  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.Info("查询结果", zap.Int64("bidding", count), zap.Int("抽取:", extCount))
  72. c = doIndex(res, eMap, bkey, stype)
  73. } else {
  74. log.Info("查询结果", zap.Int64("数据量太大,放弃", count))
  75. MgoB.DestoryMongoConn(biddingConn)
  76. }
  77. log.Info("bidding sync...over", zap.Int64("all", count), zap.Int("extract sync", c))
  78. NextNode(mapInfo)
  79. if stype == "bidding_history" {
  80. var next = &net.UDPAddr{
  81. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  82. Port: util.IntAll(config.Conf.Udp.Next.Port),
  83. }
  84. mapInfo["stype"] = "biddingdata"
  85. datas, _ := json.Marshal(mapInfo)
  86. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  87. }
  88. }
  89. func doIndex(infos []map[string]interface{}, eMap map[string]map[string]interface{}, bkey, stype string) int {
  90. syncNo := 0 //抽取表数据同步数量
  91. //对比两张表数据,减少查询次数
  92. var compare map[string]interface{}
  93. log.Info("start ...")
  94. for n, tmp := range infos {
  95. tid := mongodb.BsonIdToSId(tmp["_id"])
  96. update := map[string]interface{}{} //要更新的mongo数据
  97. //对比方法----------------
  98. if eMap[tid] != nil {
  99. compare = eMap[tid]
  100. if stype == "bidding" {
  101. // 增量id段 正常数据
  102. if dg := util.IntAll(compare["dataging"]); dg == 1 { //extract中dataging=1跳过
  103. tmp = make(map[string]interface{})
  104. compare = nil
  105. continue
  106. }
  107. delete(eMap, tid)
  108. }
  109. if stype == "bidding_history" {
  110. //增量id段 历史数据
  111. if compare["history_updatetime"] == nil { //extract中history_updatetime不存在跳过
  112. tmp = make(map[string]interface{})
  113. compare = nil
  114. continue
  115. }
  116. delete(eMap, tid)
  117. }
  118. syncNo++
  119. for _, k := range config.Conf.Serve.FieldS {
  120. v1 := compare[k] //extract
  121. v2 := tmp[k] //bidding
  122. if v2 == nil && v1 != nil {
  123. update[k] = v1
  124. } else if v2 != nil && v1 != nil {
  125. update[k] = v1
  126. } else if v2 != nil && v1 == nil {
  127. if k == "city" || k == "district" {
  128. update[k] = ""
  129. }
  130. }
  131. }
  132. if util.IntAll(compare["repeat"]) == 1 {
  133. update["extracttype"] = -1
  134. update["dataprocess"] = 7
  135. } else {
  136. update["extracttype"] = 1
  137. update["dataprocess"] = 8
  138. }
  139. } else {
  140. compare = nil
  141. if util.IntAll(tmp["dataging"]) == 1 { //修改未抽取的bidding数据的dataging
  142. update["dataging"] = 0
  143. }
  144. update["dataprocess"] = 8
  145. }
  146. //下面可以多线程跑的--->
  147. //处理分类
  148. if compare != nil { //extract
  149. fieldFun(compare, update)
  150. compare = nil
  151. }
  152. //------------------对比结束
  153. //处理key descript
  154. if bkey == "" {
  155. DealInfo(&tmp, &update)
  156. }
  157. // entidlist
  158. extractMap := make(map[string]interface{})
  159. if tmp["s_winner"] != "" {
  160. cid := companyFun(tmp)
  161. if len(cid) > 0 {
  162. tmp["entidlist"] = cid
  163. update["entidlist"] = cid
  164. extractMap["entidlist"] = cid
  165. }
  166. }
  167. // 6.10 剑鱼发布信息分类处理, 写在这里是为了修改抽取表
  168. typeFunc(tmp, update, extractMap)
  169. if len(extractMap) > 0 {
  170. if extractMap["toptype"] != nil && extractMap["subtype"] == nil {
  171. updateExtPool <- []map[string]interface{}{
  172. {"_id": tmp["_id"]},
  173. {"$set": extractMap, "$unset": map[string]interface{}{"subtype": ""}},
  174. }
  175. } else {
  176. updateExtPool <- []map[string]interface{}{
  177. {"_id": tmp["_id"]},
  178. {"$set": extractMap},
  179. }
  180. }
  181. }
  182. // 附件有效字段
  183. if i := validFile(tmp); i != 0 {
  184. if i == -1 {
  185. tmp["isValidFile"] = false
  186. update["isValidFile"] = false
  187. } else {
  188. tmp["isValidFile"] = true
  189. update["isValidFile"] = true
  190. }
  191. }
  192. if len(update) > 0 {
  193. updateBidPool <- []map[string]interface{}{{
  194. "_id": tmp["_id"],
  195. },
  196. {"$set": update},
  197. }
  198. }
  199. if n%500 == 0 {
  200. log.Info("biddingTask", zap.Int("current", n))
  201. }
  202. tmp = make(map[string]interface{})
  203. }
  204. return syncNo
  205. }
  206. // @Description subscopeclass、topscopeclass、package
  207. // @Author J 2022/6/7 5:54 PM
  208. func fieldFun(compare, update map[string]interface{}) {
  209. subscopeclass, _ := compare["subscopeclass"].([]interface{}) //subscopeclass
  210. if subscopeclass != nil {
  211. m1 := map[string]bool{}
  212. newclass := []string{}
  213. for _, sc := range subscopeclass {
  214. sclass, _ := sc.(string)
  215. if !m1[sclass] {
  216. m1[sclass] = true
  217. newclass = append(newclass, sclass)
  218. }
  219. }
  220. update["s_subscopeclass"] = strings.Join(newclass, ",")
  221. update["subscopeclass"] = newclass
  222. }
  223. topscopeclass, _ := compare["topscopeclass"].([]interface{}) //topscopeclass
  224. if topscopeclass != nil {
  225. m2 := map[string]bool{}
  226. newclass := []string{}
  227. for _, tc := range topscopeclass {
  228. tclass, _ := tc.(string)
  229. tclass = regLetter.ReplaceAllString(tclass, "") // 去除字母
  230. if !m2[tclass] {
  231. m2[tclass] = true
  232. newclass = append(newclass, tclass)
  233. }
  234. }
  235. update["topscopeclass"] = topscopeclass
  236. update["s_topscopeclass"] = strings.Join(newclass, ",")
  237. }
  238. if package1 := compare["package"]; package1 != nil {
  239. packageM, _ := package1.(map[string]interface{})
  240. update["package"] = packageM
  241. for _, p := range packageM {
  242. pm, _ := p.(map[string]interface{})
  243. if util.ObjToString(pm["winner"]) != "" || util.Float64All(pm["budget"]) > 0 ||
  244. util.Float64All(pm["bidamount"]) > 0 {
  245. update["multipackage"] = 1
  246. break
  247. }
  248. }
  249. } else {
  250. update["multipackage"] = 0
  251. }
  252. }
  253. // @Description entidlist
  254. // @Author J 2022/6/7 2:36 PM
  255. func companyFun(tmp map[string]interface{}) (cid []string) {
  256. sWinnerarr := strings.Split(util.ObjToString(tmp["s_winner"]), ",")
  257. for _, w := range sWinnerarr {
  258. if w != "" {
  259. id := redis.GetStr("qyxy_id", w)
  260. if id == "" {
  261. 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)
  262. if len(*ents) > 0 {
  263. id = util.ObjToString((*ents)[0]["_id"])
  264. redis.PutCKV("qyxy_id", w, id)
  265. } else {
  266. ent, _ := MgoP.FindOne(config.Conf.DB.MongoP.Coll, map[string]interface{}{"history_name": w})
  267. if len(*ent) > 0 {
  268. id = util.ObjToString((*ent)["company_id"])
  269. redis.PutCKV("qyxy_id", w, id)
  270. }
  271. }
  272. }
  273. if id == "" {
  274. id = "-"
  275. }
  276. cid = append(cid, id)
  277. }
  278. }
  279. return cid
  280. }
  281. // @Description update 修改bidding表,extractM修改抽取表
  282. // @Author J 2022/6/10 10:29 AM
  283. func typeFunc(tmp, update, extractM map[string]interface{}) {
  284. if jyData, ok := tmp["jyfb_data"].(map[string]interface{}); ok {
  285. if t := util.ObjToString(jyData["type"]); t != "" {
  286. switch t {
  287. //case "采购信息":
  288. case "招标公告":
  289. if util.ObjToString(tmp["toptype"]) != "招标" {
  290. update["toptype"] = "招标"
  291. extractM["toptype"] = "招标"
  292. delete(update, "subtype")
  293. }
  294. case "采购意向":
  295. if util.ObjToString(tmp["toptype"]) != "采购意向" {
  296. update["toptype"] = "采购意向"
  297. update["subtype"] = "采购意向"
  298. extractM["toptype"] = "采购意向"
  299. extractM["subtype"] = "采购意向"
  300. }
  301. case "招标预告":
  302. if util.ObjToString(tmp["toptype"]) != "预告" {
  303. update["toptype"] = "预告"
  304. extractM["toptype"] = "预告"
  305. delete(update, "subtype")
  306. }
  307. case "招标结果":
  308. if util.ObjToString(tmp["toptype"]) != "结果" {
  309. update["toptype"] = "结果"
  310. extractM["toptype"] = "结果"
  311. delete(update, "subtype")
  312. }
  313. }
  314. }
  315. }
  316. }
  317. // @Description 附件有效字段(isValidFile)
  318. // @Author J 2022/7/8 14:41
  319. func validFile(tmp map[string]interface{}) int {
  320. isContinue := false
  321. if pinfo, o := tmp["projectinfo"].(map[string]interface{}); o {
  322. if atts, o1 := pinfo["attachments"].(map[string]interface{}); o1 {
  323. for _, att := range atts {
  324. if att == nil {
  325. continue
  326. }
  327. if reflect.TypeOf(att).String() == "string" {
  328. continue
  329. }
  330. att1 := att.(map[string]interface{})
  331. if fid := util.ObjToString(att1["fid"]); fid != "" {
  332. isContinue = true
  333. break
  334. }
  335. }
  336. if isContinue {
  337. if attachTxt, o := tmp["attach_text"].(map[string]interface{}); o {
  338. if len(attachTxt) > 0 {
  339. for _, at := range attachTxt {
  340. at1 := at.(map[string]interface{})
  341. if len(at1) > 0 {
  342. for k, _ := range at1 {
  343. if reflect.TypeOf(at1[k]).String() == "string" {
  344. continue
  345. }
  346. at2 := at1[k].(map[string]interface{})
  347. s := strings.ToLower(util.ObjToString(at2["file_name"]))
  348. if !strings.Contains(s, "jpg") || !strings.Contains(s, "jpeg") != strings.Contains(s, "png") ||
  349. strings.Contains(s, "pdf") {
  350. if strings.Contains(s, "swf") || strings.Contains(s, "html") {
  351. return -1
  352. } else if AnalysisFile(oss.OssGetObject(util.ObjToString(at2["attach_url"]))) {
  353. return 1
  354. }
  355. }
  356. }
  357. break
  358. } else {
  359. break
  360. }
  361. }
  362. }
  363. }
  364. flag := false
  365. for _, att := range atts {
  366. if att == nil {
  367. continue
  368. }
  369. if reflect.TypeOf(att).String() == "string" {
  370. continue
  371. }
  372. att1 := att.(map[string]interface{})
  373. if fid := util.ObjToString(att1["fid"]); fid != "" {
  374. ftype := strings.ToLower(util.ObjToString(tmp["ftype"]))
  375. if ftype != "swf" && ftype != "html" && oss.OssObjExists("jy-datafile", fid) {
  376. return 1
  377. } else {
  378. flag = true
  379. }
  380. }
  381. }
  382. if flag {
  383. return -1
  384. }
  385. }
  386. }
  387. }
  388. return 0
  389. }
  390. // @Description id不变,内容变化 重新索引数据
  391. // @Author J 2022/8/10 13:29
  392. func taskinfo(id string) {
  393. tmp, _ := MgoB.FindById("bidding", id, nil)
  394. if tmp == nil || len(*tmp) == 0 {
  395. log.Info(fmt.Sprintf("taskinfo bidding id=%s 未查询到数据", id))
  396. return
  397. }
  398. extractM, _ := MgoE.FindById(config.Conf.DB.MongoE.Coll, id, nil)
  399. if extractM == nil || len(*extractM) == 0 {
  400. extractM, _ = MgoE.FindById(config.Conf.DB.MongoE.Coll1, id, nil)
  401. if extractM == nil || len(*extractM) == 0 {
  402. log.Info(fmt.Sprintf("taskinfo extract id=%s 未查询到数据", id))
  403. return
  404. }
  405. }
  406. update := map[string]interface{}{} //要更新的mongo数据
  407. //更新bidding表字段
  408. for _, k := range config.Conf.Serve.FieldS {
  409. v1 := (*extractM)[k] //extract
  410. v2 := (*tmp)[k] //bidding
  411. if v2 == nil && v1 != nil {
  412. update[k] = v1
  413. } else if v2 != nil && v1 != nil {
  414. update[k] = v1
  415. } else if v2 != nil && v1 == nil {
  416. if k == "city" || k == "district" {
  417. update[k] = ""
  418. }
  419. }
  420. }
  421. if util.IntAll((*extractM)["repeat"]) == 1 {
  422. update["extracttype"] = -1
  423. update["dataprocess"] = 7
  424. } else {
  425. update["extracttype"] = 1
  426. update["dataprocess"] = 8
  427. }
  428. //处理分类
  429. fieldFun(*extractM, update)
  430. //同时保存到elastic
  431. for tk, tv := range update {
  432. (*tmp)[tk] = tv
  433. }
  434. extractMap := make(map[string]interface{})
  435. if util.ObjToString((*tmp)["s_winner"]) != "" {
  436. cid := companyFun(*tmp)
  437. if len(cid) > 0 {
  438. (*tmp)["entidlist"] = cid
  439. update["entidlist"] = cid
  440. extractMap["entidlist"] = cid
  441. }
  442. updateExtPool <- []map[string]interface{}{
  443. {"_id": mongodb.StringTOBsonId(id)},
  444. {"$set": extractMap},
  445. }
  446. }
  447. // 附件有效字段
  448. if i := validFile(*tmp); i != 0 {
  449. if i == -1 {
  450. (*tmp)["isValidFile"] = false
  451. update["isValidFile"] = false
  452. } else {
  453. (*tmp)["isValidFile"] = true
  454. update["isValidFile"] = true
  455. }
  456. }
  457. if len(update) > 0 {
  458. updateBidPool <- []map[string]interface{}{{
  459. "_id": mongodb.StringTOBsonId(id),
  460. },
  461. {"$set": update},
  462. }
  463. }
  464. mapinfo := map[string]interface{}{
  465. "infoid": id,
  466. "stype": "index-by-id",
  467. }
  468. datas, _ := json.Marshal(mapinfo)
  469. var next = &net.UDPAddr{
  470. IP: net.ParseIP(config.Conf.Udp.Next.Addr),
  471. Port: util.IntAll(config.Conf.Udp.Next.Port),
  472. }
  473. log.Info("nsq data over", zap.Any("es", next), zap.String("mapinfo", string(datas)))
  474. _ = UdpClient.WriteUdp(datas, udp.OP_TYPE_DATA, next)
  475. }