public override string Import(List <IRowStream> Rows) { List <DAO.UDT_CounselUserDefDataDef> InsertData = new List <DAO.UDT_CounselUserDefDataDef>(); List <DAO.UDT_CounselUserDefDataDef> UpdateData = new List <DAO.UDT_CounselUserDefDataDef>(); List <DAO.UDT_CounselUserDefDataDef> HasData = new List <DAO.UDT_CounselUserDefDataDef>(); List <string> StudentIDList = new List <string>(); // 取得學生狀態對應 foreach (IRowStream ir in Rows) { if (ir.Contains("學號") && ir.Contains("狀態")) { StudentIDList.Add(Utility.GetStudentID(ir.GetValue("學號"), ir.GetValue("狀態")).ToString()); } } // 已有資料 HasData = _UDTTransfer.GetCounselUserDefineDataByStudentIDList(StudentIDList); foreach (IRowStream ir in Rows) { DAO.UDT_CounselUserDefDataDef CounselUserDefineData = null; int sid = 0; if (ir.Contains("學號") && ir.Contains("狀態")) { string key = ir.GetValue("學號") + "_"; if (Global._StudentStatusDBDict.ContainsKey(ir.GetValue("狀態"))) { sid = Utility.GetStudentID(ir.GetValue("學號"), ir.GetValue("狀態")); } foreach (DAO.UDT_CounselUserDefDataDef rec in HasData.Where(x => x.StudentID == sid)) { if (rec.FieldName == ir.GetValue("欄位名稱")) { CounselUserDefineData = rec; } } if (CounselUserDefineData == null) { CounselUserDefineData = new DAO.UDT_CounselUserDefDataDef(); } // 學生編號 CounselUserDefineData.StudentID = sid; // 欄位名稱 CounselUserDefineData.FieldName = ir.GetValue("欄位名稱"); // 值 CounselUserDefineData.Value = ir.GetValue("值"); if (string.IsNullOrEmpty(CounselUserDefineData.UID)) { InsertData.Add(CounselUserDefineData); } else { UpdateData.Add(CounselUserDefineData); } } } if (InsertData.Count > 0) { _UDTTransfer.InsertCounselUsereDefinfDataList(InsertData); } if (UpdateData.Count > 0) { _UDTTransfer.UpdateCounselUserDefineDataList(UpdateData); } return(""); }
/// <summary> /// 按下儲存 /// </summary> /// <param name="e"></param> protected override void OnSaveButtonClick(EventArgs e) { bool canSave = true; dgv.EndEdit(); // 檢查資料 foreach (DataGridViewRow drv in dgv.Rows) { foreach (DataGridViewCell cell in drv.Cells) { if (cell.ErrorText != "") { canSave = false; } } } if (canSave) { try { _InsertDataList.Clear(); _UpdateDataList.Clear(); _DeleteDataList.Clear(); _HasUIDList.Clear(); // 儲存資料到 UDT foreach (DataGridViewRow row in dgv.Rows) { if (row.IsNewRow) { continue; } DAO.UDT_CounselUserDefDataDef udd = new DAO.UDT_CounselUserDefDataDef(); // 資料轉型 if (row.Tag != null) { udd = (DAO.UDT_CounselUserDefDataDef)row.Tag; } udd.StudentID = int.Parse(PrimaryKey); if (row.Cells[FieldName.Index].Value != null) { udd.FieldName = row.Cells[FieldName.Index].Value.ToString(); } if (row.Cells[Value.Index].Value != null) { udd.Value = row.Cells[Value.Index].Value.ToString(); } // 新增或更新 if (string.IsNullOrEmpty(udd.UID)) { _InsertDataList.Add(udd); } else { _UpdateDataList.Add(udd); _HasUIDList.Add(udd.UID); } //_LogTransfer.SetLogValue(udd.FieldName, udd.Value); } // 新增或更新至 UDT if (_InsertDataList.Count > 0) { _UDTTransfer.InsertCounselUsereDefinfDataList(_InsertDataList); } if (_UpdateDataList.Count > 0) { _UDTTransfer.UpdateCounselUserDefineDataList(_UpdateDataList); } // 刪除舊資料 UDT foreach (DAO.UDT_CounselUserDefDataDef udd in _UserDefineData) { if (!_HasUIDList.Contains(udd.UID)) { _DeleteDataList.Add(udd); } } if (_DeleteDataList.Count > 0) { _UDTTransfer.DeleteCounselUserDefineDataList(_DeleteDataList); } this.CancelButtonVisible = false; this.SaveButtonVisible = false; } catch (Exception ex) { FISCA.Presentation.Controls.MsgBox.Show("儲存失敗!"); } } else { this.CancelButtonVisible = true; this.SaveButtonVisible = false; } }