<commit_before>var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var underscore = require('underscore');
var request = require('request');
var Zone = require('../models/zone');
var MetaPoint = require('../models/Metapoint');

var http = require("http");
var ZonePrice = require('../models/zonePrice');
var unirest = require('unirest');
var httpinvoke = require('httpinvoke');
var async = require("async");
var EventEmitter = require('events').EventEmitter;

var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var pageSize = 10; //每页十条记录


 

var Cat = mongoose.model('Cat', { name: String });

var priceUrl ="http://sh.fangjia.com/trend/yearData?defaultCityName=上海&block=&keyword=&__ajax=1&districtName=";//=建新小区&region=杨浦

var region =[{"name":"pudong","cname":"浦东","num":34},{"name":"minhang","cname":"闵行","num":14},
	{"name":"xuhui","cname":"徐汇","num":20},{"name":"putuo","cname":"普陀","num":10},{"name":"changning","cname":"长宁","num":15},
	{"name":"jingan","cname":"静安","num":10},{"name":"huangpu","cname":"黄浦","num":8},{"name":"luwan","cname":"卢湾","num":7},
	{"name":"hongkou","cname":"虹口","num":14},{"name":"zhabei","cname":"闸北","num":10},{"name":"yangpu","cname":"杨浦","num":14},
	{"name":"baoshan","cname":"宝山","num":9},{"name":"songjiang","cname":"松江","num":9},{"name":"jiading","cname":"嘉定","num":9},
	{"name":"qingpu","cname":"青浦","num":5}];
	
	
var BJRegion = ["chaoyang","haidian","fengtai","dongchenga","xicheng","chongwen","xuanwu",
"shijingshan","changping","tongzhou","daxing","shunyi","huairou","fangshan",
"mentougou","miyun","pinggua","yanqing","zhoubiana"]
	

var district ={"pudong":"浦东","yangpu":"杨浦","minhang":"闵行","xuhui":"徐汇",
               "putuo":"普陀", "changning":"长宁", "jingan":"静安","huangpu":"黄浦","luwan":"卢湾",
               "hongkou":"虹口","zhabei":"闸北","yangpu":"杨浦","baoshan":"宝山",
 			   "songjiang":"松江","jiading":"嘉定","qingpu":"青浦"};


 			 



//服务器mongodb端口号为12345
mongoose.connect('mongodb://localhost:27017/map');


router.get('/', function(req, res) {
	res.render("index");	

});

router.post("/search", function(req, res){
	var district = req.body.district;
	var sign = req.body.sign;
	var pricerate = req.body.pricerate
	var pricerateyear = req.body.year;
	var price = req.body.price;
	if (sign == ">"){
		Zone.find({})
			.where("district").equals(district)
			.where(pricerateyear).gt(pricerate)
			.sort(pricerateyear)
			.exec(function(err, results){	
				var rtResults=[];
				for (var i =0; i<results.length;i++){ //返回的小区数量
					var now = results[i].zonePrices.length - 1;
					if (now > 0){
						var price_now = results[i].zonePrices[now].price//现在的价格
						if (price_now > price){
							rtResults.push(results[i]);
						}
					}				
				}
				rtResults = rtResults.reverse()
				res.json({
					state: 0,
					rtResults: rtResults			
				})
			})
	}
	else {
		Zone.find({})
			.where("district").equals(district)
			.where(pricerateyear).gt(pricerate)
			.sort(pricerateyear)
			.exec(function(err, results){	
				var rtResults=[];
				for (var i =0; i<results.length;i++){ //返回的小区数量
					var now = results[i].zonePrices.length - 1;					
					if (now > 0){
						var price_now = results[i].zonePrices[now].price//现在的价格
						if (price_now < price){
							rtResults.push(results[i]);
						}
					}				
				}
				rtResults = rtResults.reverse()
				res.json({
					state: 0,
					rtResults: rtResults			
				})
			})
	}
	
})


function saveBJZone(name,id,district){
	
	 	var priceUrl = "http://beijing.anjuke.com/ajax/pricetrend/comm?cid=";
       	var zone = new Zone({
			    	city: "Beijing",
				district: district,
				name: name,
		        x:0,
			    y:0
		    });
		    
	    //保存小区
	    zone.save(function(error,pZone){
		    	if (error) console.log(error);
		    	 console.log("save zone success.......",pZone.name); 
		    	  //生成小区房价
		    	  request(priceUrl+id,function(err,response,body){
			        
			     if (!err && response.statusCode == 200) {
			     	
			        var body =  eval ("(" + body + ")");
			        console.log("get price success.......",body,"..........",body["comm"]);
			          	var comms = body["comm"];			          	
			          	comms.forEach(function(comm){			          		
			          		for(var i in comm){
			          		
			          		    //生成价格
				    	          	var zonePrice = new ZonePrice({
									zone: pZone._id,
									time: i,
									price: comm[i],
									district:"Beijing"
		
								});
		
								zonePrice.save(function(error, pZonePrice) {
									if (error) console.log(error);		
								});			          			
			          		}	     		
			          	})			          				          	
			          }
	       })  					   							    	
	    });		
}


