예제 #1
0
        private void _BGWDisciplineNotification_DoWork(object sender, DoWorkEventArgs e)
        {
            #region Report
            if (entityName.ToLower() == "student") //學生模式
            {
                SelectedStudents = K12.Data.Student.SelectByIDs(K12.Presentation.NLDPanels.Student.SelectedSource);
            }
            else if (entityName.ToLower() == "class") //班級模式
            {
                SelectedStudents = new List <StudentRecord>();
                foreach (StudentRecord each in Student.SelectByClassIDs(K12.Presentation.NLDPanels.Class.SelectedSource))
                {
                    if (each.Status != StudentRecord.StudentStatus.一般)
                    {
                        continue;
                    }

                    SelectedStudents.Add(each);
                }
            }
            else
            {
                throw new NotImplementedException();
            }

            SelectedStudents.Sort(new Comparison <StudentRecord>(CommonMethods.ClassSeatNoComparer));
            #endregion

            //超級資訊物件
            Dictionary <string, StudentOBJ> StudentSuperOBJ = new Dictionary <string, StudentOBJ>();
            //所有學生ID
            List <string> allStudentID = new List <string>();

            //學生人數
            int currentStudentCount = 1;
            int totalStudentNumber  = 0;

            #region 依據 ClassID 建立班級學生清單
            //List<StudentRecord> classStudent = SelectedStudents;

            //加總用
            Dictionary <string, int> StudMeritSum = new Dictionary <string, int>();

            foreach (StudentRecord aStudent in SelectedStudents)
            {
                //string aStudentID = aStudent.ID;

                if (!StudentSuperOBJ.ContainsKey(aStudent.ID))
                {
                    StudentSuperOBJ.Add(aStudent.ID, new StudentOBJ());
                }

                //學生ID清單
                if (!allStudentID.Contains(aStudent.ID))
                {
                    allStudentID.Add(aStudent.ID);
                }

                StudentSuperOBJ[aStudent.ID].student       = aStudent;
                StudentSuperOBJ[aStudent.ID].TeacherName   = aStudent.Class != null ? (aStudent.Class.Teacher != null ? aStudent.Class.Teacher.Name : "") : "";
                StudentSuperOBJ[aStudent.ID].ClassName     = aStudent.Class != null ? aStudent.Class.Name : "";
                StudentSuperOBJ[aStudent.ID].SeatNo        = aStudent.SeatNo.HasValue ? aStudent.SeatNo.Value.ToString() : "";
                StudentSuperOBJ[aStudent.ID].StudentNumber = aStudent.StudentNumber;
            }
            #endregion

            #region 取得獎懲資料(日期區間)
            List <DemeritRecord> DemeritList = new List <DemeritRecord>();

            if (obj.IsInsertDate) //發生日期
            {
                DemeritList = Demerit.SelectByOccurDate(allStudentID, obj.StartDate, obj.EndDate);
            }
            else //登錄入期
            {
                DemeritList = Demerit.SelectByRegisterDate(allStudentID, obj.StartDate, obj.EndDate);
            }
            string reportName = "留察通知單(" + obj.StartDate.ToString("yyyy-MM-dd") + "至" + obj.EndDate.ToString("yyyy-MM-dd") + ")";

            //依日期排序
            DemeritList.Sort(SortDateTime);

            foreach (DemeritRecord var in DemeritList)
            {
                string occurMonthDay = var.OccurDate.Month + "/" + var.OccurDate.Day;
                string reason        = var.Reason;

                if (var.MeritFlag == "2") //1是獎勵
                {
                    #region 留察

                    StringBuilder detailString = new StringBuilder();
                    detailString.Append(occurMonthDay + " "); //日期

                    if (!string.IsNullOrEmpty(reason))
                    {
                        detailString.Append(reason + " "); //事由
                    }
                    detailString.Append("(留校察看) ");

                    //明細資料
                    StudentSuperOBJ[var.RefStudentID].DemeritStringList.Add(detailString.ToString());
                    #endregion
                }
            }
            #endregion

            #region 取得學生通訊地址資料
            foreach (AddressRecord record in Address.SelectByStudentIDs(allStudentID))
            {
                if (obj.ReceiveAddress == "戶籍地址")
                {
                    if (!string.IsNullOrEmpty(record.PermanentAddress))
                    {
                        StudentSuperOBJ[record.RefStudentID].address = record.Permanent.County + record.Permanent.Town + record.Permanent.District + record.Permanent.Area + record.Permanent.Detail;
                    }

                    if (!string.IsNullOrEmpty(record.PermanentZipCode))
                    {
                        StudentSuperOBJ[record.RefStudentID].ZipCode = record.PermanentZipCode;

                        if (record.PermanentZipCode.Length >= 1)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode1 = record.PermanentZipCode.Substring(0, 1);
                        }
                        if (record.PermanentZipCode.Length >= 2)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode2 = record.PermanentZipCode.Substring(1, 1);
                        }
                        if (record.PermanentZipCode.Length >= 3)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode3 = record.PermanentZipCode.Substring(2, 1);
                        }
                        if (record.PermanentZipCode.Length >= 4)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode4 = record.PermanentZipCode.Substring(3, 1);
                        }
                        if (record.PermanentZipCode.Length >= 5)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode5 = record.PermanentZipCode.Substring(4, 1);
                        }
                    }
                }
                else if (obj.ReceiveAddress == "聯絡地址")
                {
                    if (!string.IsNullOrEmpty(record.MailingAddress))
                    {
                        StudentSuperOBJ[record.RefStudentID].address = record.Mailing.County + record.Mailing.Town + record.Mailing.District + record.Mailing.Area + record.Mailing.Detail; //再處理
                    }
                    if (!string.IsNullOrEmpty(record.MailingZipCode))
                    {
                        StudentSuperOBJ[record.RefStudentID].ZipCode = record.MailingZipCode;

                        if (record.MailingZipCode.Length >= 1)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode1 = record.MailingZipCode.Substring(0, 1);
                        }
                        if (record.MailingZipCode.Length >= 2)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode2 = record.MailingZipCode.Substring(1, 1);
                        }
                        if (record.MailingZipCode.Length >= 3)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode3 = record.MailingZipCode.Substring(2, 1);
                        }
                        if (record.MailingZipCode.Length >= 4)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode4 = record.MailingZipCode.Substring(3, 1);
                        }
                        if (record.MailingZipCode.Length >= 5)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode5 = record.MailingZipCode.Substring(4, 1);
                        }
                    }
                }
                else if (obj.ReceiveAddress == "其他地址")
                {
                    if (!string.IsNullOrEmpty(record.Address1Address))
                    {
                        StudentSuperOBJ[record.RefStudentID].address = record.Address1.County + record.Address1.Town + record.Address1.District + record.Address1.Area + record.Address1.Detail; //再處理
                    }
                    if (!string.IsNullOrEmpty(record.Address1ZipCode))
                    {
                        StudentSuperOBJ[record.RefStudentID].ZipCode = record.Address1ZipCode;

                        if (record.Address1ZipCode.Length >= 1)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode1 = record.Address1ZipCode.Substring(0, 1);
                        }
                        if (record.Address1ZipCode.Length >= 2)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode2 = record.Address1ZipCode.Substring(1, 1);
                        }
                        if (record.Address1ZipCode.Length >= 3)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode3 = record.Address1ZipCode.Substring(2, 1);
                        }
                        if (record.Address1ZipCode.Length >= 4)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode4 = record.Address1ZipCode.Substring(3, 1);
                        }
                        if (record.Address1ZipCode.Length >= 5)
                        {
                            StudentSuperOBJ[record.RefStudentID].ZipCode5 = record.Address1ZipCode.Substring(4, 1);
                        }
                    }
                }
            }
            #endregion

            #region 取得學生監護人父母親資料

            List <ParentRecord> ParentList = Parent.SelectByStudentIDs(allStudentID);

            foreach (ParentRecord record in ParentList)
            {
                StudentSuperOBJ[record.RefStudentID].CustodianName = record.CustodianName;
                StudentSuperOBJ[record.RefStudentID].FatherName    = record.FatherName;
                StudentSuperOBJ[record.RefStudentID].MotherName    = record.MotherName;
            }
            #endregion

            #region 產生報表

            Aspose.Words.Document template = new Aspose.Words.Document(obj.Template);
            template.MailMerge.Execute(
                new string[] { "學校名稱", "學校地址", "學校電話" },
                new object[] { School.ChineseName, School.Address, School.Telephone }
                );

            Aspose.Words.Document doc = new Aspose.Words.Document();
            doc.RemoveAllChildren();

            Aspose.Words.Node sectionNode = template.Sections[0].Clone();

            //取得學生人數
            totalStudentNumber = StudentSuperOBJ.Count;

            foreach (string student in StudentSuperOBJ.Keys)
            {
                //如果沒有學生就離開
                if (obj.PrintHasRecordOnly)
                {
                    if (StudentSuperOBJ.Count == 0)
                    {
                        continue;
                    }
                }

                if (StudentSuperOBJ[student].DemeritStringList.Count == 0)
                {
                    continue;
                }

                Aspose.Words.Document eachDoc = new Aspose.Words.Document();
                eachDoc.RemoveAllChildren();
                eachDoc.Sections.Add(eachDoc.ImportNode(sectionNode, true));

                //合併列印的資料
                Dictionary <string, object> mapping = new Dictionary <string, object>();

                StudentOBJ eachStudentInfo = StudentSuperOBJ[student];

                //學生資料
                mapping.Add("系統編號", "系統編號{" + eachStudentInfo.student.ID + "}");
                mapping.Add("學生姓名", eachStudentInfo.student.Name);
                mapping.Add("班級", eachStudentInfo.ClassName);
                mapping.Add("座號", eachStudentInfo.SeatNo);
                mapping.Add("學號", eachStudentInfo.StudentNumber);
                mapping.Add("導師", eachStudentInfo.TeacherName);
                mapping.Add("資料期間", obj.StartDate.ToShortDateString() + " 至 " + obj.EndDate.ToShortDateString());

                //收件人資料
                if (obj.ReceiveName == "監護人姓名")
                {
                    mapping.Add("收件人姓名", eachStudentInfo.CustodianName);
                }
                else if (obj.ReceiveName == "父親姓名")
                {
                    mapping.Add("收件人姓名", eachStudentInfo.FatherName);
                }
                else if (obj.ReceiveName == "母親姓名")
                {
                    mapping.Add("收件人姓名", eachStudentInfo.MotherName);
                }
                else
                {
                    mapping.Add("收件人姓名", eachStudentInfo.student.Name);
                }

                //收件人地址資料
                mapping.Add("收件人地址", eachStudentInfo.address);
                mapping.Add("郵遞區號", eachStudentInfo.ZipCode);
                mapping.Add("0", eachStudentInfo.ZipCode1);
                mapping.Add("1", eachStudentInfo.ZipCode2);
                mapping.Add("2", eachStudentInfo.ZipCode3);
                mapping.Add("4", eachStudentInfo.ZipCode4);
                mapping.Add("5", eachStudentInfo.ZipCode5);

                mapping.Add("學年度", School.DefaultSchoolYear);
                mapping.Add("學期", School.DefaultSemester);

                //懲戒明細
                object[] objectValues = new object[] { StudentSuperOBJ[student].DemeritStringList };
                mapping.Add("懲戒明細", objectValues);

                string[] keys   = new string[mapping.Count];
                object[] values = new object[mapping.Count];
                int      i      = 0;
                foreach (string key in mapping.Keys)
                {
                    keys[i]     = key;
                    values[i++] = mapping[key];
                }

                //合併列印
                eachDoc.MailMerge.MergeField           += new Aspose.Words.Reporting.MergeFieldEventHandler(DisciplineNotification_MailMerge_MergeField);
                eachDoc.MailMerge.RemoveEmptyParagraphs = true;
                eachDoc.MailMerge.Execute(keys, values);

                Aspose.Words.Node eachSectionNode = eachDoc.Sections[0].Clone();
                doc.Sections.Add(doc.ImportNode(eachSectionNode, true));

                //回報進度
                _BGWDisciplineNotification.ReportProgress((int)(((double)currentStudentCount++ *100.0) / (double)totalStudentNumber));
            }

            #endregion

            #region 產生學生清單

            Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
            if (obj.PrintStudentList)
            {
                int CountRow = 0;
                wb.Worksheets[0].Cells[CountRow, 0].PutValue("班級");
                wb.Worksheets[0].Cells[CountRow, 1].PutValue("座號");
                wb.Worksheets[0].Cells[CountRow, 2].PutValue("學號");
                wb.Worksheets[0].Cells[CountRow, 3].PutValue("學生姓名");
                wb.Worksheets[0].Cells[CountRow, 4].PutValue("收件人姓名");
                wb.Worksheets[0].Cells[CountRow, 5].PutValue("地址");
                CountRow++;
                foreach (string each in StudentSuperOBJ.Keys)
                {
                    //如果沒有學生就離開
                    if (obj.PrintHasRecordOnly)
                    {
                        if (StudentSuperOBJ.Count == 0)
                        {
                            continue;
                        }
                    }

                    if (StudentSuperOBJ[each].DemeritStringList.Count == 0)
                    {
                        continue;
                    }

                    wb.Worksheets[0].Cells[CountRow, 0].PutValue(StudentSuperOBJ[each].ClassName);
                    wb.Worksheets[0].Cells[CountRow, 1].PutValue(StudentSuperOBJ[each].SeatNo);
                    wb.Worksheets[0].Cells[CountRow, 2].PutValue(StudentSuperOBJ[each].StudentNumber);
                    wb.Worksheets[0].Cells[CountRow, 3].PutValue(StudentSuperOBJ[each].student.Name);
                    //收件人資料
                    if (obj.ReceiveName == "監護人姓名")
                    {
                        wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].CustodianName);
                    }
                    else if (obj.ReceiveName == "父親姓名")
                    {
                        wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].FatherName);
                    }
                    else if (obj.ReceiveName == "母親姓名")
                    {
                        wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].MotherName);
                    }
                    else
                    {
                        wb.Worksheets[0].Cells[CountRow, 4].PutValue(StudentSuperOBJ[each].student.Name);
                    }

                    wb.Worksheets[0].Cells[CountRow, 5].PutValue(StudentSuperOBJ[each].ZipCode + " " + StudentSuperOBJ[each].address);
                    CountRow++;
                }
                wb.Worksheets[0].AutoFitColumns();
            }
            #endregion

            string path  = Path.Combine(Application.StartupPath, "Reports");
            string path2 = Path.Combine(Application.StartupPath, "Reports");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path  = Path.Combine(path, reportName + ".doc");
            path2 = Path.Combine(path2, reportName + "(學生清單).xls");

            string message = "【電子報表通知】您好 本期「{0}」已產生,可於電子報表中檢視「資料期間:{1} 至 {2}」";
            e.Result = new object[] { reportName, path, doc, path2, obj.PrintStudentList, wb, string.Format(message, "留察通知單", obj.StartDate.ToShortDateString(), obj.EndDate.ToShortDateString()) };
        }
