欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。

修剪管理系统概述
修剪是植物养护的重要环节,它促进植物的生长和塑形。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个完整的修剪记录系统,包括修剪记录的创建、查询、统计和提醒功能。这个系统需要记录修剪的类型、位置和效果。
修剪记录数据模型
class PruningType {
constructor(id, name, description, purpose) {
this.id = id;
this.name = name;
this.description = description;
this.purpose = purpose; // 修剪目的
}
}
class PruningRecord {
constructor(plantId, pruningType, location, notes) {
this.id = 'prune_' + Date.now();
this.plantId = plantId;
this.pruningType = pruningType;
this.location = location; // 修剪位置
this.date = new Date();
this.notes = notes;
this.branchesRemoved = 0; // 移除的枝条数
}
}
class PruningManager {
constructor() {
this.records = [];
this.pruningTypes = [];
this.initDefaultPruningTypes();
this.loadFromStorage();
}
initDefaultPruningTypes() {
this.pruningTypes = [
new PruningType('prune_1', '轻修剪', '移除枯死或病弱枝条', '清理'),
new PruningType('prune_2', '中度修剪', '塑形和促进分枝', '塑形'),
new PruningType('prune_3', '重度修剪', '大幅度修剪以促进新生长', '更新'),
new PruningType('prune_4', '摘心', '移除顶端生长点', '促进分枝')
];
}
addPruningRecord(plantId, pruningType, location, notes) {
const record = new PruningRecord(plantId, pruningType, location, notes);
this.records.push(record);
this.saveToStorage();
return record;
}
}
这个修剪记录数据模型定义了PruningType、PruningRecord和PruningManager类。PruningType类定义了修剪类型及其目的,PruningRecord类记录每次修剪的详细信息,PruningManager类管理所有修剪记录。
与OpenHarmony数据库的集成
function savePruningRecordToDatabase(record) {
cordova.exec(
function(result) {
console.log("修剪记录已保存到数据库");
},
function(error) {
console.error("保存失败:", error);
},
"DatabasePlugin",
"savePruningRecord",
[{
id: record.id,
plantId: record.plantId,
pruningType: record.pruningType,
location: record.location,
date: record.date.toISOString(),
notes: record.notes,
branchesRemoved: record.branchesRemoved
}]
);
}
function loadPruningRecordsFromDatabase() {
cordova.exec(
function(result) {
console.log("修剪记录已从数据库加载");
pruningManager.records = result.map(rec => {
const record = new PruningRecord(
rec.plantId,
rec.pruningType,
rec.location,
rec.notes
);
record.id = rec.id;
record.date = new Date(rec.date);
record.branchesRemoved = rec.branchesRemoved;
return record;
});
renderPruningRecords();
},
function(error) {
console.error("加载失败:", error);
},
"DatabasePlugin",
"loadPruningRecords",
[]
);
}
这段代码展示了如何与OpenHarmony的数据库进行交互。savePruningRecordToDatabase函数将修剪记录保存到数据库,loadPruningRecordsFromDatabase函数从数据库加载所有修剪记录。通过这种方式,我们确保了修剪数据的持久化存储。
修剪记录列表展示
function renderPruningRecords(plantId) {
const plant = plants.find(p => p.id === plantId);
if (!plant) return;
const records = pruningManager.records.filter(r => r.plantId === plantId)
.sort((a, b) => new Date(b.date) - new Date(a.date));
const container = document.getElementById('page-container');
container.innerHTML = `
<div class="pruning-records-container">
<h2>${plant.name} 的修剪记录</h2>
<button class="add-record-btn" onclick="showAddPruningRecordDialog('${plantId}')">
✂️ 添加修剪记录
</button>
</div>
`;
if (records.length === 0) {
container.innerHTML += '<p class="empty-message">还没有修剪记录</p>';
return;
}
const recordsList = document.createElement('div');
recordsList.className = 'records-list';
records.forEach(record => {
const pruningType = pruningManager.pruningTypes.find(
p => p.id === record.pruningType
);
const recordItem = document.createElement('div');
recordItem.className = 'record-item';
recordItem.innerHTML = `
<div class="record-info">
<p class="record-date">${record.date.toLocaleString('zh-CN')}</p>
<p class="pruning-type">✂️ 修剪类型: ${pruningType?.name || '未知'}</p>
<p class="pruning-purpose">目的: ${pruningType?.purpose || 'N/A'}</p>
<p class="pruning-location">位置: ${record.location}</p>
<p class="branches-removed">移除枝条: ${record.branchesRemoved}条</p>
${record.notes ? `<p class="record-notes">备注: ${record.notes}</p>` : ''}
</div>
<div class="record-actions">
<button onclick="editPruningRecord('${record.id}')">编辑</button>
<button onclick="deletePruningRecord('${record.id}')">删除</button>
</div>
`;
recordsList.appendChild(recordItem);
});
container.appendChild(recordsList);
}
这个函数负责渲染修剪记录列表。它显示了特定植物的所有修剪记录,包括日期、修剪类型、目的、位置和移除的枝条数。用户可以通过"编辑"和"删除"按钮管理记录。这种设计提供了清晰的记录展示。
添加修剪记录对话框
function showAddPruningRecordDialog(plantId) {
const dialog = document.createElement('div');
dialog.className = 'modal-dialog';
let pruningTypeOptions = '';
pruningManager.pruningTypes.forEach(type => {
pruningTypeOptions += `
<option value="${type.id}">${type.name} - ${type.purpose}</option>
`;
});
dialog.innerHTML = `
<div class="modal-content">
<h3>添加修剪记录</h3>
<form id="add-pruning-form">
<div class="form-group">
<label>修剪类型</label>
<select id="pruning-type" required>
<option value="">请选择修剪类型</option>
${pruningTypeOptions}
</select>
</div>
<div class="form-group">
<label>修剪位置</label>
<input type="text" id="pruning-location" placeholder="例如: 顶端、左侧枝条" required>
</div>
<div class="form-group">
<label>移除枝条数</label>
<input type="number" id="branches-removed" min="0" value="0">
</div>
<div class="form-group">
<label>修剪日期</label>
<input type="datetime-local" id="pruning-date" required>
</div>
<div class="form-group">
<label>备注</label>
<textarea id="pruning-notes"></textarea>
</div>
<div class="form-actions">
<button type="submit">保存</button>
<button type="button" onclick="closeDialog()">取消</button>
</div>
</form>
</div>
`;
document.getElementById('modal-container').appendChild(dialog);
const now = new Date();
document.getElementById('pruning-date').value = now.toISOString().slice(0, 16);
document.getElementById('add-pruning-form').addEventListener('submit', function(e) {
e.preventDefault();
const pruningType = document.getElementById('pruning-type').value;
const location = document.getElementById('pruning-location').value;
const branchesRemoved = parseInt(document.getElementById('branches-removed').value);
const date = new Date(document.getElementById('pruning-date').value);
const notes = document.getElementById('pruning-notes').value;
const record = new PruningRecord(plantId, pruningType, location, notes);
record.date = date;
record.branchesRemoved = branchesRemoved;
pruningManager.records.push(record);
pruningManager.saveToStorage();
savePruningRecordToDatabase(record);
closeDialog();
renderPruningRecords(plantId);
showToast('修剪记录已添加');
});
}
这个函数创建并显示添加修剪记录的对话框。用户可以选择修剪类型、输入位置、枝条数、日期和备注。提交后,新记录会被添加到pruningManager中,并保存到数据库。
修剪统计功能
class PruningStatistics {
constructor(pruningManager) {
this.pruningManager = pruningManager;
}
getTotalPruningCount(plantId) {
return this.pruningManager.records.filter(r => r.plantId === plantId).length;
}
getTotalBranchesRemoved(plantId) {
const records = this.pruningManager.records.filter(r => r.plantId === plantId);
return records.reduce((sum, r) => sum + r.branchesRemoved, 0);
}
getMostCommonPruningType(plantId) {
const records = this.pruningManager.records.filter(r => r.plantId === plantId);
const typeCounts = {};
records.forEach(record => {
typeCounts[record.pruningType] = (typeCounts[record.pruningType] || 0) + 1;
});
const mostCommon = Object.keys(typeCounts).reduce((a, b) =>
typeCounts[a] > typeCounts[b] ? a : b
);
return this.pruningManager.pruningTypes.find(p => p.id === mostCommon);
}
getPruningFrequency(plantId, days = 30) {
const records = this.pruningManager.records.filter(r => r.plantId === plantId);
const cutoffDate = new Date();
cutoffDate.setDate(cutoffDate.getDate() - days);
const recentRecords = records.filter(r => new Date(r.date) > cutoffDate);
return (recentRecords.length / days * 7).toFixed(2); // 每周修剪次数
}
}
这个PruningStatistics类提供了修剪的统计功能。getTotalPruningCount返回修剪总次数,getTotalBranchesRemoved计算移除的总枝条数,getMostCommonPruningType返回最常用的修剪类型,getPruningFrequency计算修剪频率。这些统计信息可以帮助用户了解修剪规律。
修剪提醒功能
function checkPruningReminders() {
plants.forEach(plant => {
const lastPruningDate = getLastPruningDate(plant.id);
const pruningInterval = plant.pruningInterval || 30; // 默认30天
if (!lastPruningDate) {
sendPruningReminder(plant.id, plant.name, '从未修剪过');
return;
}
const daysSincePruning = Math.floor(
(new Date() - new Date(lastPruningDate)) / (24 * 60 * 60 * 1000)
);
if (daysSincePruning >= pruningInterval) {
sendPruningReminder(plant.id, plant.name, `已${daysSincePruning}天未修剪`);
}
});
}
function sendPruningReminder(plantId, plantName, message) {
cordova.exec(
function(result) {
console.log("修剪提醒已发送");
},
function(error) {
console.error("提醒发送失败:", error);
},
"NotificationPlugin",
"sendReminder",
[{
title: `${plantName} 需要修剪`,
message: message,
plantId: plantId,
type: 'pruning'
}]
);
}
function getLastPruningDate(plantId) {
const records = pruningManager.records.filter(r => r.plantId === plantId)
.sort((a, b) => new Date(b.date) - new Date(a.date));
return records.length > 0 ? records[0].date : null;
}
setInterval(checkPruningReminders, 60 * 60 * 1000); // 每小时检查一次
这段代码实现了修剪提醒功能。checkPruningReminders函数检查所有植物,如果某个植物超过设定的修剪间隔,就发送提醒。通过NotificationPlugin,我们可以向用户发送系统通知。
修剪效果追踪
class PruningEffectTracking {
constructor() {
this.effectRecords = new Map(); // 修剪ID -> 效果数据
}
recordPruningEffect(pruningRecordId, newGrowthDays, growthRate) {
this.effectRecords.set(pruningRecordId, {
recordedDate: new Date(),
newGrowthDays: newGrowthDays, // 新生长出现的天数
growthRate: growthRate // 生长速率
});
}
getPruningEffect(pruningRecordId) {
return this.effectRecords.get(pruningRecordId);
}
}
这个PruningEffectTracking类用于追踪修剪的效果。通过记录修剪后新生长出现的时间和生长速率,用户可以评估修剪的效果。
总结
修剪记录系统是植物养护应用的重要功能。通过合理的数据模型设计、与OpenHarmony系统的集成和各种统计分析功能,我们可以创建一个功能完整的修剪管理系统,帮助用户科学地管理植物的修剪。
809




被折叠的 条评论
为什么被折叠?