//对北京的房价进行处理
function praseBody(body, district,url){
	
	 if(typeof(body) == undefined) return false;
	
	 body=body.replace(/[\r\n]/g,"");
	
	 var reg =/xqlb(.+?)title/g;
	 var chReg = /[\u4e00-\u9fa5][\u4e00-\u9fa5_0-9_/+]{2,20}/ ;
	 var numReg = /[0-9]{5,}/  ;
	 var zones = body.match(reg);
	 
	 if(zones!=null && zones.length != 0){
	 	zones.forEach(function(zone){
	 			 		
	 		var name = zone.match(chReg);   //小区名称
	 		var AJKId = zone.match(numReg);  //小区在安居客的id
            if(name!=null){
            	   console.log("name.......",name[0], AJKId[0]);
          	   saveBJZone(name[0], AJKId[0],district)
            }
	 		
	 	})
	 }else{
	 	console.log("not matched",url);
	 }
}


//生成北京的小区以及房价
router.get("/genBJFangData",function(req,res){
	
	var url = "http://beijing.anjuke.com/community/";
	var testUrl ="http://beijing.anjuke.com/community/chaoyang/p100/";
	
//	request(testUrl,function(err,response,body){
//        if (!err && response.statusCode == 200) {
//        	 praseBody(body,'chaoyang');
//        	 
//        }
//	})
	
//	BJRegion.forEach(function(region){
        
        //不可用for，异步并发太多会被anjuke所屏蔽， 用async模块，并模拟随机事件
		var createZones = function(region, count){
			
			 	 console.log("create zones...............",region, count);
				 var rurl =  url+region+"p"+count;
				 request(rurl,function(err,response,body){
			            console.log("request url...............",rurl);
//						var zones = praseBody(body,region,rurl);
				 });
		}
		
//		var foo ＝ function(){
//			
//			console.log("I am do nothing");
//			
//		}

    async.forEachSeries(BJRegion,function (region, key, callback){
    	  
    	    var count = 0;

 	    console.log("region.....",region);

		async.whilst(
		    function () { return count < 100; },
		    function (cb) {
		        count++;
		        createZones(region,count);
		        console.log("count",count);
                setTimeout(cb,500);
		    }
		    ,
		    function (err, count) {
		    	
		        console.log("err...........",err,count);
		        
		        
		    }
		); //whilst
    	
     });//for eachof
		

			
	res.json({state:0});
})

//根据小区id获取zoneprices
router.post("/getPrices",function(req,res){
	
	var id = req.body.id;
	var name = req.body.name;
	
	console.log("getprices...........",id);
	
	 ZonePrice.find({"zone":id})
	 		  .sort({"time":1})
	 		  .exec(function(err, zoneprices){	  		            	  
  	  			   res.json({
						     state:0,
						     name:name,
						     zoneprices:zoneprices		        	    	
		        	    });
					  	  		
	})
})






//根据屏幕区域获取所有小区
router.post('/getMapZones',function(req,res){
	
	console.log("/getMapZones........");
	
	var leftX = req.body.leftX;
	var leftY = req.body.leftY;
	var rightX = req.body.rightX;
	var rightY = req.body.rightY;
	
	console.log("/getMapZones........",rightX,leftY,rightX,rightY);
	
	Zone.find({})
	    .where("x").gt(rightX).lt(leftX)
	    .where("y").gt(rightY).lt(leftY)
	    .exec(function(err,zones){
	    	   if(err !=null) console.log("find zone err",err);
	    	    
	    	   res.json({
             	   	  "state":0, 
             	   	  "zones":zones 
             	   })
	    	   
	    })
	
})


