明树Git Lab

Commit 0dc6afe3 authored by zfp1's avatar zfp1

update

parent 10fbc5f6
......@@ -10,31 +10,54 @@ async function plantearlywarning(params) {
users = await DB.User.find({ roles: role._id }).select('_id');
}
//这里暂时不确定 收获周期的格式
let plants = await DB.PlantPlan.find({del: 0}).populate({path: 'plantCycle'}).lean().exec();
let plants = await DB.PlantPlan.find({ del: 0 }).populate({path: "land"}).populate({ path: 'plantCycle' }).lean().exec();
let exceeds = [];
console.log(plants, "====")
for (let index = 0; index < plants.length; index++) {
const element = plants[index];
console.log(element.warning, "======")
let cycles = element.plantCycle || [];
for (let i = 0; i < cycles.length; i++) {
const cyc = cycles[i];
if(cyc.times && cyc.times.length) {
if (cyc.times && cyc.times.length) {
for (let ii = 0; ii < cyc.times.length; ii++) {
const ti = cyc.times[ii];
if(ti.startTime && ti.endTime) {
let st = moment().diff(moment(ti.startTime), 'days');
let et = moment().diff(moment(ti.endTime), 'days');
if(st >= 0 && et <= 0) {
element.warning = element.warning || [];
let key = `${element._id}_${cyc._id}_${ti._id}`;
if(!element.warning.includes(key) && ti.startTime && ti.endTime){
let months = getMonthsBetweenDates(ti.startTime, ti.endTime);
let startMonth = new Date(ti.startTime).getMonth() + 1;
let startDay = new Date(ti.startTime).getDate();
let endMonth = new Date(ti.endTime).getMonth() + 1;
let endDay = new Date(ti.endTime).getDate();
let currentMonth = new Date().getMonth() + 1;
let currentDay = new Date().getDate();
console.log(startMonth, currentMonth, startDay, currentDay, endMonth, endDay)
if (months.includes(currentMonth)) {
if ((startMonth == currentMonth) && startDay <= currentDay) {
exceeds.push({
...cyc,
})
...element,
});
element.warning.push(key);
} else if ((endMonth == currentMonth) && currentDay <= endDay) {
exceeds.push({
...element,
});
element.warning.push(key);
} else {
exceeds.push({
...element,
});
element.warning.push(key);
}
}
console.log(exceeds, "===============")
}
}
}
}
await DB.PlantPlan.findOneAndUpdate({_id: element._id}, {warning: element.warning});
}
if(users.length && exceeds.length) {
if (users.length && exceeds.length) {
for (let u = 0; u < users.length; u++) {
const user = users[u];
await DB.Notice.create({
......@@ -49,6 +72,19 @@ async function plantearlywarning(params) {
}
}
function getMonthsBetweenDates(startTime, endTime) {
let months = [];
let currentDate = new Date(startTime);
while (currentDate <= endTime) {
months.push(new Date(currentDate).getMonth() + 1);
currentDate.setMonth(currentDate.getMonth() + 1);
}
return months;
}
module.exports = {
plantearlywarning
}
\ No newline at end of file
......@@ -83,6 +83,8 @@ const PlantPlanSchema = new Schema({
comment: "种植周期",
},
warning: [],
createdAt: {
......
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