|
@@ -0,0 +1,164 @@
|
|
|
+import { Injectable } from '@angular/core';
|
|
|
+import Parse from 'parse';
|
|
|
+import { CloudQuery, CloudUser } from 'src/lib/ncloud';
|
|
|
+
|
|
|
+// 定义学习计划接口
|
|
|
+export interface LearningPlan {
|
|
|
+ id?: string;
|
|
|
+ userId: string;
|
|
|
+ title: string;
|
|
|
+ overview: string;
|
|
|
+ totalDuration: string;
|
|
|
+ weeklyHours: number;
|
|
|
+ stages: {
|
|
|
+ name: string;
|
|
|
+ duration: string;
|
|
|
+ goals: string[];
|
|
|
+ weeklyPlans: {
|
|
|
+ week: string;
|
|
|
+ focus: string;
|
|
|
+ tasks: {
|
|
|
+ name: string;
|
|
|
+ timeNeeded: string;
|
|
|
+ details: string;
|
|
|
+ resources?: string[];
|
|
|
+ }[];
|
|
|
+ }[];
|
|
|
+ }[];
|
|
|
+ milestones: {
|
|
|
+ name: string;
|
|
|
+ timing: string;
|
|
|
+ criteria: string[];
|
|
|
+ }[];
|
|
|
+ successCriteria: string[];
|
|
|
+ additionalSuggestions: string[];
|
|
|
+ createdAt?: Date;
|
|
|
+ updatedAt?: Date;
|
|
|
+}
|
|
|
+
|
|
|
+@Injectable({
|
|
|
+ providedIn: 'root'
|
|
|
+})
|
|
|
+export class LearningPlanService {
|
|
|
+
|
|
|
+ // 保存学习计划
|
|
|
+ async saveLearningPlan(plan: LearningPlan): Promise<string> {
|
|
|
+ try {
|
|
|
+ const LearningPlan = Parse.Object.extend('LearningPlan');
|
|
|
+ const learningPlan = new LearningPlan();
|
|
|
+
|
|
|
+ // 设置计划数据
|
|
|
+ learningPlan.set('userId', plan.userId);
|
|
|
+ learningPlan.set('title', plan.title);
|
|
|
+ learningPlan.set('overview', plan.overview);
|
|
|
+ learningPlan.set('totalDuration', plan.totalDuration);
|
|
|
+ learningPlan.set('weeklyHours', plan.weeklyHours);
|
|
|
+ learningPlan.set('stages', plan.stages);
|
|
|
+ learningPlan.set('milestones', plan.milestones);
|
|
|
+ learningPlan.set('successCriteria', plan.successCriteria);
|
|
|
+ learningPlan.set('additionalSuggestions', plan.additionalSuggestions);
|
|
|
+
|
|
|
+ const result = await learningPlan.save();
|
|
|
+ return result.id;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('保存学习计划失败:', error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加工具函数来格式化时长显示
|
|
|
+ private formatDuration(duration: string | number): string {
|
|
|
+ if (typeof duration === 'number') {
|
|
|
+ if (duration >= 720) { // 大于720小时显示月
|
|
|
+ return `${Math.round(duration / 720)}个月`;
|
|
|
+ } else {
|
|
|
+ return `${duration}小时`;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return duration; // 如果是字符串则直接返回
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取用户的所有学习计划
|
|
|
+ async getUserLearningPlans(userId: string): Promise<LearningPlan[]> {
|
|
|
+ try {
|
|
|
+ const LearningPlan = Parse.Object.extend('LearningPlan');
|
|
|
+ const query = new Parse.Query(LearningPlan);
|
|
|
+ query.equalTo('userId', userId);
|
|
|
+ query.descending('createdAt');
|
|
|
+
|
|
|
+ const results = await query.find();
|
|
|
+ return results.map(plan => {
|
|
|
+ const weeklyHours = plan.get('weeklyHours');
|
|
|
+ const totalDuration = plan.get('totalDuration');
|
|
|
+
|
|
|
+ const learningPlan: LearningPlan = {
|
|
|
+ id: plan.id || undefined,
|
|
|
+ userId: plan.get('userId'),
|
|
|
+ title: plan.get('title'),
|
|
|
+ overview: plan.get('overview'),
|
|
|
+ totalDuration: this.formatDuration(totalDuration), // 格式化总时长
|
|
|
+ weeklyHours: typeof weeklyHours === 'string' ?
|
|
|
+ parseFloat(weeklyHours) :
|
|
|
+ Number(weeklyHours) || 0, // 确保转换为数字
|
|
|
+ stages: plan.get('stages') || [],
|
|
|
+ milestones: plan.get('milestones') || [],
|
|
|
+ successCriteria: plan.get('successCriteria') || [],
|
|
|
+ additionalSuggestions: plan.get('additionalSuggestions') || [],
|
|
|
+ createdAt: plan.get('createdAt'),
|
|
|
+ updatedAt: plan.get('updatedAt')
|
|
|
+ };
|
|
|
+ return learningPlan;
|
|
|
+ });
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取学习计划失败:', error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取单个学习计划详情
|
|
|
+ async getLearningPlanById(planId: string): Promise<LearningPlan> {
|
|
|
+ try {
|
|
|
+ const LearningPlan = Parse.Object.extend('LearningPlan');
|
|
|
+ const query = new Parse.Query(LearningPlan);
|
|
|
+ const plan = await query.get(planId);
|
|
|
+
|
|
|
+ const weeklyHours = plan.get('weeklyHours');
|
|
|
+ const totalDuration = plan.get('totalDuration');
|
|
|
+
|
|
|
+ const learningPlan: LearningPlan = {
|
|
|
+ id: plan.id || undefined,
|
|
|
+ userId: plan.get('userId'),
|
|
|
+ title: plan.get('title'),
|
|
|
+ overview: plan.get('overview'),
|
|
|
+ totalDuration: this.formatDuration(totalDuration), // 格式化总时长
|
|
|
+ weeklyHours: typeof weeklyHours === 'string' ?
|
|
|
+ parseFloat(weeklyHours) :
|
|
|
+ Number(weeklyHours) || 0, // 确保转换为数字
|
|
|
+ stages: plan.get('stages') || [],
|
|
|
+ milestones: plan.get('milestones') || [],
|
|
|
+ successCriteria: plan.get('successCriteria') || [],
|
|
|
+ additionalSuggestions: plan.get('additionalSuggestions') || [],
|
|
|
+ createdAt: plan.get('createdAt'),
|
|
|
+ updatedAt: plan.get('updatedAt')
|
|
|
+ };
|
|
|
+
|
|
|
+ return learningPlan;
|
|
|
+ } catch (error) {
|
|
|
+ console.error('获取学习计划详情失败:', error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 添加删除方法
|
|
|
+ async deleteLearningPlan(planId: string): Promise<void> {
|
|
|
+ try {
|
|
|
+ const LearningPlan = Parse.Object.extend('LearningPlan');
|
|
|
+ const query = new Parse.Query(LearningPlan);
|
|
|
+ const plan = await query.get(planId);
|
|
|
+ await plan.destroy();
|
|
|
+ } catch (error) {
|
|
|
+ console.error('删除学习计划失败:', error);
|
|
|
+ throw error;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|