|
@@ -9,32 +9,15 @@ interface FarmField {
|
|
|
area: number;
|
|
|
location: string;
|
|
|
currentCrop: string;
|
|
|
+ growthStage: string;
|
|
|
daysPlanted: number;
|
|
|
- status?: '需灌溉' | '需施肥' | '需防治' | '正常';
|
|
|
- nextTask?: string;
|
|
|
soilType?: string;
|
|
|
}
|
|
|
|
|
|
-interface FarmTask {
|
|
|
- id: string;
|
|
|
- fieldId: string;
|
|
|
- fieldName: string;
|
|
|
+interface CropDistribution {
|
|
|
name: string;
|
|
|
- type: 'irrigation' | 'fertilization' | 'pestControl' | 'harvest';
|
|
|
- dueDate: Date;
|
|
|
- notes?: string;
|
|
|
- urgent: boolean;
|
|
|
- completed: boolean;
|
|
|
-}
|
|
|
-
|
|
|
-interface GrowthRecord {
|
|
|
- id: string;
|
|
|
- fieldId: string;
|
|
|
- fieldName: string;
|
|
|
- date: Date;
|
|
|
- activity: string;
|
|
|
- notes: string;
|
|
|
- images?: string[];
|
|
|
+ area: number;
|
|
|
+ percentage: number;
|
|
|
}
|
|
|
|
|
|
@Component({
|
|
@@ -44,10 +27,7 @@ interface GrowthRecord {
|
|
|
standalone: false,
|
|
|
})
|
|
|
export class Tab3Page implements OnInit {
|
|
|
- fields: FarmField[] = []; // 初始化为空数组,从后端加载数据
|
|
|
- tasks: FarmTask[] = [];
|
|
|
- records: GrowthRecord[] = [];
|
|
|
-
|
|
|
+ fields: FarmField[] = [];
|
|
|
showFieldForm = false;
|
|
|
editingField: FarmField | null = null;
|
|
|
fieldForm: FormGroup;
|
|
@@ -63,16 +43,16 @@ export class Tab3Page implements OnInit {
|
|
|
area: ['', [Validators.required, Validators.min(0.1)]],
|
|
|
location: [''],
|
|
|
currentCrop: ['冬小麦', Validators.required],
|
|
|
+ growthStage: ['播种期', Validators.required],
|
|
|
soilType: ['粘壤土']
|
|
|
});
|
|
|
}
|
|
|
|
|
|
async ngOnInit() {
|
|
|
await this.loadFields();
|
|
|
- await this.initializeSampleData(); // 可选:初始化示例数据
|
|
|
+ await this.initializeSampleData();
|
|
|
}
|
|
|
|
|
|
- // 从后端加载农田数据
|
|
|
async loadFields() {
|
|
|
try {
|
|
|
const cloudFields = await this.cloudQuery.find();
|
|
@@ -82,8 +62,8 @@ export class Tab3Page implements OnInit {
|
|
|
area: field.get('area'),
|
|
|
location: field.get('location'),
|
|
|
currentCrop: field.get('currentCrop'),
|
|
|
+ growthStage: field.get('growthStage') || '播种期',
|
|
|
daysPlanted: field.get('daysPlanted') || 0,
|
|
|
- status: field.get('status') || '正常',
|
|
|
soilType: field.get('soilType')
|
|
|
}));
|
|
|
} catch (error) {
|
|
@@ -91,7 +71,6 @@ export class Tab3Page implements OnInit {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // 保存农田到后端
|
|
|
async saveField() {
|
|
|
if (this.fieldForm.invalid) return;
|
|
|
|
|
@@ -99,36 +78,31 @@ export class Tab3Page implements OnInit {
|
|
|
const fieldObject = new CloudObject('FarmField');
|
|
|
|
|
|
if (this.editingField && this.editingField.id) {
|
|
|
- // 更新现有农田
|
|
|
fieldObject.id = this.editingField.id;
|
|
|
fieldObject.set(fieldData);
|
|
|
await fieldObject.save();
|
|
|
} else {
|
|
|
- // 添加新农田
|
|
|
fieldObject.set({
|
|
|
...fieldData,
|
|
|
- daysPlanted: 0,
|
|
|
- status: '正常'
|
|
|
+ daysPlanted: 0
|
|
|
});
|
|
|
const savedField = await fieldObject.save();
|
|
|
|
|
|
this.fields.push({
|
|
|
id: savedField.id || '',
|
|
|
daysPlanted: 0,
|
|
|
- status: '正常',
|
|
|
...fieldData
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- await this.loadFields(); // 重新加载数据确保同步
|
|
|
+ await this.loadFields();
|
|
|
this.cancelEdit();
|
|
|
}
|
|
|
|
|
|
- // 删除农田
|
|
|
async deleteField(fieldId: string) {
|
|
|
const alert = await this.alertCtrl.create({
|
|
|
header: '确认删除',
|
|
|
- message: '确定要删除这个农田地块吗?相关记录也将被删除',
|
|
|
+ message: '确定要删除这个农田地块吗?',
|
|
|
buttons: [
|
|
|
{
|
|
|
text: '取消',
|
|
@@ -142,8 +116,6 @@ export class Tab3Page implements OnInit {
|
|
|
await fieldObject.destroy();
|
|
|
|
|
|
this.fields = this.fields.filter(f => f.id !== fieldId);
|
|
|
- this.tasks = this.tasks.filter(t => t.fieldId !== fieldId);
|
|
|
- this.records = this.records.filter(r => r.fieldId !== fieldId);
|
|
|
}
|
|
|
}
|
|
|
]
|
|
@@ -152,7 +124,6 @@ export class Tab3Page implements OnInit {
|
|
|
await alert.present();
|
|
|
}
|
|
|
|
|
|
- // 初始化示例数据(可选)
|
|
|
async initializeSampleData() {
|
|
|
const query = new CloudQuery('FarmField');
|
|
|
const existingFields = await query.find();
|
|
@@ -164,9 +135,8 @@ export class Tab3Page implements OnInit {
|
|
|
area: 5.2,
|
|
|
location: '农场北侧',
|
|
|
currentCrop: '冬小麦',
|
|
|
+ growthStage: '生长期',
|
|
|
daysPlanted: 45,
|
|
|
- status: '需施肥',
|
|
|
- nextTask: '明日追肥',
|
|
|
soilType: '粘壤土'
|
|
|
},
|
|
|
{
|
|
@@ -174,9 +144,8 @@ export class Tab3Page implements OnInit {
|
|
|
area: 3.8,
|
|
|
location: '农场南侧',
|
|
|
currentCrop: '春玉米',
|
|
|
+ growthStage: '苗期',
|
|
|
daysPlanted: 22,
|
|
|
- status: '正常',
|
|
|
- nextTask: '下周除草',
|
|
|
soilType: '砂壤土'
|
|
|
}
|
|
|
];
|
|
@@ -187,33 +156,28 @@ export class Tab3Page implements OnInit {
|
|
|
await fieldObject.save();
|
|
|
}
|
|
|
|
|
|
- // 重新加载数据
|
|
|
await this.loadFields();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- // 原有代码不变
|
|
|
-
|
|
|
get totalArea(): number {
|
|
|
return this.fields.reduce((sum, field) => sum + field.area, 0);
|
|
|
}
|
|
|
|
|
|
- get pendingTasks(): number {
|
|
|
- return this.tasks.filter(t => !t.completed).length;
|
|
|
- }
|
|
|
-
|
|
|
- get recentTasks(): FarmTask[] {
|
|
|
- return this.tasks
|
|
|
- .filter(t => !t.completed)
|
|
|
- .sort((a, b) => a.dueDate.getTime() - b.dueDate.getTime())
|
|
|
- .slice(0, 3);
|
|
|
- }
|
|
|
+ get cropDistribution(): CropDistribution[] {
|
|
|
+ const cropMap = new Map<string, number>();
|
|
|
+
|
|
|
+ this.fields.forEach(field => {
|
|
|
+ const current = cropMap.get(field.currentCrop) || 0;
|
|
|
+ cropMap.set(field.currentCrop, current + field.area);
|
|
|
+ });
|
|
|
|
|
|
- get recentRecords(): GrowthRecord[] {
|
|
|
- return [...this.records]
|
|
|
- .sort((a, b) => b.date.getTime() - a.date.getTime())
|
|
|
- .slice(0, 4);
|
|
|
+ const total = this.totalArea;
|
|
|
+ return Array.from(cropMap.entries()).map(([name, area]) => ({
|
|
|
+ name,
|
|
|
+ area,
|
|
|
+ percentage: Math.round((area / total) * 100)
|
|
|
+ }));
|
|
|
}
|
|
|
|
|
|
getCropImage(crop: string): string {
|
|
@@ -226,34 +190,36 @@ export class Tab3Page implements OnInit {
|
|
|
return cropImages[crop] || 'assets/images/crop-default.png';
|
|
|
}
|
|
|
|
|
|
- getStatusColor(status: string): string {
|
|
|
- const statusColors: {[key: string]: string} = {
|
|
|
- '需灌溉': 'primary',
|
|
|
- '需施肥': 'warning',
|
|
|
- '需防治': 'danger',
|
|
|
- '正常': 'success'
|
|
|
+ getCropColor(crop: string): string {
|
|
|
+ const cropColors: {[key: string]: string} = {
|
|
|
+ '冬小麦': 'primary',
|
|
|
+ '春玉米': 'warning',
|
|
|
+ '水稻': 'success',
|
|
|
+ '大豆': 'tertiary'
|
|
|
};
|
|
|
- return statusColors[status] || 'medium';
|
|
|
+ return cropColors[crop] || 'medium';
|
|
|
}
|
|
|
|
|
|
- getTaskIcon(type: string): string {
|
|
|
- const taskIcons: {[key: string]: string} = {
|
|
|
- 'irrigation': 'water',
|
|
|
- 'fertilization': 'nutrition',
|
|
|
- 'pestControl': 'bug',
|
|
|
- 'harvest': 'leaf'
|
|
|
+ getStageColor(stage: string): string {
|
|
|
+ const stageColors: {[key: string]: string} = {
|
|
|
+ '播种期': 'primary',
|
|
|
+ '苗期': 'success',
|
|
|
+ '生长期': 'warning',
|
|
|
+ '成熟期': 'danger',
|
|
|
+ '收获期': 'tertiary'
|
|
|
};
|
|
|
- return taskIcons[type] || 'alert-circle';
|
|
|
+ return stageColors[stage] || 'medium';
|
|
|
}
|
|
|
|
|
|
- getTaskColor(type: string): string {
|
|
|
- const taskColors: {[key: string]: string} = {
|
|
|
- 'irrigation': 'primary',
|
|
|
- 'fertilization': 'warning',
|
|
|
- 'pestControl': 'danger',
|
|
|
- 'harvest': 'success'
|
|
|
+ getStageDays(stage: string): number {
|
|
|
+ const stageDays: {[key: string]: number} = {
|
|
|
+ '播种期': 10,
|
|
|
+ '苗期': 20,
|
|
|
+ '生长期': 30,
|
|
|
+ '成熟期': 15,
|
|
|
+ '收获期': 5
|
|
|
};
|
|
|
- return taskColors[type] || 'medium';
|
|
|
+ return stageDays[stage] || 0;
|
|
|
}
|
|
|
|
|
|
toggleFieldForm() {
|
|
@@ -262,6 +228,7 @@ export class Tab3Page implements OnInit {
|
|
|
this.editingField = null;
|
|
|
this.fieldForm.reset({
|
|
|
currentCrop: '冬小麦',
|
|
|
+ growthStage: '播种期',
|
|
|
soilType: '粘壤土'
|
|
|
});
|
|
|
}
|
|
@@ -274,23 +241,19 @@ export class Tab3Page implements OnInit {
|
|
|
area: field.area,
|
|
|
location: field.location,
|
|
|
currentCrop: field.currentCrop,
|
|
|
+ growthStage: field.growthStage,
|
|
|
soilType: field.soilType
|
|
|
});
|
|
|
this.showFieldForm = true;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
cancelEdit() {
|
|
|
this.showFieldForm = false;
|
|
|
this.editingField = null;
|
|
|
this.fieldForm.reset({
|
|
|
currentCrop: '冬小麦',
|
|
|
+ growthStage: '播种期',
|
|
|
soilType: '粘壤土'
|
|
|
});
|
|
|
}
|
|
|
-
|
|
|
- viewTaskDetail(task: FarmTask) {
|
|
|
- console.log('查看任务详情:', task);
|
|
|
- // 实际应导航到任务详情页
|
|
|
- }
|
|
|
-}
|
|
|
+}
|