task.go 18 KB

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