private void btnBrowse_Click(object sender, EventArgs e)
 {
     PhotoBatchFileManager1 = new PhotoBatchFileManager();
     txtFilePath.Text = PhotoBatchFileManager1.GetFilefoldrBrowserDialog();
 }
        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (PhotoBatchFileManager1 == null)
                PhotoBatchFileManager1 = new PhotoBatchFileManager();

            if (string.IsNullOrEmpty(txtFilePath.Text))
            {
                MessageBox.Show("請輸入匯出路徑");
                return;
            }

            if (PhotoBatchFileManager1.checkHasFolder(txtFilePath.Text) == false)
            {
                MessageBox.Show("匯出目錄不存在");
                return;
            }

            if (cbxEnroll.Checked)
                PhotoKind = StudPhotoEntity.PhotoKind.入學;

            if (cbxGraduate.Checked)
                PhotoKind = StudPhotoEntity.PhotoKind.畢業;

            if (cbxByStudentNum.Checked)
                PhotoNameRule = StudPhotoEntity.PhotoNameRule.學號;

            //if (cbxByStudentIDNumber.Checked)
            //    PhotoNameRule = StudPhotoEntity.PhotoNameRule.身分證號;

            //if (cbxByClassNameSeatNo.Checked)
            //    PhotoNameRule = StudPhotoEntity.PhotoNameRule.班級姓名;

            SelStudenrList = new List<StudentRecord>();
            SelStudenrList = Student.SelectByIDs(NLDPanels.Student.SelectedSource);
            List<StudPhotoEntity> StudPhotoEntityList = new List<StudPhotoEntity>();

            _ExportFolderName.Clear();
            foreach (StudentRecord studRec in SelStudenrList)
            {
                StudPhotoEntity spe = new StudPhotoEntity();
                spe.StudentNumber = studRec.StudentNumber;
                spe.StudentID = studRec.ID;
                spe.SeatNo = K12.Data.Int.GetString(studRec.SeatNo);
                spe.StudentName = studRec.Name;
                spe.StudentIDNumber = studRec.IDNumber;
                if (studRec.Class != null)
                    spe.ClassName = studRec.Class.Name;
                spe._PhotoKind = PhotoKind;
                spe._PhotoNameRule = PhotoNameRule;

                if (spe._PhotoNameRule == StudPhotoEntity.PhotoNameRule.學號)
                    spe.SaveFilePathAndName = txtFilePath.Text + "\\" + spe.StudentNumber + ".jpg";

                //if (spe._PhotoNameRule == StudPhotoEntity.PhotoNameRule.身分證號)
                //    spe.SaveFilePathAndName = txtFilePath.Text + "\\" + spe.StudentIDNumber + ".jpg";

                //if (spe._PhotoNameRule == StudPhotoEntity.PhotoNameRule.班級姓名)
                //{
                //    string exportFolderName = txtFilePath.Text + "\\" + spe.ClassName;
                //    spe.SaveFilePathAndName = exportFolderName + "\\" + spe.SeatNo + ".jpg";
                //    if (!_ExportFolderName.Contains(exportFolderName))
                //        _ExportFolderName.Add(exportFolderName);
                //}

                StudPhotoEntityList.Add(spe);
            }

            StudPhotoEntityList = Transfer.GetStudentPhotoBitmap(StudPhotoEntityList);

            Dictionary<string, Bitmap> saveFreshmanFiles = new Dictionary<string, Bitmap>();
            Dictionary<string, Bitmap> saveGraduateFiles = new Dictionary<string, Bitmap>();
            // 入學
            if (cbxEnroll.Checked)
            {
                foreach (StudPhotoEntity spe in StudPhotoEntityList)
                {
                    if (!saveFreshmanFiles.ContainsKey(spe.SaveFilePathAndName))
                    {
                        if (spe.FreshmanPhotoBitmap == null)
                            spe.ErrorMessage += "沒有照片, 命名方式:" + spe._PhotoNameRule.ToString() + "," + spe.SaveFilePathAndName;
                        else
                        {
                            saveFreshmanFiles.Add(spe.SaveFilePathAndName, spe.FreshmanPhotoBitmap);
                            Log(spe);
                        }
                    }
                    else
                        spe.ErrorMessage += "檔名有重複無法匯出, 命名方式:" + spe._PhotoNameRule.ToString() + "," + spe.SaveFilePathAndName;
                }
            }

            // 畢業
            if (cbxGraduate.Checked)
            {
                foreach (StudPhotoEntity spe in StudPhotoEntityList)
                {
                    if (!saveGraduateFiles.ContainsKey(spe.SaveFilePathAndName))
                    {
                        if (spe.GraduatePhotoBitmap == null)
                            spe.ErrorMessage += " 沒有照片, 命名方式:" + spe._PhotoNameRule.ToString() + "," + spe.SaveFilePathAndName;
                        else
                        {
                            saveGraduateFiles.Add(spe.SaveFilePathAndName, spe.GraduatePhotoBitmap);
                            Log(spe);
                        }
                    }
                    else
                        spe.ErrorMessage += " 檔名有重複無法匯出, 命名方式:" + spe._PhotoNameRule.ToString() + "," + spe.SaveFilePathAndName;
                }
            }

            // 收集錯誤訊息
            List<StudPhotoEntity> ErrorData = new List<StudPhotoEntity>();

            foreach (StudPhotoEntity spe in StudPhotoEntityList)
            {
                if (!string.IsNullOrEmpty(spe.ErrorMessage))
                    ErrorData.Add(spe);
            }

            // 當選擇班級時建立資料夾
            if (cbxByClassNameSeatNo.Checked)
            {
                foreach (string str in _ExportFolderName)
                    PhotoBatchFileManager1.CreateFolder(str);

            }

            if (saveFreshmanFiles.Count > 0)
                PhotoBatchFileManager1.SaveFiles(saveFreshmanFiles);
            if (saveGraduateFiles.Count > 0)
                PhotoBatchFileManager1.SaveFiles(saveGraduateFiles);

            // 匯出完後畫面顯示
            PhotoCompleteMessage pcm = new PhotoCompleteMessage();
            string msg = "匯出檔案數:" + StudPhotoEntityList.Count + ", 匯出成功數:" + (StudPhotoEntityList.Count - ErrorData.Count) + " ,匯出失敗數:" + ErrorData.Count;

            //PermRecLogProcess prlp = new PermRecLogProcess();
            //prlp.SaveLog("學生,批次匯出照片", "匯出", "批次匯出學生照片," + msg);

            pcm.SetFormText("匯出照片訊息");
            pcm.SetMessageText(msg);
            if (ErrorData.Count > 0)
            {
                pcm.SetStudPhotoEntity(ErrorData);
                pcm.SetErrorMsgBottonEnable(true);
            }
            else
                pcm.SetErrorMsgBottonEnable(false);
            pcm.ShowDialog();
            //MessageBox.Show("存檔完成");
        }
        private void btnUpload_Click(object sender, EventArgs e)
        {
            StudPhotoEntity.PhotoKind PhotoKind = 0;
            StudPhotoEntity.PhotoNameRule PhotoNameRule = 0;
            if (PhotoBatchFileManager1 == null)
            {
                PhotoBatchFileManager1 = new PhotoBatchFileManager();
                PhotoBatchFileManager1.SetFilePath(txtFilePath.Text);
            }

            if (string.IsNullOrEmpty(PhotoBatchFileManager1.GetFilePath()))
                PhotoBatchFileManager1.SetFilePath(txtFilePath.Text);

            // 入學
            if (cbxEnroll.Checked)
                PhotoKind = StudPhotoEntity.PhotoKind.入學;

            // 畢業
            if (cbxGraduate.Checked)
                PhotoKind = StudPhotoEntity.PhotoKind.畢業;

            //if (cbxByClassNameSeatNo.Checked)
            //    PhotoNameRule = StudPhotoEntity.PhotoNameRule.班級姓名;

            //if (cbxByStudentIDNumber.Checked)
            //    PhotoNameRule = StudPhotoEntity.PhotoNameRule.身分證號;

            if (cbxByStudentNum.Checked)
                PhotoNameRule = StudPhotoEntity.PhotoNameRule.學號;

            List<StudPhotoEntity> StudPhotoEntityList = new List<StudPhotoEntity>();

            // 當沒有選擇檔案
            if (PhotoBatchFileManager1.GetFileInfo() == null)
                return;

            // 取得所選檔案資訊
            foreach (FileInfo fi in PhotoBatchFileManager1.GetFileInfo())
            {
                StudPhotoEntity spe = new StudPhotoEntity();
                spe.PhotoFileInfo = fi;
                spe._PhotoKind = PhotoKind;
                spe._PhotoNameRule = PhotoNameRule;

                StudPhotoEntityList.Add(spe);
            }

            if (cbxByClassNameSeatNo.Checked)
            {
                if (PhotoBatchFileManager1.SetCurrentFullFoldersAndFilesInfo(txtFilePath.Text) == false)
                    return;
            }

            // 放入待匯入資料
            List<StudPhotoEntity> ImportStudPhotoEntityList = new List<StudPhotoEntity>();

            // 讀取學生資料
            StudPhotoEntityList = Transfer.SetStudBaseInfo(PhotoNameRule,StudPhotoEntityList);

            // 驗證資料
            foreach (StudPhotoEntity spe in StudPhotoEntityList)
            {
                bool checkCanUpdate = false;

                if (string.IsNullOrEmpty(spe.StudentID))
                {
                    spe.ErrorMessage += spe.GetPhotoFullName() + " 沒有學生 ID";
                    checkCanUpdate = false;
                }

                // 當使用班級座號驗證與其他不同
                //if (cbxByClassNameSeatNo.Checked)
                //{
                    //if (spe._PhotoNameRule == StudPhotoEntity.PhotoNameRule.班級姓名)
                    //{
                    //    if (PhotoBatchFileManager1.CheckFolderAndFileInCurrentForClassNameSeatNo(spe.ClassName, spe.SeatNo))
                    //        checkCanUpdate = true;
                    //    else
                    //    {
                    //        spe.ErrorMessage += "檔名與命名規則不符, 命名方式:" + spe._PhotoNameRule.ToString() + ", " + spe.GetPhotoFullName();
                    //        checkCanUpdate = false;
                    //    }
                    //}
                //}
                //else
                //{

                    if (spe.CheckSelectPhotoNameRule() == false)
                    {
                        spe.ErrorMessage += "檔名與命名規則不符, 命名方式:" + spe._PhotoNameRule.ToString() + ", " + spe.GetPhotoFullName();
                        checkCanUpdate = false;
                    }
                    else
                        checkCanUpdate = true;
                //}

                if (checkCanUpdate)
                {
                    ImportStudPhotoEntityList.Add(spe);
                }
            }

            // 上傳照片
            Transfer.ImportPhotos(ImportStudPhotoEntityList);

            List<StudPhotoEntity> ErrorData = new List<StudPhotoEntity>();

            // 讀取錯誤
            foreach (StudPhotoEntity spe in StudPhotoEntityList)
            {
                if (!string.IsNullOrEmpty(spe.ErrorMessage))
                    ErrorData.Add(spe);
            }
            // 匯入完後畫面顯示
            PhotoCompleteMessage pcm = new PhotoCompleteMessage();
            string msg = "匯入檔案數:" + StudPhotoEntityList.Count + ",匯入成功數:" + (StudPhotoEntityList.Count - ErrorData.Count) + ",匯入失敗數:" + ErrorData.Count;

            PermRecLogProcess prlp = new PermRecLogProcess();
            prlp.SaveLog("學生,批次匯入照片", "匯入", "批次匯入學生照片," + msg);

            pcm.SetFormText("匯入照片訊息");
            pcm.SetMessageText(msg);
            if (ErrorData.Count > 0)
            {
                pcm.SetStudPhotoEntity(ErrorData);
                pcm.SetErrorMsgBottonEnable(true);
            }
            else
                pcm.SetErrorMsgBottonEnable(false);
            pcm.ShowDialog();
        }