task.go 24 KB

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