基于AppGallery Connect云数据库实现的课程表项目。
软件要求
DevEco Studio版本:DevEco Studio NEXT Developer Beta1及以上。
HarmonyOS SDK版本:HarmonyOS NEXT Developer Beta1 SDK及以上。
项目结构解读

图片

  • common: CourseCommon 有关课程表的常量
  • database: Yun 云数据库配置信息和方法
  • entryability: EntryAbility 应用入口
  • entrybackupability: EntryBackupAbility 应用数据备份和恢复的一个扩展功能。
  • model: CourseDatamodel 课程表单实例
  • pages: Index 主页面; MeetingDetail 预约界面;
  • utils: Colors 生成不同的颜色
  • view: AddDialog 添加课程弹窗组件; ClassDetailLayout 课程详细信息组件; ClassTestLayout 考试详细信息组件; ScheduleLayout 课程表组件; StatusLayout 课程表添加、修改和删除按钮组件; UpdateDialog 修改课程弹窗组件;
  • viewModel: Course 课程表逻辑类; YunCourseSchedules 课程表实体类;

部分代码解读:

position(coursesData:YunCourseSchedules[][]){
    let x=13
    let position:YunCourseSchedules[]=[]
    let allposition:YunCourseSchedules[][]=[]
    let exactnum:number[]=[]   //精确每个课是第几节开始上的
    let num:number[]=[]
    let selectweekcourseLessons :number[][] = []  //每个星期所有课程是第几节课上课
    let courseDuration:number[]=[]//课程时长
    for (let index = 0; index < 7; index++) {
      for (let y = 0; y < coursesData[index].length; y++) {
        x-=coursesData[index][y].courseDuration
        num.push(coursesData[index][y].courseLessons)
        exactnum.push(coursesData[index][y].courseLessons)
        courseDuration.push(coursesData[index][y].courseDuration)
      }
      selectweekcourseLessons.push(num)
      console.log(TAG+JSON.stringify(CourseCommon.TABLEWEEK[index])+" 剩余空数量" + JSON.stringify(x));
      console.log(TAG+JSON.stringify(CourseCommon.TABLEWEEK[index])+" 第几节开始上:" + JSON.stringify(exactnum));
      console.log(TAG+JSON.stringify(CourseCommon.TABLEWEEK[index])+" 课程时长:" + JSON.stringify(courseDuration));
      // 精确课程位置
      let s=0
      for (let y = 0; y < exactnum.length; y++){
        if (exactnum[y]+courseDuration[y]>=exactnum[y+1]){
          exactnum[y+1]=exactnum[y]+1-s
        }
        if (exactnum[y]+courseDuration[y]<exactnum[y+1]){
          exactnum[y+1]=exactnum[y+1]-courseDuration[y]+1-s
        }
        s=courseDuration[y]+s-1 //去掉之前的课程时长
      }
      for (let z = 0; z < x+exactnum.length; z++) {
        position.push(new YunCourseSchedules())
      }
     for(let z = 0;z<exactnum.length;z++){
       position[exactnum[z]-1]=coursesData[index][z]
     }
      console.log(TAG,"position:" + JSON.stringify(position));
      allposition.push(position) //所有课程位置
      // 重置相关变量为下一天做准备
      position=[]
      exactnum=[]
      num=[]
      courseDuration=[]
      x=13
    }
    CourseDatamodel.getContext().setSelectWeekCourseLessons(selectweekcourseLessons)
    console.log( "ScheduleLayout"+JSON.stringify(selectweekcourseLessons));
    return allposition
  }

这段代码是Course里面定义的一个名为position的函数,它主要的功能是基于传入的课程数据(二维数组形式,按星期维度组织)来确定每门课程在每天中的具体位置安排,同时还会记录一些和课程相关的信息,比如每门课程在星期几是从第几节课开始上、课程时长等,并最终返回所有课程位置信息的二维数组。

