task.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  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": [{"term": {"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 := 0
  439. countRepeat := 0
  440. pool := make(chan bool, p.thread)
  441. log.Println("start project", q, p.pici)
  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. go func(tmp map[string]interface{}) {
  453. defer func() {
  454. <-pool
  455. }()
  456. if util.IntAll(tmp["repeat"]) == 0 {
  457. if P_QL.currentType == "project" && util.IntAll(tmp["dataging"]) == 1 {
  458. //增量 dataging为1不参与合并
  459. util.Debug("增量 dataging == 1 ", tmp["_id"])
  460. return
  461. }
  462. p.fillInPlace(tmp)
  463. info := ParseInfo(tmp)
  464. p.currentTime = info.Publishtime
  465. //普通合并
  466. p.CommonMerge(tmp, info)
  467. } else {
  468. //信息错误,进行更新
  469. p.mapBidLock.Lock()
  470. countRepeat++
  471. p.mapBidLock.Unlock()
  472. }
  473. }(tmp)
  474. case <-over:
  475. break L
  476. }
  477. }
  478. }()
  479. //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,
  480. // "projectcode": 1, "buyerclass": 1, "winner": 1, "buyer": 1, "buyerperson": 1, "buyertel": 1, "infoformat": 1, "toptype": 1, "subtype": 1, "spidercode": 1, "projectscope": 1, "contractcode": 1,
  481. // "site": 1, "topscopeclass": 1, "subscopeclass": 1, "bidamount": 1, "budget": 1, "agency": 1, "package": 1, "jsondata": 1, "review_experts": 1, "purchasing": 1, "winnerorder": 1}
  482. fields := map[string]interface{}{"kvtext": 0, "repeat_reason": 0}
  483. if p.currentType == "project" {
  484. c, _ := sess.DB(db).C(coll).Find(q).Count()
  485. util.Debug("共查询:", c, "条")
  486. }
  487. ms := sess.DB(db).C(coll).Find(q).Select(fields).Sort("publishtime")
  488. if Sysconfig["hints"] != nil {
  489. ms.Hint(Sysconfig["hints"])
  490. }
  491. query := ms.Iter()
  492. var lastid interface{}
  493. L:
  494. for {
  495. select {
  496. case <-queryClose:
  497. log.Println("receive interrupt sign")
  498. log.Println("close iter..", lastid, query.Cursor.Close(nil))
  499. queryCloseOver <- true
  500. break L
  501. default:
  502. tmp := make(map[string]interface{})
  503. if query.Next(&tmp) {
  504. lastid = tmp["_id"]
  505. if count%10000 == 0 {
  506. log.Println("current", count, lastid)
  507. }
  508. infoPool <- tmp
  509. count++
  510. } else {
  511. break L
  512. }
  513. }
  514. }
  515. time.Sleep(5 * time.Second)
  516. over <- true
  517. //阻塞
  518. for n := 0; n < p.thread; n++ {
  519. pool <- true
  520. }
  521. log.Println("所有线程执行完成...", count, countRepeat)
  522. }
  523. func (p *ProjectTask) CommonMerge(tmp map[string]interface{}, info *Info) {
  524. if info != nil && !((info.pnbval == 1 && info.Buyer != "") || info.pnbval == 0) {
  525. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  526. proHref := util.ObjToString(jsonData["projecthref"])
  527. if jsonData != nil && proHref != "" {
  528. //projectHref字段合并
  529. tmp["projecthref"] = proHref
  530. p.mapHrefLock.Lock()
  531. pid := p.mapHref[proHref]
  532. p.mapHrefLock.Unlock()
  533. if pid != "" {
  534. p.AllIdsMapLock.Lock()
  535. comparePro := p.AllIdsMap[pid].P
  536. p.AllIdsMapLock.Unlock()
  537. _, ex := p.CompareStatus(comparePro, info)
  538. p.UpdateProject(tmp, info, comparePro, -1, "AAAAAAAAAA", ex)
  539. } else {
  540. id, p1 := p.NewProject(tmp, info)
  541. p.mapHrefLock.Lock()
  542. p.mapHref[proHref] = id
  543. p.mapHrefLock.Unlock()
  544. p.AllIdsMapLock.Lock()
  545. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  546. p.AllIdsMapLock.Unlock()
  547. }
  548. } else {
  549. //项目合并
  550. p.startProjectMerge(info, tmp)
  551. }
  552. } else {
  553. //项目合并
  554. p.startProjectMerge(info, tmp)
  555. }
  556. }
  557. }
  558. func ParseInfo(tmp map[string]interface{}) (info *Info) {
  559. bys, _ := json.Marshal(tmp)
  560. var thisinfo *Info
  561. _ = json.Unmarshal(bys, &thisinfo)
  562. if thisinfo == nil {
  563. return nil
  564. }
  565. if len(thisinfo.Topscopeclass) == 0 {
  566. thisinfo.Topscopeclass = []string{}
  567. }
  568. if len(thisinfo.Subscopeclass) == 0 {
  569. thisinfo.Subscopeclass = []string{}
  570. }
  571. if thisinfo.SubType == "" {
  572. thisinfo.SubType = util.ObjToString(tmp["bidstatus"])
  573. }
  574. //从标题中查找项目编号
  575. res := titleGetPc.FindStringSubmatch(thisinfo.Title)
  576. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  577. thisinfo.PTC = res[1]
  578. } else {
  579. res = titleGetPc1.FindStringSubmatch(thisinfo.Title)
  580. if len(res) > 3 && len(res[3]) > 6 && thisinfo.ProjectCode != res[3] && !numCheckPc.MatchString(res[3]) && !_zimureg1.MatchString(res[3]) {
  581. thisinfo.PTC = res[3]
  582. } else {
  583. res = titleGetPc2.FindStringSubmatch(thisinfo.Title)
  584. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  585. thisinfo.PTC = res[1]
  586. }
  587. }
  588. }
  589. if thisinfo.ProjectName != "" && len([]rune(thisinfo.ProjectName)) > 0 {
  590. thisinfo.ProjectName = pcReplace.ReplaceAllString(thisinfo.ProjectName, "")
  591. if thisinfo.ProjectName != "" {
  592. thisinfo.pnbval++
  593. }
  594. }
  595. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  596. if thisinfo.ProjectCode != "" {
  597. thisinfo.ProjectCode = pcReplace.ReplaceAllString(thisinfo.ProjectCode, "")
  598. if thisinfo.pnbval == 0 && len([]rune(thisinfo.ProjectCode)) < 5 {
  599. thisinfo.ProjectCode = StrOrNum.ReplaceAllString(thisinfo.ProjectCode, "")
  600. }
  601. } else {
  602. thisinfo.PTC = pcReplace.ReplaceAllString(thisinfo.PTC, "")
  603. if thisinfo.pnbval == 0 && len([]rune(thisinfo.PTC)) < 5 {
  604. thisinfo.PTC = StrOrNum.ReplaceAllString(thisinfo.PTC, "")
  605. }
  606. }
  607. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  608. thisinfo.pnbval++
  609. }
  610. }
  611. if thisinfo.ProjectCode == thisinfo.PTC || strings.Index(thisinfo.ProjectCode, thisinfo.PTC) > -1 {
  612. thisinfo.PTC = ""
  613. }
  614. if thisinfo.Buyer != "" && len([]rune(thisinfo.Buyer)) > 2 {
  615. thisinfo.pnbval++
  616. } else {
  617. thisinfo.Buyer = ""
  618. }
  619. //清理评审专家名单
  620. if len(thisinfo.ReviewExperts) > 0 {
  621. thisinfo.ReviewExperts = ClearRp(thisinfo.ReviewExperts)
  622. }
  623. //winners整理、清理
  624. winner := QyFilter(util.ObjToString(tmp["winner"]), "winner")
  625. tmp["winner"] = winner
  626. m1 := map[string]bool{}
  627. winners := []string{}
  628. if winner != "" {
  629. m1[winner] = true
  630. winners = append(winners, winner)
  631. }
  632. packageM, _ := tmp["package"].(map[string]interface{})
  633. if packageM != nil {
  634. thisinfo.HasPackage = true
  635. for _, p := range packageM {
  636. pm, _ := p.(map[string]interface{})
  637. pw := QyFilter(util.ObjToString(pm["winner"]), "winner")
  638. if pw != "" && !m1[pw] {
  639. m1[pw] = true
  640. winners = append(winners, pw)
  641. }
  642. }
  643. }
  644. thisinfo.Winners = winners
  645. //清理winnerorder
  646. var wins []map[string]interface{}
  647. for _, v := range thisinfo.WinnerOrder {
  648. w := QyFilter(util.ObjToString(v["entname"]), "winner")
  649. if w != "" {
  650. v["entname"] = w
  651. wins = append(wins, v)
  652. }
  653. }
  654. thisinfo.WinnerOrder = wins
  655. //清理buyer
  656. buyer := QyFilter(util.ObjToString(tmp["buyer"]), "buyer")
  657. tmp["buyer"] = buyer
  658. thisinfo.Buyer = buyer
  659. thisinfo.LenPC = len([]rune(thisinfo.ProjectCode))
  660. thisinfo.LenPTC = len([]rune(thisinfo.PTC))
  661. thisinfo.LenPN = len([]rune(thisinfo.ProjectName))
  662. //处理分包中数据异常问题
  663. for k, tmp := range thisinfo.Package {
  664. if ps, ok := tmp.([]map[string]interface{}); ok {
  665. for i, p := range ps {
  666. name, _ := p["name"].(string)
  667. if len([]rune(name)) > 100 {
  668. p["name"] = fmt.Sprint([]rune(name[:100]))
  669. }
  670. ps[i] = p
  671. }
  672. thisinfo.Package[k] = ps
  673. }
  674. }
  675. return thisinfo
  676. }
  677. func (p *ProjectTask) updateJudge(infoMap map[string]interface{}, pid string) {
  678. tmpPro := MongoTool.FindById(ProjectColl, pid)
  679. modifyProMap := make(map[string]interface{}) // 修改项目的字段
  680. if modifyMap, ok := infoMap["modifyinfo"].(map[string]interface{}); ok {
  681. for k := range modifyMap {
  682. if modifyMap[k] != nil && modifyMap[k] != "" && tmpPro[k] != nil {
  683. modifyProMap[k] = infoMap[k]
  684. }
  685. }
  686. }
  687. if len(modifyProMap) == 0 {
  688. util.Debug("修改招标公告信息不需要修改项目信息字段", infoMap["_id"])
  689. return
  690. }
  691. p.AllIdsMapLock.Lock()
  692. _, ok := p.AllIdsMap[pid]
  693. p.AllIdsMapLock.Unlock()
  694. ids := []interface{}(tmpPro["ids"].(primitive.A))
  695. index, position := -1, 0 // index 0:第一个,1:中间,2:最后一个 position list中位置
  696. for i, v := range ids {
  697. if util.ObjToString(v) == mongodb.BsonIdToSId(infoMap["_id"]) {
  698. position = i
  699. if i == 0 {
  700. index = 0
  701. }else if i == len(ids) - 1 {
  702. index = 2
  703. }else {
  704. index = 1
  705. }
  706. }
  707. }
  708. if ok {
  709. // 周期内
  710. //projecthref字段
  711. if infoMap["jsondata"] != nil {
  712. jsonData := infoMap["jsondata"].(map[string]interface{})
  713. if proHref, ok := jsonData["projecthref"].(string); ok {
  714. p.mapHrefLock.Lock()
  715. tempId := p.mapHref[proHref]
  716. p.mapHrefLock.Unlock()
  717. if pid == tempId {
  718. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  719. }else {
  720. util.Debug("projecthref data id err---pid=" + pid, "---"+tempId)
  721. }
  722. }else {
  723. f := modifyEle(modifyProMap)
  724. if f {
  725. //合并、修改
  726. util.Debug("合并修改更新", "----------------------------")
  727. p.mergeAndModify(pid, index, position, infoMap, tmpPro, modifyProMap)
  728. } else {
  729. //修改
  730. util.Debug("修改更新", "----------------------------")
  731. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  732. }
  733. }
  734. }else {
  735. f := modifyEle(modifyProMap)
  736. if f {
  737. //合并、修改
  738. util.Debug("合并修改更新", "----------------------------")
  739. p.mergeAndModify(pid, index, position, infoMap, tmpPro, modifyProMap)
  740. } else {
  741. //修改
  742. util.Debug("修改更新", "----------------------------")
  743. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  744. }
  745. }
  746. }else {
  747. // 周期外
  748. util.Debug("周期外数据直接修改", "----------------------------")
  749. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  750. }
  751. }
  752. var Elements = []string{
  753. "projectname",
  754. "projectcode",
  755. "buyer",
  756. "agency",
  757. "area",
  758. "city",
  759. "publishtime",
  760. "toptype",
  761. "subtype",
  762. }
  763. /**
  764. 修改的字段
  765. 修改的字段是否是影响合并流程的要素字段
  766. */
  767. func modifyEle(tmp map[string]interface{}) bool {
  768. merge := false
  769. for _, str := range Elements {
  770. if tmp[str] != nil {
  771. merge = true
  772. break
  773. }
  774. }
  775. return merge
  776. }
  777. //补全位置信息
  778. func (p *ProjectTask) fillInPlace(tmp map[string]interface{}) {
  779. area := util.ObjToString(tmp["area"])
  780. city := util.ObjToString(tmp["city"])
  781. if area != "" && city != "" {
  782. return
  783. }
  784. tmpSite := util.ObjToString(tmp["site"])
  785. if tmpSite == "" {
  786. return
  787. }
  788. p.mapSiteLock.Lock()
  789. defer p.mapSiteLock.Unlock()
  790. site := p.mapSite[tmpSite]
  791. if site != nil {
  792. if area != "" {
  793. if area == "全国" {
  794. tmp["area"] = site.Area
  795. tmp["city"] = site.City
  796. tmp["district"] = site.District
  797. return
  798. }
  799. if area != site.Area {
  800. return
  801. } else {
  802. if site.City != "" {
  803. tmp["area"] = site.Area
  804. tmp["city"] = site.City
  805. tmp["district"] = site.District
  806. }
  807. }
  808. } else {
  809. tmp["area"] = site.Area
  810. tmp["city"] = site.City
  811. tmp["district"] = site.District
  812. return
  813. }
  814. }
  815. }
  816. //从数组中删除元素
  817. func deleteSlice(arr []string, v, stype string) []string {
  818. for k, v1 := range arr {
  819. if v1 == v {
  820. ts := time.Now().Unix()
  821. arr = append(arr[:k], arr[k+1:]...)
  822. rt := time.Now().Unix() - ts
  823. if rt > 0 {
  824. log.Println("deleteSlice", stype, rt, v, len(arr))
  825. }
  826. return arr
  827. }
  828. }
  829. return arr
  830. }
  831. //校验评审专家
  832. func ClearRp(tmp []string) []string {
  833. arrTmp := []string{}
  834. for _, v := range tmp {
  835. // 汉字过滤(全汉字,2-4个字)
  836. if ok, _ := regexp.MatchString("^[\\p{Han}]{2,4}$", v); !ok {
  837. continue
  838. }
  839. //黑名单过滤
  840. if BlaskListMap[v] {
  841. continue
  842. }
  843. arrTmp = append(arrTmp, v)
  844. }
  845. return arrTmp
  846. }
  847. func QyFilter(name, stype string) string {
  848. name = strings.ReplaceAll(name, " ", "")
  849. preReg := PreRegexp[stype]
  850. for _, v := range preReg {
  851. name = v.ReplaceAllString(name, "")
  852. }
  853. backReg := BackRegexp[stype]
  854. for _, v := range backReg {
  855. name = v.ReplaceAllString(name, "")
  856. }
  857. backRepReg := BackRepRegexp[stype]
  858. for _, v := range backRepReg {
  859. name = v.regs.ReplaceAllString(name, v.repstr)
  860. }
  861. blackReg := BlackRegexp[stype]
  862. for _, v := range blackReg {
  863. if v.MatchString(name) {
  864. name = ""
  865. break
  866. }
  867. }
  868. if !regexp.MustCompile("[\\p{Han}]{4,}").MatchString(name) {
  869. name = ""
  870. }
  871. if utf8.RuneCountInString(name) > 60 {
  872. name = ""
  873. }
  874. return name
  875. }