public CoursePageViewModel()
 {
     currentcourse = new ClassDetail();
     currentcourse = App.currentcourse??currentcourse;
     if (currentcourse.CourseName == null)
     {
         currentcourse.CourseName = "";
     }
     nextcourse = App.nextcourse;
     coursename = (from n in App.classtable.ToList() where n.CourseNum != null select n.CourseName).ToList();
     coursename = coursename.Distinct().ToList();
 }
 public StatisticsDetailViewModel(ClassDetail d)
 {
     this.DetailCourse = d;
     //set(d);
 }
 void set(ClassDetail d)
 {
     Currenthomelist =  gethomeworkasync(d.StudentListUrl);
 }
        //从数据库获取课表
        void getclasstable()
        {
            List<ClassDetail> classtable = new List<ClassDetail>();
            string sql = "select * from classtable";

            OleDbDataReader reader = AccessDBHelper.ExecuteReader(sql, App.Databasefilepath);
            while (reader.Read())
            {
                ClassDetail tcsa = new ClassDetail();
                tcsa.CourseNum = reader["coursenum"].ToString();
                tcsa.CourseName = reader["coursename"].ToString();
                tcsa.Classroom = reader["classroom"].ToString();
                tcsa.LastWeeks = Array.ConvertAll<string, int>(reader["lastweeks"].ToString().Split(','), s => int.Parse(s));
                tcsa.ClassType = reader["classtype"].ToString();
                tcsa.Subject = reader["subject"].ToString();
                tcsa.StuClassNum = reader["stuclassnum"].ToString().Split(',');
                tcsa.CourseDay = reader["courseday"].ToString();
                tcsa.CourseTime = reader["coursetime"].ToString();
                tcsa.StudentListUrl = reader["stulisturl"].ToString();
                tcsa.CourseStuCount = AccessDBHelper.ExecuteNonQuery($"select count(*) from {tcsa.StudentListUrl}",App.Databasefilepath);
                classtable.Add(tcsa);

                //比较是否是同一节课,只是周数不同
                for (int i = 0; i < classtable.Count - 1; i++)
                {
                    for (int j = i + 1; j < classtable.Count; j++)
                    {
                        if (classtable[i].Compare(classtable[j]))
                        {
                            int[] int1 = classtable[i].LastWeeks;
                            int[] int2 = classtable[j].LastWeeks;
                            int[] all = new int[int1.Length + int2.Length];
                            int1.CopyTo(all, 0);
                            int2.CopyTo(all, int1.Length);
                            Array.Sort(all);
                            classtable[i].LastWeeks = all;
                            classtable.Remove(classtable[j]);
                        }
                    }
                }
                for (int i = 0; i < classtable.Count; i++)
                {
                    if (classtable[i].LastWeeks[0] == -1)
                    {
                        var v = from n in classtable where n.StudentListUrl == classtable[i].StudentListUrl select n.LastWeeks;
                        var all = v.ToArray();
                        for (int k = 0; k < all.GetLength(0); k++)
                        {
                            if (all[k].Length > 1)
                            {
                                var odd = from n in all[k] where n % 2 != 0 select n;
                                classtable[i].LastWeeks = odd.ToArray();
                            }
                        }
                    }
                    if (classtable[i].LastWeeks[0] == -2)
                    {
                        var v = from n in classtable where n.StudentListUrl == classtable[i].StudentListUrl select n.LastWeeks;
                        var all = v.ToArray();
                        for (int k = 0; k < all.GetLength(0); k++)
                        {
                            if (all[k].Length > 1)
                            {
                                var even = from n in all[k] where n % 2 == 0 select n;
                                classtable[i].LastWeeks = even.ToArray();
                            }
                        }
                    }
                }
                classtable.Sort(new ClassDetail());
                App.classtable = classtable;
                listclass = classtable;

            }
            reader.Close();
            AccessDBHelper.CloseConnectDB();
        }
        //void getdata()
        //{
        //    OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + App.Databasefilepath);
        //    OleDbDataAdapter da = new OleDbDataAdapter("Select * from A041518124736", conn);
        //    DataSet ds = new DataSet();
        //    DataSet quertDs = new DataSet();
        //    OleDbCommandBuilder cmd = new OleDbCommandBuilder(da);
        //    conn.Open();
        //    da = new OleDbDataAdapter("Select * from A041518124736", conn);
        //    //da.InsertCommand = cmd.GetInsertCommand();
        //    //da.UpdateCommand = cmd.GetUpdateCommand();
        //    //da.DeleteCommand = cmd.GetDeleteCommand();
        //    da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
        //    da.Fill(ds);
        //    conn.Close();
        //    stulist.DataContext = ds.Tables[0];
        //}
        async void getclasstable()
        {
            string sql = "select * from classtable";
            await Task.Run(() =>
            {
                OleDbDataReader reader = AccessDBHelper.ExecuteReader(sql, App.Databasefilepath);
                while (reader.Read())
                {
                    ClassDetail tcsa = new ClassDetail();
                    tcsa.CourseNum = reader["coursenum"].ToString();
                    tcsa.CourseName = reader["coursename"].ToString();
                    tcsa.Classroom = reader["classroom"].ToString();
                    tcsa.LastWeeks = Array.ConvertAll<string, int>(reader["lastweeks"].ToString().Split(','), s => int.Parse(s));
                    tcsa.ClassType = reader["classtype"].ToString();
                    tcsa.Subject = reader["subject"].ToString();
                    tcsa.StuClassNum = reader["stuclassnum"].ToString().Split(',');
                    tcsa.CourseDay = reader["courseday"].ToString();
                    tcsa.CourseTime = reader["coursetime"].ToString();
                    tcsa.StudentListUrl = reader["stulisturl"].ToString();
                    classtable.Add(tcsa);
                }
            });
            //比较是否是同一节课,只是周数不同
            for (int i = 0; i < classtable.Count - 1; i++)
            {
                for (int j = i + 1; j < classtable.Count; j++)
                {
                    if (classtable[i].Compare(classtable[j]))
                    {
                        int[] int1 = classtable[i].LastWeeks;
                        int[] int2 = classtable[j].LastWeeks;
                        int[] all = new int[int1.Length + int2.Length];
                        int1.CopyTo(all, 0);
                        int2.CopyTo(all, int1.Length);
                        Array.Sort(all);
                        classtable[i].LastWeeks = all;
                        classtable.Remove(classtable[j]);
                    }
                }
            }
            for (int i = 0; i < classtable.Count; i++)
            {
                if (classtable[i].LastWeeks[0] == -1)
                {
                    var v = from n in classtable where n.StudentListUrl == classtable[i].StudentListUrl select n.LastWeeks;
                    var all = v.ToArray();
                    for (int k = 0; k < all.GetLength(0); k++)
                    {
                        if (all[k].Length > 1)
                        {
                            var odd = from n in all[k] where n % 2 != 0 select n;
                            classtable[i].LastWeeks = odd.ToArray();
                        }
                    }
                }
                if (classtable[i].LastWeeks[0] == -2)
                {
                    var v = from n in classtable where n.StudentListUrl == classtable[i].StudentListUrl select n.LastWeeks;
                    var all = v.ToArray();
                    for (int k = 0; k < all.GetLength(0); k++)
                    {
                        if (all[k].Length > 1)
                        {
                            var even = from n in all[k] where n % 2 == 0 select n;
                            classtable[i].LastWeeks = even.ToArray();
                        }
                    }
                }
            }
            classtable.Sort(new ClassDetail());
            this.table.ItemsSource = classtable;
            //List<ClassDetail> cd2 = new List<ClassDetail>();
            //var group = classtable.GroupBy(p => p.CourseNum);
            //foreach (var item in group)
            //{
            //    ClassDetail c=new  ClassDetail
            //}

            //classtable.ForEach(c =>
            //{
            //    var group = classtable.Where(a => a.CourseDay == c.CourseDay && a.CourseTime == c.CourseDay && a.CourseNum == c.CourseNum);
            //    var weeks = group.Select(t => t.LastWeeks).ToArray();

            //});
        }
 /// <summary>
 /// 获取教师课表
 /// </summary>
 /// <param name="teachernum">教师编号</param>
 /// <returns>返回课程列表</returns>
 public static List<ClassDetail> GetClassTable(string teachernum)
 {
     MemoryStream ms;
     HtmlDocument doc = new HtmlDocument();
     HtmlDocument docStockContext = new HtmlDocument();
     string url = @"http://jwzx.cqupt.edu.cn/new/labkebiao/showteakebiao2.php?tid=" + teachernum;
     byte[] downloadbytes = DownLoad(url);
     if (downloadbytes.Length > 1 && downloadbytes[0] != 0)
     {
         ms = new MemoryStream(downloadbytes);
         doc.Load(ms, Encoding.GetEncoding("gbk"));
     }
     else
     {
         return null;
     }
     var table = doc.DocumentNode.SelectSingleNode("/html/body/div/table[1]").InnerHtml;
     docStockContext.LoadHtml(table);
     //获得星期的列表
     HtmlNodeCollection nodeHeaders = docStockContext.DocumentNode.SelectNodes("/tr[1]/td");
     //移除第一个空格
     nodeHeaders.RemoveAt(0);
     //星期列表
     string[] date = new string[7];
     for (int i = 0; i < nodeHeaders.Count; i++)
     {
         date[i] = nodeHeaders[i].InnerHtml;
     }
     //上课时间段数组
     string[] times = new string[6];
     //获取表格每一列
     HtmlNodeCollection list = docStockContext.DocumentNode.SelectNodes("/tr");
     //移除表头
     list.RemoveAt(0);
     //大课表
     List<ClassDetail> classtable = new List<ClassDetail>();
     //循环每个上课时间段
     for (int i = 0; i < list.Count; i++)
     {
         var item = list[i].SelectNodes("td");//获取行
         times[i] = item[0].InnerText.Trim();//时间段
         int timeclasscout = 0;
         for (int j = 1; j < item.Count; j++)//从星期一到星期天
         {
             //var iii = item[j].InnerHtml.Trim().LastIndexOf('&');
             if (item[j].InnerHtml.Trim().Length != 6)
             {
                 string innerhtml = item[j].InnerHtml.Replace("&nbsp;", "");
                 string temp = innerhtml.Replace("学生名单", "");
                 int k = (innerhtml.Length - temp.Length) / 4;
                 item[j].InnerHtml = item[j].InnerHtml.Replace("&nbsp;", "");
                 var inner = item[j].SelectNodes("./text()");
                 for (int l = 0; l < k; l++)
                 {
                     int offset = inner.Count / k * l;
                     string coursenum = inner[0 + offset].InnerText.Trim().Substring(0, 6);
                     string coursename = inner[0 + offset].InnerText.Trim().Substring(6);
                     string classroom = inner[1 + offset].InnerText.Trim();
                     int[] lastweeks = TransLastWeeks(inner[2 + offset].InnerText.Trim());
                     var classtype = item[j].SelectNodes("./font")[l * 2].InnerText.Trim();
                     string subject = inner[4 + offset].InnerText.Trim();
                     string[] stuclasses = TransClasslist(inner[5 + offset].InnerText.Trim());
                     string listurl = item[j].SelectNodes("..//a[@href]")[timeclasscout].Attributes["href"].Value.Trim().Substring(20);
                     ClassDetail cd = new ClassDetail { CourseNum = coursenum, CourseName = coursename, Classroom = classroom, LastWeeks = lastweeks, ClassType = classtype, Subject = subject, StuClassNum = stuclasses, StudentListUrl = listurl, CourseDay = date[j - 1], CourseTime = times[i] };
                     classtable.Add(cd);
                     timeclasscout++;
                 }
             }
         }
     }
     return classtable;
 }