datamap.go 15 KB

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