task.go 19 KB

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