task.go 26 KB

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