/// <summary>
        /// 儲存檔案
        /// </summary>
        private void SaveValue(string SelectPath, GraduateUDT value1, WriteCounselingUDT value2)
        {
            //讀取資料
            byte[] base64 = Convert.FromBase64String(value2.Content);

            //建立檔案
            StringBuilder sb = new StringBuilder();

            sb.Append(SelectPath + "\\");
            sb.Append(value1.StudentNumber + "_");
            sb.Append(value2.Name);
            sb.Append("(" + value1.Name + ")" + value2.Format);

            FileStream fs = new FileStream(sb.ToString(), FileMode.Create);

            //寫入檔案
            fs.Write(base64, 0, base64.Length);
            fs.Close();
        }
Exemple #2
0
        void linkBGW_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] ofd = (string[])e.Argument;

            List <string> ofdName = new List <string>();

            foreach (string each in ofd)
            {
                ofdName.Add(Path.GetFileName(each));
            }

            QueryHelper  _queryhelper  = new QueryHelper();
            AccessHelper _AccessHelper = new AccessHelper();

            string    TableName = Tn._WriteCounselingUDT;
            DataTable dt        = _queryhelper.Select("select name,format from " + TableName.ToLower() + " where " + string.Format("RefUDT_ID = '{0}'", this.PrimaryKey));

            ReNameCheck = new List <string>();
            foreach (DataRow each in dt.Rows)
            {
                string information = "" + each["name"] + each["format"];

                //上傳檔案必須確認
                //1.學生身上有哪些書面資料
                if (ofdName.Contains(information))
                {
                    //2.檔名來源是否重覆
                    ReNameCheck.Add(information);
                }
            }

            //如果檔名重覆
            if (ReNameCheck.Count != 0)
            {
                e.Cancel = true;
            }
            else
            {
                List <WriteCounselingUDT> InsertUDTData = new List <WriteCounselingUDT>();

                //Log
                StringBuilder Logsb = new StringBuilder();
                Logsb.AppendLine("批次上傳書面(輔導)資料:");
                string SeatNo = _StudentUdt.SeatNo.HasValue ? _StudentUdt.SeatNo.Value.ToString() : "";
                Logsb.AppendLine("班級「" + _StudentUdt.ClassName + "」座號「" + SeatNo + "」學號「" + _StudentUdt.StudentNumber + "」姓名「" + _StudentUdt.Name + "」");


                //3.使用者選擇之資料(去路徑&副檔名)即為檔名
                foreach (string each in ofd)
                {
                    WriteCounselingUDT udt = new WriteCounselingUDT();
                    udt.RefUDT_ID = this.PrimaryKey;
                    udt.StudentID = _StudentUdt.StudentID;
                    udt.Name      = Path.GetFileNameWithoutExtension(each);
                    udt.Format    = Path.GetExtension(each).ToLower();
                    udt.Date      = DateTime.Today; //封存日期為今天

                    Logsb.Append("書面名稱(輔導)「" + udt.Name + udt.Format + "」");

                    FileStream fs         = new FileStream(each, FileMode.Open);
                    byte[]     tempBuffer = new byte[fs.Length];
                    fs.Read(tempBuffer, 0, tempBuffer.Length);
                    string base64 = Convert.ToBase64String(tempBuffer);

                    if (base64 != "") //如果不是空值
                    {
                        udt.Content = base64;
                        InsertUDTData.Add(udt);
                    }
                }



                if (InsertUDTData.Count != 0)
                {
                    _AccessHelper.InsertValues(InsertUDTData.ToArray());

                    ApplicationLog.Log("畢業生檔案檢索.個人書面資料(輔導)", "批次上傳", Logsb.ToString());
                }
            }
        }
        /// <summary>
        /// 依學號匯入
        /// </summary>
        private void InsertByIDNumber(Dictionary <string, string> FileDic)
        {
            if (FileDic.Count == 0)
            {
                return;
            }

            //新增與更新內容
            List <WriteCounselingUDT> InsertUDTData = new List <WriteCounselingUDT>();
            List <WriteCounselingUDT> UpDataUDTData = new List <WriteCounselingUDT>();

            BGW.ReportProgress(10, "判斷是否為封存學生...");

            //依照檔名,取得目前系統內該學號,是否為現存封存學生
            //需新增條件 - 封存分類(11/11日)
            List <GraduateUDT> ExtantStudentUDT = _AccessHelper.Select <GraduateUDT>(UDT_S.PopOneCondition("StudentNumber", FileDic.Keys.ToList()) + " AND ArchiveNote='" + _Note + "'");

            BGW.ReportProgress(15, "是否已有封存書面資料...");
            Dictionary <string, WriteCounselingUDT> HaveWrittenDic = new Dictionary <string, WriteCounselingUDT>();
            //取得該學生,是否有封存"學籍表"書面資料
            List <string> list = new List <string>();

            foreach (GraduateUDT each in ExtantStudentUDT)
            {
                list.Add(each.UID);
            }
            BGW.ReportProgress(18, "是否已有封存書面資料...");
            if (ExtantStudentUDT.Count != 0)
            {
                List <WriteCounselingUDT> HaveWrittenList = _AccessHelper.Select <WriteCounselingUDT>(UDT_S.PopOneCondition("RefUDT_ID", list));
                BGW.ReportProgress(25, "判斷書面資料...");
                //RefUDT_ID / Record
                foreach (WriteCounselingUDT each in HaveWrittenList)
                {
                    if (each.Name != WritleName) //不是本表單則跳開
                    {
                        continue;
                    }

                    if (!HaveWrittenDic.ContainsKey(each.RefUDT_ID))
                    {
                        HaveWrittenDic.Add(each.RefUDT_ID, each);
                    }
                    else
                    {
                        MsgBox.Show("學生有重覆2張不同之" + WritleName + "!!");
                    }
                }
            }
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("書面名稱「" + WritleName + "」");

            int countTime = 35;

            BGW.ReportProgress(countTime);

            double count1 = ExtantStudentUDT.Count / 55; //百分之1的度量
            double count  = 0;

            foreach (GraduateUDT each in ExtantStudentUDT)
            {
                count++;
                if (count >= count1)
                {
                    count = 0;   //歸0
                    countTime++; //步進1
                    BGW.ReportProgress(countTime, "建立學生書面資料...");
                }

                if (FileDic.ContainsKey(each.StudentNumber)) //匯入資料夾中,是否有該學號記錄
                {
                    //沒有相同的書面資料
                    if (!HaveWrittenDic.ContainsKey(each.UID))
                    {
                        sb.Append("「新增」書面資料,");
                        sb.Append("班級「" + each.ClassName + "」");
                        sb.Append("座號「" + (each.SeatNo.HasValue ? each.SeatNo.Value.ToString() : "") + "」");
                        sb.AppendLine("姓名「" + each.Name + "」");

                        WriteCounselingUDT udt = new WriteCounselingUDT();
                        udt.RefUDT_ID = each.UID;
                        udt.StudentID = each.StudentID;
                        udt.Name      = WritleName;
                        udt.Format    = Path.GetExtension(FileDic[each.StudentNumber]).ToLower();
                        udt.Date      = DateTime.Today; //封存日期為今天

                        //依照副檔名,取得檔案內容
                        string base64 = InsertFormat(FileDic[each.StudentNumber]);
                        if (base64 != "") //如果不是空值
                        {
                            udt.Content = base64;
                            InsertUDTData.Add(udt);
                        }
                    }
                    else //如果有相同檔名
                    {
                        sb.Append("「更新」書面資料,");
                        sb.Append("班級「" + each.ClassName + "」");
                        sb.Append("座號「" + (each.SeatNo.HasValue ? each.SeatNo.Value.ToString() : "") + "」");
                        sb.AppendLine("姓名「" + each.Name + "」");

                        WriteCounselingUDT udt = HaveWrittenDic[each.UID];
                        //書面之名稱
                        udt.Name = WritleName;
                        //格式可調整如:DOC -> XLS
                        udt.Format = Path.GetExtension(FileDic[each.StudentNumber]).ToLower();
                        //更新日期調整為今天
                        udt.Date = DateTime.Today;
                        //依照副檔名,取得檔案內容
                        string base64 = InsertFormat(FileDic[each.StudentNumber]);
                        if (base64 != "")
                        {
                            udt.Content = base64;
                            UpDataUDTData.Add(udt);
                        }
                    }
                }
            }

            if (InsertUDTData.Count != 0)
            {
                BGW.ReportProgress(80, "開始新增上傳書面資料...");
                _AccessHelper.InsertValues(InsertUDTData.ToArray());
            }
            if (UpDataUDTData.Count != 0)
            {
                BGW.ReportProgress(90, "開始更新上傳書面資料...");
                _AccessHelper.UpdateValues(UpDataUDTData.ToArray());
            }

            BGW.ReportProgress(100, "上傳完成...");
            ApplicationLog.Log("畢業生檔案檢索.書面資料", "上傳", sb.ToString());
        }