private void btnSave_Click(object sender, EventArgs e) { if (!IsValid()) { FISCA.Presentation.Controls.MsgBox.Show("資料驗證失敗,請修正後再行儲存", "驗證失敗", MessageBoxButtons.OK, MessageBoxIcon.Stop); return; } List <AttendanceRecord> InsertHelper = new List <AttendanceRecord>(); //新增 List <AttendanceRecord> updateHelper = new List <AttendanceRecord>(); //更新 List <string> deleteList = new List <string>(); //清空 ISemester semester = SemesterProvider.GetInstance(); foreach (DataGridViewRow row in dataGridView.Rows) { RowTag tag = row.Tag as RowTag; semester.SetDate(tag.Date); ////log 紀錄修改後的資料 日期部分 if (row.Cells[0].Tag is string) { if (!LOG[row.Cells[0].Tag.ToString()].afterData.ContainsKey(tag.Date.ToShortDateString())) { LOG[row.Cells[0].Tag.ToString()].afterData.Add(tag.Date.ToShortDateString(), new Dictionary <string, string>()); } } else { AttendanceRecord attRecord = row.Cells[0].Tag as AttendanceRecord; if (!LOG[attRecord.RefStudentID].afterData.ContainsKey(tag.Date.ToShortDateString())) { LOG[attRecord.RefStudentID].afterData.Add(tag.Date.ToShortDateString(), new Dictionary <string, string>()); } } if (tag.IsNew) { #region IsNew string studentID = row.Cells[0].Tag as string; AttendanceRecord attRecord = new AttendanceRecord(); bool hasContent = false; for (int i = _startIndex; i < dataGridView.Columns.Count; i++) { DataGridViewCell cell = row.Cells[i]; if (string.IsNullOrEmpty(("" + cell.Value).Trim())) { continue; } PeriodInfo pinfo = dataGridView.Columns[i].Tag as PeriodInfo; AbsenceCellInfo acInfo = cell.Tag as AbsenceCellInfo; AbsenceInfo ainfo = acInfo.AbsenceInfo; K12.Data.AttendancePeriod ap = new K12.Data.AttendancePeriod(); ap.Period = pinfo.Name; ap.AbsenceType = ainfo.Name; attRecord.PeriodDetail.Add(ap); hasContent = true; //log 紀錄修改後的資料 缺曠明細部分 if (!LOG[studentID].afterData[tag.Date.ToShortDateString()].ContainsKey(pinfo.Name)) { LOG[studentID].afterData[tag.Date.ToShortDateString()].Add(pinfo.Name, ainfo.Name); } } if (hasContent) { attRecord.RefStudentID = studentID; attRecord.SchoolYear = int.Parse("" + row.Cells[ColumnIndex["學年度"]].Value); attRecord.Semester = int.Parse("" + row.Cells[ColumnIndex["學期"]].Value); attRecord.OccurDate = DateTime.Parse(tag.Date.ToShortDateString()); InsertHelper.Add(attRecord); } #endregion } else // 若是原本就有紀錄的 { #region 是舊的 AttendanceRecord attRecord = row.Cells[0].Tag as AttendanceRecord; attRecord.PeriodDetail.Clear(); //清空 bool hasContent = false; for (int i = _startIndex; i < dataGridView.Columns.Count; i++) { DataGridViewCell cell = row.Cells[i]; if (string.IsNullOrEmpty(("" + cell.Value).Trim())) { continue; //避免是空白 } PeriodInfo pinfo = dataGridView.Columns[i].Tag as PeriodInfo; AbsenceCellInfo acInfo = cell.Tag as AbsenceCellInfo; AbsenceInfo ainfo = acInfo.AbsenceInfo; K12.Data.AttendancePeriod ap = new K12.Data.AttendancePeriod(); ap.Period = pinfo.Name; ap.AbsenceType = ainfo.Name; attRecord.PeriodDetail.Add(ap); hasContent = true; //log 紀錄修改後的資料 缺曠明細部分 if (!LOG[attRecord.RefStudentID].afterData[tag.Date.ToShortDateString()].ContainsKey(pinfo.Name)) { LOG[attRecord.RefStudentID].afterData[tag.Date.ToShortDateString()].Add(pinfo.Name, ainfo.Name); } } if (hasContent) { attRecord.SchoolYear = int.Parse("" + row.Cells[ColumnIndex["學年度"]].Value); attRecord.Semester = int.Parse("" + row.Cells[ColumnIndex["學期"]].Value); updateHelper.Add(attRecord); } else { deleteList.Add(tag.Key); //log 紀錄被刪除的資料 LOG[attRecord.RefStudentID].afterData.Remove(tag.Date.ToShortDateString()); LOG[attRecord.RefStudentID].deleteData.Add(tag.Date.ToShortDateString()); } #endregion } } #region InsertHelper if (InsertHelper.Count > 0) { try { Attendance.Insert(InsertHelper); } catch (Exception ex) { FISCA.Presentation.Controls.MsgBox.Show("缺曠紀錄新增失敗 : " + ex.Message, "新增失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); } //log 寫入log foreach (string each in LOG.Keys) { foreach (string date in LOG[each].afterData.Keys) { if (!LOG[each].beforeData.ContainsKey(date) && LOG[each].afterData[date].Count > 0) { StringBuilder desc = new StringBuilder(""); desc.AppendLine("學生「" + K12.Data.Student.SelectByID(each).Name + "」"); desc.AppendLine("日期「" + date + "」"); foreach (string period in LOG[each].afterData[date].Keys) { desc.AppendLine("節次「" + period + "」設為「" + LOG[each].afterData[date][period] + "」"); } ApplicationLog.Log("學務系統.缺曠資料", "批次新增缺曠資料", "student", each, desc.ToString()); //Log部份 //CurrentUser.Instance.AppLog.Write(EntityType.Student, EntityAction.Insert, _student.ID, desc.ToString(), this.Text, ""); } } } } #endregion #region updateHelper if (updateHelper.Count > 0) { try { Attendance.Update(updateHelper); } catch (Exception ex) { FISCA.Presentation.Controls.MsgBox.Show("缺曠紀錄更新失敗 : " + ex.Message, "更新失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); } //log 寫入log foreach (string each in LOG.Keys) { foreach (string date in LOG[each].afterData.Keys) { if (LOG[each].beforeData.ContainsKey(date) && LOG[each].afterData[date].Count > 0) { bool dirty = false; StringBuilder desc = new StringBuilder(""); desc.AppendLine("學生「" + K12.Data.Student.SelectByID(each).Name + "」 "); desc.AppendLine("日期「" + date + "」 "); foreach (string period in LOG[each].beforeData[date].Keys) { if (!LOG[each].afterData[date].ContainsKey(period)) { LOG[each].afterData[date].Add(period, ""); } } foreach (string period in LOG[each].afterData[date].Keys) { if (LOG[each].beforeData[date].ContainsKey(period)) { if (LOG[each].beforeData[date][period] != LOG[each].afterData[date][period]) { dirty = true; desc.AppendLine("節次「" + period + "」由「" + LOG[each].beforeData[date][period] + "」變更為「" + LOG[each].afterData[date][period] + "」"); } } else { dirty = true; desc.AppendLine("節次「" + period + "」由「」變更為「" + LOG[each].afterData[date][period] + "」 "); } } if (dirty) { //Log部份 ApplicationLog.Log("學務系統.缺曠資料", "批次修改缺曠資料", "student", each, desc.ToString()); //CurrentUser.Instance.AppLog.Write(EntityType.Student, EntityAction.Update, _student.ID, desc.ToString(), this.Text, ""); } } } } } #endregion #region deleteList if (deleteList.Count > 0) { try { Attendance.Delete(deleteList); } catch (Exception ex) { FISCA.Presentation.Controls.MsgBox.Show("缺曠紀錄刪除失敗 : " + ex.Message, "刪除失敗", MessageBoxButtons.OK, MessageBoxIcon.Error); } //log 寫入被刪除的資料的log foreach (string each in LOG.Keys) { StringBuilder desc = new StringBuilder(""); desc.AppendLine("學生「" + K12.Data.Student.SelectByID(each).Name + "」"); foreach (string date in LOG[each].deleteData) { desc.AppendLine("刪除「" + date + "」缺曠紀錄 "); } //Log部份 ApplicationLog.Log("學務系統.缺曠資料", "批次刪除缺曠資料", "student", each, desc.ToString()); //CurrentUser.Instance.AppLog.Write(EntityType.Student, EntityAction.Delete, _student.ID, desc.ToString(), this.Text, ""); } } #endregion FISCA.Presentation.Controls.MsgBox.Show("儲存缺曠資料成功!", "完成", MessageBoxButtons.OK, MessageBoxIcon.Information); this.Close(); SaveDateSetting(); //儲存日期是否鎖定的設定 }