task.go 25 KB

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