private void _writeWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            #region 寫入離校資訊
            try
            {
                List <JHLeaveInfoRecord> leaveInfoRecordList = new List <JHLeaveInfoRecord>();
                foreach (JHLeaveInfoRecord record in JHLeaveIfno.SelectByStudentIDs(_students.AsKeyList()))
                {
                    if (!_passList.ContainsKey(record.RefStudentID))
                    {
                        continue;
                    }

                    leaveInfoRecordList.Add(record);

                    if (_passList[record.RefStudentID] == true)
                    {
                        record.Reason = "畢業";
                        record.Memo   = string.Empty;
                    }
                    else
                    {
                        record.Reason = "修業";

                        StringBuilder builder = new StringBuilder();
                        _result[record.RefStudentID].Sort();
                        foreach (ResultDetail rd in _result[record.RefStudentID])
                        {
                            string semester = rd.GradeYear + (rd.Semester.Equals("1") ? "上" : "下") + ":";
                            string details  = string.Empty;
                            foreach (string detail in rd.Details)
                            {
                                details += detail + ",";
                            }
                            if (details.EndsWith(","))
                            {
                                details = details.Substring(0, details.Length - 1);
                            }
                            builder.AppendLine(semester + details);
                        }
                        record.Memo = builder.ToString();
                    }

                    // 系統目前預設學年度
                    int SchoolYear;
                    int.TryParse(JHSchool.School.DefaultSchoolYear, out SchoolYear);
                    record.SchoolYear = SchoolYear;
                }
                JHLeaveIfno.Update(leaveInfoRecordList);
            }
            catch (Exception ex)
            {
                BugReporter.ReportException(ex, false);
            }

            #endregion

            #region 寫入類別(Tags)
            string FailurePrefix = "未達畢業標準";
            try
            {
                List <JHTagConfigRecord> tagList            = JHTagConfig.SelectByCategory(K12.Data.TagCategory.Student);
                TagConfigRecordComparer  tagComparer        = new TagConfigRecordComparer();
                StudentTagRecordComparer studentTagComparer = new StudentTagRecordComparer();

                #region 製作各種未達畢業標準的標籤
                List <JHTagConfigRecord> tagConfigRecordList = new List <JHSchool.Data.JHTagConfigRecord>();
                foreach (StudentRecord student in _students)
                {
                    //如果學生通過畢業審查
                    if (_passList[student.ID])
                    {
                        continue;
                    }

                    //如果審查結果沒有該學生編號則跳到下一筆 (這應該不會發生才對)
                    if (!_result.ContainsKey(student.ID))
                    {
                        continue;
                    }

                    List <ResultDetail> rdList = _result[student.ID];
                    foreach (ResultDetail rd in rdList)
                    {
                        foreach (string msg in rd.Messages)
                        {
                            JHTagConfigRecord tagConfigRecord = new JHTagConfigRecord();
                            tagConfigRecord.Prefix   = FailurePrefix;
                            tagConfigRecord.Name     = msg;
                            tagConfigRecord.Category = "Student";
                            if (!tagList.Contains(tagConfigRecord, tagComparer))
                            {
                                tagConfigRecordList.Add(tagConfigRecord);
                                tagList.Add(tagConfigRecord);
                                //foreach (string tag_id in JHSchool.Data.TagConfig.Insert(tagConfigRecord))
                                //{
                                //    if (!tagIDs.ContainsKey(tagConfigRecord.FullName))
                                //        tagIDs.Add(tagConfigRecord.FullName, tag_id);
                                //}
                            }
                        }
                    }
                }

                JHTagConfig.Insert(tagConfigRecordList);
                tagList = JHTagConfig.SelectByCategory(K12.Data.TagCategory.Student);
                #endregion

                #region 貼上標籤
                List <JHStudentTagRecord> studentTagsList = new List <JHStudentTagRecord>();
                List <JHStudentTagRecord> deleteList      = new List <JHStudentTagRecord>();
                foreach (StudentRecord student in _students)
                {
                    //如果學生通過畢業審查,應該把標籤拿下來
                    if (_passList[student.ID])
                    {
                        foreach (JHStudentTagRecord student_tag in JHStudentTag.SelectByStudentID(student.ID))
                        {
                            if (student_tag.Prefix == FailurePrefix)
                            {
                                deleteList.Add(student_tag);
                            }
                        }
                        continue;
                    }

                    if (!_result.ContainsKey(student.ID))
                    {
                        continue;
                    }

                    Dictionary <string, JHStudentTagRecord> studentTags = new Dictionary <string, JHStudentTagRecord>();
                    foreach (JHStudentTagRecord student_tag in JHStudentTag.SelectByStudentID(student.ID))
                    {
                        if (!studentTags.ContainsKey(student_tag.RefTagID))
                        {
                            studentTags.Add(student_tag.RefTagID, student_tag);
                        }
                    }

                    List <ResultDetail> rdList = _result[student.ID];
                    foreach (ResultDetail rd in rdList)
                    {
                        foreach (string msg in rd.Messages)
                        {
                            string fullname = FailurePrefix + ":" + msg;

                            foreach (JHTagConfigRecord record in tagList)
                            {
                                if (record.FullName == fullname && !studentTags.ContainsKey(record.ID))
                                {
                                    JHStudentTagRecord r = new JHStudentTagRecord();
                                    r.RefTagID    = record.ID;
                                    r.RefEntityID = student.ID;
                                    if (!studentTagsList.Contains(r, studentTagComparer))
                                    {
                                        studentTagsList.Add(r);
                                    }
                                }
                            }
                        }
                    }
                }

                JHStudentTag.Insert(studentTagsList);
                JHStudentTag.Delete(deleteList);
                #endregion
            }
            catch (Exception ex)
            {
                BugReporter.ReportException(ex, false);
            }

            #endregion
        }
Esempio n. 2
0
        /// <summary>
        /// 未達畢業標準通知單
        /// </summary>
        private void ExportDoc()
        {
            if (_students.Count == 0)
            {
                return;
            }
            _doc.Sections.Clear();

            if (_rc.Template == null)
            {
                _rc.Template = new ReportTemplate(Properties.Resources.未達畢業標準通知單樣板, TemplateType.Word);
            }

            _template = _rc.Template.ToDocument();

            string UserSelAddresseeAddress = _rc.GetString(PrintConfigForm.setupAddresseeAddress, "聯絡地址");
            string UserSelAddresseeName    = _rc.GetString(PrintConfigForm.setupAddresseeName, "監護人");

            _UserSelExportStudentList = _rc.GetBoolean(PrintConfigForm.setupExportStudentList, false);

            string   UserSeldtDate = "";
            DateTime dt;

            if (DateTime.TryParse(_rc.GetString(PrintConfigForm.setupdtDocDate, ""), out dt))
            {
                UserSeldtDate = ConvertDate1(dt);
            }
            else
            {
                UserSeldtDate = ConvertDate1(DateTime.Now);
            }

            List <StudentGraduationPredictData> StudentGraduationPredictDataList = new List <StudentGraduationPredictData>();
            // 取得學生ID,製作 Dict 用
            List <string> StudIDList = (from data in _students select data.ID).ToList();

            // Student Address,Key:StudentID
            Dictionary <string, JHAddressRecord> AddressDict = new Dictionary <string, JHAddressRecord>();
            // Student Parent,Key:StudentID
            Dictionary <string, JHParentRecord> ParentDict = new Dictionary <string, JHParentRecord>();

            // 地址
            foreach (JHAddressRecord rec in JHAddress.SelectByStudentIDs(StudIDList))
            {
                if (!AddressDict.ContainsKey(rec.RefStudentID))
                {
                    AddressDict.Add(rec.RefStudentID, rec);
                }
            }

            // 父母監護人
            foreach (JHParentRecord rec in JHParent.SelectByStudentIDs(StudIDList))
            {
                if (!ParentDict.ContainsKey(rec.RefStudentID))
                {
                    ParentDict.Add(rec.RefStudentID, rec);
                }
            }



            // 資料轉換 ..
            foreach (StudentRecord StudRec in _students)
            {
                if (!_result.ContainsKey(StudRec.ID))
                {
                    continue;
                }

                //處理年級為0的資料
                List <ResultDetail> zeroGrades = new List <ResultDetail>();

                StudentGraduationPredictData sgpd = new StudentGraduationPredictData();

                if (StudRec.Class != null)
                {
                    sgpd.ClassName = StudRec.Class.Name;
                }

                sgpd.Name = StudRec.Name;

                sgpd.SchoolAddress = K12.Data.School.Address;
                sgpd.SchoolName    = K12.Data.School.ChineseName;
                sgpd.SchoolPhone   = K12.Data.School.Telephone;

                sgpd.SeatNo        = StudRec.SeatNo;
                sgpd.StudentNumber = StudRec.StudentNumber;

                // 文字
                if (_result.ContainsKey(StudRec.ID))
                {
                    int GrYear;
                    foreach (ResultDetail rd in _result[StudRec.ID])
                    {
                        if (int.TryParse(rd.GradeYear, out GrYear))
                        {
                            //if (GrYear == 0) continue;
                            //後續處理
                            if (GrYear == 0)
                            {
                                zeroGrades.Add(rd);
                            }

                            // 組訊息
                            string Detail = "";
                            if (rd.Details.Count > 0)
                            {
                                Detail = string.Join(",", rd.Details.ToArray());
                            }

                            // 一年級
                            if (GrYear == 1 || GrYear == 7)
                            {
                                if (rd.Semester.Trim() == "1")
                                {
                                    sgpd.Text11 = Detail;
                                }

                                if (rd.Semester.Trim() == "2")
                                {
                                    sgpd.Text12 = Detail;
                                }
                            }

                            // 二年級
                            if (GrYear == 2 || GrYear == 8)
                            {
                                if (rd.Semester.Trim() == "1")
                                {
                                    sgpd.Text21 = Detail;
                                }

                                if (rd.Semester.Trim() == "2")
                                {
                                    sgpd.Text22 = Detail;
                                }
                            }

                            // 三年級
                            if (GrYear == 3 || GrYear == 9)
                            {
                                if (rd.Semester.Trim() == "1")
                                {
                                    sgpd.Text31 = Detail;
                                }

                                if (rd.Semester.Trim() == "2")
                                {
                                    sgpd.Text32 = Detail;
                                }
                            }
                        }
                    }
                }

                // 地址
                if (AddressDict.ContainsKey(StudRec.ID))
                {
                    if (UserSelAddresseeAddress == "聯絡地址")
                    {
                        sgpd.AddresseeAddress = AddressDict[StudRec.ID].MailingAddress;
                    }

                    if (UserSelAddresseeAddress == "戶籍地址")
                    {
                        sgpd.AddresseeAddress = AddressDict[StudRec.ID].PermanentAddress;
                    }
                }

                // 父母監護人
                if (ParentDict.ContainsKey(StudRec.ID))
                {
                    if (UserSelAddresseeName == "父親")
                    {
                        sgpd.AddresseeName = ParentDict[StudRec.ID].FatherName;
                    }

                    if (UserSelAddresseeName == "母親")
                    {
                        sgpd.AddresseeName = ParentDict[StudRec.ID].MotherName;
                    }

                    if (UserSelAddresseeName == "監護人")
                    {
                        sgpd.AddresseeName = ParentDict[StudRec.ID].CustodianName;
                    }
                }

                sgpd.DocDate = UserSeldtDate;

                foreach (ResultDetail rd in zeroGrades)
                {
                    sgpd.Text += string.Join(",", rd.Details);
                }

                StudentGraduationPredictDataList.Add(sgpd);
            }

            // 產生Word 套印
            Dictionary <string, object> FieldData = new Dictionary <string, object>();

            // 班座排序
            StudentGraduationPredictDataList = (from data in StudentGraduationPredictDataList orderby data.ClassName, data.SeatNo.PadLeft(3, '0') ascending select data).ToList();

            foreach (StudentGraduationPredictData sgpd in StudentGraduationPredictDataList)
            {
                FieldData.Clear();
                FieldData.Add("學校名稱", sgpd.SchoolName);
                FieldData.Add("學校電話", sgpd.SchoolPhone);
                FieldData.Add("學校地址", sgpd.SchoolAddress);
                FieldData.Add("收件人地址", sgpd.AddresseeAddress);
                FieldData.Add("收件人姓名", sgpd.AddresseeName);
                FieldData.Add("班級", sgpd.ClassName);
                FieldData.Add("座號", sgpd.SeatNo);
                FieldData.Add("姓名", sgpd.Name);
                FieldData.Add("學號", sgpd.StudentNumber);
                FieldData.Add("一上文字", sgpd.Text11);
                FieldData.Add("一下文字", sgpd.Text12);
                FieldData.Add("二上文字", sgpd.Text21);
                FieldData.Add("二下文字", sgpd.Text22);
                FieldData.Add("三上文字", sgpd.Text31);
                FieldData.Add("三下文字", sgpd.Text32);
                FieldData.Add("發文日期", sgpd.DocDate);
                FieldData.Add("所有說明", sgpd.Text);

                Aspose.Words.Document        each    = (Aspose.Words.Document)_template.Clone(true);
                Aspose.Words.DocumentBuilder builder = new Aspose.Words.DocumentBuilder(each);
                // 合併
                if (FieldData.Count > 0)
                {
                    builder.Document.MailMerge.Execute(FieldData.Keys.ToArray(), FieldData.Values.ToArray());
                }

                foreach (Aspose.Words.Section sec in each.Sections)
                {
                    _doc.Sections.Add(_doc.ImportNode(sec, true));
                }
            }

            // 產生學生清單
            if (_UserSelExportStudentList)
            {
                _wbStudentList = new Workbook();
                _wbStudentList.Worksheets[0].Cells[0, 0].PutValue("班級");
                _wbStudentList.Worksheets[0].Cells[0, 1].PutValue("座號");
                _wbStudentList.Worksheets[0].Cells[0, 2].PutValue("學號");
                _wbStudentList.Worksheets[0].Cells[0, 3].PutValue("學生姓名");
                _wbStudentList.Worksheets[0].Cells[0, 4].PutValue("收件人姓名");
                _wbStudentList.Worksheets[0].Cells[0, 5].PutValue("地址");
                //班級	座號	學號	學生姓名	收件人姓名	地址

                int rowIdx = 1;
                foreach (StudentGraduationPredictData sgpd in StudentGraduationPredictDataList)
                {
                    _wbStudentList.Worksheets[0].Cells[rowIdx, 0].PutValue(sgpd.ClassName);
                    _wbStudentList.Worksheets[0].Cells[rowIdx, 1].PutValue(sgpd.SeatNo);
                    _wbStudentList.Worksheets[0].Cells[rowIdx, 2].PutValue(sgpd.StudentNumber);
                    _wbStudentList.Worksheets[0].Cells[rowIdx, 3].PutValue(sgpd.Name);
                    _wbStudentList.Worksheets[0].Cells[rowIdx, 4].PutValue(sgpd.AddresseeName);
                    _wbStudentList.Worksheets[0].Cells[rowIdx, 5].PutValue(sgpd.AddresseeAddress);
                    rowIdx++;
                }

                _wbStudentList.Worksheets[0].AutoFitColumns();
            }
        }