task.go 24 KB

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