Example #1
0
        protected void btnMakeSchedule_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbYear.Text))
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('学年不能为空!')</script>");
                return;
            }
            int term = default(int);
            if (string.IsNullOrEmpty(ddlTerm.SelectedValue) || !int.TryParse(ddlTerm.SelectedValue, out term) || term < 1 || term > 2)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('学期不能为空!')</script>");
                return;
            }

            bool IsSucess = false;
            try
            {
                List<CY.GFive.Core.Business.CoursePlan> Plans = CY.GFive.Core.Business.CoursePlan.FillPlans(tbYear.Text, ddlTerm.SelectedValue) as List<CY.GFive.Core.Business.CoursePlan>;

                if (Plans != null)
                {
                    // 教师信息转化
                    List<CY.GFive.Core.Business.StaffInfo> professorList = CY.GFive.Core.Business.CoursePlan.FillTeachers(Plans) as List<CY.GFive.Core.Business.StaffInfo>;
                    List<GAAlgorithm.Model.Professor> GAProfessorList = new List<Professor>();

                    for (int i = 0; i < professorList.Count; i++)
                    {
                        GAAlgorithm.Model.Professor tempProfessor = new GAAlgorithm.Model.Professor(professorList[i].Id, professorList[i].Name);

                        GAProfessorList.Add(tempProfessor);
                    }

                    // 排课前初始化教师信息
                    SimpleCourseSchedule.Service.ScheduleService.InitialProfessors(GAProfessorList);

                    // 初始化教室信息
                    List<Room> roomList = new List<Room>();
                    List<CY.GFive.Core.Business.ClassRoom> classRoomList = new List<CY.GFive.Core.Business.ClassRoom>();
                    classRoomList = CY.GFive.Core.Business.ClassRoom.GetAvailableRooms() as List<CY.GFive.Core.Business.ClassRoom>;
                    for (int r = 0; r < classRoomList.Count; r++)
                    {
                        GAAlgorithm.Model.Room room = new Room(classRoomList[r].Code, false, classRoomList[r].AvailNum);
                        room.SetId(classRoomList[r].Id);
                        room.SetRoomCode(classRoomList[r].Code);
                        roomList.Add(room);
                    }

                    // 课程信息转化
                    Dictionary<CY.GFive.Core.Business.ClassInfo, List<CY.GFive.Core.Business.PlanCourse>> courseDic = CY.GFive.Core.Business.CoursePlan.FillClassCourse(Plans) as Dictionary<CY.GFive.Core.Business.ClassInfo, List<CY.GFive.Core.Business.PlanCourse>>;
                    Hashtable courseHash = new Hashtable();
                    foreach (KeyValuePair<CY.GFive.Core.Business.ClassInfo, List<CY.GFive.Core.Business.PlanCourse>> it in courseDic)
                    {
                        GAAlgorithm.Model.StudentsGroup tempGroup = new StudentsGroup(((CY.GFive.Core.Business.ClassInfo)it.Key).Id, ((CY.GFive.Core.Business.ClassInfo)it.Key).ClassName.ToString(), ((CY.GFive.Core.Business.ClassInfo)it.Key).OrStdNum);
                        tempGroup.SetRoomId(-1);
                        List<CourseClass> tempCourseClassList = new List<CourseClass>();
                        for (int j = 0; j < ((List<CY.GFive.Core.Business.PlanCourse>)it.Value).Count; j++)
                        {
                            CY.GFive.Core.Business.PlanCourse tempPlanCourse = ((List<CY.GFive.Core.Business.PlanCourse>)it.Value)[j];
                            GAAlgorithm.Model.Professor tempProfessor = new Professor(tempPlanCourse.Teacher.Id, tempPlanCourse.Teacher.Name);
                            GAAlgorithm.Model.Course tempCourse = new Course(tempPlanCourse.Course.Id, tempPlanCourse.Course.Name);
                            GAAlgorithm.Model.CourseClass tempCourseClass = new CourseClass(tempProfessor, tempCourse, new List<StudentsGroup> { tempGroup }, false, 1);

                            tempCourseClassList.Add(tempCourseClass);
                            // 根据学分增加课时
                            if ((tempPlanCourse.CourseScore % 2) > 0)
                            {
                                tempPlanCourse.CourseScore++;
                            }
                            for (int i = 1; i < tempPlanCourse.CourseScore/2; i++)
                            {
                                GAAlgorithm.Model.CourseClass copyCourse = new CourseClass(tempProfessor, tempCourse, new List<StudentsGroup> { tempGroup }, false, 1);
                                tempCourseClassList.Add(copyCourse);
                            }
                        }

                        courseHash.Add(tempGroup, tempCourseClassList);
                    }

                    // 分班, 分班要在"课程信息转化"步骤之后
                    List<StudentsGroup> groupList = new List<StudentsGroup>();      // 等待分班的学生们
                    int limitedNbr = 2;
                    foreach (DictionaryEntry it in courseHash)
                    {
                        groupList.Add(it.Key as StudentsGroup);
                    }
                    groupList = ScheduleService.AssignClassRoom(groupList, roomList, limitedNbr);

                    for (int h = 0; h < groupList.Count; h++)
                    {
                        IDictionaryEnumerator it = courseHash.GetEnumerator();

                        for (int d = 0; d < courseHash.Count; d++)
                        {
                            it.MoveNext();
                            if ((it.Key as StudentsGroup).GetId() == groupList[h].GetId())
                            {
                                (it.Key as StudentsGroup).SetRoomId(groupList[h].GetRoomId());
                                (it.Key as StudentsGroup).SetRoomCode(groupList[h].GetRoomCode());
                            }
                        }
                    }

                    //排课前信息检验
                    int iDay = int.Parse(ddDays.SelectedValue);
                    int iNbr = int.Parse(ddNbr.SelectedValue);
                    string msg = "";
                    if (!ScheduleService.ValidateProfessorInfo(out msg))
                    {
                        Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('" + msg + "');</script>");
                        return;
                    }
                    else
                    {
                        if (!ScheduleService.ValidateCourseInfo(courseHash, iNbr, iDay, out msg))
                        {
                            Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('" + msg + "');</script>");
                            return;
                        }
                    }

                    // 排课并返回逻辑课表
                    Hashtable scheduledTable = ScheduleService.MakeSchedule(courseHash, iDay, iNbr);

                    // 清除排课的教师信息
                    ScheduleService.DelProfessors();

                    // 生成将课表数据保存到数据库中
                    if (ScheduleService.StoreSchedule(scheduledTable, tbYear.Text, term.ToString()))
                    {
                        IsSucess = true;
                    }

                    /*
                    string ExcelPathForStudent = GetExcelPathForStudent();
                    string ExcelPathForTeacher = GetExcelPathForTeacher();
                    string ActualExcelPathForStudent = string.Empty;
                    string ActualExcelPathForTeacher = string.Empty;

                    ScheduleService.ExportSchedule(scheduledTable, ref ExcelPathForStudent, ref ExcelPathForTeacher, out ActualExcelPathForStudent, out ActualExcelPathForTeacher);
                    // 将课表导入Excel 并存储课表的保存地址
                    //if (!File.Exists(ExcelPathForStudent) && !File.Exists(ExcelPathForTeacher))
                    //{
                    //    // 在没有生成课表的情况下才存课表,如果已存有课表的话,是不需要生成课表的,也是不允许生成新的课表的。
                    //    ScheduleService.ExportSchedule(scheduledTable, ref ExcelPathForStudent, ref ExcelPathForTeacher, out ActualExcelPathForStudent, out ActualExcelPathForTeacher);

                    //    if (StordScheduleRcd(ExcelNameForStudent, ExcelRelativeFilePath, tbYear.Text.Trim(), ddlTerm.SelectedValue.Trim()) && StordScheduleRcd(ExcelNameForTeacher, ExcelRelativeFilePath, tbYear.Text.Trim(), ddlTerm.SelectedValue.Trim()))
                    //    {
                    //        //IsSucess = true;
                    //    }
                    //}
                     *
                     */

                }
            }
            catch (Exception ex)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('" + ex.Message.ToString() + "');</script>");
            }

            if (IsSucess)
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('生成课表成功');</script>");
                return;
            }
            else
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "script", "<script type='text/javascript'>alert('该学期的课表已存在!');</script>");
                return;
            }
        }