router.get('/modifyXy',function(req,res){
	
	var i=0;
	Zone.find({}).exec( <commit_msg>getMapZones select {name,x,y,priceRate}<commit_after>var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var underscore = require('underscore');
var request = require('request');
var Zone = require('../models/zone');
var MetaPoint = require('../models/Metapoint');

var http = require("http");
var ZonePrice = require('../models/zonePrice');
var unirest = require('unirest');
var httpinvoke = require('httpinvoke');
var async = require("async");
var EventEmitter = require('events').EventEmitter;

var Schema = mongoose.Schema;
var ObjectId = Schema.Types.ObjectId;
var pageSize = 10; //每页十条记录


 

var Cat = mongoose.model('Cat', { name: String });

var priceUrl ="http://sh.fangjia.com/trend/yearData?defaultCityName=上海&block=&keyword=&__ajax=1&districtName=";//=建新小区&region=杨浦

var region =[{"name":"pudong","cname":"浦东","num":34},{"name":"minhang","cname":"闵行","num":14},
	{"name":"xuhui","cname":"徐汇","num":20},{"name":"putuo","cname":"普陀","num":10},{"name":"changning","cname":"长宁","num":15},
	{"name":"jingan","cname":"静安","num":10},{"name":"huangpu","cname":"黄浦","num":8},{"name":"luwan","cname":"卢湾","num":7},
	{"name":"hongkou","cname":"虹口","num":14},{"name":"zhabei","cname":"闸北","num":10},{"name":"yangpu","cname":"杨浦","num":14},
	{"name":"baoshan","cname":"宝山","num":9},{"name":"songjiang","cname":"松江","num":9},{"name":"jiading","cname":"嘉定","num":9},
	{"name":"qingpu","cname":"青浦","num":5}];
	
	
var BJRegion = ["chaoyang","haidian","fengtai","dongchenga","xicheng","chongwen","xuanwu",
"shijingshan","changping","tongzhou","daxing","shunyi","huairou","fangshan",
"mentougou","miyun","pinggua","yanqing","zhoubiana"]
	

var district ={"pudong":"浦东","yangpu":"杨浦","minhang":"闵行","xuhui":"徐汇",
               "putuo":"普陀", "changning":"长宁", "jingan":"静安","huangpu":"黄浦","luwan":"卢湾",
               "hongkou":"虹口","zhabei":"闸北","yangpu":"杨浦","baoshan":"宝山",
 			   "songjiang":"松江","jiading":"嘉定","qingpu":"青浦"};


 			 



//服务器mongodb端口号为12345
mongoose.connect('mongodb://localhost:27017/map');


router.get('/', function(req, res) {
	res.render("index");	

});

router.post("/search", function(req, res){
	var district = req.body.district;
	var sign = req.body.sign;
	var pricerate = req.body.pricerate
	var pricerateyear = req.body.year;
	var price = req.body.price;
	if (sign == ">"){
		Zone.find({})
			.where("district").equals(district)
			.where(pricerateyear).gt(pricerate)
			.sort(pricerateyear)
			.exec(function(err, results){	
				var rtResults=[];
				for (var i =0; i<results.length;i++){ //返回的小区数量
					var now = results[i].zonePrices.length - 1;
					if (now > 0){
						var price_now = results[i].zonePrices[now].price//现在的价格
						if (price_now > price){
							rtResults.push(results[i]);
						}
					}				
				}
				rtResults = rtResults.reverse()
				res.json({
					state: 0,
					rtResults: rtResults			
				})
			})
	}
	else {
		Zone.find({})
			.where("district").equals(district)
			.where(pricerateyear).gt(pricerate)
			.sort(pricerateyear)
			.exec(function(err, results){	
				var rtResults=[];
				for (var i =0; i<results.length;i++){ //返回的小区数量
					var now = results[i].zonePrices.length - 1;					
					if (now > 0){
						var price_now = results[i].zonePrices[now].price//现在的价格
						if (price_now < price){
							rtResults.push(results[i]);
						}
					}				
				}
				rtResults = rtResults.reverse()
				res.json({
					state: 0,
					rtResults: rtResults			
				})
			})
	}
	
})


function saveBJZone(name,id,district){
	
	 	var priceUrl = "http://beijing.anjuke.com/ajax/pricetrend/comm?cid=";
       	var zone = new Zone({
			    	city: "Beijing",
				district: district,
				name: name,
		        x:0,
			    y:0
		    });
		    
	    //保存小区
	    zone.save(function(error,pZone){
		    	if (error) console.log(error);
		    	 console.log("save zone success.......",pZone.name); 
		    	  //生成小区房价
		    	  request(priceUrl+id,function(err,response,body){
			        
			     if (!err && response.statusCode == 200) {
			     	
			        var body =  eval ("(" + body + ")");
			        console.log("get price success.......",body,"..........",body["comm"]);
			          	var comms = body["comm"];			          	
			          	comms.forEach(function(comm){			          		
			          		for(var i in comm){
			          		
			          		    //生成价格
				    	          	var zonePrice = new ZonePrice({
									zone: pZone._id,
									time: i,
									price: comm[i],
									district:"Beijing"
		
								});
		
								zonePrice.save(function(error, pZonePrice) {
									if (error) console.log(error);		
								});			          			
			          		}	     		
			          	})			          				          	
			          }
	       })  					   							    	
	    });		
}


//对北京的房价进行处理
function praseBody(body, district,url){
	
	 if(typeof(body) == undefined) return false;
	
	 body=body.replace(/[\r\n]/g,"");
	
	 var reg =/xqlb(.+?)title/g;
	 var chReg = /[\u4e00-\u9fa5][\u4e00-\u9fa5_0-9_/+]{2,20}/ ;
	 var numReg = /[0-9]{5,}/  ;
	 var zones = body.match(reg);
	 
	 if(zones!=null && zones.length != 0){
	 	zones.forEach(function(zone){
	 			 		
	 		var name = zone.match(chReg);   //小区名称
	 		var AJKId = zone.match(numReg);  //小区在安居客的id
            if(name!=null){
            	   console.log("name.......",name[0], AJKId[0]);
          	   saveBJZone(name[0], AJKId[0],district)
            }
	 		
	 	})
	 }else{
	 	console.log("not matched",url);
	 }
}


//生成北京的小区以及房价
router.get("/genBJFangData",function(req,res){
	
	var url = "http://beijing.anjuke.com/community/";
	var testUrl ="http://beijing.anjuke.com/community/chaoyang/p100/";
	
//	request(testUrl,function(err,response,body){
//        if (!err && response.statusCode == 200) {
//        	 praseBody(body,'chaoyang');
//        	 
//        }
//	})
	
//	BJRegion.forEach(function(region){
        
        //不可用for，异步并发太多会被anjuke所屏蔽， 用async模块，并模拟随机事件
		var createZones = function(region, count){
			
			 	 console.log("create zones...............",region, count);
				 var rurl =  url+region+"p"+count;
				 request(rurl,function(err,response,body){
			            console.log("request url...............",rurl);
//						var zones = praseBody(body,region,rurl);
				 });
		}
		
//		var foo ＝ function(){
//			
//			console.log("I am do nothing");
//			
//		}

    async.forEachSeries(BJRegion,function (region, key, callback){
    	  
    	    var count = 0;

 	    console.log("region.....",region);

		async.whilst(
		    function () { return count < 100; },
		    function (cb) {
		        count++;
		        createZones(region,count);
		        console.log("count",count);
                setTimeout(cb,500);
		    }
		    ,
		    function (err, count) {
		    	
		        console.log("err...........",err,count);
		        
		        
		    }
		); //whilst
    	
     });//for eachof
		

			
	res.json({state:0});
})

//根据小区id获取zoneprices
router.post("/getPrices",function(req,res){
	
	var id = req.body.id;
	var name = req.body.name;
	
	console.log("getprices...........",id);
	
	 ZonePrice.find({"zone":id})
	 		  .sort({"time":1})
	 		  .exec(function(err, zoneprices){	  		            	  
  	  			   res.json({
						     state:0,
						     name:name,
						     zoneprices:zoneprices		        	    	
		        	    });
					  	  		
	})
})






//根据屏幕区域获取所有小区
router.post('/getMapZones',function(req,res){
	
	console.log("/getMapZones........");
	
	var leftX = req.body.leftX;
	var leftY = req.body.leftY;
	var rightX = req.body.rightX;
	var rightY = req.body.rightY;
	
	console.log("/getMapZones........",rightX,leftY,rightX,rightY);
	
	Zone.find({})
		.select("name x y priceRate")
	    .where("x").gt(rightX).lt(leftX)
	    .where("y").gt(rightY).lt(leftY)
	    .exec(function(err,zones){
	    	   if(err !=null) console.log("find zone err",err);
	    	    
	    	   res.json({
             	   	  "state":0, 
             	   	  "zones":zones 
             	   })
	    	   
	    })
	
})


router.get('/modifyXy',function(req,res){
	
	var i=0;
	Zone.find({}).exec( 
{'fraction_non_alphanumeric': 0.2069524046847745, 'fraction_numerical': 0.011338151009220036, 'mean_word_length': 2.3810025273799496, 'pattern_counts': {'":': 130, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6669411', 'n_tokens_mistral': 7108, 'n_tokens_neox': 6043, 'n_words': 796}