task.go 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "project/config"
  6. "regexp"
  7. "strings"
  8. "sync"
  9. "time"
  10. "unicode/utf8"
  11. "github.com/goinggo/mapstructure"
  12. "github.com/robfig/cron"
  13. "go.mongodb.org/mongo-driver/bson"
  14. "go.uber.org/zap"
  15. util "jygit.jydev.jianyu360.cn/data_processing/common_utils"
  16. "jygit.jydev.jianyu360.cn/data_processing/common_utils/log"
  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. /**
  22. 任务入口
  23. 全量、增量合并
  24. 更新、插入,内存清理
  25. 转换成info对象
  26. **/
  27. // var PreRegexp = map[string][]*regexp.Regexp{}
  28. // var BackRegexp = map[string][]*regexp.Regexp{}
  29. // var BackRepRegexp = map[string][]RegexpInfo{}
  30. var BlackRegexp = map[string][]*regexp.Regexp{}
  31. var (
  32. //从标题获取项目编号
  33. titleGetPc = regexp.MustCompile("^([-0-9a-zA-Z第号采招政询电审竞#]{8,}[-0-9a-zA-Z#]+)")
  34. titleGetPc1 = regexp.MustCompile("[\\[【((](.{0,6}(编号|编码|项号|包号|代码|标段?号)[::为])?([-0-9a-zA-Z第号采招政询电审竞#]{5,}([\\[\\]()()][-0-9a-zA-Z第号采招审竞#]+[\\[\\]()()][-0-9a-zA-Z第号采招审竞#]+)?)[\\]】))]")
  35. titleGetPc2 = regexp.MustCompile("([-0-9a-zA-Z第号采政招询电审竞#]{8,}[-0-9a-zA-Z#]+)(.{0,5}公告)?$")
  36. //项目编号过滤
  37. pcReplace = regexp.MustCompile("([\\[【((〖〔《{﹝{](重|第?[二三四再]次.{0,4})[\\]】))〗〕》}﹞}])$|[\\[\\]【】()()〖〗〔〕《》{}﹝﹞-;{}–  ]+|(号|重|第?[二三四五再]次(招标)?)$|[ __]+|((采购)?项目|采购(项目)?)$")
  38. //项目编号只是数字或只是字母4个以下
  39. StrOrNum = regexp.MustCompile("^[0-9_-]{1,4}$|^[a-zA-Z_-]{1,4}$")
  40. //纯数字或纯字母
  41. StrOrNum2 = regexp.MustCompile("^[0-9_-]+$|^[a-zA-Z_-]+$")
  42. //含分包词,招标未识别分包 合并到一个项目
  43. KeyPackage = regexp.MustCompile("[0-9a-zA-Z一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ]+.{0,2}(包|段)|(包|段)[0-9a-zA-Z一二三四五六七八九十ⅠⅡⅢⅣⅤⅥⅦⅧⅨⅩ]+.{0,2}")
  44. )
  45. type RegexpInfo struct {
  46. regs *regexp.Regexp
  47. repstr string
  48. }
  49. // 项目合并对象
  50. type ProjectTask struct {
  51. InitMinTime int64 //最小时间,小于0的处理一次
  52. name string
  53. thread int //线程数
  54. //查找锁
  55. findLock sync.Mutex
  56. wg sync.WaitGroup
  57. //map锁
  58. AllIdsMapLock sync.Mutex
  59. //对应的id
  60. AllIdsMap map[string]*ID
  61. //采购单位、项目名称、项目编号
  62. mapPb, mapPn, mapPc map[string]*Key
  63. //流程数据 字段相同,直接合并
  64. mapHref map[string]string
  65. mapHrefLock sync.Mutex
  66. //站点
  67. mapSite map[string]*Site
  68. mapSiteLock sync.Mutex
  69. //spider isflow
  70. mapSpider map[string]int
  71. mapSpiderLock sync.Mutex
  72. //bidtype、bidstatus 锁
  73. mapBidLock sync.Mutex
  74. //更新或新增通道
  75. updatePool chan []map[string]interface{}
  76. //savePool chan map[string]interface{}
  77. //saveSign, updateSign chan bool
  78. //表名
  79. coll string
  80. //当前状态是全量还是增量
  81. currentType string //当前是跑全量还是跑增量
  82. //
  83. clearContimes int
  84. //当前时间
  85. currentTime int64
  86. //保存长度
  87. saveSize int
  88. pici int64
  89. validTime int64
  90. statusTime int64
  91. //结果时间的更新 最近两天的公告不再更新jgtime
  92. jgTime int64
  93. Brun bool
  94. }
  95. func NewPT() *ProjectTask {
  96. p := &ProjectTask{
  97. InitMinTime: int64(1325347200),
  98. name: "全/增量对象",
  99. thread: Thread,
  100. updatePool: make(chan []map[string]interface{}, 5000),
  101. //savePool: make(chan map[string]interface{}, 2000),
  102. wg: sync.WaitGroup{},
  103. AllIdsMap: make(map[string]*ID, 5000000),
  104. mapPb: make(map[string]*Key, 1500000),
  105. mapPn: make(map[string]*Key, 5000000),
  106. mapPc: make(map[string]*Key, 5000000),
  107. mapHref: make(map[string]string, 1500000),
  108. mapSite: make(map[string]*Site, 1000000),
  109. mapSpider: make(map[string]int, 1000000),
  110. saveSize: 200,
  111. //saveSign: make(chan bool, 1),
  112. //updateSign: make(chan bool, 1),
  113. coll: ProjectColl,
  114. validTime: int64(util.IntAllDef(config.Conf.Serve.ValidDays, 150)) * 86400,
  115. statusTime: int64(util.IntAllDef(config.Conf.Serve.StatusDays, 15) * 86400),
  116. jgTime: int64(util.IntAllDef(7, 7) * 86400),
  117. currentType: "ql",
  118. }
  119. return p
  120. }
  121. var P_QL *ProjectTask
  122. var sp = make(chan bool, 1)
  123. func (p *ProjectTask) updateAllQueue() {
  124. arru := make([][]map[string]interface{}, p.saveSize)
  125. indexu := 0
  126. for {
  127. select {
  128. case v := <-p.updatePool:
  129. arru[indexu] = v
  130. indexu++
  131. if indexu == p.saveSize {
  132. sp <- true
  133. go func(arru [][]map[string]interface{}) {
  134. defer func() {
  135. <-sp
  136. }()
  137. MgoP.UpSertBulk(p.coll, arru...)
  138. }(arru)
  139. arru = make([][]map[string]interface{}, p.saveSize)
  140. indexu = 0
  141. }
  142. case <-time.After(1 * time.Second):
  143. if indexu > 0 {
  144. sp <- true
  145. go func(arru [][]map[string]interface{}) {
  146. defer func() {
  147. <-sp
  148. }()
  149. MgoP.UpSertBulk(p.coll, arru...)
  150. }(arru[:indexu])
  151. arru = make([][]map[string]interface{}, p.saveSize)
  152. indexu = 0
  153. }
  154. }
  155. }
  156. }
  157. // 项目合并内存更新
  158. func (p *ProjectTask) clearMem() {
  159. c := cron.New()
  160. // 创建项目的时间大于7天
  161. //在内存中保留最近6个月的信息
  162. //跑全量时每5分钟跑一次,跑增量时400分钟跑一次
  163. _ = c.AddFunc("0 */30 * * * ?", func() {
  164. if (p.currentType == "ql" && SingleClear == 0) || p.clearContimes >= 20 {
  165. SingleClear = 1
  166. //跳过的次数清零
  167. p.clearContimes = 0
  168. //信息进入查找对比全局锁
  169. p.findLock.Lock()
  170. //defer p.findLock.Unlock()
  171. //合并进行的任务都完成
  172. p.wg.Wait()
  173. //遍历id
  174. //所有内存中的项目信息
  175. p.AllIdsMapLock.Lock()
  176. p.mapHrefLock.Lock()
  177. log.Info("清除开始")
  178. //清除计数
  179. clearNum := 0
  180. for kHref, pid := range p.mapHref { //删除mapHref,p.AllIdsMap删除之前执行
  181. v := p.AllIdsMap[pid]
  182. if v != nil && p.currentTime-v.P.LastTime > p.validTime {
  183. delete(p.mapHref, kHref)
  184. }
  185. }
  186. for k, v := range p.AllIdsMap {
  187. if p.currentTime-v.P.LastTime > p.validTime {
  188. clearNum++
  189. redis.Del(RedisProject, k)
  190. //删除id的map
  191. delete(p.AllIdsMap, k)
  192. //删除pb
  193. if v.P.Buyer != "" {
  194. ids := p.mapPb[v.P.Buyer]
  195. if ids != nil {
  196. ids.Lock.Lock()
  197. ids.Arr = deleteSlice(ids.Arr, k, "pb")
  198. if len(ids.Arr) == 0 {
  199. delete(p.mapPb, v.P.Buyer)
  200. }
  201. ids.Lock.Unlock()
  202. }
  203. }
  204. //删除mapPn
  205. for _, vn := range append([]string{v.P.ProjectName}, v.P.MPN...) {
  206. if vn != "" {
  207. ids := p.mapPn[vn]
  208. if ids != nil {
  209. ids.Lock.Lock()
  210. ids.Arr = deleteSlice(ids.Arr, k, "pn")
  211. if len(ids.Arr) == 0 {
  212. delete(p.mapPn, vn)
  213. }
  214. ids.Lock.Unlock()
  215. }
  216. }
  217. }
  218. //删除mapPc
  219. for _, vn := range append([]string{v.P.ProjectCode}, v.P.MPC...) {
  220. if vn != "" {
  221. ids := p.mapPc[vn]
  222. if ids != nil {
  223. ids.Lock.Lock()
  224. ids.Arr = deleteSlice(ids.Arr, k, "pc")
  225. if len(ids.Arr) == 0 {
  226. delete(p.mapPc, vn)
  227. }
  228. ids.Lock.Unlock()
  229. }
  230. }
  231. }
  232. v = nil
  233. }
  234. }
  235. p.mapHrefLock.Unlock()
  236. p.AllIdsMapLock.Unlock()
  237. p.findLock.Unlock()
  238. SingleClear = 0
  239. log.Info("清除完成:", zap.Int("clearNum", clearNum), zap.Int("AllIdsMap:", len(p.AllIdsMap)), zap.Int("mapPn:", len(p.mapPn)),
  240. zap.Int("mapPc:", len(p.mapPc)), zap.Int("mapPb:", len(p.mapPb)), zap.Int("mapHref:", len(p.mapHref)))
  241. } else {
  242. p.clearContimes++
  243. }
  244. })
  245. c.Start()
  246. }
  247. // 全量合并
  248. func (p *ProjectTask) taskQl(udpInfo map[string]interface{}) {
  249. defer util.Catch()
  250. p.thread = util.IntAllDef(Thread, 4)
  251. q, _ := udpInfo["query"].(map[string]interface{})
  252. if q == nil {
  253. q = map[string]interface{}{}
  254. lteid, _ := udpInfo["lteid"].(string)
  255. var idmap map[string]interface{}
  256. if len(lteid) > 15 {
  257. idmap = map[string]interface{}{
  258. "$lte": mongodb.StringTOBsonId(lteid),
  259. }
  260. }
  261. gtid, _ := udpInfo["gtid"].(string)
  262. if len(gtid) > 15 {
  263. if idmap == nil {
  264. idmap = map[string]interface{}{}
  265. }
  266. idmap["$gte"] = mongodb.StringTOBsonId(gtid)
  267. }
  268. if idmap != nil {
  269. q["_id"] = idmap
  270. }
  271. }
  272. if c := util.ObjToString(udpInfo["coll"]); c != "" {
  273. BiddingColl = c
  274. } else {
  275. BiddingColl = config.Conf.DB.MongoB.Coll
  276. }
  277. //生成查询语句执行
  278. p.enter(MgoB.DbName, BiddingColl, q)
  279. }
  280. // 增量合并
  281. func (p *ProjectTask) taskZl(udpInfo map[string]interface{}) {
  282. defer util.Catch()
  283. //1、检查pubilshtime索引
  284. db, _ := udpInfo["db"].(string)
  285. if db == "" {
  286. db = MgoB.DbName
  287. }
  288. if c := util.ObjToString(udpInfo["coll"]); c != "" {
  289. BiddingColl = c
  290. } else {
  291. BiddingColl = config.Conf.DB.MongoB.Coll
  292. }
  293. thread := util.IntAllDef(Thread, 1)
  294. if thread > 0 {
  295. p.thread = thread
  296. }
  297. //开始id和结束id
  298. q, _ := udpInfo["query"].(map[string]interface{})
  299. gtid := udpInfo["gtid"].(string)
  300. lteid := udpInfo["lteid"].(string)
  301. q = map[string]interface{}{
  302. "_id": map[string]interface{}{
  303. "$gt": mongodb.StringTOBsonId(gtid),
  304. "$lte": mongodb.StringTOBsonId(lteid),
  305. },
  306. }
  307. p.enter(db, BiddingColl, q)
  308. if udpInfo["stop"] == nil {
  309. for i := 0; i < 1; i++ {
  310. sp <- true
  311. }
  312. for i := 0; i < 1; i++ {
  313. <-sp
  314. }
  315. }
  316. log.Info("保存完成,生索引", zap.Int64("pici:", p.pici))
  317. time.Sleep(5 * time.Second)
  318. nextNode(udpInfo, p.pici)
  319. }
  320. // 招标字段更新
  321. func (p *ProjectTask) taskUpdateInfo(udpInfo map[string]interface{}) {
  322. defer util.Catch()
  323. infoid := udpInfo["infoid"].(string)
  324. infoMap, _ := MgoB.FindById(util.ObjToString(udpInfo["coll"]), infoid, nil)
  325. if (*infoMap)["modifyinfo"] == nil {
  326. log.Info("does not exist modifyinfo ---," + infoid)
  327. return
  328. }
  329. //client := Es.GetEsConn()
  330. //defer Es.DestoryEsConn(client)
  331. //esquery := `{"query": {"bool": {"must": [{"match": {"ids": "` + infoid + `"}}]}}}`
  332. //data := Es.Get(config.Conf.DB.Es.Index, esquery)
  333. data, _ := MgoP.FindOne(config.Conf.DB.MongoP.Coll, bson.M{"ids": infoid})
  334. if len(*data) > 0 {
  335. pid := mongodb.BsonIdToSId((*data)["_id"])
  336. p.updateJudge(*infoMap, pid)
  337. } else {
  338. log.Info("not find project---," + infoid)
  339. }
  340. }
  341. func (p *ProjectTask) taskUpdatePro(udpInfo map[string]interface{}) {
  342. defer util.Catch()
  343. pid := util.ObjToString(udpInfo["pid"])
  344. updateMap := util.ObjToMap(udpInfo["updateField"])
  345. if pid == "" || len(*updateMap) == 0 {
  346. log.Error("参数有误")
  347. return
  348. }
  349. proMap, _ := MgoP.FindById(ProjectColl, pid, nil)
  350. if len(*proMap) > 1 {
  351. (*proMap)["reason"] = "直接修改项目字段信息"
  352. backupPro(*proMap)
  353. delete(*proMap, "reason")
  354. updataMap := make(map[string]interface{})
  355. modifyInfo := make(map[string]interface{})
  356. for k, v := range *updateMap {
  357. if strings.Contains(k, "time") {
  358. updataMap[k] = util.Int64All(v)
  359. } else {
  360. updataMap[k] = v
  361. }
  362. modifyInfo[k] = true
  363. }
  364. updataMap["modifyinfo"] = modifyInfo
  365. bol := MgoP.UpdateById(ProjectColl, pid, map[string]interface{}{"$set": updataMap})
  366. if bol {
  367. //es索引
  368. by, _ := json.Marshal(map[string]interface{}{
  369. "query": map[string]interface{}{
  370. "_id": bson.M{
  371. "$gte": pid,
  372. "$lte": pid,
  373. }},
  374. "stype": "project",
  375. })
  376. _ = udpclient.WriteUdp(by, udp.OP_TYPE_DATA, toaddr[1])
  377. }
  378. // 内存
  379. var pro ProjectCache
  380. err := mapstructure.Decode(proMap, &pro)
  381. if err != nil {
  382. log.Error(err.Error())
  383. }
  384. p.AllIdsMapLock.Lock()
  385. if v, ok := p.AllIdsMap[pid]; ok {
  386. v.P = &pro
  387. }
  388. p.AllIdsMapLock.Unlock()
  389. } else {
  390. log.Info("Not find project---" + pid)
  391. }
  392. }
  393. func (p *ProjectTask) delInfoPro(udpInfo map[string]interface{}) {
  394. defer util.Catch()
  395. infoid := util.ObjToString(udpInfo["infoid"])
  396. if infoid == "" {
  397. return
  398. }
  399. //client := Es.GetEsConn()
  400. //defer Es.DestoryEsConn(client)
  401. //esquery := `{"query": {"bool": {"must": [{"term": {"ids": "` + infoid + `"}}]}}}`
  402. //data := Es.Get(config.Conf.DB.Es.Index, esquery)
  403. data, _ := MgoP.FindOne(config.Conf.DB.MongoP.Coll, bson.M{"ids": infoid})
  404. if len(*data) > 0 {
  405. pid := mongodb.BsonIdToSId((*data)["_id"])
  406. p.delJudge(infoid, pid)
  407. } else {
  408. log.Info("not find project---," + infoid)
  409. }
  410. }
  411. // 通知下个节点nextNode
  412. func nextNode(mapInfo map[string]interface{}, pici int64) {
  413. mapInfo["stype"] = "project"
  414. mapInfo["query"] = map[string]interface{}{
  415. "pici": pici,
  416. }
  417. key := fmt.Sprintf("%d-%s-%d", pici, "project", 0)
  418. mapInfo["key"] = key
  419. datas, _ := json.Marshal(mapInfo)
  420. node := &udpNode{datas, toaddr[0], time.Now().Unix(), 0}
  421. udptaskmap.Store(key, node)
  422. _ = udpclient.WriteUdp(datas, udp.OP_TYPE_DATA, toaddr[0])
  423. }
  424. func (p *ProjectTask) enter(db, coll string, q map[string]interface{}) {
  425. defer util.Catch()
  426. defer func() {
  427. p.Brun = false
  428. }()
  429. p.Brun = true
  430. count := 0
  431. countRepeat := 0
  432. pool := make(chan bool, p.thread)
  433. log.Info("start project", zap.Any("q:", q), zap.String("coll:", coll), zap.Int64("pici", p.pici))
  434. sess := MgoB.GetMgoConn()
  435. defer MgoB.DestoryMongoConn(sess)
  436. infoPool := make(chan map[string]interface{}, 2000)
  437. over := make(chan bool)
  438. go func() {
  439. L:
  440. for {
  441. select {
  442. case tmp := <-infoPool:
  443. pool <- true
  444. go func(tmp map[string]interface{}) {
  445. defer func() {
  446. <-pool
  447. }()
  448. p.fillInPlace(tmp)
  449. info := ParseInfo(tmp)
  450. p.currentTime = info.Publishtime
  451. //普通合并
  452. p.CommonMerge(tmp, info)
  453. }(tmp)
  454. default:
  455. select {
  456. case tmp := <-infoPool:
  457. pool <- true
  458. go func(tmp map[string]interface{}) {
  459. defer func() {
  460. <-pool
  461. }()
  462. p.fillInPlace(tmp)
  463. info := ParseInfo(tmp)
  464. p.currentTime = info.Publishtime
  465. //普通合并
  466. p.CommonMerge(tmp, info)
  467. }(tmp)
  468. case <-over:
  469. break L
  470. }
  471. }
  472. }
  473. }()
  474. //fields := map[string]interface{}{"kvtext": 0, "repeat_reason": 0, "field_source": 0}
  475. fields := map[string]interface{}{"detail": 0, "contenthtml": 0, "jsondata": 0, "regions_log": 0, "field_source": 0}
  476. if p.currentType == "project" || p.currentType == "project_history" {
  477. c, _ := sess.DB(db).C(coll).Find(q).Count()
  478. log.Info(fmt.Sprintf("共查询: %d条", c))
  479. }
  480. ms := sess.DB(db).C(coll).Find(q).Select(fields)
  481. query := ms.Iter()
  482. var lastid interface{}
  483. L:
  484. for {
  485. select {
  486. case <-queryClose:
  487. log.Info("receive interrupt sign")
  488. log.Info("close iter..", zap.Any("lastid:", lastid), zap.Any("err:", query.Cursor.Close(nil)))
  489. queryCloseOver <- true
  490. break L
  491. default:
  492. tmp := make(map[string]interface{})
  493. if query.Next(&tmp) {
  494. lastid = tmp["_id"]
  495. if P_QL.currentType == "ql" {
  496. if count%50000 == 0 {
  497. log.Info("current---", zap.Int("count", count), zap.String("lastid", mongodb.BsonIdToSId(lastid)))
  498. }
  499. } else {
  500. if count%2000 == 0 {
  501. log.Info("current---", zap.Int("count", count), zap.String("lastid", mongodb.BsonIdToSId(lastid)))
  502. }
  503. }
  504. // 判重过滤 中标记录
  505. if !siteJudge(util.ObjToString(tmp["spidercode"])) {
  506. //extracttype -1: 重复,1: 不重复
  507. if util.IntAll(tmp["extracttype"]) == 1 {
  508. if util.ObjToString(tmp["toptype"]) != "采购意向" && util.ObjToString(tmp["toptype"]) != "产权" && util.ObjToString(tmp["toptype"]) != "拟建" {
  509. if P_QL.currentType == "ql" {
  510. infoPool <- tmp
  511. } else if P_QL.currentType == "project" && util.IntAll(tmp["dataging"]) == 0 {
  512. // id段增量数据
  513. infoPool <- tmp
  514. } else if P_QL.currentType == "project_history" && tmp["history_updatetime"] != nil {
  515. // id段 历史数据
  516. infoPool <- tmp
  517. }
  518. }
  519. } else {
  520. countRepeat++
  521. //if P_QL.currentType == "project" {
  522. // log.Info("repeat err---", tmp["_id"])
  523. //}
  524. }
  525. count++
  526. }
  527. } else {
  528. break L
  529. }
  530. }
  531. }
  532. time.Sleep(5 * time.Second)
  533. over <- true
  534. //阻塞
  535. for n := 0; n < p.thread; n++ {
  536. pool <- true
  537. }
  538. log.Info("所有线程执行完成...", zap.Int("count:", count), zap.Int("countRepeat", countRepeat))
  539. }
  540. func (p *ProjectTask) CommonMerge(tmp map[string]interface{}, info *Info) {
  541. if info != nil && !((info.pnbval == 1 && info.Buyer != "") || info.pnbval == 0) {
  542. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  543. proHref := util.ObjToString(jsonData["projecthref"]) // 网站本身发布的公告具有招投标流程,直接参与合并
  544. if jsonData != nil && proHref != "" {
  545. //projectHref字段合并
  546. tmp["projecthref"] = proHref
  547. p.mapHrefLock.Lock()
  548. pid := p.mapHref[proHref]
  549. p.mapHrefLock.Unlock()
  550. if pid != "" {
  551. p.AllIdsMapLock.Lock()
  552. res := p.AllIdsMap[pid]
  553. p.AllIdsMapLock.Unlock()
  554. if res != nil {
  555. comparePro := res.P
  556. _, ex := p.CompareStatus(comparePro, info)
  557. p.UpdateProject(tmp, info, comparePro, -1, "AAAAAAAAAA", ex)
  558. } else {
  559. p.startProjectMerge(info, tmp)
  560. }
  561. } else {
  562. id, p1 := p.NewProject(tmp, info)
  563. p.mapHrefLock.Lock()
  564. p.mapHref[proHref] = id
  565. p.mapHrefLock.Unlock()
  566. p.AllIdsMapLock.Lock()
  567. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  568. p.AllIdsMapLock.Unlock()
  569. }
  570. } else {
  571. //项目合并
  572. p.startProjectMerge(info, tmp)
  573. }
  574. } else {
  575. //项目合并
  576. p.startProjectMerge(info, tmp)
  577. }
  578. } else {
  579. }
  580. }
  581. func ParseInfo(tmp map[string]interface{}) (info *Info) {
  582. bys, _ := json.Marshal(tmp)
  583. var thisinfo *Info
  584. _ = json.Unmarshal(bys, &thisinfo)
  585. if thisinfo == nil {
  586. return nil
  587. }
  588. // 处理publishtime为空
  589. if thisinfo.Publishtime <= 0 {
  590. for _, d := range DateTimeSelect {
  591. if tmp[d] != nil {
  592. thisinfo.Publishtime = util.Int64All(tmp[d])
  593. tmp["publishtime"] = tmp[d]
  594. break
  595. }
  596. }
  597. }
  598. if len(thisinfo.Topscopeclass) == 0 {
  599. thisinfo.Topscopeclass = []string{}
  600. }
  601. if len(thisinfo.Subscopeclass) == 0 {
  602. thisinfo.Subscopeclass = []string{}
  603. }
  604. if thisinfo.SubType == "" {
  605. thisinfo.SubType = util.ObjToString(tmp["bidstatus"])
  606. }
  607. //从标题中查找项目编号
  608. res := titleGetPc.FindStringSubmatch(thisinfo.Title)
  609. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  610. thisinfo.PTC = res[1]
  611. } else {
  612. res = titleGetPc1.FindStringSubmatch(thisinfo.Title)
  613. if len(res) > 3 && len(res[3]) > 6 && thisinfo.ProjectCode != res[3] && !numCheckPc.MatchString(res[3]) && !_zimureg1.MatchString(res[3]) {
  614. thisinfo.PTC = res[3]
  615. } else {
  616. res = titleGetPc2.FindStringSubmatch(thisinfo.Title)
  617. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  618. thisinfo.PTC = res[1]
  619. }
  620. }
  621. }
  622. if thisinfo.ProjectName != "" && len([]rune(thisinfo.ProjectName)) > 0 {
  623. //thisinfo.ProjectName = pcReplace.ReplaceAllString(thisinfo.ProjectName, "")
  624. //if thisinfo.ProjectName != "" {
  625. thisinfo.pnbval++
  626. //}
  627. }
  628. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  629. if thisinfo.ProjectCode != "" {
  630. //thisinfo.ProjectCode = pcReplace.ReplaceAllString(thisinfo.ProjectCode, "")
  631. if thisinfo.pnbval == 0 && len([]rune(thisinfo.ProjectCode)) < 5 {
  632. thisinfo.ProjectCode = StrOrNum.ReplaceAllString(thisinfo.ProjectCode, "")
  633. }
  634. } else {
  635. //thisinfo.PTC = pcReplace.ReplaceAllString(thisinfo.PTC, "")
  636. if thisinfo.pnbval == 0 && len([]rune(thisinfo.PTC)) < 5 {
  637. thisinfo.PTC = StrOrNum.ReplaceAllString(thisinfo.PTC, "")
  638. }
  639. }
  640. thisinfo.pnbval++
  641. }
  642. if thisinfo.ProjectCode == thisinfo.PTC || strings.Index(thisinfo.ProjectCode, thisinfo.PTC) > -1 {
  643. thisinfo.PTC = ""
  644. }
  645. if thisinfo.Buyer != "" && len([]rune(thisinfo.Buyer)) > 2 {
  646. thisinfo.pnbval++
  647. } else {
  648. thisinfo.Buyer = ""
  649. }
  650. //清理评审专家名单
  651. if thisinfo.ReviewExperts != "" {
  652. thisinfo.ReviewExperts = ClearRp(thisinfo.ReviewExperts)
  653. }
  654. //winners整理、清理
  655. winner := QyFilter(util.ObjToString(tmp["winner"]), "winner")
  656. tmp["winner"] = winner
  657. m1 := map[string]bool{}
  658. winners := []string{}
  659. if winner != "" {
  660. m1[winner] = true
  661. winners = append(winners, winner)
  662. }
  663. packageM, _ := tmp["package"].(map[string]interface{})
  664. if packageM != nil {
  665. thisinfo.HasPackage = true
  666. for _, p := range packageM {
  667. pm, _ := p.(map[string]interface{})
  668. pw := QyFilter(util.ObjToString(pm["winner"]), "winner")
  669. if pw != "" && !m1[pw] {
  670. m1[pw] = true
  671. winners = append(winners, pw)
  672. }
  673. }
  674. }
  675. thisinfo.Winners = winners
  676. //清理winnerorder
  677. var wins []map[string]interface{}
  678. for _, v := range thisinfo.WinnerOrder {
  679. w := QyFilter(util.ObjToString(v["entname"]), "winner")
  680. if w != "" {
  681. v["entname"] = w
  682. wins = append(wins, v)
  683. }
  684. }
  685. thisinfo.WinnerOrder = wins
  686. //清理buyer
  687. buyer := QyFilter(util.ObjToString(tmp["buyer"]), "buyer")
  688. tmp["buyer"] = buyer
  689. thisinfo.Buyer = buyer
  690. thisinfo.LenPC = len([]rune(thisinfo.ProjectCode))
  691. thisinfo.LenPTC = len([]rune(thisinfo.PTC))
  692. thisinfo.LenPN = len([]rune(thisinfo.ProjectName))
  693. //处理分包中数据异常问题
  694. for k, tmp := range thisinfo.Package {
  695. if ps, ok := tmp.([]map[string]interface{}); ok {
  696. for i, p := range ps {
  697. name, _ := p["name"].(string)
  698. if len([]rune(name)) > 100 {
  699. p["name"] = fmt.Sprint([]rune(name[:100]))
  700. }
  701. ps[i] = p
  702. }
  703. thisinfo.Package[k] = ps
  704. }
  705. }
  706. return thisinfo
  707. }
  708. func (p *ProjectTask) updateJudge(infoMap map[string]interface{}, pid string) {
  709. tmpPro, _ := MgoP.FindById(ProjectColl, pid, nil)
  710. modifyProMap := make(map[string]interface{}) // 修改项目的字段
  711. if modifyMap, ok := infoMap["modifyinfo"].(map[string]interface{}); ok {
  712. for k := range modifyMap {
  713. if modifyMap[k] != nil && modifyMap[k] != "" && (*tmpPro)[k] != nil {
  714. modifyProMap[k] = infoMap[k]
  715. }
  716. }
  717. }
  718. if len(modifyProMap) == 0 {
  719. log.Info("修改招标公告信息不需要修改项目信息字段", zap.Any("id", infoMap["_id"]))
  720. return
  721. }
  722. p.AllIdsMapLock.Lock()
  723. _, ok := p.AllIdsMap[pid]
  724. p.AllIdsMapLock.Unlock()
  725. ids := (*tmpPro)["ids"].([]interface{})
  726. index, position := -1, 0 // index 0:第一个,1:中间,2:最后一个 position list中位置
  727. for i, v := range ids {
  728. if util.ObjToString(v) == mongodb.BsonIdToSId(infoMap["_id"]) {
  729. position = i
  730. if i == 0 {
  731. index = 0
  732. } else if i == len(ids)-1 {
  733. index = 2
  734. } else {
  735. index = 1
  736. }
  737. }
  738. }
  739. if ok {
  740. // 周期内
  741. //projecthref字段
  742. if infoMap["jsondata"] != nil {
  743. jsonData := infoMap["jsondata"].(map[string]interface{})
  744. if proHref, ok := jsonData["projecthref"].(string); ok {
  745. p.mapHrefLock.Lock()
  746. tempId := p.mapHref[proHref]
  747. p.mapHrefLock.Unlock()
  748. if pid == tempId {
  749. p.modifyUpdate(pid, index, position, *tmpPro, modifyProMap)
  750. } else {
  751. log.Info("projecthref data id err---pid=" + pid + "---" + tempId)
  752. }
  753. } else {
  754. f := modifyEle(modifyProMap)
  755. if f {
  756. //合并、修改
  757. log.Info("合并修改更新" + "----------------------------")
  758. p.mergeAndModify(pid, index, position, infoMap, *tmpPro, modifyProMap)
  759. } else {
  760. //修改
  761. log.Info("修改更新" + "----------------------------")
  762. p.modifyUpdate(pid, index, position, *tmpPro, modifyProMap)
  763. }
  764. }
  765. } else {
  766. f := modifyEle(modifyProMap)
  767. if f {
  768. //合并、修改
  769. log.Info("合并修改更新" + "----------------------------")
  770. p.mergeAndModify(pid, index, position, infoMap, *tmpPro, modifyProMap)
  771. } else {
  772. //修改
  773. log.Info("修改更新" + "----------------------------")
  774. p.modifyUpdate(pid, index, position, *tmpPro, modifyProMap)
  775. }
  776. }
  777. } else {
  778. // 周期外
  779. log.Info("周期外数据直接修改" + "----------------------------")
  780. p.modifyUpdate(pid, index, position, *tmpPro, modifyProMap)
  781. }
  782. }
  783. var Elements = []string{
  784. "projectname",
  785. "projectcode",
  786. "buyer",
  787. "agency",
  788. "area",
  789. "city",
  790. "publishtime",
  791. "toptype",
  792. "subtype",
  793. }
  794. /*
  795. *
  796. 修改的字段
  797. 修改的字段是否是影响合并流程的要素字段
  798. */
  799. func modifyEle(tmp map[string]interface{}) bool {
  800. merge := false
  801. for _, str := range Elements {
  802. if tmp[str] != nil {
  803. merge = true
  804. break
  805. }
  806. }
  807. return merge
  808. }
  809. // 补全位置信息
  810. func (p *ProjectTask) fillInPlace(tmp map[string]interface{}) {
  811. area := util.ObjToString(tmp["area"])
  812. city := util.ObjToString(tmp["city"])
  813. if area != "" && city != "" {
  814. return
  815. }
  816. tmpSite := util.ObjToString(tmp["site"])
  817. if tmpSite == "" {
  818. return
  819. }
  820. p.mapSiteLock.Lock()
  821. defer p.mapSiteLock.Unlock()
  822. site := p.mapSite[tmpSite]
  823. if site != nil {
  824. if area != "" {
  825. if area == "全国" {
  826. tmp["area"] = site.Area
  827. tmp["city"] = site.City
  828. tmp["district"] = site.District
  829. return
  830. }
  831. if area != site.Area {
  832. return
  833. } else {
  834. if site.City != "" {
  835. tmp["area"] = site.Area
  836. tmp["city"] = site.City
  837. tmp["district"] = site.District
  838. }
  839. }
  840. } else {
  841. tmp["area"] = site.Area
  842. tmp["city"] = site.City
  843. tmp["district"] = site.District
  844. return
  845. }
  846. }
  847. }
  848. // 从数组中删除元素
  849. func deleteSlice(arr []string, v, stype string) []string {
  850. for k, v1 := range arr {
  851. if v1 == v {
  852. arr = append(arr[:k], arr[k+1:]...)
  853. return arr
  854. }
  855. }
  856. return arr
  857. }
  858. // 校验评审专家
  859. func ClearRp(tmp string) string {
  860. arrTmp := []string{}
  861. for _, v := range strings.Split(tmp, ",") {
  862. // 汉字过滤(全汉字,2-4个字)
  863. if ok, _ := regexp.MatchString("^[\\p{Han}]{2,4}$", v); !ok {
  864. continue
  865. }
  866. //黑名单过滤
  867. if BlaskListMap[v] {
  868. continue
  869. }
  870. arrTmp = append(arrTmp, v)
  871. }
  872. return strings.Join(arrTmp, ",")
  873. }
  874. func QyFilter(name, stype string) string {
  875. name = strings.ReplaceAll(name, " ", "")
  876. //preReg := PreRegexp[stype]
  877. //for _, v := range preReg {
  878. // name = v.ReplaceAllString(name, "")
  879. //}
  880. //backReg := BackRegexp[stype]
  881. //for _, v := range backReg {
  882. // name = v.ReplaceAllString(name, "")
  883. //}
  884. //backRepReg := BackRepRegexp[stype]
  885. //for _, v := range backRepReg {
  886. // name = v.regs.ReplaceAllString(name, v.repstr)
  887. //}
  888. blackReg := BlackRegexp[stype]
  889. for _, v := range blackReg {
  890. if v.MatchString(name) {
  891. name = ""
  892. break
  893. }
  894. }
  895. if !regexp.MustCompile("[\\p{Han}]{4,}").MatchString(name) {
  896. name = ""
  897. }
  898. if utf8.RuneCountInString(name) > 60 {
  899. name = ""
  900. }
  901. return name
  902. }
  903. // @Description 过滤中标记录数据
  904. // @Author J 2023/6/12 15:11
  905. func siteJudge(code string) bool {
  906. for _, s := range SkipSiteList {
  907. if code == s {
  908. return true
  909. }
  910. }
  911. return false
  912. }