datamap.go 14 KB

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