tab3.page.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. import { Component, OnInit } from '@angular/core';
  2. import { FormBuilder, FormGroup, Validators } from '@angular/forms';
  3. import { AlertController } from '@ionic/angular';
  4. import { CloudObject, CloudQuery } from 'src/lib/ncloud';
  5. interface FarmField {
  6. id: string;
  7. name: string;
  8. area: number;
  9. location: string;
  10. currentCrop: string;
  11. growthStage: string;
  12. daysPlanted: number;
  13. soilType?: string;
  14. }
  15. interface CropDistribution {
  16. name: string;
  17. area: number;
  18. percentage: number;
  19. }
  20. @Component({
  21. selector: 'app-tab3',
  22. templateUrl: './tab3.page.html',
  23. styleUrls: ['./tab3.page.scss'],
  24. standalone: false,
  25. })
  26. export class Tab3Page implements OnInit {
  27. fields: FarmField[] = [];
  28. showFieldForm = false;
  29. editingField: FarmField | null = null;
  30. fieldForm: FormGroup;
  31. private cloudQuery = new CloudQuery('FarmField');
  32. constructor(
  33. private fb: FormBuilder,
  34. private alertCtrl: AlertController
  35. ) {
  36. this.fieldForm = this.fb.group({
  37. name: ['', Validators.required],
  38. area: ['', [Validators.required, Validators.min(0.1)]],
  39. location: [''],
  40. currentCrop: ['冬小麦', Validators.required],
  41. growthStage: ['播种期', Validators.required],
  42. soilType: ['粘壤土']
  43. });
  44. }
  45. async ngOnInit() {
  46. await this.loadFields();
  47. await this.initializeSampleData();
  48. }
  49. async loadFields() {
  50. try {
  51. const cloudFields = await this.cloudQuery.find();
  52. this.fields = cloudFields.map(field => ({
  53. id: field.id || '',
  54. name: field.get('name'),
  55. area: field.get('area'),
  56. location: field.get('location'),
  57. currentCrop: field.get('currentCrop'),
  58. growthStage: field.get('growthStage') || '播种期',
  59. daysPlanted: field.get('daysPlanted') || 0,
  60. soilType: field.get('soilType')
  61. }));
  62. } catch (error) {
  63. console.error('加载农田数据失败:', error);
  64. }
  65. }
  66. async saveField() {
  67. if (this.fieldForm.invalid) return;
  68. const fieldData = this.fieldForm.value;
  69. const fieldObject = new CloudObject('FarmField');
  70. if (this.editingField && this.editingField.id) {
  71. fieldObject.id = this.editingField.id;
  72. fieldObject.set(fieldData);
  73. await fieldObject.save();
  74. } else {
  75. fieldObject.set({
  76. ...fieldData,
  77. daysPlanted: 0
  78. });
  79. const savedField = await fieldObject.save();
  80. this.fields.push({
  81. id: savedField.id || '',
  82. daysPlanted: 0,
  83. ...fieldData
  84. });
  85. }
  86. await this.loadFields();
  87. this.cancelEdit();
  88. }
  89. async deleteField(fieldId: string) {
  90. const alert = await this.alertCtrl.create({
  91. header: '确认删除',
  92. message: '确定要删除这个农田地块吗?',
  93. buttons: [
  94. {
  95. text: '取消',
  96. role: 'cancel'
  97. },
  98. {
  99. text: '删除',
  100. handler: async () => {
  101. const fieldObject = new CloudObject('FarmField');
  102. fieldObject.id = fieldId;
  103. await fieldObject.destroy();
  104. this.fields = this.fields.filter(f => f.id !== fieldId);
  105. }
  106. }
  107. ]
  108. });
  109. await alert.present();
  110. }
  111. async initializeSampleData() {
  112. const query = new CloudQuery('FarmField');
  113. const existingFields = await query.find();
  114. if (existingFields.length === 0) {
  115. const sampleFields = [
  116. {
  117. name: '北区小麦田',
  118. area: 5.2,
  119. location: '农场北侧',
  120. currentCrop: '冬小麦',
  121. growthStage: '生长期',
  122. daysPlanted: 45,
  123. soilType: '粘壤土'
  124. },
  125. {
  126. name: '南区玉米地',
  127. area: 3.8,
  128. location: '农场南侧',
  129. currentCrop: '春玉米',
  130. growthStage: '苗期',
  131. daysPlanted: 22,
  132. soilType: '砂壤土'
  133. }
  134. ];
  135. for (const field of sampleFields) {
  136. const fieldObject = new CloudObject('FarmField');
  137. fieldObject.set(field);
  138. await fieldObject.save();
  139. }
  140. await this.loadFields();
  141. }
  142. }
  143. get totalArea(): number {
  144. return this.fields.reduce((sum, field) => sum + field.area, 0);
  145. }
  146. get cropDistribution(): CropDistribution[] {
  147. const cropMap = new Map<string, number>();
  148. this.fields.forEach(field => {
  149. const current = cropMap.get(field.currentCrop) || 0;
  150. cropMap.set(field.currentCrop, current + field.area);
  151. });
  152. const total = this.totalArea;
  153. return Array.from(cropMap.entries()).map(([name, area]) => ({
  154. name,
  155. area,
  156. percentage: Math.round((area / total) * 100)
  157. }));
  158. }
  159. getCropImage(crop: string): string {
  160. const cropImages: {[key: string]: string} = {
  161. '冬小麦': 'assets/images/wheat-icon.png',
  162. '春玉米': 'assets/images/corn-icon.png',
  163. '水稻': 'assets/images/rice-icon.png',
  164. '大豆': 'assets/images/soybean-icon.png'
  165. };
  166. return cropImages[crop] || 'assets/images/crop-default.png';
  167. }
  168. getCropColor(crop: string): string {
  169. const cropColors: {[key: string]: string} = {
  170. '冬小麦': 'primary',
  171. '春玉米': 'warning',
  172. '水稻': 'success',
  173. '大豆': 'tertiary'
  174. };
  175. return cropColors[crop] || 'medium';
  176. }
  177. getStageColor(stage: string): string {
  178. const stageColors: {[key: string]: string} = {
  179. '播种期': 'primary',
  180. '苗期': 'success',
  181. '生长期': 'warning',
  182. '成熟期': 'danger',
  183. '收获期': 'tertiary'
  184. };
  185. return stageColors[stage] || 'medium';
  186. }
  187. getStageDays(stage: string): number {
  188. const stageDays: {[key: string]: number} = {
  189. '播种期': 10,
  190. '苗期': 20,
  191. '生长期': 30,
  192. '成熟期': 15,
  193. '收获期': 5
  194. };
  195. return stageDays[stage] || 0;
  196. }
  197. toggleFieldForm() {
  198. this.showFieldForm = !this.showFieldForm;
  199. if (!this.showFieldForm) {
  200. this.editingField = null;
  201. this.fieldForm.reset({
  202. currentCrop: '冬小麦',
  203. growthStage: '播种期',
  204. soilType: '粘壤土'
  205. });
  206. }
  207. }
  208. editField(field: FarmField) {
  209. this.editingField = field;
  210. this.fieldForm.patchValue({
  211. name: field.name,
  212. area: field.area,
  213. location: field.location,
  214. currentCrop: field.currentCrop,
  215. growthStage: field.growthStage,
  216. soilType: field.soilType
  217. });
  218. this.showFieldForm = true;
  219. }
  220. cancelEdit() {
  221. this.showFieldForm = false;
  222. this.editingField = null;
  223. this.fieldForm.reset({
  224. currentCrop: '冬小麦',
  225. growthStage: '播种期',
  226. soilType: '粘壤土'
  227. });
  228. }
  229. }