async addorupdate(className:string,teacher:string,place:string,people:number,courseDuration:number,content:string,classroomtype:number,id:number): Promise<boolean>{
    try {
      if (className!=''&&teacher!=''&&place!=''&&content!='') {
        let ison = false
        //判断添加的时长有没有占用其他课程的位置和有没有超出最大时间槽
        for (let index = 0; index <= CourseDatamodel.getContext()
          .getSelectWeekCourseLessons()[CourseDatamodel.getContext().getselectWeek()]
          .length; index++) {
          //判断当前日期为不为空
          if (CourseDatamodel.getContext()
            .getSelectWeekCourseLessons()[CourseDatamodel.getContext().getselectWeek()]
            .length != 0) {
            //排在其中一个课程的前面
            if (CourseDatamodel.getContext().getIndex() < CourseDatamodel.getContext()
              .getSelectWeekCourseLessons()[CourseDatamodel.getContext().getselectWeek()][index]) {
              if (courseDuration + CourseDatamodel.getContext().getIndex() - 1 >= 13 ||
                courseDuration + CourseDatamodel.getContext().getIndex() - 1 >= CourseDatamodel.getContext()
                  .getSelectWeekCourseLessons()[CourseDatamodel.getContext().getselectWeek()][index]) {
                ison = false
                break
              } else {
                ison = true
                break
              }
            }
            //排在所有课程后面
            if (index == CourseDatamodel.getContext()
              .getSelectWeekCourseLessons()[CourseDatamodel.getContext().getselectWeek()]
              .length) {
              if (courseDuration + CourseDatamodel.getContext().getIndex() - 1 <= 13) {
                ison = true
                break
              } else {
                ison = false
                break
              }
            }
          } else {
            if (courseDuration + CourseDatamodel.getContext().getIndex() - 1 <= 13) {
              ison = true
              break
            } else {
              ison = false
              break
            }
          }
        }
        if (ison == true) {
          const courseSchedules = new YunCourseSchedules()
          if (id != 0) {
            courseSchedules.setId(id)
          }
          courseSchedules.setClassName(className)
          courseSchedules.setTeacher(teacher)
          courseSchedules.setClassroom(place)
          courseSchedules.setPeople(content)
          courseSchedules.setPeople(people)
          courseSchedules.setCourseDuration(courseDuration)
          courseSchedules.setIntroduction(content)
          courseSchedules.setColor(Colors.creatcolors())
          courseSchedules.setClassroomtype(classroomtype)
          courseSchedules.setWeek(CourseDatamodel.getContext().getWeek())
          courseSchedules.setCourseLessons(CourseDatamodel.getContext().getIndex())
          await this.database.upsert(courseSchedules)
          console.log(this.TAG, "添加数据成功");
          promptAction.showToast({
            message: '添加成功',
            duration: 2000,
          });
          return true
        } else {
          AlertDialog.show({
            title: "警告",
            message: '添加失败,时长过长'
          })
          console.log(this.TAG, "添加数据失败", "原因:时长过长");
          return false
        }
      }else {
          AlertDialog.show({
            title:"警告",
            message:'不能为空'
          })
          console.error(this.TAG,"添加数据失败");
          return false
        }
      
    } catch (e) {
      AlertDialog.show({
        title:"警告",
        message:'添加失败'
      })
      console.error(this.TAG,"添加数据失败","原因:",JSON.stringify(e));
      return false
    }
  }

这段代码是Yun里面的一个异步函数,功能主要是向云数据库中添加或更新课程相关的数据。在添加或更新之前,会进行一系列的验证,包括判断各项必要信息是否为空以及要添加课程的时长是否合理(是否会与已有课程冲突、是否超出最大时间限制等),根据验证结果决定是否执行实际的添加或更新操作到云数据库中,并返回相应的成功或失败状态。

原文链接:https://blog.csdn.net/m0_65816838/article/details/144799826
源码链接:https://gitee.com/AaBbBbbbqazxsw/CurriculumProjectDemo