Beispiel #1
0
        void _bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            _bgWorker.ReportProgress(1);
            // 取得所選學生資料
            _StudentDataList = QueryData.GetStudentDataListByStudentIDs(_SelectStudentIDList);

            // 取得領域成績並判斷
            _StudentDataList = Utility.CalcStudDomainScorePass(_StudentDataList, _SchoolYear, _Semester, _passScore);

            _bgWorker.ReportProgress(30);


            // 取得收件人資料,取聯絡、戶籍 
            // 取得父母監護人資訊
            Dictionary <string, JHParentRecord> ParentRecordDict = new Dictionary <string, JHParentRecord>();

            foreach (JHParentRecord rec in JHParent.SelectByStudentIDs(_SelectStudentIDList))
            {
                if (!ParentRecordDict.ContainsKey(rec.RefStudentID))
                {
                    ParentRecordDict.Add(rec.RefStudentID, rec);
                }
            }

            // 取得地址相關資訊
            Dictionary <string, JHAddressRecord> AddressRecordDict = new Dictionary <string, JHAddressRecord>();

            foreach (JHAddressRecord rec in JHAddress.SelectByStudentIDs(_SelectStudentIDList))
            {
                if (!AddressRecordDict.ContainsKey(rec.RefStudentID))
                {
                    AddressRecordDict.Add(rec.RefStudentID, rec);
                }
            }


            Document DocTemp = null;

            // 讀取範本,如果沒有使用預設樣版
            if (_Configure.Template == null)
            {
                _Configure.Template = new Document(new MemoryStream(Properties.Resources.領域補考通知單範本));
                _Configure.Encode();
                _Configure.Save();
            }

            DocTemp = _Configure.Template.Clone();

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

            ColumnList.Add("學年度");
            ColumnList.Add("學期");
            ColumnList.Add("班級");
            ColumnList.Add("座號");
            ColumnList.Add("姓名");
            ColumnList.Add("學號");
            ColumnList.Add("學校名稱");
            ColumnList.Add("學校電話");
            ColumnList.Add("學校地址");
            ColumnList.Add("收件人地址");
            ColumnList.Add("收件人姓名");
            ColumnList.Add("補考數");
            ColumnList.Add("監護人姓名");
            ColumnList.Add("父親姓名");
            ColumnList.Add("母親姓名");
            ColumnList.Add("戶籍地址");
            ColumnList.Add("聯絡地址");
            ColumnList.Add("其他地址");



            for (int i = 1; i <= 10; i++)
            {
                ColumnList.Add("領域名稱" + i);
                ColumnList.Add("原始成績" + i);
            }


            List <Document> docList = new List <Document>();
            DataTable       dt      = new DataTable();

            // 填入欄位
            foreach (string name in ColumnList)
            {
                dt.Columns.Add(name);
            }

            _bgWorker.ReportProgress(70);

            foreach (StudentData sd in _StudentDataList)
            {
                // 填入資料
                DataRow row = dt.NewRow();
                row["學年度"]  = _SchoolYear;
                row["學期"]   = _Semester;
                row["班級"]   = sd.ClassName;
                row["座號"]   = sd.SeatNo;
                row["姓名"]   = sd.Name;
                row["學號"]   = sd.StudentNumber;
                row["學校名稱"] = K12.Data.School.ChineseName;
                row["學校電話"] = K12.Data.School.Telephone;
                row["學校地址"] = K12.Data.School.Address;

                int ReDomainCount = sd.GetReDomainCount();
                row["補考數"] = ReDomainCount;

                int rDomCOunt = 1;
                if (sd.StudSemesterScoreRecord != null)
                {
                    foreach (K12.Data.DomainScore ds in sd.StudSemesterScoreRecord.Domains.Values)
                    {
                        if (ds.Score.HasValue)
                        {
                            if (ds.Domain != "國語文" && ds.Domain != "英語") // 2017/1/9 穎驊 註解, 高雄不再分別使用 國語文 、英語 領域,而是兩領域合一算一"語文"領域成績 來看有沒有及格。
                            {
                                if (ds.Score.Value < _passScore)
                                {
                                    row["領域名稱" + rDomCOunt] = ds.Domain;
                                    row["原始成績" + rDomCOunt] = "";

                                    if (ds.ScoreOrigin.HasValue)
                                    {
                                        row["原始成績" + rDomCOunt] = ds.ScoreOrigin.Value;
                                    }

                                    rDomCOunt++;
                                }
                            }
                        }
                    }
                }

                if (ParentRecordDict.ContainsKey(sd.StudentID))
                {
                    row["監護人姓名"] = ParentRecordDict[sd.StudentID].CustodianName;
                    row["父親姓名"]  = ParentRecordDict[sd.StudentID].FatherName;
                    row["母親姓名"]  = ParentRecordDict[sd.StudentID].MotherName;

                    if (!string.IsNullOrEmpty(ParentRecordDict[sd.StudentID].CustodianName))
                    {
                        row["收件人姓名"] = ParentRecordDict[sd.StudentID].CustodianName;
                    }
                    else if (!string.IsNullOrEmpty(ParentRecordDict[sd.StudentID].FatherName))
                    {
                        row["收件人姓名"] = ParentRecordDict[sd.StudentID].FatherName;
                    }
                    else
                    {
                        row["收件人姓名"] = ParentRecordDict[sd.StudentID].MotherName;
                    }
                }
                if (AddressRecordDict.ContainsKey(sd.StudentID))
                {
                    row["戶籍地址"] = AddressRecordDict[sd.StudentID].PermanentAddress;
                    row["聯絡地址"] = AddressRecordDict[sd.StudentID].MailingAddress;
                    row["其他地址"] = AddressRecordDict[sd.StudentID].Address1Address;

                    if (!string.IsNullOrEmpty(AddressRecordDict[sd.StudentID].MailingAddress))
                    {
                        row["收件人地址"] = AddressRecordDict[sd.StudentID].MailingAddress;
                    }
                    else if (!string.IsNullOrEmpty(AddressRecordDict[sd.StudentID].PermanentAddress))
                    {
                        row["收件人地址"] = AddressRecordDict[sd.StudentID].PermanentAddress;
                    }
                    else
                    {
                        row["收件人地址"] = AddressRecordDict[sd.StudentID].Address1Address;
                    }
                }

                // 需要補考再出現
                if (ReDomainCount > 0)
                {
                    dt.Rows.Add(row);
                }
            }
            //#region debug
            //dt.TableName = "debug";
            //dt.WriteXml(Application.StartupPath+"\\dttable.xml");
            //StreamWriter sw = new StreamWriter(Application.StartupPath + "\\tempField.txt", false);
            //foreach (string str in DocTemp.MailMerge.GetFieldNames())
            //    sw.WriteLine(str);
            //sw.Flush();
            //sw.Close();

            //#endregion


            if (DocTemp != null)
            {
                // 處理固定欄位對應
                DocTemp.MailMerge.Execute(dt);
                DocTemp.MailMerge.RemoveEmptyParagraphs = true;
                DocTemp.MailMerge.DeleteFields();
            }

            _bgWorker.ReportProgress(90);
            e.Result = DocTemp;
        }