datamap.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. qutil "qfw/util"
  6. "reflect"
  7. "regexp"
  8. "strings"
  9. "sync"
  10. "time"
  11. )
  12. type Info struct {
  13. id string //id
  14. title string //标题
  15. area string //省份
  16. city string //城市
  17. subtype string //信息类型
  18. buyer string //采购单位
  19. agency string //代理机构
  20. winner string //中标单位
  21. budget float64 //预算金额
  22. bidamount float64 //中标金额
  23. projectname string //项目名称
  24. projectcode string //项目编号
  25. contractnumber string //合同编号
  26. publishtime int64 //发布时间
  27. comeintime int64 //入库时间
  28. bidopentime int64 //开标时间
  29. bidopenaddress string //开标地点
  30. site string //站点
  31. href string //正文的url
  32. repeatid string //重复id
  33. titleSpecialWord bool //标题特殊词
  34. specialWord bool //再次判断的特殊词
  35. mergemap map[string]interface{} //合并记录
  36. is_site bool //是否站点城市
  37. repeat_ids []string //记录所有重复id
  38. }
  39. var datelimit = float64(432000) //五天
  40. var sitelock sync.Mutex //锁
  41. //一般数据判重
  42. type datamap struct {
  43. lock sync.Mutex //锁
  44. days int //保留几天数据
  45. data map[string][]*Info
  46. keymap []string
  47. areakeys []string
  48. keys map[string]bool
  49. }
  50. //历史
  51. func TimedTaskDatamap(days int,lasttime int64,numIndex int) *datamap {
  52. datelimit = qutil.Float64All(days * 86400)
  53. dm := &datamap{sync.Mutex{}, days, map[string][]*Info{}, []string{}, []string{},map[string]bool{}}
  54. if lasttime <0 {
  55. log.Println("数据池空数据")
  56. return dm
  57. }
  58. start := int(time.Now().Unix())
  59. sess := data_mgo.GetMgoConn()
  60. defer data_mgo.DestoryMongoConn(sess)
  61. query := map[string]interface{}{"publishtime": map[string]interface{}{
  62. "$lt": lasttime,
  63. }}
  64. log.Println("query", query)
  65. it := sess.DB(data_mgo.DbName).C(extract_back).Find(query).Sort("-publishtime").Iter()
  66. n, continuSum := 0, 0
  67. for tmp := make(map[string]interface{}); it.Next(&tmp); n++ {
  68. if qutil.IntAll(tmp["repeat"]) == 1 || qutil.IntAll(tmp["repeat"]) == -1 ||
  69. qutil.IntAll(tmp["dataging"]) == 1 {
  70. } else {
  71. if fmt.Sprint(reflect.TypeOf(tmp["publishtime"]))=="string" {
  72. continue
  73. }
  74. pt := tmp["publishtime"]
  75. pt_time := qutil.Int64All(pt)
  76. if pt_time > time.Now().Unix() {
  77. continue
  78. }
  79. if qutil.Float64All(lasttime-pt_time) < datelimit {
  80. continuSum++
  81. info := NewInfo(tmp)
  82. dkey := qutil.FormatDateWithObj(&pt, qutil.Date_yyyyMMdd)
  83. k := fmt.Sprintf("%s_%s_%s", dkey, info.subtype, info.area)
  84. data := dm.data[k]
  85. if data == nil {
  86. data = []*Info{}
  87. }
  88. data = append(data, info)
  89. dm.data[k] = data
  90. dm.keys[dkey] = true
  91. //添加省
  92. isAreaExist :=false
  93. for _,v:= range dm.areakeys {
  94. if v==info.area {
  95. isAreaExist = true
  96. }
  97. }
  98. if !isAreaExist {
  99. areaArr := dm.areakeys
  100. areaArr = append(areaArr,info.area)
  101. dm.areakeys = areaArr
  102. }
  103. } else {
  104. break
  105. }
  106. }
  107. tmp = make(map[string]interface{})
  108. }
  109. log.Printf("第%d组:数据池构建完成:%d秒,%d个\n",numIndex ,int(time.Now().Unix())-start, n)
  110. return dm
  111. }
  112. //增量
  113. func NewDatamap(days int, lastid string) *datamap {
  114. datelimit = qutil.Float64All(days * 86400 * 2)
  115. dm := &datamap{sync.Mutex{}, days, map[string][]*Info{}, []string{},[]string{}, map[string]bool{}}
  116. if lastid == "" {
  117. log.Println("不构建数据池")
  118. return dm
  119. }
  120. //初始化加载数据
  121. sess := data_mgo.GetMgoConn()
  122. defer data_mgo.DestoryMongoConn(sess)
  123. query := map[string]interface{}{"_id": map[string]interface{}{
  124. "$lte": StringTOBsonId(lastid),
  125. }}
  126. log.Println("query", query)
  127. it := sess.DB(data_mgo.DbName).C(extract).Find(query).Sort("-publishtime").Iter()
  128. nowTime := time.Now().Unix()//当前时间的时间戳
  129. n, continuSum := 0, 0
  130. for tmp := make(map[string]interface{}); it.Next(&tmp); n++ {
  131. //source := util.ObjToMap(tmp["jsondata"]) //修复临时添加
  132. //if util.IntAll((*source)["sourcewebsite"]) == 1 {
  133. // continue
  134. //}
  135. if qutil.IntAll(tmp["repeat"]) == 1 || qutil.IntAll(tmp["repeat"]) == -1{
  136. } else {
  137. if fmt.Sprint(reflect.TypeOf(tmp["publishtime"]))=="string" {
  138. continue
  139. }
  140. pt:= tmp["publishtime"]
  141. pt_time := qutil.Int64All(pt)
  142. if pt_time > time.Now().Unix() {
  143. continue
  144. }
  145. if qutil.Float64All(nowTime-pt_time) <= datelimit {
  146. continuSum++
  147. info := NewInfo(tmp)
  148. dkey := qutil.FormatDateWithObj(&pt, qutil.Date_yyyyMMdd)
  149. k := fmt.Sprintf("%s_%s_%s", dkey, info.subtype, info.area)
  150. data := dm.data[k]
  151. if data == nil {
  152. data = []*Info{}
  153. }
  154. data = append(data, info)
  155. dm.data[k] = data
  156. dm.keys[dkey] = true
  157. //添加省
  158. isAreaExist :=false
  159. for _,v:= range dm.areakeys {
  160. if v==info.area {
  161. isAreaExist = true
  162. }
  163. }
  164. if !isAreaExist {
  165. areaArr := dm.areakeys
  166. areaArr = append(areaArr,info.area)
  167. dm.areakeys = areaArr
  168. }
  169. } else {
  170. break
  171. }
  172. }
  173. if n%10000 == 0 {
  174. log.Println("当前 n:", n,"数量:" ,continuSum,tmp["_id"])
  175. }
  176. tmp = make(map[string]interface{})
  177. }
  178. log.Println("load data:", n,"总数:",continuSum)
  179. return dm
  180. }
  181. //数据构建
  182. func NewInfo(tmp map[string]interface{}) *Info {
  183. subtype := qutil.ObjToString(tmp["subtype"])
  184. if subtype=="招标"||subtype=="邀标"||subtype=="询价"||
  185. subtype=="竞谈"||subtype=="竞价" {
  186. subtype = "招标"
  187. }
  188. area := qutil.ObjToString(tmp["area"])
  189. if area == "A" {
  190. area = "全国"
  191. }
  192. info := &Info{}
  193. info.id = BsonTOStringId(tmp["_id"])
  194. info.title = qutil.ObjToString(tmp["title"])
  195. info.area = area
  196. info.subtype = subtype
  197. info.buyer = qutil.ObjToString(tmp["buyer"])
  198. info.projectname = qutil.ObjToString(tmp["projectname"])
  199. info.contractnumber = qutil.ObjToString(tmp["contractnumber"])
  200. info.projectcode = qutil.ObjToString(tmp["projectcode"])
  201. info.city = qutil.ObjToString(tmp["city"])
  202. info.agency = qutil.ObjToString(tmp["agency"])
  203. info.winner = qutil.ObjToString(tmp["winner"])
  204. info.budget = qutil.Float64All(tmp["budget"])
  205. info.bidamount = qutil.Float64All(tmp["bidamount"])
  206. info.publishtime = qutil.Int64All(tmp["publishtime"])
  207. info.comeintime = qutil.Int64All(tmp["comeintime"])
  208. info.bidopentime = qutil.Int64All(tmp["bidopentime"])
  209. info.bidopenaddress = qutil.ObjToString(tmp["bidopenaddress"])
  210. info.site = qutil.ObjToString(tmp["site"])
  211. info.href = qutil.ObjToString(tmp["href"])
  212. info.repeatid = qutil.ObjToString(tmp["repeatid"])
  213. info.specialWord = FilterRegTitle.MatchString(info.title)
  214. info.titleSpecialWord = FilterRegTitle_0.MatchString(info.title) ||FilterRegTitle_1.MatchString(info.title) || FilterRegTitle_2.MatchString(info.title)
  215. info.mergemap = *qutil.ObjToMap(tmp["merge"])
  216. if info.mergemap == nil {
  217. info.mergemap = make(map[string]interface{}, 0)
  218. }
  219. if info.repeat_ids == nil {
  220. info.repeat_ids = make([]string, 0)
  221. }
  222. info.is_site = false
  223. return info
  224. }
  225. //判重方法
  226. //判重方法
  227. //判重方法
  228. func (d *datamap) check(info *Info) (b bool, source *Info, reasons string) {
  229. reason := ""
  230. isTestLog := false
  231. keys := []string{}
  232. d.lock.Lock()
  233. for k, _ := range d.keys { //不同时间段
  234. if info.area=="全国" {//匹配所有省
  235. for _,v := range d.areakeys{
  236. keys = append(keys, fmt.Sprintf("%s_%s_%s", k, info.subtype, v))
  237. }
  238. }else {//匹配指定省
  239. keys = append(keys, fmt.Sprintf("%s_%s_%s", k, info.subtype, info.area))
  240. }
  241. keys = append(keys, fmt.Sprintf("%s_%s_%s", k, info.subtype, "全国"))
  242. }
  243. d.lock.Unlock()
  244. L:
  245. for _, k := range keys {
  246. d.lock.Lock()
  247. data := d.data[k]
  248. d.lock.Unlock()
  249. if len(data) > 0 { //对比v 找到同类型,同省或全国的数据作对比
  250. for _, v := range data {
  251. reason = ""
  252. isTestLog = false
  253. if v.id == info.id { //正常重复
  254. return false, v, ""
  255. }
  256. //buyer 优先级高,有值且不相等过滤
  257. if info.buyer!=""&&v.buyer!=""&&info.buyer!=v.buyer {
  258. if v.title != info.title && v.title != "" && info.title != "" {
  259. isTestLog = true
  260. }
  261. if buyerIsContinue(v,info) {
  262. continue
  263. }
  264. }
  265. if info.site != "" {//站点临时赋值
  266. sitelock.Lock()
  267. dict := SiteMap[info.site]
  268. sitelock.Unlock()
  269. if dict != nil {
  270. if (info.area == "全国" && dict["area"] != "")||
  271. (info.city == "" && dict["city"] != ""){
  272. info.is_site = true
  273. info.area = qutil.ObjToString(dict["area"])
  274. info.city = qutil.ObjToString(dict["city"])
  275. }
  276. }
  277. }
  278. //前置条件 - 站点相关
  279. if info.site != "" && info.site == v.site {
  280. if info.href != "" && info.href == v.href {
  281. reason = "同站点-href相同"
  282. b = true
  283. source = v
  284. reasons = reason
  285. break L
  286. }
  287. //相同发布时间-标题无包含关系 - 项目名称不等
  288. if isTheSameDay(info.publishtime,v.publishtime) &&
  289. !(strings.Contains(v.title, info.title) || strings.Contains(info.title, v.title)) {
  290. continue
  291. }
  292. //不同href
  293. if info.href != "" && info.href != v.href {
  294. if v.title==info.title{
  295. if !againRepeat(v, info,true) {//进行同站点二次判断
  296. reason = "同站点-href不同-标题相同等"
  297. b = true
  298. source = v
  299. reasons = reason
  300. break L
  301. }else {
  302. continue
  303. }
  304. }else {
  305. if againRepeat(v, info,true) {
  306. continue
  307. }
  308. }
  309. }
  310. }
  311. //特殊词处理
  312. specialNum:= dealWithSpecialWordNumber(info,v)
  313. //前置条件 - 标题相关,有且一个关键词
  314. if specialNum==1 {
  315. if againRepeat(v, info,false) {
  316. continue
  317. }
  318. }
  319. //前置条件3 - 标题相关,均含有关键词
  320. if specialNum==2 {
  321. if len([]rune(v.title)) > 10 && len([]rune(info.title)) > 10 &&
  322. v.title != "" && info.title != "" {
  323. letter1,letter2:=v.title,info.title
  324. res, _ := regexp.Compile("[0-9a-zA-Z]+");
  325. if res.MatchString(letter1)||res.MatchString(letter2) {
  326. letter1=convertArabicNumeralsAndLetters(letter1)
  327. letter2=convertArabicNumeralsAndLetters(letter2)
  328. }
  329. if strings.Contains(letter1,"重新招标")|| strings.Contains(letter2,"重新招标"){
  330. letter1,letter2=dealWithSpecialPhrases(letter1,letter2)
  331. }
  332. if letter1==letter2 {
  333. reason = reason + "标题关键词相等关系"
  334. if !againRepeat(v, info,false) {//进行二级金额判断
  335. b = true
  336. source = v
  337. reasons = reason
  338. break L
  339. }
  340. }else {
  341. if !(strings.Contains(letter1, letter2) || strings.Contains(letter2, letter1)) {
  342. //无包含关系-即不相等
  343. if againContainSpecialWord(v, info) {
  344. continue
  345. }
  346. }
  347. }
  348. }
  349. }
  350. //前置条件-五要素均相等
  351. if leadingElementSame(v,info) {
  352. reason = "五要素-相同-满足"
  353. b = true
  354. source = v
  355. reasons = reason
  356. break L
  357. }
  358. //新增快速数据过少判重
  359. if LowHeavy {
  360. repeat := false
  361. if repeat, reason = fastLowQualityHeavy(v, info, reason); repeat {
  362. b = true
  363. source = v
  364. reasons = reason
  365. break L
  366. }
  367. }
  368. //代理机构相同-非空相等
  369. if v.agency != "" && info.agency != "" && v.agency == info.agency {
  370. reason = reason + "同机构-"
  371. repeat := false
  372. if repeat, reason = quickHeavyMethodTwo(v, info, reason); repeat {
  373. b = true
  374. source = v
  375. reasons = reason
  376. break L
  377. }
  378. } else {
  379. reason = reason + "非同机构-"
  380. if info.city != "" && info.city == v.city {
  381. reason = reason + "同城-"
  382. repeat := false
  383. if repeat, reason = quickHeavyMethodTwo(v, info, reason); repeat {
  384. b = true
  385. source = v
  386. reasons = reason
  387. break L
  388. }
  389. } else {
  390. reason = reason + "不同城-"
  391. repeat := false
  392. if repeat, reason = quickHeavyMethodOne(v, info, reason); repeat {
  393. b = true
  394. source = v
  395. reasons = reason
  396. break L
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }
  403. //往预存数据 d 添加
  404. if !b {
  405. ct := info.publishtime
  406. dkey := qutil.FormatDateByInt64(&ct, qutil.Date_yyyyMMdd)
  407. k := fmt.Sprintf("%s_%s_%s", dkey, info.subtype, info.area)
  408. d.lock.Lock()
  409. data := d.data[k]
  410. if data == nil {
  411. data = []*Info{info}
  412. d.data[k] = data
  413. if !d.keys[dkey] {
  414. d.keys[dkey] = true
  415. d.update(ct)
  416. }
  417. } else {
  418. data = append(data, info)
  419. d.data[k] = data
  420. }
  421. //添加省
  422. isAreaExist :=false
  423. for _,v:= range d.areakeys {
  424. if v==info.area {
  425. isAreaExist = true
  426. }
  427. }
  428. if !isAreaExist {
  429. areaArr := d.areakeys
  430. areaArr = append(areaArr,info.area)
  431. d.areakeys = areaArr
  432. }
  433. d.lock.Unlock()
  434. }
  435. if isTestLog {
  436. reasons = reasons+"-新修改"
  437. }
  438. return
  439. }
  440. func (d *datamap) update(t int64) {
  441. if TimingTask {
  442. }else {
  443. if IsFull {
  444. d.keymap = d.GetLatelyFiveDay(t)//全量
  445. }else {
  446. d.keymap = d.GetLatelyFiveDayDouble(t) //增量
  447. }
  448. m := map[string]bool{}
  449. for _, v := range d.keymap {
  450. m[v] = true
  451. }
  452. for k, _ := range d.data {
  453. if !m[k[:8]] {
  454. delete(d.data, k)
  455. }
  456. }
  457. for k, _ := range d.keys {
  458. if !m[k] {
  459. delete(d.keys, k)
  460. }
  461. }
  462. }
  463. }
  464. func (d *datamap) GetLatelyFiveDay(t int64) []string {
  465. array := make([]string, d.days)
  466. now := time.Unix(t, 0)
  467. for i := 0; i < d.days; i++ {
  468. array[i] = now.Format(qutil.Date_yyyyMMdd)
  469. now = now.AddDate(0, 0, -1)
  470. }
  471. return array
  472. }
  473. func (d *datamap) GetLatelyFiveDayDouble(t int64) []string {//增量-两倍
  474. array := make([]string, d.days*2)
  475. now := time.Now()
  476. for i := 0; i < d.days*2; i++ {
  477. array[i] = now.Format(qutil.Date_yyyyMMdd)
  478. now = now.AddDate(0, 0, -1)
  479. }
  480. return array
  481. }
  482. //替换原始数据池-更新
  483. func (d *datamap) replacePoolData(newData *Info) {
  484. d.lock.Lock()
  485. ct := newData.publishtime
  486. dkey := qutil.FormatDateByInt64(&ct, qutil.Date_yyyyMMdd)
  487. k := fmt.Sprintf("%s_%s_%s", dkey, newData.subtype, newData.area)
  488. data := d.data[k]
  489. for k, v := range data {
  490. if v.id == newData.id {//替换
  491. data[k] = newData
  492. break
  493. }
  494. }
  495. d.data[k] = data
  496. d.lock.Unlock()
  497. }
  498. //相互替换数据池-暂时弃用
  499. func (d *datamap) replaceSourceData(newData *Info, oldData *Info) {
  500. //删除数据池的老数据
  501. ct_old := oldData.publishtime
  502. dkey_old := qutil.FormatDateByInt64(&ct_old, qutil.Date_yyyyMMdd)
  503. k_old := fmt.Sprintf("%s_%s_%s", dkey_old, oldData.subtype, oldData.area)
  504. data_old := d.data[k_old]
  505. for k, v := range data_old {
  506. if v.id == oldData.id {//删除对应当前的老数据
  507. data_old = append(data_old[:k], data_old[k+1:]...)
  508. break
  509. }
  510. }
  511. d.data[k_old] = data_old
  512. //添加新的
  513. ct := newData.publishtime
  514. dkey := qutil.FormatDateByInt64(&ct, qutil.Date_yyyyMMdd)
  515. k := fmt.Sprintf("%s_%s_%s", dkey, newData.subtype, newData.area)
  516. d.lock.Lock()
  517. data := d.data[k]
  518. if data == nil {
  519. data = []*Info{newData}
  520. d.data[k] = data
  521. if !d.keys[dkey] {
  522. d.keys[dkey] = true
  523. d.update(ct)
  524. }
  525. } else {
  526. data = append(data, newData)
  527. d.data[k] = data
  528. }
  529. //添加省
  530. isAreaExist :=false
  531. for _,v:= range d.areakeys {
  532. if v==newData.area {
  533. isAreaExist = true
  534. }
  535. }
  536. if !isAreaExist {
  537. areaArr := d.areakeys
  538. areaArr = append(areaArr,newData.area)
  539. d.areakeys = areaArr
  540. }
  541. d.lock.Unlock()
  542. }
  543. //总计条数-暂时弃用
  544. func (d *datamap) currentTotalCount() int {
  545. num:=qutil.IntAll(0)
  546. for _,v:=range d.data {
  547. num = num+qutil.IntAll(len(v))
  548. }
  549. return num
  550. }