datamap.go 14 KB

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