예제 #2
0
        private void _BGWDisciplineNotification_DoWork(object sender, DoWorkEventArgs e)
        {
            #region 表頭
            GetReduceList(); //獎懲對照表

            string reportName = "獎懲通知單";

            object[] args = e.Argument as object[];

            DateTime     startDate          = (DateTime)args[0];
            DateTime     endDate            = (DateTime)args[1];
            bool         printHasRecordOnly = (bool)args[2];
            MemoryStream templateStream     = (MemoryStream)args[3];
            string       receiveName        = (string)args[4];
            string       receiveAddress     = (string)args[5];
            string       condName           = (string)args[6];
            int          condNumber         = int.Parse((string)args[7]);
            bool         IsInsertDate       = (bool)args[8];
            bool         printStudentList   = (bool)args[9];

            ChengeDemerit(condName, condNumber);

            Dictionary <string, int> MDMapping = new Dictionary <string, int>();
            MDMapping.Add("大功", 0);
            MDMapping.Add("小功", 1);
            MDMapping.Add("嘉獎", 2);
            MDMapping.Add("大過", 3);
            MDMapping.Add("小過", 4);
            MDMapping.Add("警告", 5);

            int flag = 2;
            if (!string.IsNullOrEmpty(condName))
            {
                flag = (MDMapping[condName] < 3) ? 1 : 0;
            }

            MDFilter filter = new MDFilter();
            if (flag < 2)
            {
                filter.SetCondition(MDMapping[condName], condNumber);
            }
            #endregion

            #region 快取資訊

            //學生資訊
            Dictionary <string, Dictionary <string, string> > studentInfo = new Dictionary <string, Dictionary <string, string> >();

            //獎懲累計資料
            Dictionary <string, Dictionary <string, int> > studentDiscipline = new Dictionary <string, Dictionary <string, int> >();

            //獎懲明細
            Dictionary <string, List <string> > studentDisciplineDetail = new Dictionary <string, List <string> >();

            //所有學生ID
            List <string> allStudentID = new List <string>();

            //學生人數
            int currentStudentCount = 1;
            int totalStudentNumber  = 0;

            //獎勵項目
            Dictionary <string, string> meritTable = new Dictionary <string, string>();
            meritTable.Add("大功", "A");
            meritTable.Add("小功", "B");
            meritTable.Add("嘉獎", "C");

            //懲戒項目
            Dictionary <string, string> demeritTable = new Dictionary <string, string>();
            demeritTable.Add("大過", "A");
            demeritTable.Add("小過", "B");
            demeritTable.Add("警告", "C");

            #endregion

            #region 依據 ClassID 建立班級學生清單
            List <StudentRecord>     classStudent = SelectedStudents;
            Dictionary <string, int> StudMeritSum = new Dictionary <string, int>();

            foreach (StudentRecord aStudent in classStudent)
            {
                string aStudentID = aStudent.ID;


                if (!StudMeritSum.ContainsKey(aStudentID))
                {
                    StudMeritSum.Add(aStudentID, 0);
                }

                if (!studentInfo.ContainsKey(aStudentID))
                {
                    studentInfo.Add(aStudentID, new Dictionary <string, string>());
                }

                TeacherRecord objT = aStudent.Class == null ? null : aStudent.Class.Teacher;

                studentInfo[aStudentID].Add("Name", aStudent.Name);
                studentInfo[aStudentID].Add("EnName", aStudent.EnglishName);



                studentInfo[aStudentID].Add("ClassName", aStudent.Class == null ? "" : aStudent.Class.Name);

                if (aStudent.SeatNo.HasValue)
                {
                    studentInfo[aStudentID].Add("SeatNo", aStudent.SeatNo.Value.ToString());
                }
                else
                {
                    studentInfo[aStudentID].Add("SeatNo", "");
                }

                studentInfo[aStudentID].Add("StudentNumber", aStudent.StudentNumber);
                studentInfo[aStudentID].Add("Teacher", objT == null ? "" : objT.Name);

                if (!studentDiscipline.ContainsKey(aStudentID))
                {
                    studentDiscipline.Add(aStudentID, new Dictionary <string, int>());
                }
                if (!studentDisciplineDetail.ContainsKey(aStudentID))
                {
                    studentDisciplineDetail.Add(aStudentID, new List <string>());
                }

                if (!allStudentID.Contains(aStudentID))
                {
                    allStudentID.Add(aStudentID);
                }
            }
            #endregion

            //雙語部 - 英文別名欄位
            //學生ID : 英文別名
            Dictionary <string, string> StudentEXTDic = tool.GetStudentEXT(allStudentID);

            #region 取得獎懲資料 日期區間
            DSXmlHelper helper = new DSXmlHelper("Request");
            helper.AddElement("Field");
            helper.AddElement("Field", "All");
            helper.AddElement("Condition");
            foreach (string var in allStudentID)
            {
                helper.AddElement("Condition", "RefStudentID", var);
            }

            if (IsInsertDate)
            {
                helper.AddElement("Condition", "StartDate", startDate.ToShortDateString());
                helper.AddElement("Condition", "EndDate", endDate.ToShortDateString());
            }
            else
            {
                helper.AddElement("Condition", "StartRegisterDate", startDate.ToShortDateString());
                helper.AddElement("Condition", "EndRegisterDate", endDate.ToShortDateString());
            }

            helper.AddElement("Order");
            helper.AddElement("Order", "OccurDate", "asc");
            DSResponse dsrsp = tool.GetDiscipline(new DSRequest(helper));

            foreach (XmlElement var in dsrsp.GetContent().GetElements("Discipline"))
            {
                string studentID = var.SelectSingleNode("RefStudentID").InnerText;

                DateTime occurDate     = DateTime.Parse(var.SelectSingleNode("OccurDate").InnerText);
                string   occurMonthDay = occurDate.Month + "/" + occurDate.Day;
                string   reason        = var.SelectSingleNode("Reason").InnerText;

                if (!studentDisciplineDetail.ContainsKey(studentID))
                {
                    studentDisciplineDetail.Add(studentID, new List <string>());
                }

                if (!studentDiscipline.ContainsKey(studentID))
                {
                    studentDiscipline.Add(studentID, new Dictionary <string, int>());
                }

                if (var.SelectSingleNode("MeritFlag").InnerText == "1")
                {
                    XmlElement meritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Merit");
                    if (meritElement == null)
                    {
                        continue;
                    }

                    if (MeritDemerit == "獎勵")
                    {
                        if (StudMeritSum.ContainsKey(studentID))
                        {
                            StudMeritSum[studentID] += GetMeritType(meritElement);
                        }
                    }

                    bool          comma        = false;
                    StringBuilder detailString = new StringBuilder("");
                    detailString.Append(occurMonthDay + " ");
                    if (!string.IsNullOrEmpty(reason))
                    {
                        detailString.Append(reason + " ");
                    }

                    foreach (string merit in meritTable.Keys)
                    {
                        int tryTimes;
                        int times = int.TryParse(meritElement.GetAttribute(meritTable[merit]), out tryTimes) ? tryTimes : 0;
                        if (times > 0)
                        {
                            if (!studentDiscipline[studentID].ContainsKey("Range" + merit))
                            {
                                studentDiscipline[studentID].Add("Range" + merit, 0);
                            }
                            studentDiscipline[studentID]["Range" + merit] += times;
                            if (comma)
                            {
                                detailString.Append(",");
                            }
                            detailString.Append(merit + times + "次");
                            comma = true;
                        }
                    }

                    studentDisciplineDetail[studentID].Add(detailString.ToString());
                }
                else if (var.SelectSingleNode("MeritFlag").InnerText == "0")
                {
                    XmlElement demeritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Demerit");
                    if (demeritElement == null)
                    {
                        continue;
                    }

                    bool cleared = false;
                    if (demeritElement.GetAttribute("Cleared") == "是")
                    {
                        cleared = true;
                    }

                    #region 懲戒比例換算 & 判斷

                    if (cleared == false && MeritDemerit == "懲戒")
                    {
                        if (StudMeritSum.ContainsKey(studentID))
                        {
                            StudMeritSum[studentID] += GetDemeritType(demeritElement);
                        }
                    }

                    #endregion

                    bool          comma        = false;
                    StringBuilder detailString = new StringBuilder("");
                    detailString.Append(occurMonthDay + " ");
                    if (!string.IsNullOrEmpty(reason))
                    {
                        detailString.Append(reason + " ");
                    }

                    foreach (string demerit in demeritTable.Keys)
                    {
                        int tryTimes;
                        int times = int.TryParse(demeritElement.GetAttribute(demeritTable[demerit]), out tryTimes) ? tryTimes : 0;
                        if (times > 0)
                        {
                            if (!studentDiscipline[studentID].ContainsKey("Range" + demerit))
                            {
                                studentDiscipline[studentID].Add("Range" + demerit, 0);
                            }
                            if (!cleared)
                            {
                                studentDiscipline[studentID]["Range" + demerit] += times;
                                if (comma)
                                {
                                    detailString.Append(",");
                                }
                                detailString.Append(demerit + times + "次");
                                comma = true;
                            }
                        }
                    }

                    if (!cleared)
                    {
                        studentDisciplineDetail[studentID].Add(detailString.ToString());
                    }
                }
            }
            #endregion

            #region 取得獎懲資料 學期累計
            helper = new DSXmlHelper("Request");
            helper.AddElement("Field");
            helper.AddElement("Field", "All");
            helper.AddElement("Condition");
            foreach (string var in allStudentID)
            {
                helper.AddElement("Condition", "RefStudentID", var);
            }
            helper.AddElement("Condition", "SchoolYear", School.DefaultSchoolYear);
            helper.AddElement("Condition", "Semester", School.DefaultSemester);
            helper.AddElement("Order");
            helper.AddElement("Order", "OccurDate", "asc");
            dsrsp = tool.GetDiscipline(new DSRequest(helper));

            foreach (XmlElement var in dsrsp.GetContent().GetElements("Discipline"))
            {
                DateTime occurDate = DateTime.Parse(var.SelectSingleNode("OccurDate").InnerText);
                if (occurDate.CompareTo(endDate) == 1)
                {
                    continue;
                }

                string studentID = var.SelectSingleNode("RefStudentID").InnerText;

                if (!studentDiscipline.ContainsKey(studentID))
                {
                    studentDiscipline.Add(studentID, new Dictionary <string, int>());
                }

                if (var.SelectSingleNode("MeritFlag").InnerText == "1")
                {
                    XmlElement meritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Merit");
                    if (meritElement == null)
                    {
                        continue;
                    }

                    foreach (string merit in meritTable.Keys)
                    {
                        int tryTimes;
                        int times = int.TryParse(meritElement.GetAttribute(meritTable[merit]), out tryTimes) ? tryTimes : 0;
                        if (times > 0)
                        {
                            if (!studentDiscipline[studentID].ContainsKey("Semester" + merit))
                            {
                                studentDiscipline[studentID].Add("Semester" + merit, 0);
                            }
                            studentDiscipline[studentID]["Semester" + merit] += times;
                        }
                    }
                }
                else
                {
                    XmlElement demeritElement = (XmlElement)var.SelectSingleNode("Detail/Discipline/Demerit");
                    if (demeritElement == null)
                    {
                        continue;
                    }

                    bool cleared = false;
                    if (demeritElement.GetAttribute("Cleared") == "是")
                    {
                        cleared = true;
                    }

                    foreach (string demerit in demeritTable.Keys)
                    {
                        int tryTimes;
                        int times = int.TryParse(demeritElement.GetAttribute(demeritTable[demerit]), out tryTimes) ? tryTimes : 0;
                        if (times > 0)
                        {
                            if (!studentDiscipline[studentID].ContainsKey("Semester" + demerit))
                            {
                                studentDiscipline[studentID].Add("Semester" + demerit, 0);
                            }
                            if (!cleared)
                            {
                                studentDiscipline[studentID]["Semester" + demerit] += times;
                            }
                        }
                    }
                }
            }
            #endregion

            #region 取得學生通訊地址資料
            List <AddressRecord> AddressList = K12.Data.Address.SelectByStudentIDs(allStudentID);
            foreach (AddressRecord var in AddressList)
            {
                string studentID = var.RefStudentID;

                if (!studentInfo.ContainsKey(studentID))
                {
                    studentInfo.Add(studentID, new Dictionary <string, string>());
                }

                studentInfo[studentID].Add("Address", "");
                studentInfo[studentID].Add("ZipCode", "");
                studentInfo[studentID].Add("ZipCode1", "");
                studentInfo[studentID].Add("ZipCode2", "");
                studentInfo[studentID].Add("ZipCode3", "");
                studentInfo[studentID].Add("ZipCode4", "");
                studentInfo[studentID].Add("ZipCode5", "");

                if (receiveAddress == "聯絡地址")
                {
                    if (!string.IsNullOrEmpty(var.MailingAddress))
                    {
                        studentInfo[studentID]["Address"] = var.MailingCounty + var.MailingTown + var.MailingDistrict + var.MailingArea + var.MailingDetail;
                    }

                    if (!string.IsNullOrEmpty(var.MailingZipCode))
                    {
                        studentInfo[studentID]["ZipCode"] = var.MailingZipCode;

                        if (var.MailingZipCode.Length >= 1)
                        {
                            studentInfo[studentID]["ZipCode1"] = var.MailingZipCode.Substring(0, 1);
                        }
                        if (var.MailingZipCode.Length >= 2)
                        {
                            studentInfo[studentID]["ZipCode2"] = var.MailingZipCode.Substring(1, 1);
                        }
                        if (var.MailingZipCode.Length >= 3)
                        {
                            studentInfo[studentID]["ZipCode3"] = var.MailingZipCode.Substring(2, 1);
                        }
                        if (var.MailingZipCode.Length >= 4)
                        {
                            studentInfo[studentID]["ZipCode4"] = var.MailingZipCode.Substring(3, 1);
                        }
                        if (var.MailingZipCode.Length >= 5)
                        {
                            studentInfo[studentID]["ZipCode5"] = var.MailingZipCode.Substring(4, 1);
                        }
                    }
                }
                else if (receiveAddress == "其他地址")
                {
                    if (!string.IsNullOrEmpty(var.Address1Address))
                    {
                        studentInfo[studentID]["Address"] = var.Address1County + var.Address1Town + var.Address1District + var.Address1Area + var.Address1Detail;
                    }

                    if (!string.IsNullOrEmpty(var.Address1ZipCode))
                    {
                        studentInfo[studentID]["ZipCode"] = var.Address1ZipCode;

                        if (var.Address1ZipCode.Length >= 1)
                        {
                            studentInfo[studentID]["ZipCode1"] = var.Address1ZipCode.Substring(0, 1);
                        }
                        if (var.Address1ZipCode.Length >= 2)
                        {
                            studentInfo[studentID]["ZipCode2"] = var.Address1ZipCode.Substring(1, 1);
                        }
                        if (var.Address1ZipCode.Length >= 3)
                        {
                            studentInfo[studentID]["ZipCode3"] = var.Address1ZipCode.Substring(2, 1);
                        }
                        if (var.Address1ZipCode.Length >= 4)
                        {
                            studentInfo[studentID]["ZipCode4"] = var.Address1ZipCode.Substring(3, 1);
                        }
                        if (var.Address1ZipCode.Length >= 5)
                        {
                            studentInfo[studentID]["ZipCode5"] = var.Address1ZipCode.Substring(4, 1);
                        }
                    }
                }
                else //戶籍地址
                {
                    if (!string.IsNullOrEmpty(var.PermanentAddress))
                    {
                        studentInfo[studentID]["Address"] = var.PermanentCounty + var.PermanentTown + var.PermanentDistrict + var.PermanentArea + var.PermanentDetail;
                    }

                    if (!string.IsNullOrEmpty(var.PermanentZipCode))
                    {
                        studentInfo[studentID]["ZipCode"] = var.PermanentZipCode;

                        if (var.PermanentZipCode.Length >= 1)
                        {
                            studentInfo[studentID]["ZipCode1"] = var.PermanentZipCode.Substring(0, 1);
                        }
                        if (var.PermanentZipCode.Length >= 2)
                        {
                            studentInfo[studentID]["ZipCode2"] = var.PermanentZipCode.Substring(1, 1);
                        }
                        if (var.PermanentZipCode.Length >= 3)
                        {
                            studentInfo[studentID]["ZipCode3"] = var.PermanentZipCode.Substring(2, 1);
                        }
                        if (var.PermanentZipCode.Length >= 4)
                        {
                            studentInfo[studentID]["ZipCode4"] = var.PermanentZipCode.Substring(3, 1);
                        }
                        if (var.PermanentZipCode.Length >= 5)
                        {
                            studentInfo[studentID]["ZipCode5"] = var.PermanentZipCode.Substring(4, 1);
                        }
                    }
                }
            }
            #endregion

            #region 取得學生監護人父母親資料
            dsrsp = tool.GetMultiParentInfo(allStudentID.ToArray());
            foreach (XmlElement var in dsrsp.GetContent().GetElements("ParentInfo"))
            {
                string studentID = var.GetAttribute("StudentID");

                studentInfo[studentID].Add("CustodianName", var.SelectSingleNode("CustodianName").InnerText);
                studentInfo[studentID].Add("FatherName", var.SelectSingleNode("FatherName").InnerText);
                studentInfo[studentID].Add("MotherName", var.SelectSingleNode("MotherName").InnerText);
            }
            #endregion

            #region 產生報表

            Aspose.Words.Document template = new Aspose.Words.Document(templateStream, "", Aspose.Words.LoadFormat.Doc, "");
            template.MailMerge.Execute(
                new string[] { "學校名稱", "學校地址", "學校電話", "學校英文名稱" },
                new object[] { School.ChineseName, School.Address, School.Telephone, School.EnglishName }
                );

            Aspose.Words.Document doc = new Aspose.Words.Document();
            doc.RemoveAllChildren();

            Aspose.Words.Node sectionNode = template.Sections[0].Clone();

            totalStudentNumber = studentDiscipline.Count;

            foreach (string student in studentDiscipline.Keys)
            {
                if (printHasRecordOnly)
                {
                    if (studentDisciplineDetail[student].Count == 0)
                    {
                        continue;
                    }
                }

                if (MeritDemerit == "獎勵")
                {
                    if (StudMeritSum[student] < MaxMerit)
                    {
                        continue;
                    }
                }
                else if (MeritDemerit == "懲戒")
                {
                    if (StudMeritSum[student] < MaxDemerit)
                    {
                        continue;
                    }
                }
                else //未設定
                {
                }

                #region 過濾不需要列印的學生

                if (flag < 2)
                {
                    int A = 0, B = 0, C = 0;

                    if (flag == 1)
                    {
                        int tryM;
                        A = studentDiscipline[student].TryGetValue("Range大功", out tryM) ? tryM : 0;
                        B = studentDiscipline[student].TryGetValue("Range小功", out tryM) ? tryM : 0;
                        C = studentDiscipline[student].TryGetValue("Range嘉獎", out tryM) ? tryM : 0;
                    }
                    else if (flag == 0)
                    {
                        int tryD;
                        A = studentDiscipline[student].TryGetValue("Range大過", out tryD) ? tryD : 0;
                        B = studentDiscipline[student].TryGetValue("Range小過", out tryD) ? tryD : 0;
                        C = studentDiscipline[student].TryGetValue("Range警告", out tryD) ? tryD : 0;
                    }

                    //if (filter.IsFilter(A, B, C))
                    //    continue;
                }

                #endregion

                Aspose.Words.Document eachDoc = new Aspose.Words.Document();
                eachDoc.RemoveAllChildren();
                eachDoc.Sections.Add(eachDoc.ImportNode(sectionNode, true));

                //合併列印的資料
                Dictionary <string, object> mapping = new Dictionary <string, object>();

                Dictionary <string, string> eachStudentInfo = studentInfo[student];

                XmlElement SchoolXml  = K12.Data.School.Configuration["學校資訊"].PreviousData;
                XmlElement SchoolXml2 = (XmlElement)SchoolXml.SelectSingleNode("EnglishAddress");
                if (SchoolXml2 != null)
                {
                    mapping.Add("學校英文地址", SchoolXml2.InnerText);
                }
                else
                {
                    mapping.Add("學校英文地址", "");
                }

                //學生資料
                mapping.Add("學生姓名", eachStudentInfo["Name"]);
                mapping.Add("英文姓名", eachStudentInfo["EnName"]);
                mapping.Add("班級", eachStudentInfo["ClassName"]);
                mapping.Add("座號", eachStudentInfo["SeatNo"]);
                mapping.Add("學號", eachStudentInfo["StudentNumber"]);
                mapping.Add("導師", eachStudentInfo["Teacher"]);
                mapping.Add("資料期間", startDate.ToShortDateString() + " 至 " + endDate.ToShortDateString());

                //收件人資料
                if (receiveName == "監護人姓名")
                {
                    mapping.Add("收件人姓名", eachStudentInfo["CustodianName"]);
                }
                else if (receiveName == "父親姓名")
                {
                    mapping.Add("收件人姓名", eachStudentInfo["FatherName"]);
                }
                else if (receiveName == "母親姓名")
                {
                    mapping.Add("收件人姓名", eachStudentInfo["MotherName"]);
                }
                else
                {
                    mapping.Add("收件人姓名", eachStudentInfo["Name"]);
                }

                //收件人地址資料
                mapping.Add("收件人地址", eachStudentInfo["Address"]);
                mapping.Add("郵遞區號", eachStudentInfo["ZipCode"]);
                mapping.Add("0", eachStudentInfo["ZipCode1"]);
                mapping.Add("1", eachStudentInfo["ZipCode2"]);
                mapping.Add("2", eachStudentInfo["ZipCode3"]);
                mapping.Add("4", eachStudentInfo["ZipCode4"]);
                mapping.Add("5", eachStudentInfo["ZipCode5"]);

                if (StudentEXTDic.ContainsKey(student))
                {
                    mapping.Add("英文別名", StudentEXTDic[student]);
                }
                else
                {
                    mapping.Add("英文別名", "");
                }

                mapping.Add("學年度", tool.GetSchoolChange(School.DefaultSchoolYear));
                mapping.Add("學期", School.DefaultSemester);

                Dictionary <string, int> eachStudentDiscipline = studentDiscipline[student];

                //學生獎懲累計資料
                int count;
                mapping.Add("學期累計大功", eachStudentDiscipline.TryGetValue("Semester大功", out count) ? "" + count : "0");
                mapping.Add("學期累計小功", eachStudentDiscipline.TryGetValue("Semester小功", out count) ? "" + count : "0");
                mapping.Add("學期累計嘉獎", eachStudentDiscipline.TryGetValue("Semester嘉獎", out count) ? "" + count : "0");
                mapping.Add("學期累計大過", eachStudentDiscipline.TryGetValue("Semester大過", out count) ? "" + count : "0");
                mapping.Add("學期累計小過", eachStudentDiscipline.TryGetValue("Semester小過", out count) ? "" + count : "0");
                mapping.Add("學期累計警告", eachStudentDiscipline.TryGetValue("Semester警告", out count) ? "" + count : "0");
                mapping.Add("本期累計大功", eachStudentDiscipline.TryGetValue("Range大功", out count) ? "" + count : "0");
                mapping.Add("本期累計小功", eachStudentDiscipline.TryGetValue("Range小功", out count) ? "" + count : "0");
                mapping.Add("本期累計嘉獎", eachStudentDiscipline.TryGetValue("Range嘉獎", out count) ? "" + count : "0");
                mapping.Add("本期累計大過", eachStudentDiscipline.TryGetValue("Range大過", out count) ? "" + count : "0");
                mapping.Add("本期累計小過", eachStudentDiscipline.TryGetValue("Range小過", out count) ? "" + count : "0");
                mapping.Add("本期累計警告", eachStudentDiscipline.TryGetValue("Range警告", out count) ? "" + count : "0");

                //獎懲明細
                object[] objectValues = new object[] { studentDisciplineDetail[student] };
                mapping.Add("獎懲明細", objectValues);

                string[] keys   = new string[mapping.Count];
                object[] values = new object[mapping.Count];
                int      i      = 0;
                foreach (string key in mapping.Keys)
                {
                    keys[i]     = key;
                    values[i++] = mapping[key];
                }

                //合併列印
                eachDoc.MailMerge.MergeField           += new Aspose.Words.Reporting.MergeFieldEventHandler(DisciplineNotification_MailMerge_MergeField);
                eachDoc.MailMerge.RemoveEmptyParagraphs = true;
                eachDoc.MailMerge.Execute(keys, values);

                Aspose.Words.Node eachSectionNode = eachDoc.Sections[0].Clone();
                doc.Sections.Add(doc.ImportNode(eachSectionNode, true));

                //回報進度
                _BGWDisciplineNotification.ReportProgress((int)(((double)currentStudentCount++ *100.0) / (double)totalStudentNumber));
            }

            #endregion

            #region 產生學生清單

            Aspose.Cells.Workbook wb = new Aspose.Cells.Workbook();
            if (printStudentList)
            {
                if (printHasRecordOnly)
                {
                    int CountRow = 0;
                    wb.Worksheets[0].Cells[CountRow, 0].PutValue("班級");
                    wb.Worksheets[0].Cells[CountRow, 1].PutValue("座號");
                    wb.Worksheets[0].Cells[CountRow, 2].PutValue("學號");
                    wb.Worksheets[0].Cells[CountRow, 3].PutValue("學生姓名");
                    wb.Worksheets[0].Cells[CountRow, 4].PutValue("英文姓名");
                    wb.Worksheets[0].Cells[CountRow, 5].PutValue("英文別名");
                    wb.Worksheets[0].Cells[CountRow, 6].PutValue("收件人姓名");
                    wb.Worksheets[0].Cells[CountRow, 7].PutValue("地址");
                    CountRow++;
                    foreach (string each in studentInfo.Keys)
                    {
                        if (studentDisciplineDetail[each].Count == 0)
                        {
                            continue;
                        }

                        if (MeritDemerit == "獎勵")
                        {
                            if (StudMeritSum[each] < MaxMerit)
                            {
                                continue;
                            }
                        }
                        else if (MeritDemerit == "懲戒")
                        {
                            if (StudMeritSum[each] < MaxDemerit)
                            {
                                continue;
                            }
                        }
                        else //未設定
                        {
                        }

                        wb.Worksheets[0].Cells[CountRow, 0].PutValue(studentInfo[each]["ClassName"]);
                        wb.Worksheets[0].Cells[CountRow, 1].PutValue(studentInfo[each]["SeatNo"]);
                        wb.Worksheets[0].Cells[CountRow, 2].PutValue(studentInfo[each]["StudentNumber"]);
                        wb.Worksheets[0].Cells[CountRow, 3].PutValue(studentInfo[each]["Name"]);
                        wb.Worksheets[0].Cells[CountRow, 4].PutValue(studentInfo[each]["EnName"]);
                        wb.Worksheets[0].Cells[CountRow, 5].PutValue(studentInfo[each]["ExName"]);
                        //收件人資料
                        if (receiveName == "監護人姓名")
                        {
                            wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["CustodianName"]);
                        }
                        else if (receiveName == "父親姓名")
                        {
                            wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["FatherName"]);
                        }
                        else if (receiveName == "母親姓名")
                        {
                            wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["MotherName"]);
                        }
                        else
                        {
                            wb.Worksheets[0].Cells[CountRow, 6].PutValue(studentInfo[each]["Name"]);
                        }

                        wb.Worksheets[0].Cells[CountRow, 7].PutValue(studentInfo[each]["ZipCode"] + " " + studentInfo[each]["Address"]);
                        CountRow++;
                    }
                    wb.Worksheets[0].AutoFitColumns();
                }
            }
            #endregion

            string path  = Path.Combine(Application.StartupPath, "Reports");
            string path2 = Path.Combine(Application.StartupPath, "Reports");

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            path     = Path.Combine(path, reportName + ".doc");
            path2    = Path.Combine(path2, reportName + "(學生清單).xls");
            e.Result = new object[] { reportName, path, doc, path2, printStudentList, wb };
        }