task.go 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  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, 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. 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. countRepeat++
  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. countRepeat++
  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. ms := sess.DB(db).C(coll).Find(q).Select(fields).Sort("publishtime")
  484. if Sysconfig["hints"] != nil {
  485. ms.Hint(Sysconfig["hints"])
  486. }
  487. query := ms.Iter()
  488. //query := sess.DB(db).C(coll).Find(q).Sort("publishtime").Iter()
  489. var lastid interface{}
  490. L:
  491. for {
  492. select {
  493. case <-queryClose:
  494. log.Println("receive interrupt sign")
  495. log.Println("close iter..", lastid, query.Cursor.Close(nil))
  496. queryCloseOver <- true
  497. break L
  498. default:
  499. tmp := make(map[string]interface{})
  500. if query.Next(&tmp) {
  501. lastid = tmp["_id"]
  502. if count%10000 == 0 {
  503. log.Println("current", count, lastid)
  504. }
  505. infoPool <- tmp
  506. count++
  507. } else {
  508. break L
  509. }
  510. }
  511. }
  512. time.Sleep(5 * time.Second)
  513. over <- true
  514. //阻塞
  515. for n := 0; n < p.thread; n++ {
  516. pool <- true
  517. }
  518. log.Println("所有线程执行完成...", count, taskcount, countRepeat)
  519. }
  520. func (p *ProjectTask) CommonMerge(tmp map[string]interface{}, info *Info) {
  521. if info != nil && !((info.pnbval == 1 && info.Buyer != "") || info.pnbval == 0) {
  522. if jsonData, ok := tmp["jsondata"].(map[string]interface{}); ok {
  523. proHref := util.ObjToString(jsonData["projecthref"])
  524. if jsonData != nil && proHref != "" {
  525. //projectHref字段合并
  526. tmp["projecthref"] = proHref
  527. p.mapHrefLock.Lock()
  528. pid := p.mapHref[proHref]
  529. p.mapHrefLock.Unlock()
  530. if pid != "" {
  531. p.AllIdsMapLock.Lock()
  532. comparePro := p.AllIdsMap[pid].P
  533. p.AllIdsMapLock.Unlock()
  534. _, ex := p.CompareStatus(comparePro, info)
  535. p.UpdateProject(tmp, info, comparePro, -1, "AAAAAAAAAA", ex)
  536. } else {
  537. id, p1 := p.NewProject(tmp, info)
  538. p.mapHrefLock.Lock()
  539. p.mapHref[proHref] = id
  540. p.mapHrefLock.Unlock()
  541. p.AllIdsMapLock.Lock()
  542. p.AllIdsMap[id] = &ID{Id: id, P: p1}
  543. p.AllIdsMapLock.Unlock()
  544. }
  545. } else {
  546. //项目合并
  547. p.startProjectMerge(info, tmp)
  548. }
  549. } else {
  550. //项目合并
  551. p.startProjectMerge(info, tmp)
  552. }
  553. }
  554. }
  555. func ParseInfo(tmp map[string]interface{}) (info *Info) {
  556. bys, _ := json.Marshal(tmp)
  557. var thisinfo *Info
  558. _ = json.Unmarshal(bys, &thisinfo)
  559. if thisinfo == nil {
  560. return nil
  561. }
  562. if len(thisinfo.Topscopeclass) == 0 {
  563. thisinfo.Topscopeclass = []string{}
  564. }
  565. if len(thisinfo.Subscopeclass) == 0 {
  566. thisinfo.Subscopeclass = []string{}
  567. }
  568. if thisinfo.SubType == "" {
  569. thisinfo.SubType = util.ObjToString(tmp["bidstatus"])
  570. }
  571. //从标题中查找项目编号
  572. res := titleGetPc.FindStringSubmatch(thisinfo.Title)
  573. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  574. thisinfo.PTC = res[1]
  575. } else {
  576. res = titleGetPc1.FindStringSubmatch(thisinfo.Title)
  577. if len(res) > 3 && len(res[3]) > 6 && thisinfo.ProjectCode != res[3] && !numCheckPc.MatchString(res[3]) && !_zimureg1.MatchString(res[3]) {
  578. thisinfo.PTC = res[3]
  579. } else {
  580. res = titleGetPc2.FindStringSubmatch(thisinfo.Title)
  581. if len(res) > 1 && len(res[1]) > 6 && thisinfo.ProjectCode != res[1] && !numCheckPc.MatchString(res[1]) && !_zimureg1.MatchString(res[1]) {
  582. thisinfo.PTC = res[1]
  583. }
  584. }
  585. }
  586. if thisinfo.ProjectName != "" && len([]rune(thisinfo.ProjectName)) > 0 {
  587. thisinfo.ProjectName = pcReplace.ReplaceAllString(thisinfo.ProjectName, "")
  588. if thisinfo.ProjectName != "" {
  589. thisinfo.pnbval++
  590. }
  591. }
  592. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  593. if thisinfo.ProjectCode != "" {
  594. thisinfo.ProjectCode = pcReplace.ReplaceAllString(thisinfo.ProjectCode, "")
  595. if thisinfo.pnbval == 0 && len([]rune(thisinfo.ProjectCode)) < 5 {
  596. thisinfo.ProjectCode = StrOrNum.ReplaceAllString(thisinfo.ProjectCode, "")
  597. }
  598. } else {
  599. thisinfo.PTC = pcReplace.ReplaceAllString(thisinfo.PTC, "")
  600. if thisinfo.pnbval == 0 && len([]rune(thisinfo.PTC)) < 5 {
  601. thisinfo.PTC = StrOrNum.ReplaceAllString(thisinfo.PTC, "")
  602. }
  603. }
  604. if thisinfo.ProjectCode != "" || thisinfo.PTC != "" {
  605. thisinfo.pnbval++
  606. }
  607. }
  608. if thisinfo.ProjectCode == thisinfo.PTC || strings.Index(thisinfo.ProjectCode, thisinfo.PTC) > -1 {
  609. thisinfo.PTC = ""
  610. }
  611. if thisinfo.Buyer != "" && len([]rune(thisinfo.Buyer)) > 2 {
  612. thisinfo.pnbval++
  613. } else {
  614. thisinfo.Buyer = ""
  615. }
  616. //清理评审专家名单
  617. if len(thisinfo.ReviewExperts) > 0 {
  618. thisinfo.ReviewExperts = ClearRp(thisinfo.ReviewExperts)
  619. }
  620. //winners整理、清理
  621. winner := QyFilter(util.ObjToString(tmp["winner"]), "winner")
  622. tmp["winner"] = winner
  623. m1 := map[string]bool{}
  624. winners := []string{}
  625. if winner != "" {
  626. m1[winner] = true
  627. winners = append(winners, winner)
  628. }
  629. packageM, _ := tmp["package"].(map[string]interface{})
  630. if packageM != nil {
  631. thisinfo.HasPackage = true
  632. for _, p := range packageM {
  633. pm, _ := p.(map[string]interface{})
  634. pw := QyFilter(util.ObjToString(pm["winner"]), "winner")
  635. if pw != "" && !m1[pw] {
  636. m1[pw] = true
  637. winners = append(winners, pw)
  638. }
  639. }
  640. }
  641. thisinfo.Winners = winners
  642. //清理winnerorder
  643. var wins []map[string]interface{}
  644. for _, v := range thisinfo.WinnerOrder {
  645. w := QyFilter(util.ObjToString(v["entname"]), "winner")
  646. if w != "" {
  647. v["entname"] = w
  648. wins = append(wins, v)
  649. }
  650. }
  651. thisinfo.WinnerOrder = wins
  652. //清理buyer
  653. buyer := QyFilter(util.ObjToString(tmp["buyer"]), "buyer")
  654. tmp["buyer"] = buyer
  655. thisinfo.Buyer = buyer
  656. thisinfo.LenPC = len([]rune(thisinfo.ProjectCode))
  657. thisinfo.LenPTC = len([]rune(thisinfo.PTC))
  658. thisinfo.LenPN = len([]rune(thisinfo.ProjectName))
  659. //处理分包中数据异常问题
  660. for k, tmp := range thisinfo.Package {
  661. if ps, ok := tmp.([]map[string]interface{}); ok {
  662. for i, p := range ps {
  663. name, _ := p["name"].(string)
  664. if len([]rune(name)) > 100 {
  665. p["name"] = fmt.Sprint([]rune(name[:100]))
  666. }
  667. ps[i] = p
  668. }
  669. thisinfo.Package[k] = ps
  670. }
  671. }
  672. return thisinfo
  673. }
  674. func (p *ProjectTask) updateJudge(infoMap map[string]interface{}, pid string) {
  675. tmpPro := MongoTool.FindById(ProjectColl, pid)
  676. modifyProMap := make(map[string]interface{}) // 修改项目的字段
  677. if modifyMap, ok := infoMap["modifyinfo"].(map[string]interface{}); ok {
  678. for k := range modifyMap {
  679. if modifyMap[k] != nil && modifyMap[k] != "" && tmpPro[k] != nil {
  680. modifyProMap[k] = infoMap[k]
  681. }
  682. }
  683. }
  684. if len(modifyProMap) == 0 {
  685. util.Debug("修改招标公告信息不需要修改项目信息字段", infoMap["_id"])
  686. return
  687. }
  688. p.AllIdsMapLock.Lock()
  689. _, ok := p.AllIdsMap[pid]
  690. p.AllIdsMapLock.Unlock()
  691. ids := []interface{}(tmpPro["ids"].(primitive.A))
  692. index, position := -1, 0 // index 0:第一个,1:中间,2:最后一个 position list中位置
  693. for i, v := range ids {
  694. if util.ObjToString(v) == mongodb.BsonIdToSId(infoMap["_id"]) {
  695. position = i
  696. if i == 0 {
  697. index = 0
  698. }else if i == len(ids) - 1 {
  699. index = 2
  700. }else {
  701. index = 1
  702. }
  703. }
  704. }
  705. if ok {
  706. // 周期内
  707. //projecthref字段
  708. if infoMap["jsondata"] != nil {
  709. jsonData := infoMap["jsondata"].(map[string]interface{})
  710. if proHref, ok := jsonData["projecthref"].(string); ok {
  711. p.mapHrefLock.Lock()
  712. tempId := p.mapHref[proHref]
  713. p.mapHrefLock.Unlock()
  714. if pid == tempId {
  715. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  716. }else {
  717. util.Debug("projecthref data id err---pid=" + pid, "---"+tempId)
  718. }
  719. }else {
  720. f := modifyEle(modifyProMap)
  721. if f {
  722. //合并、修改
  723. util.Debug("合并修改更新", "----------------------------")
  724. p.mergeAndModify(pid, index, position, infoMap, tmpPro, modifyProMap)
  725. } else {
  726. //修改
  727. util.Debug("修改更新", "----------------------------")
  728. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  729. }
  730. }
  731. }else {
  732. f := modifyEle(modifyProMap)
  733. if f {
  734. //合并、修改
  735. util.Debug("合并修改更新", "----------------------------")
  736. p.mergeAndModify(pid, index, position, infoMap, tmpPro, modifyProMap)
  737. } else {
  738. //修改
  739. util.Debug("修改更新", "----------------------------")
  740. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  741. }
  742. }
  743. }else {
  744. // 周期外
  745. util.Debug("周期外数据直接修改", "----------------------------")
  746. p.modifyUpdate(pid, index, position, tmpPro, modifyProMap)
  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. }