task.go 24 KB

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