/// <summary>
 /// 
 /// </summary>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 public MouseThumbnailWindow(LocalStudentWorkStore store, LocalStudent ls, Point point)
 {
     this.store = store;
     this.ls = ls;
     this.point = point;
     InitializeComponent();
 }
 /// <summary>
 /// 创建缩略图。
 /// </summary>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 /// <param name="wf"></param>
 /// <param name="w"></param>
 /// <param name="h"></param>
 /// <returns></returns>
 public static Image CreateThumbnail(LocalStudentWorkStore store, LocalStudent ls, LocalStudentWorkFile wf, int w, int h)
 {
     if (store != null && ls != null && wf != null)
     {
         try
         {
             string root = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures) + "\\SFIT_TeaClient\\" + store.TeacherID + "_" + store.CatalogID;
             if (!Directory.Exists(root)) Directory.CreateDirectory(root);
             string path = Path.GetFullPath(string.Format("{0}\\{1}_{2}_{3}x{4}.jpg", root, ls.StudentID, wf.FileID, w, h));
             if (File.Exists(path)) return Image.FromFile(path);
             if (DefaultThumbnailFormat.IsExistThumbnailFormat(wf.FileExt))
             {
                 string source = ls.Work.StudentWorkFilePath(store, ls, wf);
                 if (File.Exists(source))
                 {
                     Image img = ImageHelper.MakeThumbnail(Image.FromFile(source), w, h);
                     if (img != null)
                     {
                         img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg);
                         return img;
                     }
                 }
             }
         }
         catch (Exception e)
         {
             UtilTools.OnExceptionRecord(e, typeof(ThumbnailsHelpers));
         }
     }
     return null;
 }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 /// <param name="handler"></param>
 private void IssueWorkData(LocalStudentWorkStore store,LocalStudent ls, RaiseChangedHandler handler)
 {
     lock (this)
     {
         if (store != null && ls != null && ls.HasWork())
         {
             LocalStudentWork lsw = ls.Work;
             int len = 0;
             if (lsw.WorkFiles != null && (len = lsw.WorkFiles.Count) > 0)
             {
                 this.RaiseChanged(handler, string.Format("下发[{0},{1}]作品数据{2}", ls.StudentName, ls.StudentCode, lsw.WorkName));
                 string[] filePaths = new string[len];
                 for (int i = 0; i < len; i++)
                 {
                     filePaths[i] = lsw.StudentWorkFilePath(store, ls, lsw.WorkFiles[i]);
                 }
                 byte[] data = ZipUtils.Zip(filePaths);
                 if (data != null && data.Length > 0)
                 {
                     IssueWorkFile issue = new IssueWorkFile();
                     issue.StudentID = ls.StudentID;
                     issue.WorkName = lsw.WorkName;
                     issue.UID = store.TeacherID;
                     issue.Time = DateTime.Now;
                     issue.Data = data;
                     this.netService.SendIssueWork(issue);
                 }
                 else
                 {
                     this.RaiseChanged(handler, string.Format("压缩数据文件失败![{0},{1}]", ls.StudentName, lsw.WorkName));
                 }
             }
         }
     }
 }
        /// <summary>
        /// 启动侦听。
        /// </summary>
        /// <param name="students"></param>
        public void StartListen(StudentsEx students)
        {
            if (students == null)
            {
                throw new ArgumentNullException("students", "学生未准备就绪");
            }
            students.SetOfflineStatus();
            #region 初始化学生作品存储。
            if (this.userInfo != null && this.sci != null)
            {
                lock (this)
                {
                    this.classId = this.sci.ClassInfo.ClassID;
                    LocalStudentWorkStore store = LocalStudentWorkStore.DeSerializer(this.userInfo.UserID, this.sci.CatalogInfo.CatalogID, this.sci.ClassInfo.ClassID);
                    #region 初始化。
                    if (store == null)
                    {
                        store = new LocalStudentWorkStore();
                        store.TeacherID = this.userInfo.UserID;
                        store.GradeID = this.sci.GradeID;
                        store.CatalogID = this.sci.CatalogInfo.CatalogID;
                        store.ClassID = this.sci.ClassInfo.ClassID;
                        store.Evaluate = this.sci.Evaluate;
                    }
                    #endregion

                    #region 更新。
                    store.TeacherName = this.userInfo.UserName;
                    store.GradeName = this.sci.GradeName;
                    store.CatalogName = this.sci.CatalogInfo.CatalogName;
                    store.ClassName = this.sci.ClassInfo.ClassName;
                    store.Evaluate = this.sci.Evaluate;
                    #endregion

                    #region 更新学生信息。
                    if (students != null && students.Count > 0)
                    {
                        if (store.Students == null) store.Students = new LocalStudents();
                        string sId = null;
                        for (int i = 0; i < students.Count; i++)
                        {
                            sId = students[i].StudentID;
                            if (!string.IsNullOrEmpty(sId))
                            {
                                LocalStudent ls = store.Students[sId];
                                if (ls == null)
                                {
                                    ls = new LocalStudent(students[i]);
                                    store.Students.Add(ls);
                                }
                                //恢复关联。
                                Tools.RecoveryWorkAssociation(ref store, ls.StudentID);
                                if (ls.HasWork())
                                {
                                    students[i].Status |= StudentControl.EnumStudentState.Upload;
                                    if ((ls.Work.Status & EnumWorkStatus.Review) == EnumWorkStatus.Review)
                                    {
                                        students[i].Status |= StudentControl.EnumStudentState.Review;
                                    }
                                }

                            }
                        }
                    }
                    #endregion

                    //序列化数据。
                    if (!WorkStoreHelper.Serializer(ref store))
                    {
                        throw new Exception("生成索引文件失败(索引文件被占用),请稍后重试!");
                    }
                    //设置当前班级学生。
                    Program.STUDENTS = students;
                }
            }
            #endregion

            this.RaiseChanged("开启端口监听...");
            this.udpSocket.Start();

            this.RaiseChanged("打开文件上传端口侦听...");
            this.workUpTcpService.StartListen();
        }
 /// <summary>
 /// 获取学生作品路径。
 /// </summary>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 /// <param name="workFile"></param>
 /// <returns></returns>
 public string StudentWorkFilePath(LocalStudentWorkStore store, LocalStudent ls, LocalStudentWorkFile workFile)
 {
     if (store != null && ls != null && workFile != null)
     {
         string rootDir = store.RootDir();
         if (!string.IsNullOrEmpty(rootDir) && Directory.Exists(rootDir))
         {
             string dir = Path.GetFullPath(string.Format("{0}/{1}", rootDir, ls.StudentID));
             if (!Directory.Exists(dir))
             {
                 Directory.CreateDirectory(dir);
             }
             return Path.GetFullPath(string.Format("{0}/{1}{2}", dir, workFile.FileID, workFile.FileExt));
         }
     }
     return null;
 }
        /// <summary>
        /// 保存数据。
        /// </summary>
        /// <param name="store">学生作品存储。</param>
        /// <param name="ls">学生信息。</param>
        /// <param name="files">文件数据。</param>
        public void SaveFiles(LocalStudentWorkStore store, LocalStudent ls, StudentWorkFiles files)
        {
            if (store != null && ls != null && files != null && files.Count > 0)
            {
                if (this.WorkFiles == null)
                    this.WorkFiles = new LocalStudentWorkFiles();
                else
                    this.WorkFiles.Clear();
                for (int i = 0; i < files.Count; i++)
                {
                    StudentWorkFile swf = files[i];
                    if (swf.Data != null && swf.Data.Length > 0)
                    {
                        LocalStudentWorkFile file = new LocalStudentWorkFile();
                        file.FileID = swf.FileID;
                        file.FileName = Path.GetFileNameWithoutExtension(swf.FileName);
                        file.FileExt = swf.FileExt;
                        file.Size = swf.Size > 0 ? swf.Size : swf.Data.Length;

                        if (this.FileExt == null)
                            this.FileExt = file.FileExt;
                        else if (this.FileExt.IndexOf(file.FileExt) == -1)
                            this.FileExt += string.Format("|{0}", file.FileExt);

                        string path = this.StudentWorkFilePath(store, ls, file);
                        if (!string.IsNullOrEmpty(path))
                        {
                            using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write))
                            {
                                fs.Write(swf.Data, 0, swf.Data.Length);
                            }
                            this.WorkFiles.Add(file);
                        }
                    }
                }
            }
        }
 /// <summary>
 /// 删除作业文件。
 /// </summary>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 public bool DeleteFiles(LocalStudentWorkStore store, LocalStudent ls)
 {
     if (store == null || ls == null) return false;
     if (this.WorkFiles == null || this.WorkFiles.Count == 0) return true;
     for (int i = 0; i < this.WorkFiles.Count; i++)
     {
         try
         {
             string path = this.StudentWorkFilePath(store, ls, this.WorkFiles[i]);
             if (File.Exists(path))
             {
                 File.Delete(path);
                 this.WorkFiles.Remove(this.WorkFiles[i]);
             }
         }
         catch (Exception e)
         {
             UtilTools.OnExceptionRecord(e, this.GetType());
             return false;
         }
     }
     return true;
 }
 private static ThumbnailsControl BuildThumbnail(LocalStudentWorkStore store, LocalStudent ls)
 {
     if (store == null || ls == null) return null;
     if (ls.Work != null)
     {
         ThumbnailsControl control = new ThumbnailsControl();
         Image img = ThumbnailsHelpers.CreateThumbnailFrist(store, ls.StudentID, control.Width, control.Height);
         if (img != null)
         {
             control.WorkID = ls.Work.WorkID;
             control.StudentID = ls.StudentID;
             control.Text = ls.Work.WorkName;
             control.StudentName = ls.StudentName;
             control.Time = ls.Work.Time;
             control.ToolTip = string.Format("学生姓名:{0}\r\n作品名称:{1}\r\n目录名称:{2}\r\n作品状态:{3}\r\n作品类型:{4}\r\n描述:{5}\r\n提交时间:{6:yyyy-MM-dd HH:mm:ss}",
                 ls.StudentName, ls.Work.WorkName, store.CatalogName, EnumWorkStatusOperaTools.GetStatusName(ls.Work.Status), EnumWorkTypeOperaTools.GetTypeName(ls.Work.Type),
                 ls.Work.Description, ls.Work.Time);
             control.Thumbnails = img;
             return control;
         }
     }
     return null;
 }
 /// <summary>
 /// 构造函数。
 /// </summary>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 public StudentWorkUpload(LocalStudentWorkStore store, LocalStudent ls)
 {
     this.Status = 0;
     this.Store = store;
     this.Student = ls;
     this.Number = 1;
 }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="storage"></param>
        /// <param name="handler"></param>
        private void ImportWorkData(StudentWorkTeaStorage storage,RaiseChangedHandler handler)
        {
            lock (this)
            {
                if (string.IsNullOrEmpty(this.teacherID) || string.IsNullOrEmpty(this.teacherName))
                {
                    return;
                }
                if (this.zipFile != null && storage != null && !string.IsNullOrEmpty(storage.CatalogID) && !string.IsNullOrEmpty(storage.ClassID) &&
                    (storage.Student != null) && !string.IsNullOrEmpty(storage.Student.StudentID))
                {
                    LocalStudentWorkStore store = LocalStudentWorkStore.DeSerializer(this.teacherID, storage.CatalogID, storage.ClassID);
                    #region 初始化数据。
                    if (store == null)
                    {
                        store = new LocalStudentWorkStore();
                        store.TeacherID = this.teacherID;
                        store.TeacherName = this.teacherName;

                        store.GradeID = storage.GradeID;
                        store.GradeName = null;

                        store.CatalogID = storage.CatalogID;
                        store.CatalogName = storage.CatalogName;

                        store.ClassID = storage.ClassID;
                        store.ClassName = null;

                        store.Evaluate = null;
                    }
                    #endregion

                    if (store.Students == null)
                    {
                        store.Students = new LocalStudents();
                    }

                    LocalStudent ls = store.Students[storage.Student.StudentID];
                    if (ls == null)
                    {
                        ls = new LocalStudent();
                        ls.StudentID = storage.Student.StudentID;
                        ls.StudentCode = storage.Student.StudentCode;
                        ls.StudentName = storage.Student.StudentName;
                        store.Students.Add(ls);
                    }

                    if (ls.Work == null) ls.Work = new LocalStudentWork();
                    ls.Work.WorkID = storage.WorkID;
                    ls.Work.WorkName = storage.WorkName;
                    ls.Work.UploadIP = storage.UploadIP;
                    ls.Work.Type = storage.Type;
                    ls.Work.Time = storage.Time;
                    ls.Work.Status = storage.Status;
                    if (storage.Review != null)
                    {
                        ls.Work.Review = new LocalWorkReview();
                        ls.Work.Review.ReviewValue = storage.Review.ReviewValue;
                        ls.Work.Review.SubjectiveReviews = storage.Review.SubjectiveReviews;
                        ls.Work.Review.TeacherID = storage.Review.TeacherID;
                        ls.Work.Review.TeacherName = storage.Review.TeacherName;
                    }
                    ls.Work.FileExt = storage.FileExt;
                    ls.Work.Description = storage.Description;
                    //ls.Work.CheckCode = storage.CheckCode;

                    this.RaiseChanged(handler, string.Format("导入[{0},{1}]作品数据...", ls.StudentName, ls.StudentCode));
                    if (this.isImportFile && !string.IsNullOrEmpty(storage.WorkPath))
                    {
                        //导入文件。
                        ls.Work.WorkFiles = new LocalStudentWorkFiles();
                        this.ImportWorkFiles(storage.WorkPath, store, ls, ls.Work, handler);
                    }
                    //序列化保存。
                    WorkStoreHelper.Serializer(ref store);
                }
            }
        }
 /// <summary>
 /// 
 /// </summary>
 /// <param name="zipWorkPath"></param>
 /// <param name="store"></param>
 /// <param name="ls"></param>
 /// <param name="lsw"></param>
 /// <param name="handler"></param>
 private void ImportWorkFiles(string zipWorkPath, LocalStudentWorkStore store, LocalStudent ls, LocalStudentWork lsw, RaiseChangedHandler handler)
 {
     if (this.zipFile != null && !string.IsNullOrEmpty(zipWorkPath))
     {
         Stream stream = stream = ZipUtils.ZipFileData(this.zipFile, zipWorkPath);
         if (stream != null)
         {
             if (string.Equals(Path.GetExtension(zipWorkPath), ".zip", StringComparison.InvariantCultureIgnoreCase))
             {
                 #region 压缩文件解压。
                 using (ZipInputStream zipStream = new ZipInputStream(stream))
                 {
                     if (zipStream == null)
                     {
                         return;
                     }
                     ZipEntry entry = null;
                     while ((entry = zipStream.GetNextEntry()) != null)
                     {
                         if (entry != null && entry.IsFile && !string.IsNullOrEmpty(entry.Name))
                         {
                             LocalStudentWorkFile lswf = new LocalStudentWorkFile();
                             lswf.FileID = string.Format("{0}", Guid.NewGuid()).Replace("-", string.Empty);
                             lswf.FileExt = Path.GetExtension(entry.Name);
                             lswf.FileName = Path.GetFileNameWithoutExtension(entry.Name);
                             if (string.IsNullOrEmpty(lsw.FileExt))
                             {
                                 lsw.FileExt = lswf.FileExt;
                             }
                             else if (lsw.FileExt.IndexOf(lswf.FileExt) == -1)
                             {
                                 lsw.FileExt += "|" + lswf.FileExt;
                             }
                             lswf.Size = entry.Size;
                             lsw.WorkFiles.Add(lswf);
                             this.RaiseChanged(handler, string.Format("导入作品{0}{1}...", lswf.FileName, lswf.FileExt));
                             using (FileStream fs = new FileStream(lsw.StudentWorkFilePath(store, ls, lswf), FileMode.Create, FileAccess.Write))
                             {
                                 byte[] buf = new byte[512];
                                 int len = 0;
                                 while ((len = zipStream.Read(buf, 0, buf.Length)) > 0)
                                 {
                                     fs.Write(buf, 0, len);
                                 }
                             }
                         }
                     }
                 }
                 #endregion
             }
             else
             {
                 #region 导入单独文件。
                 LocalStudentWorkFile lswf = new LocalStudentWorkFile();
                 lswf.FileID = string.Format("{0}", Guid.NewGuid()).Replace("-", string.Empty);
                 lswf.FileName = lsw.WorkName;
                 lswf.FileExt = Path.GetExtension(zipWorkPath);
                 if (string.IsNullOrEmpty(lsw.FileExt))
                 {
                     lsw.FileExt = lswf.FileExt;
                 }
                 else if (lsw.FileExt.IndexOf(lswf.FileExt) == -1)
                 {
                     lsw.FileExt += "|" + lswf.FileExt;
                 }
                 lswf.Size = stream.Length;
                 lsw.WorkFiles.Add(lswf);
                 this.RaiseChanged(handler, string.Format("导入作品{0}{1}...", lswf.FileName, lswf.FileExt));
                 using (FileStream fs = new FileStream(lsw.StudentWorkFilePath(store, ls, lswf), FileMode.Create, FileAccess.Write))
                 {
                     byte[] buf = new byte[512];
                     StreamUtils.Copy(stream, fs, buf);
                 }
                 #endregion
             }
         }
     }
 }