明树Git Lab

Commit d46bc0ae authored by zengfanpei's avatar zengfanpei

update:排序

parent 343da57f
......@@ -35,7 +35,9 @@ async function getSMCChangeTrend(req, res, next) {
potassium: { $avg: "$potassium" },
soilPH: { $avg: "$soilPH" },
}
}
},
{ $sort: { _id: 1 } }
];
let ret = await DB.CollectSMC.aggregate(aggr);
res.sendData(ret);
......@@ -70,7 +72,8 @@ async function getWSChangeTrend(req, res, next) {
windDirection: { $avg: "$windDirection" },
rainfall: { $avg: "$rainfall" },
}
}
},
{ $sort: { _id: 1 } }
];
let ret = await DB.CollectWS.aggregate(aggr);
res.sendData(ret);
......@@ -174,41 +177,43 @@ async function getDeviceCount(req, res, next) {
async function getAreaDistribution(req, res, next) {
try {
// 跟前端约定 传递对应区域名称
let zonePre = await DB.Zone.findOne({name: req.body.name, del: 0});
if(!zonePre) {
let zonePre = await DB.Zone.findOne({ name: req.body.name, del: 0 });
if (!zonePre) {
res.sendError(errorMessage.resourceNotFound);
}
//往下取一层
let len = zonePre.parentIds && zonePre.parentIds.length || 0;
let zones = await DB.Zone.find({"parentIds": {$size: len + 1, $elemMatch: {$eq: zonePre._id}}}).lean().exec(); //查找所有子区域
let zoneIds = zones.map(o => {return o._id});
let zones = await DB.Zone.find({ "parentIds": { $size: len + 1, $elemMatch: { $eq: zonePre._id } } }).lean().exec(); //查找所有子区域
let zoneIds = zones.map(o => { return o._id });
zoneIds.push(zonePre._id);
let lands = await DB.Land.aggregate([
{$match: {zone: {$in: zoneIds }}},
{$group: {
_id: "$varieties",
area: {$sum: "$area"},
latestYield: {$sum: "$latestYield"},
latestYieldValue: {$sum: "$latestYieldValue"},
}}
{ $match: { zone: { $in: zoneIds } } },
{
$group: {
_id: "$varieties",
area: { $sum: "$area" },
latestYield: { $sum: "$latestYield" },
latestYieldValue: { $sum: "$latestYieldValue" },
}
}
]);
let totalArea = _.sumBy(lands, 'area');
let totalArea = _.sumBy(lands, 'area');
let totalYield = _.sumBy(lands, 'latestYield');
let totalYieldValue = _.sumBy(lands, 'latestYieldValue');
let data = [];
for (let index = 0; index < lands.length; index++) {
const element = lands[index];
if(totalArea != 0) {
if (totalArea != 0) {
element.areaPercentage = _.multiply(_.divide(element.area, totalArea), 100).toFixed(0) + '%';
} else {
element.areaPercentage = "0%"
}
if(totalYield != 0) {
if (totalYield != 0) {
element.yieldPercentage = _.multiply(_.divide(element.latestYield, totalYield), 100).toFixed(0) + '%';
} else {
element.yieldPercentage = "0%"
}
if(totalYieldValue != 0) {
if (totalYieldValue != 0) {
element.yieldValuePercentage = _.multiply(_.divide(element.latestYieldValue, totalYieldValue), 100).toFixed(0) + '%';
} else {
element.yieldValuePercentage = "0%"
......@@ -232,17 +237,17 @@ async function getAreaDistribution2(req, res, next) {
year: moment().format("YYYY")
}
// 跟前端约定 传递对应区域名称
if(req.body.name == "雷州半岛") {
if (req.body.name == "雷州半岛") {
search.county = "雷州市";
} else
if(req.body.name == "乐田农业") {
search.$or = [
{county: {$regex: "乐田"}},
{zone: {$regex: "乐田"}}
];
} else {
search.plantArea = req.body.name;
}
if (req.body.name == "乐田农业") {
search.$or = [
{ county: { $regex: "乐田" } },
{ zone: { $regex: "乐田" } }
];
} else {
search.plantArea = req.body.name;
}
let areaReds = await DB.AreaRecord.find(search);
res.sendData(areaReds);
} catch (error) {
......
......@@ -45,7 +45,7 @@ const collectWeatherStationSchema = new Schema({
},
windDirection: {
type: Number,
type: String,
comment: "风向",
},
windDirectionUnit: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment