欢迎大家加入开源鸿蒙跨平台开发者社区,一起共建开源鸿蒙跨平台生态。
修剪管理系统概述
修剪是植物养护的重要环节,它促进植物的生长和塑形。在Cordova框架与OpenHarmony系统的结合下,我们需要实现一个完整的修剪记录系统,包括修剪记录的创建、查询、统计和提醒功能。这个系统需要记录修剪的类型、位置和效果。
修剪记录数据模型
classPruningType{constructor(id,name,description,purpose){this.id=id;this.name=name;this.description=description;this.purpose=purpose;// 修剪目的}}classPruningRecord{constructor(plantId,pruningType,location,notes){this.id='prune_'+Date.now();this.plantId=plantId;this.pruningType=pruningType;this.location=location;// 修剪位置this.date=newDate();this.notes=notes;this.branchesRemoved=0;// 移除的枝条数}}classPruningManager{constructor(){this.records=[];this.pruningTypes=[];this.initDefaultPruningTypes();this.loadFromStorage();}initDefaultPruningTypes(){this.pruningTypes=[newPruningType('prune_1','轻修剪','移除枯死或病弱枝条','清理'),newPruningType('prune_2','中度修剪','塑形和促进分枝','塑形'),newPruningType('prune_3','重度修剪','大幅度修剪以促进新生长','更新'),newPruningType('prune_4','摘心','移除顶端生长点','促进分枝')];}addPruningRecord(plantId,pruningType,location,notes){constrecord=newPruningRecord(plantId,pruningType,location,notes);this.records.push(record);this.saveToStorage();returnrecord;}}这个修剪记录数据模型定义了PruningType、PruningRecord和PruningManager类。PruningType类定义了修剪类型及其目的,PruningRecord类记录每次修剪的详细信息,PruningManager类管理所有修剪记录。
与OpenHarmony数据库的集成
functionsavePruningRecordToDatabase(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}]);}functionloadPruningRecordsFromDatabase(){cordova.exec(function(result){console.log("修剪记录已从数据库加载");pruningManager.records=result.map(rec=>{constrecord=newPruningRecord(rec.plantId,rec.pruningType,rec.location,rec.notes);record.id=rec.id;record.date=newDate(rec.date);record.branchesRemoved=rec.branchesRemoved;returnrecord;});renderPruningRecords();},function(error){console.error("加载失败:",error);},"DatabasePlugin","loadPruningRecords",[]);}这段代码展示了如何与OpenHarmony的数据库进行交互。savePruningRecordToDatabase函数将修剪记录保存到数据库,loadPruningRecordsFromDatabase函数从数据库加载所有修剪记录。通过这种方式,我们确保了修剪数据的持久化存储。
修剪记录列表展示
functionrenderPruningRecords(plantId){constplant=plants.find(p=>p.id===plantId);if(!plant)return;constrecords=pruningManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));constcontainer=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;}constrecordsList=document.createElement('div');recordsList.className='records-list';records.forEach(record=>{constpruningType=pruningManager.pruningTypes.find(p=>p.id===record.pruningType);constrecordItem=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);}这个函数负责渲染修剪记录列表。它显示了特定植物的所有修剪记录,包括日期、修剪类型、目的、位置和移除的枝条数。用户可以通过"编辑"和"删除"按钮管理记录。这种设计提供了清晰的记录展示。
添加修剪记录对话框
functionshowAddPruningRecordDialog(plantId){constdialog=document.createElement('div');dialog.className='modal-dialog';letpruningTypeOptions='';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);constnow=newDate();document.getElementById('pruning-date').value=now.toISOString().slice(0,16);document.getElementById('add-pruning-form').addEventListener('submit',function(e){e.preventDefault();constpruningType=document.getElementById('pruning-type').value;constlocation=document.getElementById('pruning-location').value;constbranchesRemoved=parseInt(document.getElementById('branches-removed').value);constdate=newDate(document.getElementById('pruning-date').value);constnotes=document.getElementById('pruning-notes').value;constrecord=newPruningRecord(plantId,pruningType,location,notes);record.date=date;record.branchesRemoved=branchesRemoved;pruningManager.records.push(record);pruningManager.saveToStorage();savePruningRecordToDatabase(record);closeDialog();renderPruningRecords(plantId);showToast('修剪记录已添加');});}这个函数创建并显示添加修剪记录的对话框。用户可以选择修剪类型、输入位置、枝条数、日期和备注。提交后,新记录会被添加到pruningManager中,并保存到数据库。
修剪统计功能
classPruningStatistics{constructor(pruningManager){this.pruningManager=pruningManager;}getTotalPruningCount(plantId){returnthis.pruningManager.records.filter(r=>r.plantId===plantId).length;}getTotalBranchesRemoved(plantId){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);returnrecords.reduce((sum,r)=>sum+r.branchesRemoved,0);}getMostCommonPruningType(plantId){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);consttypeCounts={};records.forEach(record=>{typeCounts[record.pruningType]=(typeCounts[record.pruningType]||0)+1;});constmostCommon=Object.keys(typeCounts).reduce((a,b)=>typeCounts[a]>typeCounts[b]?a:b);returnthis.pruningManager.pruningTypes.find(p=>p.id===mostCommon);}getPruningFrequency(plantId,days=30){constrecords=this.pruningManager.records.filter(r=>r.plantId===plantId);constcutoffDate=newDate();cutoffDate.setDate(cutoffDate.getDate()-days);constrecentRecords=records.filter(r=>newDate(r.date)>cutoffDate);return(recentRecords.length/days*7).toFixed(2);// 每周修剪次数}}这个PruningStatistics类提供了修剪的统计功能。getTotalPruningCount返回修剪总次数,getTotalBranchesRemoved计算移除的总枝条数,getMostCommonPruningType返回最常用的修剪类型,getPruningFrequency计算修剪频率。这些统计信息可以帮助用户了解修剪规律。
修剪提醒功能
functioncheckPruningReminders(){plants.forEach(plant=>{constlastPruningDate=getLastPruningDate(plant.id);constpruningInterval=plant.pruningInterval||30;// 默认30天if(!lastPruningDate){sendPruningReminder(plant.id,plant.name,'从未修剪过');return;}constdaysSincePruning=Math.floor((newDate()-newDate(lastPruningDate))/(24*60*60*1000));if(daysSincePruning>=pruningInterval){sendPruningReminder(plant.id,plant.name,`已${daysSincePruning}天未修剪`);}});}functionsendPruningReminder(plantId,plantName,message){cordova.exec(function(result){console.log("修剪提醒已发送");},function(error){console.error("提醒发送失败:",error);},"NotificationPlugin","sendReminder",[{title:`${plantName}需要修剪`,message:message,plantId:plantId,type:'pruning'}]);}functiongetLastPruningDate(plantId){constrecords=pruningManager.records.filter(r=>r.plantId===plantId).sort((a,b)=>newDate(b.date)-newDate(a.date));returnrecords.length>0?records[0].date:null;}setInterval(checkPruningReminders,60*60*1000);// 每小时检查一次这段代码实现了修剪提醒功能。checkPruningReminders函数检查所有植物,如果某个植物超过设定的修剪间隔,就发送提醒。通过NotificationPlugin,我们可以向用户发送系统通知。
修剪效果追踪
classPruningEffectTracking{constructor(){this.effectRecords=newMap();// 修剪ID -> 效果数据}recordPruningEffect(pruningRecordId,newGrowthDays,growthRate){this.effectRecords.set(pruningRecordId,{recordedDate:newDate(),newGrowthDays:newGrowthDays,// 新生长出现的天数growthRate:growthRate// 生长速率});}getPruningEffect(pruningRecordId){returnthis.effectRecords.get(pruningRecordId);}}这个PruningEffectTracking类用于追踪修剪的效果。通过记录修剪后新生长出现的时间和生长速率,用户可以评估修剪的效果。
总结
修剪记录系统是植物养护应用的重要功能。通过合理的数据模型设计、与OpenHarmony系统的集成和各种统计分析功能,我们可以创建一个功能完整的修剪管理系统,帮助用户科学地管理植物的修剪。