main.go 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. package main
  2. /**
  3. 招标信息判重
  4. **/
  5. import (
  6. "encoding/json"
  7. "flag"
  8. "fmt"
  9. "log"
  10. mu "mfw/util"
  11. "net"
  12. "os"
  13. "qfw/util"
  14. "regexp"
  15. "sync"
  16. "time"
  17. "github.com/cron"
  18. "gopkg.in/mgo.v2/bson"
  19. )
  20. var (
  21. Sysconfig map[string]interface{} //配置文件
  22. mconf map[string]interface{} //mongodb配置信息
  23. mgo *MongodbSim //mongodb操作对象
  24. extract string
  25. extract_back string
  26. udpclient mu.UdpClient //udp对象
  27. nextNode []map[string]interface{} //下节点数组
  28. dupdays = 5 //初始化判重范围
  29. DM *datamap //
  30. HM *historymap //判重数据
  31. lastid = ""
  32. //正则筛选相关
  33. FilterRegTitle = regexp.MustCompile("^_$")
  34. FilterRegTitle_0 = regexp.MustCompile("^_$")
  35. FilterRegTitle_1 = regexp.MustCompile("^_$")
  36. FilterRegTitle_2 = regexp.MustCompile("^_$")
  37. isMerger bool //是否合并
  38. Is_Sort bool //是否排序
  39. threadNum int //线程数量
  40. SiteMap map[string]map[string]interface{} //站点map
  41. LowHeavy bool //低质量数据判重
  42. TimingTask bool //是否定时任务
  43. timingSpanDay int64 //时间跨度
  44. timingPubScope int64 //发布时间周期
  45. sid, eid string //测试人员判重使用
  46. )
  47. func init() {
  48. flag.StringVar(&lastid, "id", "", "最后加载id") //以小于等于此id开始加载最近几天的数据
  49. flag.StringVar(&sid, "sid", "", "开始id")
  50. flag.StringVar(&eid, "eid", "", "结束id")
  51. flag.Parse()
  52. //172.17.145.163:27080
  53. util.ReadConfig(&Sysconfig)
  54. nextNode = util.ObjArrToMapArr(Sysconfig["nextNode"].([]interface{}))
  55. mconf = Sysconfig["mongodb"].(map[string]interface{})
  56. mgo = &MongodbSim{
  57. MongodbAddr: mconf["addr"].(string),
  58. DbName: mconf["db"].(string),
  59. Size: util.IntAllDef(mconf["pool"], 10),
  60. }
  61. mgo.InitPool()
  62. extract = mconf["extract"].(string)
  63. extract_back = mconf["extract_back"].(string)
  64. dupdays = util.IntAllDef(Sysconfig["dupdays"], 3)
  65. //加载数据
  66. DM = NewDatamap(dupdays, lastid)
  67. FilterRegTitle = regexp.MustCompile(util.ObjToString(Sysconfig["specialwords"]))
  68. FilterRegTitle_0 = regexp.MustCompile(util.ObjToString(Sysconfig["specialtitle_0"]))
  69. FilterRegTitle_1 = regexp.MustCompile(util.ObjToString(Sysconfig["specialtitle_1"]))
  70. FilterRegTitle_2 = regexp.MustCompile(util.ObjToString(Sysconfig["specialtitle_2"]))
  71. isMerger = Sysconfig["isMerger"].(bool)
  72. Is_Sort = Sysconfig["isSort"].(bool)
  73. threadNum = util.IntAllDef(Sysconfig["threads"], 1)
  74. LowHeavy = Sysconfig["lowHeavy"].(bool)
  75. TimingTask = Sysconfig["timingTask"].(bool)
  76. timingSpanDay = util.Int64All(Sysconfig["timingSpanDay"])
  77. timingPubScope = util.Int64All(Sysconfig["timingPubScope"])
  78. //站点配置
  79. site := mconf["site"].(map[string]interface{})
  80. SiteMap = make(map[string]map[string]interface{}, 0)
  81. start := int(time.Now().Unix())
  82. sess_site := mgo.GetMgoConn()
  83. defer mgo.DestoryMongoConn(sess_site)
  84. res_site := sess_site.DB(site["dbname"].(string)).C(site["coll"].(string)).Find(map[string]interface{}{}).Sort("_id").Iter()
  85. for site_dict := make(map[string]interface{}); res_site.Next(&site_dict); {
  86. data_map := map[string]interface{}{
  87. "area": util.ObjToString(site_dict["area"]),
  88. "city": util.ObjToString(site_dict["city"]),
  89. "district": util.ObjToString(site_dict["district"]),
  90. "sitetype": util.ObjToString(site_dict["sitetype"]),
  91. "level": util.ObjToString(site_dict["level"]),
  92. }
  93. SiteMap[util.ObjToString(site_dict["site"])] = data_map
  94. }
  95. log.Printf("站点加载用时:%d秒,%d个\n", int(time.Now().Unix())-start, len(SiteMap))
  96. }
  97. func main() {
  98. go checkMapJob()
  99. updport := Sysconfig["udpport"].(string)
  100. udpclient = mu.UdpClient{Local: updport, BufSize: 1024}
  101. if TimingTask {
  102. go timedTaskDay()
  103. } else {
  104. udpclient.Listen(processUdpMsg)
  105. log.Println("Udp服务监听", updport)
  106. }
  107. time.Sleep(99999 * time.Hour)
  108. }
  109. //测试组人员使用
  110. func mainT() {
  111. /*
  112. ObjectId("5da3f31aa5cb26b9b798d3aa")
  113. ObjectId("5da418c4a5cb26b9b7e3e9a6")
  114. ObjectId("5da3f2c5a5cb26b9b79847fc")
  115. ObjectId("5db2735ba5cb26b9b7c99c6f")
  116. */
  117. if TimingTask {
  118. log.Println("定时任务测试开始")
  119. go timedTaskDay()
  120. time.Sleep(99999 * time.Hour)
  121. } else {
  122. sid = "5c2c10fda5cb26b9b75e6f7f"
  123. eid = "5e976e4a50b5ea296ef376b9"
  124. log.Println("正常判重测试开始")
  125. log.Println(sid, "---", eid)
  126. mapinfo := map[string]interface{}{}
  127. if sid == "" || eid == "" {
  128. log.Println("sid,eid参数不能为空")
  129. os.Exit(0)
  130. }
  131. mapinfo["gtid"] = sid
  132. mapinfo["lteid"] = eid
  133. mapinfo["stop"] = "true"
  134. task([]byte{}, mapinfo)
  135. time.Sleep(10 * time.Second)
  136. }
  137. }
  138. func processUdpMsg(act byte, data []byte, ra *net.UDPAddr) {
  139. fmt.Println("接受的段数据")
  140. switch act {
  141. case mu.OP_TYPE_DATA: //上个节点的数据
  142. //从表中开始处理
  143. var mapInfo map[string]interface{}
  144. err := json.Unmarshal(data, &mapInfo)
  145. log.Println("err:", err, "mapInfo:", mapInfo)
  146. if err != nil {
  147. udpclient.WriteUdp([]byte("err:"+err.Error()), mu.OP_NOOP, ra)
  148. } else if mapInfo != nil {
  149. taskType := util.ObjToString(mapInfo["stype"])
  150. if taskType == "historyTask" {
  151. //历史更新流程
  152. go historyTask(data, mapInfo)
  153. } else if taskType == "normalTask" {
  154. //判重流程
  155. go task(data, mapInfo)
  156. } else {
  157. //其他
  158. go task(data, mapInfo)
  159. }
  160. key, _ := mapInfo["key"].(string)
  161. if key == "" {
  162. key = "udpok"
  163. }
  164. udpclient.WriteUdp([]byte(key), mu.OP_NOOP, ra)
  165. }
  166. case mu.OP_NOOP: //下个节点回应
  167. ok := string(data)
  168. if ok != "" {
  169. log.Println("ok:", ok)
  170. udptaskmap.Delete(ok)
  171. }
  172. }
  173. }
  174. //开始判重程序
  175. func task(data []byte, mapInfo map[string]interface{}) {
  176. log.Println("开始数据判重")
  177. defer util.Catch()
  178. //区间id
  179. q := map[string]interface{}{
  180. "_id": map[string]interface{}{
  181. "$gt": StringTOBsonId(mapInfo["gtid"].(string)),
  182. "$lte": StringTOBsonId(mapInfo["lteid"].(string)),
  183. },
  184. }
  185. log.Println(mgo.DbName, extract, q)
  186. sess := mgo.GetMgoConn()
  187. defer mgo.DestoryMongoConn(sess)
  188. //是否排序
  189. it := sess.DB(mgo.DbName).C(extract).Find(&q).Sort("_id").Iter()
  190. if Is_Sort {
  191. log.Println("排序:publishtime")
  192. it = sess.DB(mgo.DbName).C(extract).Find(&q).Sort("publishtime").Iter()
  193. }
  194. //it = sess.DB(mgo.DbName).C(extract).Find(&q).Iter()
  195. updateExtract := [][]map[string]interface{}{}
  196. log.Println("线程数:", threadNum)
  197. pool := make(chan bool, threadNum)
  198. wg := &sync.WaitGroup{}
  199. n, repeateN := 0, 0
  200. for tmp := make(map[string]interface{}); it.Next(&tmp); n++ {
  201. if n%10000 == 0 {
  202. log.Println("current:", n, tmp["_id"], "repeateN:", repeateN)
  203. }
  204. if util.IntAll(tmp["repeat"]) == 1 || util.IntAll(tmp["repeat"]) == -1 {
  205. tmp = make(map[string]interface{})
  206. repeateN++
  207. continue
  208. }
  209. pool <- true
  210. wg.Add(1)
  211. go func(tmp map[string]interface{}) {
  212. defer func() {
  213. <-pool
  214. wg.Done()
  215. }()
  216. info := NewInfo(tmp)
  217. if !LowHeavy { //是否进行低质量数据判重
  218. if invalidData(info.buyer, info.projectname, info.projectcode, info.contractnumber) {
  219. updateExtract = append(updateExtract, []map[string]interface{}{
  220. map[string]interface{}{
  221. "_id": tmp["_id"],
  222. },
  223. map[string]interface{}{
  224. "$set": map[string]interface{}{
  225. "repeat": -1, //无效数据标签
  226. },
  227. },
  228. })
  229. if len(updateExtract) > 500 {
  230. mgo.UpSertBulk(extract, updateExtract...)
  231. updateExtract = [][]map[string]interface{}{}
  232. }
  233. return
  234. }
  235. }
  236. b, source, reason := DM.check(info)
  237. if b { //有重复,生成更新语句,更新抽取和更新招标
  238. repeateN++
  239. var is_replace = false
  240. var mergeArr = []int64{} //更改合并数组记录
  241. var newData = &Info{} //更换新的数据池数据
  242. var repeat_idMap = map[string]interface{}{} //记录判重的
  243. var merge_idMap = map[string]interface{}{} //记录合并的
  244. repeat_idMap["_id"] = StringTOBsonId(info.id)
  245. merge_idMap["_id"] = StringTOBsonId(source.id)
  246. repeat_id := source.id //初始化一个数据
  247. if isMerger { //合并相关
  248. basic_bool := basicDataScore(source, info)
  249. if basic_bool {
  250. //已原始数据为标准 - 对比数据打判重标签-
  251. newData, mergeArr, is_replace = mergeDataFields(source, info)
  252. DM.replaceSourceData(newData, source.id) //替换
  253. //对比数据打重复标签的id,原始数据id的记录
  254. repeat_idMap["_id"] = StringTOBsonId(info.id)
  255. merge_idMap["_id"] = StringTOBsonId(source.id)
  256. repeat_id = source.id
  257. } else {
  258. //已对比数据为标准 ,数据池的数据打判重标签
  259. newData, mergeArr, is_replace = mergeDataFields(info, source)
  260. DM.replaceSourceData(newData, source.id) //替换
  261. //原始数据打重复标签的id, 对比数据id的记录
  262. repeat_idMap["_id"] = StringTOBsonId(source.id)
  263. merge_idMap["_id"] = StringTOBsonId(info.id)
  264. repeat_id = info.id
  265. }
  266. merge_map := make(map[string]interface{}, 0)
  267. if is_replace { //有过合并-更新数据
  268. merge_map = map[string]interface{}{
  269. "$set": map[string]interface{}{
  270. "merge": newData.mergemap,
  271. },
  272. }
  273. //更新合并后的数据
  274. for _, value := range mergeArr {
  275. if value == 0 {
  276. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  277. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  278. } else if value == 1 {
  279. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  280. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  281. } else if value == 2 {
  282. merge_map["$set"].(map[string]interface{})["projectname"] = newData.projectname
  283. } else if value == 3 {
  284. merge_map["$set"].(map[string]interface{})["projectcode"] = newData.projectcode
  285. } else if value == 4 {
  286. merge_map["$set"].(map[string]interface{})["buyer"] = newData.buyer
  287. } else if value == 5 {
  288. merge_map["$set"].(map[string]interface{})["budget"] = newData.budget
  289. } else if value == 6 {
  290. merge_map["$set"].(map[string]interface{})["winner"] = newData.winner
  291. } else if value == 7 {
  292. merge_map["$set"].(map[string]interface{})["bidamount"] = newData.bidamount
  293. } else if value == 8 {
  294. merge_map["$set"].(map[string]interface{})["bidopentime"] = newData.bidopentime
  295. } else if value == 9 {
  296. merge_map["$set"].(map[string]interface{})["contractnumber"] = newData.contractnumber
  297. } else if value == 10 {
  298. merge_map["$set"].(map[string]interface{})["publishtime"] = newData.publishtime
  299. } else if value == 11 {
  300. merge_map["$set"].(map[string]interface{})["agency"] = newData.agency
  301. } else {
  302. }
  303. }
  304. //模板数据更新
  305. updateExtract = append(updateExtract, []map[string]interface{}{
  306. merge_idMap,
  307. merge_map,
  308. })
  309. }
  310. } else { //高质量数据
  311. basic_bool := basicDataScore(source, info)
  312. if !basic_bool {
  313. DM.replaceSourceData(info, source.id) //替换
  314. repeat_idMap["_id"] = StringTOBsonId(source.id)
  315. repeat_id = info.id
  316. }
  317. }
  318. //重复数据打标签
  319. updateExtract = append(updateExtract, []map[string]interface{}{
  320. repeat_idMap,
  321. map[string]interface{}{
  322. "$set": map[string]interface{}{
  323. "repeat": 1,
  324. "repeat_reason": reason,
  325. "repeat_id": repeat_id,
  326. },
  327. },
  328. })
  329. }
  330. }(tmp)
  331. if len(updateExtract) > 500 {
  332. mgo.UpSertBulk(extract, updateExtract...)
  333. updateExtract = [][]map[string]interface{}{}
  334. }
  335. tmp = make(map[string]interface{})
  336. }
  337. wg.Wait()
  338. if len(updateExtract) > 0 {
  339. mgo.UpSertBulk(extract, updateExtract...)
  340. }
  341. log.Println("this task over.", n, "repeateN:", repeateN, mapInfo["stop"])
  342. //任务完成,开始发送广播通知下面节点
  343. if n > repeateN && mapInfo["stop"] == nil {
  344. for _, to := range nextNode {
  345. sid, _ := mapInfo["gtid"].(string)
  346. eid, _ := mapInfo["lteid"].(string)
  347. key := sid + "-" + eid + "-" + util.ObjToString(to["stype"])
  348. by, _ := json.Marshal(map[string]interface{}{
  349. "gtid": sid,
  350. "lteid": eid,
  351. "stype": util.ObjToString(to["stype"]),
  352. "key": key,
  353. })
  354. addr := &net.UDPAddr{
  355. IP: net.ParseIP(to["addr"].(string)),
  356. Port: util.IntAll(to["port"]),
  357. }
  358. node := &udpNode{by, addr, time.Now().Unix(), 0}
  359. udptaskmap.Store(key, node)
  360. udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  361. }
  362. }
  363. }
  364. //支持历史更新
  365. func historyTask(data []byte, mapInfo map[string]interface{}) {
  366. fmt.Println("开始取历史时间段")
  367. defer util.Catch()
  368. sess := mgo.GetMgoConn()
  369. defer mgo.DestoryMongoConn(sess)
  370. q := map[string]interface{}{
  371. "_id": map[string]interface{}{
  372. "$gt": StringTOBsonId(mapInfo["gtid"].(string)),
  373. "$lte": StringTOBsonId(mapInfo["lteid"].(string)),
  374. },
  375. }
  376. it := sess.DB(mgo.DbName).C(extract).Find(&q).Iter()
  377. minTime, maxTime := int64(0), int64(0)
  378. for tmp := make(map[string]interface{}); it.Next(&tmp); {
  379. //取出最大最小时间
  380. info_time := tmp["comeintime"]
  381. if Is_Sort {
  382. info_time = tmp["publishtime"]
  383. }
  384. if minTime == 0 || maxTime == 0 && util.Int64All(info_time) != 0 {
  385. minTime = util.Int64All(info_time)
  386. maxTime = util.Int64All(info_time)
  387. } else {
  388. t := util.Int64All(info_time)
  389. if t < minTime && t != 0 {
  390. minTime = t
  391. }
  392. if t > maxTime && t != 0 {
  393. maxTime = t
  394. }
  395. }
  396. }
  397. //时间不正确时
  398. if minTime == 0 && maxTime == 0 {
  399. log.Println("段数据区间 不符合")
  400. return
  401. }
  402. fmt.Println("最小时间==", minTime, "最大时间==", maxTime)
  403. gtid, lteid := util.BsonIdToSId(mapInfo["gtid"].(string)), util.BsonIdToSId(mapInfo["lteid"].(string))
  404. fmt.Println(gtid, lteid)
  405. HM = NewHistorymap(gtid, lteid, minTime, maxTime)
  406. fmt.Println("开始历史数据判重")
  407. defer util.Catch()
  408. //区间id
  409. sess_history := mgo.GetMgoConn()
  410. defer mgo.DestoryMongoConn(sess_history)
  411. q_history := map[string]interface{}{
  412. "_id": map[string]interface{}{
  413. "$gt": StringTOBsonId(mapInfo["gtid"].(string)),
  414. "$lte": StringTOBsonId(mapInfo["lteid"].(string)),
  415. },
  416. }
  417. log.Println(mgo.DbName, extract, q_history)
  418. //是否排序
  419. it_history := sess_history.DB(mgo.DbName).C(extract).Find(&q_history).Iter()
  420. if Is_Sort {
  421. it_history = sess_history.DB(mgo.DbName).C(extract).Find(&q_history).Sort("publishtime").Iter()
  422. }
  423. updateExtract := [][]map[string]interface{}{}
  424. log.Println("线程数:", threadNum)
  425. pool := make(chan bool, threadNum)
  426. wg := &sync.WaitGroup{}
  427. n, repeateN := 0, 0
  428. for tmp := make(map[string]interface{}); it_history.Next(&tmp); n++ {
  429. if n%10000 == 0 {
  430. log.Println("current:", n, tmp["_id"], "repeateN:", repeateN)
  431. }
  432. if util.IntAll(tmp["dataging"]) == 1 {
  433. tmp = make(map[string]interface{})
  434. continue
  435. }
  436. pool <- true
  437. wg.Add(1)
  438. go func(tmp map[string]interface{}) {
  439. defer func() {
  440. <-pool
  441. wg.Done()
  442. }()
  443. info := NewInfo(tmp)
  444. if !LowHeavy { //是否进行低质量数据判重
  445. if invalidData(info.buyer, info.projectname, info.projectcode, info.contractnumber) {
  446. updateExtract = append(updateExtract, []map[string]interface{}{
  447. map[string]interface{}{
  448. "_id": tmp["_id"],
  449. },
  450. map[string]interface{}{
  451. "$set": map[string]interface{}{
  452. "repeat": -1, //无效数据标签
  453. },
  454. },
  455. })
  456. if len(updateExtract) > 500 {
  457. mgo.UpSertBulk(extract, updateExtract...)
  458. updateExtract = [][]map[string]interface{}{}
  459. }
  460. return
  461. }
  462. }
  463. b, source, reason := HM.checkHistory(info)
  464. if b { //有重复,生成更新语句,更新抽取和更新招标
  465. if reason == "未判重记录" {
  466. fmt.Println("未判重记录")
  467. //把info的数据判重的标签更换,并新增字段
  468. HM.replaceSourceData(info, info.id) //替换即添加
  469. updateExtract = append(updateExtract, []map[string]interface{}{
  470. map[string]interface{}{
  471. "_id": tmp["_id"],
  472. },
  473. map[string]interface{}{
  474. "$set": map[string]interface{}{
  475. "repeat": 0,
  476. "repeatid": -2,
  477. },
  478. },
  479. })
  480. } else {
  481. repeateN++
  482. var is_replace = false
  483. var mergeArr = []int64{} //更改合并数组记录
  484. var newData = &Info{} //更换新的数据池数据
  485. var repeat_idMap = map[string]interface{}{} //记录判重的
  486. var merge_idMap = map[string]interface{}{} //记录合并的
  487. repeat_idMap["_id"] = StringTOBsonId(info.id)
  488. merge_idMap["_id"] = StringTOBsonId(source.id)
  489. repeat_id := source.id
  490. //以下合并相关
  491. if isMerger {
  492. basic_bool := basicDataScore(source, info)
  493. if basic_bool {
  494. //已原始数据为标准 - 对比数据打判重标签-
  495. newData, mergeArr, is_replace = mergeDataFields(source, info)
  496. HM.replaceSourceData(newData, source.id) //替换
  497. //对比数据打重复标签的id,原始数据id的记录
  498. repeat_idMap["_id"] = StringTOBsonId(info.id)
  499. merge_idMap["_id"] = StringTOBsonId(source.id)
  500. repeat_id = source.id
  501. } else {
  502. //已对比数据为标准 ,数据池的数据打判重标签
  503. newData, mergeArr, is_replace = mergeDataFields(info, source)
  504. HM.replaceSourceData(newData, source.id) //替换
  505. //原始数据打重复标签的id, 对比数据id的记录
  506. repeat_idMap["_id"] = StringTOBsonId(source.id)
  507. merge_idMap["_id"] = StringTOBsonId(info.id)
  508. repeat_id = info.id
  509. }
  510. merge_map := make(map[string]interface{}, 0)
  511. if is_replace { //有过合并-更新数据
  512. merge_map = map[string]interface{}{
  513. "$set": map[string]interface{}{
  514. "merge": newData.mergemap,
  515. },
  516. }
  517. //更新合并后的数据
  518. for _, value := range mergeArr {
  519. if value == 0 {
  520. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  521. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  522. } else if value == 1 {
  523. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  524. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  525. } else if value == 2 {
  526. merge_map["$set"].(map[string]interface{})["projectname"] = newData.projectname
  527. } else if value == 3 {
  528. merge_map["$set"].(map[string]interface{})["projectcode"] = newData.projectcode
  529. } else if value == 4 {
  530. merge_map["$set"].(map[string]interface{})["buyer"] = newData.buyer
  531. } else if value == 5 {
  532. merge_map["$set"].(map[string]interface{})["budget"] = newData.budget
  533. } else if value == 6 {
  534. merge_map["$set"].(map[string]interface{})["winner"] = newData.winner
  535. } else if value == 7 {
  536. merge_map["$set"].(map[string]interface{})["bidamount"] = newData.bidamount
  537. } else if value == 8 {
  538. merge_map["$set"].(map[string]interface{})["bidopentime"] = newData.bidopentime
  539. } else if value == 9 {
  540. merge_map["$set"].(map[string]interface{})["contractnumber"] = newData.contractnumber
  541. } else if value == 10 {
  542. merge_map["$set"].(map[string]interface{})["publishtime"] = newData.publishtime
  543. } else if value == 11 {
  544. merge_map["$set"].(map[string]interface{})["agency"] = newData.agency
  545. } else {
  546. }
  547. }
  548. //模板数据更新
  549. updateExtract = append(updateExtract, []map[string]interface{}{
  550. merge_idMap,
  551. merge_map,
  552. })
  553. }
  554. } else { //高质量数据
  555. basic_bool := basicDataScore(source, info)
  556. if !basic_bool {
  557. HM.replaceSourceData(info, source.id) //替换
  558. repeat_idMap["_id"] = StringTOBsonId(source.id)
  559. repeat_id = info.id
  560. }
  561. }
  562. //重复数据打标签
  563. updateExtract = append(updateExtract, []map[string]interface{}{
  564. repeat_idMap,
  565. map[string]interface{}{
  566. "$set": map[string]interface{}{
  567. "repeat": 1,
  568. "repeat_reason": reason,
  569. "repeat_id": repeat_id,
  570. },
  571. },
  572. })
  573. }
  574. }
  575. }(tmp)
  576. if len(updateExtract) > 500 {
  577. mgo.UpSertBulk(extract, updateExtract...)
  578. updateExtract = [][]map[string]interface{}{}
  579. }
  580. tmp = make(map[string]interface{})
  581. }
  582. wg.Wait()
  583. if len(updateExtract) > 0 {
  584. mgo.UpSertBulk(extract, updateExtract...)
  585. //mgo.UpdateBulk(bidding, updateBidding...)
  586. }
  587. log.Println("this task over.", n, "repeateN:", repeateN, mapInfo["stop"])
  588. //任务完成,开始发送广播通知下面节点
  589. if n > repeateN && mapInfo["stop"] == nil {
  590. for _, to := range nextNode {
  591. sid, _ := mapInfo["gtid"].(string)
  592. eid, _ := mapInfo["lteid"].(string)
  593. key := sid + "-" + eid + "-" + util.ObjToString(to["stype"])
  594. by, _ := json.Marshal(map[string]interface{}{
  595. "gtid": sid,
  596. "lteid": eid,
  597. "stype": util.ObjToString(to["stype"]),
  598. "key": key,
  599. })
  600. addr := &net.UDPAddr{
  601. IP: net.ParseIP(to["addr"].(string)),
  602. Port: util.IntAll(to["port"]),
  603. }
  604. node := &udpNode{by, addr, time.Now().Unix(), 0}
  605. udptaskmap.Store(key, node)
  606. udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  607. }
  608. }
  609. }
  610. //定时任务
  611. func timedTaskDay() {
  612. c := cron.New()
  613. c.AddFunc("0 0 1 * * ?", func() { movedata() }) //每天凌晨1点执行一次
  614. c.AddFunc("0 0 2 * * ?", func() { timedTaskOnce() }) //每天凌晨2点执行一次
  615. c.Start()
  616. timedTaskOnce()
  617. }
  618. func timedTaskOnce() {
  619. log.Println("开始一次定时任务")
  620. defer util.Catch()
  621. now := time.Now()
  622. preTime := time.Date(now.Year(), now.Month(), now.Day()-1, 0, 0, 0, 0, time.Local)
  623. curTime := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, time.Local)
  624. task_sid := util.BsonIdToSId(bson.NewObjectIdWithTime(preTime))
  625. task_eid := util.BsonIdToSId(bson.NewObjectIdWithTime(curTime))
  626. //发布时间间隔时间 半年
  627. //测试数据 6点每个间隔6个月
  628. //task_sid = "5e20965785a9271abf0ad6bd"
  629. //task_eid = "5e20968d85a9271abf0ad6c2"
  630. //between_time := int64(1565801997)
  631. //测试数据 180个点 每个隔1天
  632. //task_sid = "5e208f9b50b5ea296eccbb8a"
  633. //task_eid = "5e20968d85a9271abf0ad6c2"
  634. //between_time := int64(1563641997)
  635. between_time := curTime.Unix() - (86400 * timingPubScope)
  636. lasttime := int64(0)
  637. log.Println(task_sid, task_eid, curTime.Unix(), between_time)
  638. //区间id
  639. q_start := map[string]interface{}{
  640. "_id": map[string]interface{}{
  641. "$gte": StringTOBsonId(task_sid),
  642. "$lte": StringTOBsonId(task_eid),
  643. },
  644. }
  645. sess_start := mgo.GetMgoConn()
  646. defer mgo.DestoryMongoConn(sess_start)
  647. it_start := sess_start.DB(mgo.DbName).C(extract).Find(&q_start).Sort("publishtime").Iter()
  648. startNum := 0
  649. for tmp_start := make(map[string]interface{}); it_start.Next(&tmp_start); startNum++ {
  650. if startNum%10000 == 0 {
  651. log.Println("正序遍历:", startNum)
  652. }
  653. //取-符合-发布时间半年内的数据
  654. if util.IntAll(tmp_start["dataging"]) == 1 {
  655. pubtime := util.Int64All(tmp_start["publishtime"])
  656. if pubtime > 0 && pubtime >= between_time {
  657. lasttime = pubtime
  658. log.Println("找到第一条符合条件的数据")
  659. break
  660. }
  661. }
  662. }
  663. log.Println("... ...", lasttime)
  664. if lasttime <= 0 {
  665. log.Println("没找到dataging==1的数据")
  666. return
  667. }
  668. //构建第一条需要判重的数据 (数据池)
  669. log.Println("开始构建第一条需要判重的数据 ---(数据池)")
  670. DM = TimedTaskDatamap(dupdays, lasttime)
  671. sess := mgo.GetMgoConn()
  672. defer mgo.DestoryMongoConn(sess)
  673. q := map[string]interface{}{
  674. "_id": map[string]interface{}{
  675. "$gte": StringTOBsonId(task_sid),
  676. "$lte": StringTOBsonId(task_eid),
  677. },
  678. }
  679. it := sess.DB(mgo.DbName).C(extract).Find(&q).Sort("publishtime").Iter()
  680. updateExtract := [][]map[string]interface{}{}
  681. log.Println("线程数只能为1")
  682. pool := make(chan bool, threadNum)
  683. wg := &sync.WaitGroup{}
  684. n, repeateN := 0, 0
  685. pre_publishtime := int64(0)
  686. for tmp := make(map[string]interface{}); it.Next(&tmp); n++ {
  687. if n%10000 == 0 {
  688. log.Println("定时任务判重当前数量current:", n, tmp["_id"], "repeateN:", repeateN)
  689. }
  690. //log.Println("当前测试重复数量:",repeateN)
  691. if util.IntAll(tmp["repeat"]) == 1 || util.IntAll(tmp["repeat"]) == -1 {
  692. tmp = make(map[string]interface{})
  693. continue
  694. }
  695. if util.IntAll(tmp["dataging"]) != 1 {
  696. tmp = make(map[string]interface{})
  697. continue
  698. }
  699. pool <- true
  700. wg.Add(1)
  701. go func(tmp map[string]interface{}) {
  702. defer func() {
  703. <-pool
  704. wg.Done()
  705. }()
  706. //log.Println("上个时间:",pre_publishtime,"当前时间--",util.Int64All(tmp["publishtime"]))
  707. if pre_publishtime == 0 {
  708. pre_publishtime = util.Int64All(tmp["publishtime"])
  709. } else {
  710. //时间跨度是否大于X天
  711. if (util.Int64All(tmp["publishtime"]) - pre_publishtime) >= (86400 * timingSpanDay) {
  712. //重新构建数据池
  713. //log.Println("超过跨度-重新构建:",util.Int64All(tmp["publishtime"]),"---",pre_publishtime)
  714. pre_publishtime = util.Int64All(tmp["publishtime"])
  715. DM = TimedTaskDatamap(dupdays, pre_publishtime)
  716. }
  717. }
  718. info := NewInfo(tmp)
  719. if !LowHeavy { //是否进行低质量数据判重
  720. if invalidData(info.buyer, info.projectname, info.projectcode, info.contractnumber) {
  721. log.Println("无效数据")
  722. updateExtract = append(updateExtract, []map[string]interface{}{
  723. map[string]interface{}{
  724. "_id": tmp["_id"],
  725. },
  726. map[string]interface{}{
  727. "$set": map[string]interface{}{
  728. "repeat": -1, //无效数据标签
  729. "dataging": 0,
  730. },
  731. },
  732. })
  733. if len(updateExtract) > 500 {
  734. mgo.UpSertBulk(extract, updateExtract...)
  735. updateExtract = [][]map[string]interface{}{}
  736. }
  737. return
  738. }
  739. }
  740. b, source, reason := DM.check(info)
  741. //log.Println("判重结果", b, reason)
  742. if b { //有重复,生成更新语句,更新抽取和更新招标
  743. repeateN++
  744. var is_replace = false
  745. var mergeArr = []int64{} //更改合并数组记录
  746. var newData = &Info{} //更换新的数据池数据
  747. var repeat_idMap = map[string]interface{}{} //记录判重的
  748. var merge_idMap = map[string]interface{}{} //记录合并的
  749. repeat_idMap["_id"] = StringTOBsonId(info.id)
  750. merge_idMap["_id"] = StringTOBsonId(source.id)
  751. repeat_id := source.id //初始化一个数据
  752. if isMerger { //合并相关
  753. basic_bool := basicDataScore(source, info)
  754. if basic_bool {
  755. //已原始数据为标准 - 对比数据打判重标签-
  756. newData, mergeArr, is_replace = mergeDataFields(source, info)
  757. DM.replaceSourceData(newData, source.id) //替换
  758. //对比数据打重复标签的id,原始数据id的记录
  759. repeat_idMap["_id"] = StringTOBsonId(info.id)
  760. merge_idMap["_id"] = StringTOBsonId(source.id)
  761. repeat_id = source.id
  762. } else {
  763. //已对比数据为标准 ,数据池的数据打判重标签
  764. newData, mergeArr, is_replace = mergeDataFields(info, source)
  765. DM.replaceSourceData(newData, source.id) //替换
  766. //原始数据打重复标签的id, 对比数据id的记录
  767. repeat_idMap["_id"] = StringTOBsonId(source.id)
  768. merge_idMap["_id"] = StringTOBsonId(info.id)
  769. repeat_id = info.id
  770. }
  771. merge_map := make(map[string]interface{}, 0)
  772. if is_replace { //有过合并-更新数据
  773. merge_map = map[string]interface{}{
  774. "$set": map[string]interface{}{
  775. "merge": newData.mergemap,
  776. "dataging": 0,
  777. },
  778. }
  779. //更新合并后的数据
  780. for _, value := range mergeArr {
  781. if value == 0 {
  782. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  783. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  784. } else if value == 1 {
  785. merge_map["$set"].(map[string]interface{})["area"] = newData.area
  786. merge_map["$set"].(map[string]interface{})["city"] = newData.city
  787. } else if value == 2 {
  788. merge_map["$set"].(map[string]interface{})["projectname"] = newData.projectname
  789. } else if value == 3 {
  790. merge_map["$set"].(map[string]interface{})["projectcode"] = newData.projectcode
  791. } else if value == 4 {
  792. merge_map["$set"].(map[string]interface{})["buyer"] = newData.buyer
  793. } else if value == 5 {
  794. merge_map["$set"].(map[string]interface{})["budget"] = newData.budget
  795. } else if value == 6 {
  796. merge_map["$set"].(map[string]interface{})["winner"] = newData.winner
  797. } else if value == 7 {
  798. merge_map["$set"].(map[string]interface{})["bidamount"] = newData.bidamount
  799. } else if value == 8 {
  800. merge_map["$set"].(map[string]interface{})["bidopentime"] = newData.bidopentime
  801. } else if value == 9 {
  802. merge_map["$set"].(map[string]interface{})["contractnumber"] = newData.contractnumber
  803. } else if value == 10 {
  804. merge_map["$set"].(map[string]interface{})["publishtime"] = newData.publishtime
  805. } else if value == 11 {
  806. merge_map["$set"].(map[string]interface{})["agency"] = newData.agency
  807. } else {
  808. }
  809. }
  810. //模板数据更新
  811. updateExtract = append(updateExtract, []map[string]interface{}{
  812. merge_idMap,
  813. merge_map,
  814. })
  815. }
  816. } else { //高质量数据
  817. basic_bool := basicDataScore(source, info)
  818. if !basic_bool {
  819. DM.replaceSourceData(info, source.id) //替换
  820. repeat_idMap["_id"] = StringTOBsonId(source.id)
  821. repeat_id = info.id
  822. }
  823. }
  824. //重复数据打标签
  825. updateExtract = append(updateExtract, []map[string]interface{}{
  826. repeat_idMap,
  827. map[string]interface{}{
  828. "$set": map[string]interface{}{
  829. "repeat": 1,
  830. "repeat_reason": reason,
  831. "repeat_id": repeat_id,
  832. "dataging": 0,
  833. },
  834. },
  835. })
  836. }
  837. }(tmp)
  838. if len(updateExtract) > 500 {
  839. mgo.UpSertBulk(extract, updateExtract...)
  840. updateExtract = [][]map[string]interface{}{}
  841. }
  842. tmp = make(map[string]interface{})
  843. }
  844. wg.Wait()
  845. if len(updateExtract) > 0 {
  846. mgo.UpSertBulk(extract, updateExtract...)
  847. }
  848. log.Println("this timeTask over.", n, "repeateN:", repeateN)
  849. //任务完成,开始发送广播通知下面节点 发udp 去升索引待定 + 合并
  850. if n > repeateN {
  851. for _, to := range nextNode {
  852. next_sid := util.BsonIdToSId(task_sid)
  853. next_eid := util.BsonIdToSId(task_eid)
  854. key := next_sid + "-" + next_eid + "-" + util.ObjToString(to["stype"])
  855. by, _ := json.Marshal(map[string]interface{}{
  856. "gtid": next_sid,
  857. "lteid": next_eid,
  858. "stype": util.ObjToString(to["stype"]),
  859. "key": key,
  860. })
  861. addr := &net.UDPAddr{
  862. IP: net.ParseIP(to["addr"].(string)),
  863. Port: util.IntAll(to["port"]),
  864. }
  865. node := &udpNode{by, addr, time.Now().Unix(), 0}
  866. udptaskmap.Store(key, node)
  867. udpclient.WriteUdp(by, mu.OP_TYPE_DATA, addr)
  868. }
  869. }
  870. }
  871. //合并字段-并更新merge字段的值
  872. func mergeDataFields(source *Info, info *Info) (*Info, []int64, bool) {
  873. merge_recordMap := make(map[string]interface{}, 0)
  874. mergeArr := make([]int64, 0)
  875. //是否替换数据了-记录原始的数据
  876. is_replace := false
  877. //1、城市
  878. if source.area == "" || source.area == "全国" {
  879. //为空
  880. if info.area != "全国" && info.area != "" {
  881. merge_recordMap["area"] = source.area
  882. merge_recordMap["city"] = source.city
  883. source.area = info.area
  884. source.city = info.city
  885. mergeArr = append(mergeArr, 1)
  886. is_replace = true
  887. }
  888. } else {
  889. //不为空-查看站点相关-有值必替换
  890. if source.is_site {
  891. //是站点替换的城市
  892. merge_recordMap["site_area"] = source.area
  893. merge_recordMap["site_city"] = source.city
  894. mergeArr = append(mergeArr, 0)
  895. is_replace = true
  896. source.is_site = false
  897. }
  898. }
  899. //2、项目名称
  900. if source.projectname == "" && info.projectname != "" {
  901. merge_recordMap["projectname"] = source.projectname
  902. source.projectname = info.projectname
  903. mergeArr = append(mergeArr, 2)
  904. is_replace = true
  905. }
  906. //3、项目编号
  907. if source.projectcode == "" && info.projectcode != "" {
  908. merge_recordMap["projectcode"] = source.projectcode
  909. source.projectcode = info.projectcode
  910. mergeArr = append(mergeArr, 3)
  911. is_replace = true
  912. }
  913. //4、采购单位
  914. if source.buyer == "" && info.buyer != "" {
  915. merge_recordMap["buyer"] = source.buyer
  916. source.buyer = info.buyer
  917. mergeArr = append(mergeArr, 4)
  918. is_replace = true
  919. }
  920. //5、预算
  921. if source.budget == 0 && info.budget != 0 {
  922. merge_recordMap["budget"] = source.budget
  923. source.budget = info.budget
  924. mergeArr = append(mergeArr, 5)
  925. is_replace = true
  926. }
  927. //6、中标单位
  928. if source.winner == "" && info.winner != "" {
  929. merge_recordMap["winner"] = source.winner
  930. source.winner = info.winner
  931. mergeArr = append(mergeArr, 6)
  932. is_replace = true
  933. }
  934. //7、中标金额
  935. if source.bidamount == 0 && info.bidamount != 0 {
  936. merge_recordMap["bidamount"] = source.bidamount
  937. source.bidamount = info.bidamount
  938. mergeArr = append(mergeArr, 7)
  939. is_replace = true
  940. }
  941. //8、开标时间-地点
  942. if source.bidopentime == 0 && info.bidopentime != 0 {
  943. merge_recordMap["bidopentime"] = source.bidopentime
  944. source.bidopentime = info.bidopentime
  945. mergeArr = append(mergeArr, 8)
  946. is_replace = true
  947. }
  948. //9、合同编号
  949. if source.contractnumber == "" && info.contractnumber != "" {
  950. merge_recordMap["contractnumber"] = source.contractnumber
  951. source.contractnumber = info.contractnumber
  952. mergeArr = append(mergeArr, 9)
  953. is_replace = true
  954. }
  955. //10、发布时间
  956. if source.publishtime == 0 && info.publishtime != 0 {
  957. merge_recordMap["publishtime"] = source.publishtime
  958. source.publishtime = info.publishtime
  959. mergeArr = append(mergeArr, 10)
  960. is_replace = true
  961. }
  962. //11、代理机构
  963. if source.agency == "" && info.agency != "" {
  964. merge_recordMap["agency"] = source.agency
  965. source.agency = info.agency
  966. mergeArr = append(mergeArr, 11)
  967. is_replace = true
  968. }
  969. if is_replace { //有过替换更新
  970. //总次数+1
  971. source.mergemap["total_num"] = util.Int64All(source.mergemap["total_num"]) + 1
  972. merge_recordMap["num"] = util.Int64All(source.mergemap["total_num"])
  973. //和哪一个数据id进行非空替换的-记录
  974. key := info.id
  975. source.mergemap[key] = merge_recordMap
  976. }
  977. //待进一步优化
  978. return source, mergeArr, is_replace
  979. }
  980. //权重评估
  981. func basicDataScore(v *Info, info *Info) bool {
  982. /*
  983. 权重评估
  984. 网站优先级判定规则:
  985. 1、中央>省>市>县区
  986. 2、政府采购>公共资源>采购单位官网>招标代理公司/平台
  987. */
  988. v_score, info_score := -1, -1
  989. dict_v := SiteMap[v.site]
  990. dict_info := SiteMap[info.site]
  991. //先判断level
  992. if dict_v != nil {
  993. v_level := util.ObjToString(dict_v["level"])
  994. if v_level == "中央" {
  995. v_score = 4
  996. } else if v_level == "省级" {
  997. v_score = 3
  998. } else if v_level == "市级" {
  999. v_score = 2
  1000. } else if v_level == "县区" {
  1001. v_score = 1
  1002. } else if v_level == "" {
  1003. } else {
  1004. v_score = 0
  1005. }
  1006. }
  1007. if dict_info != nil {
  1008. info_level := util.ObjToString(dict_info["level"])
  1009. if info_level == "中央" {
  1010. info_score = 4
  1011. } else if info_level == "省级" {
  1012. info_score = 3
  1013. } else if info_level == "市级" {
  1014. info_score = 2
  1015. } else if info_level == "县区" {
  1016. info_score = 1
  1017. } else if info_level == "" {
  1018. } else {
  1019. v_score = 0
  1020. }
  1021. }
  1022. if v_score > info_score {
  1023. return true
  1024. }
  1025. if v_score < info_score {
  1026. return false
  1027. }
  1028. //判断sitetype
  1029. if dict_v != nil {
  1030. v_sitetype := util.ObjToString(dict_v["sitetype"])
  1031. if v_sitetype == "政府采购" || v_sitetype == "政府门户" {
  1032. v_score = 4
  1033. } else if v_sitetype == "公共资源" {
  1034. v_score = 3
  1035. } else if v_sitetype == "官方网站" {
  1036. v_score = 2
  1037. } else if v_sitetype == "社会公共招标平台" || v_sitetype == "企业招标平台" {
  1038. v_score = 1
  1039. } else if v_sitetype == "" {
  1040. } else {
  1041. v_score = 0
  1042. }
  1043. }
  1044. if dict_info != nil {
  1045. info_sitetype := util.ObjToString(dict_info["sitetype"])
  1046. if info_sitetype == "政府采购" || info_sitetype == "政府门户" {
  1047. info_score = 4
  1048. } else if info_sitetype == "公共资源" {
  1049. info_score = 3
  1050. } else if info_sitetype == "官方网站" {
  1051. info_score = 2
  1052. } else if info_sitetype == "社会公共招标平台" || info_sitetype == "企业招标平台" {
  1053. info_score = 1
  1054. } else if info_sitetype == "" {
  1055. } else {
  1056. info_score = 0
  1057. }
  1058. }
  1059. if v_score > info_score {
  1060. return true
  1061. }
  1062. if v_score < info_score {
  1063. return false
  1064. }
  1065. //网站评估
  1066. m, n := 0, 0
  1067. if v.projectname != "" {
  1068. m++
  1069. }
  1070. if v.buyer != "" {
  1071. m++
  1072. }
  1073. if v.projectcode != "" || v.contractnumber != "" {
  1074. m++
  1075. }
  1076. if v.budget != 0 {
  1077. m++
  1078. }
  1079. if v.bidamount != 0 {
  1080. m++
  1081. }
  1082. if v.winner != "" {
  1083. m++
  1084. }
  1085. if v.bidopentime != 0 {
  1086. m++
  1087. }
  1088. if v.bidopenaddress != "" {
  1089. m++
  1090. }
  1091. if v.agency != "" {
  1092. m = m + 2
  1093. }
  1094. if v.city != "" {
  1095. m = m + 2
  1096. }
  1097. if info.projectname != "" {
  1098. n++
  1099. }
  1100. if info.buyer != "" {
  1101. n++
  1102. }
  1103. if info.projectcode != "" || info.contractnumber != "" {
  1104. n++
  1105. }
  1106. if info.budget != 0 {
  1107. n++
  1108. }
  1109. if info.bidamount != 0 {
  1110. n++
  1111. }
  1112. if info.winner != "" {
  1113. n++
  1114. }
  1115. if info.bidopentime != 0 {
  1116. n++
  1117. }
  1118. if info.bidopenaddress != "" {
  1119. n++
  1120. }
  1121. if info.agency != "" {
  1122. n = n + 2
  1123. }
  1124. if info.city != "" {
  1125. n = n + 2
  1126. }
  1127. if m > n {
  1128. return true
  1129. } else if m == n {
  1130. if v.publishtime >= info.publishtime {
  1131. return true
  1132. } else {
  1133. return false
  1134. }
  1135. } else {
  1136. return false
  1137. }
  1138. }
  1139. //无效数据
  1140. func invalidData(d1 string, d2 string, d3 string, d4 string) bool {
  1141. var n int
  1142. if d1 != "" {
  1143. n++
  1144. }
  1145. if d2 != "" {
  1146. n++
  1147. }
  1148. if d3 != "" {
  1149. n++
  1150. }
  1151. if d4 != "" {
  1152. n++
  1153. }
  1154. if n == 0 {
  1155. return true
  1156. }
  1157. return false
  1158. }
  1159. //迁移数据dupdays+5之前的数据
  1160. func movedata() {
  1161. sess := mgo.GetMgoConn()
  1162. defer mgo.DestoryMongoConn(sess)
  1163. year, month, day := time.Now().Date()
  1164. q := map[string]interface{}{
  1165. "comeintime": map[string]interface{}{
  1166. "$lt": time.Date(year, month, day, 0, 0, 0, 0, time.Local).Add(-time.Duration(dupdays) * 24 * time.Hour).Unix(),
  1167. },
  1168. }
  1169. log.Println(q)
  1170. it := sess.DB(mgo.DbName).C(extract).Find(&q).Iter()
  1171. index := 0
  1172. for tmp := make(map[string]interface{}); it.Next(&tmp); index++ {
  1173. mgo.Save(extract_back, tmp)
  1174. tmp = map[string]interface{}{}
  1175. if index%1000 == 0 {
  1176. log.Println("index", index)
  1177. }
  1178. }
  1179. log.Println("save to", extract_back, " ok index", index)
  1180. delnum := mgo.Delete(extract, q)
  1181. log.Println("remove from ", extract, delnum)
  1182. }