agent-backend/contracts/AppDataAnalysis/DataAnalysis.yjs

300 lines
11 KiB
Plaintext
Raw Permalink Normal View History

2021-09-26 04:49:24 +00:00
import "loadResource.yjs"
contract AppDataContract{
function onCreate(arg){
var ret =
executeContract("AppDataSource","apply","我是数据分析合约合约名称DataAnalysis");
print("onCreate:"+ret);
return ret;
}
export function getMainFrame(arg){
return "/html/main.html";
}
export function connectDBAndQueryChengjiao(args) {
args = JSON.parse(args);
if (args["type"] == "chengjiao") {
//TODO change!
if (this.ret_chengjiao != null && this.ret_chengjiao[args["district"]] != null) {
print("find!");
return JSON.stringify(this.ret_chengjiao[args["district"]]);
}
if (this.houselist_chengjiao == null || this.districtDict_chengjiao == null || this.bussinessDict_chengjiao == null) {
this.houselist_chengjiao = [];
var houselist = this.houselist_chengjiao;
var districtDict = {
};
var pattern = /朝阳|西城|丰台|海淀|密云|东城|昌平|怀柔|大兴|通州|房山|石景山|平谷|顺义|延庆|门头沟/g;
var bussinessDict2 = {
};
for (var i = 0; i < houselist.length; i++) {
var addr = houselist[i]["district"];
var district = addr.match(pattern);
if (district == null)
continue;
district = district[0];
if (district.length > 5)
continue;
if (districtDict[district] == null)
districtDict[district] = [];
if (bussinessDict2[district] == null)
bussinessDict2[district] = {
};
var buss = houselist[i]["bussiness"];
if (bussinessDict2[district][buss] == null)
bussinessDict2[district][buss] = [];
var item = {};
if (houselist[i]["price"] == null || houselist[i]["area"] == null)
continue;
if (houselist[i]["roomNum"] == null)
continue;
var unitPrice = houselist[i]["price"].replace("挂牌", "").replace("万", "");
unitPrice = parseInt(unitPrice);
houselist[i]["price"] = unitPrice;
item["price"] = unitPrice;
item["roomNum"] = houselist[i]["roomNum"];
item["area"] = parseInt(houselist[i]["area"].replace("平米", ""));
houselist[i]["area"] = item["area"];
item["district"] = houselist[i]["district"];
item["bussiness"] = houselist[i]["bussiness"];
item["buildingAge"] = houselist[i]["buildingAge"].replace("塔结合", "").replace("年建板", "").replace("楼", "");
districtDict[district].push(item);
bussinessDict2[district][buss].push(item);
}
districtDict["延庆"] = [];
districtDict["怀柔"] = [];
bussinessDict2["延庆"] = {
};
bussinessDict2["怀柔"] = {
};
this.districtDict_chengjiao = districtDict;
var districtnames = Object.keys(districtDict);
var bussinessDict = {
};
for (var i = 0; i < districtnames.length; i++) {
var district = districtnames[i];
bussinessDict[district] = {
};
var desc = function (a, b) {
return (bussinessDict2[district][a].length < bussinessDict2[district][b].length) ? 1 : -1;
};
var bussnames = (Object.keys(bussinessDict2[district])).sort(desc);
var len = bussnames.length;
if (len > 20)
len = 20;
for (j = 0; j < len; j++) {
bussinessDict[district][bussnames[j]] = bussinessDict2[district][bussnames[j]];
}
}
this.bussinessDict_chengjiao = bussinessDict;
}
var houselist = this.houselist_chengjiao;
var districtDict = this.districtDict_chengjiao;
var bussinessDict = this.bussinessDict_chengjiao;
var districtkeys = Object.keys(districtDict);
var fig1 = [];
for (var j = 0; j < districtkeys.length; j++) {
var key = districtkeys[j];
var item = {
}
var name = key + "区";
if (key == "延庆" || key == "密云")
name = key + "县";
item["name"] = name;
item["value"] = {
};
item["value"]["num"] = districtDict[key].length;
var sumPrice = 0;
for (var i = 0; i < districtDict[key].length; i++) {
if (districtDict[key][i]["price"] >= 0 && districtDict[key][i]["price"] <= 10000000)
sumPrice += districtDict[key][i]["price"];
}
var avgPrice = 0;
if (districtDict[key].length > 0) {
avgPrice = sumPrice / districtDict[key].length;
}
item["value"]["avgPrice"] = avgPrice.toFixed(2);
fig1.push(item);
}
var houselist2 = houselist;
if (args["district"] != "" && districtDict[args["district"]] != null) {
houselist2 = districtDict[args["district"]];
}
var fig2 = [];
var priceInterval = ["<200w", "200-400w", "400-600w", "600-800w", "800-1000w", ">1000w"];
var houseNum = [0, 0, 0, 0, 0, 0, 0];
for (var i = 0; i < houselist2.length; i++) {
if (houselist2[i]["price"] < 200) {
houseNum[0] += 1;
}
if (houselist2[i]["price"] >= 200 && houselist2[i]["price"] < 400) {
houseNum[1] += 1;
}
if (houselist2[i]["price"] >= 400 && houselist2[i]["price"] < 600) {
houseNum[2] += 1;
}
if (houselist2[i]["price"] >= 600 && houselist2[i]["price"] < 800) {
houseNum[3] += 1;
}
if (houselist2[i]["price"] >= 800 && houselist2[i]["price"] < 1000) {
houseNum[4] += 1;
}
if (houselist2[i]["price"] >= 1000) {
houseNum[5] += 1;
}
}
var cnt = 0;
for (var i = 0; i < priceInterval.length; i++) {
var item = {
};
cnt += houseNum[i];
item["name"] = priceInterval[i];
item["value"] = cnt;
fig2.push(item);
}
var districtDict2 = districtDict;
if (args["district"] != "" && districtDict[args["district"]] != null) {
districtDict2 = bussinessDict[args["district"]];
districtkeys = Object.keys(bussinessDict[args["district"]]);
}
var fig3 = [];
var tmpList = [];
var desc = function (x, y) {
return (x["value"] < y["value"]) ? 1 : -1;
}
for (var i = 0; i < districtkeys.length; i++) {
var item = {
"name": districtkeys[i]};
item["value"] = districtDict2[districtkeys[i]].length;
if (item["value"] >= 0 && item["value"] < 10000000000)
tmpList.push(item);
}
tmpList.sort(desc);
var len = tmpList.length;
if (len > 50) len = 50;
for (var i = 0; i < len; i++) {
fig3.push(tmpList[i]);
}
var fig4 = [];
var roomTypeName = ["一居", "二居", "三居", "四居及以上"];
var areaSizeName = ["50m2以下", "50-80m2", "80-110m2", "110-150m2", "150-300m2", ">300m2"];
var priceName = ["<200w", "200-400w", "400-600w", "600-800w", "800-1000w", ">1000w"];
var yearName = ["<5年", "5-10年", "10-15年", "15-20年", "20-30年", ">30年"];
for (var k = 0; k < districtkeys.length; k++) {
var district = districtkeys[k];
var districtItem = {
"name": district};
districtItem["value"] = {
"roomType": [], "areaSize": [], "price": [], "age": []};
var roomTypeNum = [0, 0, 0, 0];
var areaSizeNum = [0, 0, 0, 0, 0, 0];
var priceNum = [0, 0, 0, 0, 0, 0];
var yearNum = [0, 0, 0, 0, 0, 0];
for (var j = 0; j < districtDict2[district].length; j++) {
var house = districtDict2[district][j];
var flag = false;
if (house["roomNum"].match("1室") != null) {
roomTypeNum[0] += 1;
flag = true;
}
if (house["roomNum"].match("2室") != null) {
roomTypeNum[1] += 1;
flag = true;
}
if (house["roomNum"].match("3室") != null) {
roomTypeNum[2] += 1;
flag = true;
}
if (flag == false) {
roomTypeNum[3] += 1;
}
if (house["area"] < 50)
areaSizeNum[0] += 1;
if (house["area"] >= 50 && house["area"] < 80)
areaSizeNum[1] += 1;
if (house["area"] >= 80 && house["area"] < 110)
areaSizeNum[2] += 1;
if (house["area"] >= 110 && house["area"] < 150)
areaSizeNum[3] += 1;
if (house["area"] >= 150 && house["area"] < 300)
areaSizeNum[4] += 1;
if (house["area"] >= 300)
areaSizeNum[5] += 1;
if (house["price"] < 200)
priceNum[0] += 1;
if (house["price"] >= 200 && house["price"] < 400)
priceNum[1] += 1;
if (house["price"] >= 400 && house["price"] < 800)
priceNum[2] += 1;
if (house["price"] >= 800 && house["price"] < 1000)
priceNum[3] += 1;
if (house["price"] >= 1000)
priceNum[4] += 1;
var age = 2019 - house["buildingAge"];
if (age < 5)
yearNum[0] += 1;
if (age >= 5 && age < 10)
yearNum[1] += 1;
if (age >= 10 && age < 15)
yearNum[2] += 1;
if (age >= 15 && age < 20)
yearNum[3] += 1;
if (age >= 20 && age < 30)
yearNum[4] += 1;
if (age >= 30)
yearNum[5] += 1;
}
for (var i = 0; i < roomTypeName.length; i++) {
var item = {
};
item["name"] = roomTypeName[i];
item["value"] = roomTypeNum[i];
districtItem["value"]["roomType"].push(item);
}
for (var i = 0; i < areaSizeName.length; i++) {
var item = {
};
item["name"] = areaSizeName[i];
item["value"] = areaSizeNum[i];
districtItem["value"]["areaSize"].push(item);
}
for (var i = 0; i < priceNum.length; i++) {
var item = {
};
item["name"] = priceName[i];
item["value"] = priceNum[i];
districtItem["value"]["price"].push(item);
}
for (var i = 0; i < yearNum.length; i++) {
var item = {
};
item["name"] = yearName[i];
item["value"] = yearNum[i];
districtItem["value"]["age"].push(item);
}
fig4.push(districtItem);
}
if (this.ret_chengjiao == null)
this.ret_chengjiao = {
};
var res = {
};
res["fig1"] = fig1;
res["fig2"] = fig2;
res["fig3"] = fig3;
res["fig4"] = fig4;
if (this.ret_chengjiao[args["district"]] == null)
this.ret_chengjiao[args["district"]] = res;
return JSON.stringify(res);
}
else
return null;
}
export function connectDBAndQuery(args) {
print("args:" + args);
var result = executeContract("AppDataSource","connectDBAndQuery",args);
var obj = JSON.parse(result);
print(result);
return obj.result;
}
}