using BLL; using BLL.Common; using Model; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; namespace FineUIPro.Web.BaseInfo { public partial class Department : PageBase { #region 加载 /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower();//按钮权限 drpDepartLeader.DataTextField = "UserName"; drpDepartLeader.DataValueField = "UserId"; this.drpDepartLeader.DataSource = BLL.Sys_UserService.GetUserList(); this.drpDepartLeader.DataBind(); Funs.FineUIPleaseSelect(this.drpDepartLeader); this.drpDepartLeader.SelectedIndex = 0; ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 BindGrid(); } } /// /// 绑定数据 /// private void BindGrid() { string strSql = @"SELECT dep.DepartId,dep.DepartCode,dep.DepartName,dep.DepartLeader,u.UserName FROM dbo.Base_Depart AS dep LEFT JOIN dbo.Sys_User AS u ON u.UserId = dep.DepartLeader Where 1=1"; // 2.获取当前分页数据 List listStr = new List(); if (!string.IsNullOrEmpty(this.txtDepCode.Text.Trim())) { strSql += " AND dep.DepartCode LIKE @departCode"; listStr.Add(new SqlParameter("@departCode", "%" + this.txtDepCode.Text.Trim() + "%")); } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } protected void Text_TextChanged(object sender, EventArgs e) { BindGrid(); } /// /// 改变索引事件 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { Grid1.PageIndex = e.NewPageIndex; BindGrid(); } #endregion #region 分页下拉选择 /// /// 分页下拉选择 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } #endregion #region 删除 /// /// 删除 /// /// /// protected void btnDelete_Click(object sender, EventArgs e) { BLL.DepartService.DeleteDepartById(hfFormID.Text); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete department"); // 重新绑定表格,并模拟点击[新增按钮] BindGrid(); //PageContext.RegisterStartupScript("onNewButtonClick();"); } /// /// 右键删除事件 /// /// /// protected void btnMenuDelete_Click(object sender, EventArgs e) { this.DeleteData(); } /// /// 删除方法 /// private void DeleteData() { if (Grid1.SelectedRowIndexArray.Length > 0) { foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); BLL.DepartService.DeleteDepartById(rowID); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete department"); } BindGrid(); //PageContext.RegisterStartupScript("onNewButtonClick();"); } } #endregion #region 编辑 /// /// 右键编辑事件 /// /// /// protected void btnMenuEdit_Click(object sender, EventArgs e) { this.EditData(); } /// /// 编辑数据方法 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("Please select at least one record!", MessageBoxIcon.Warning); return; } string Id = Grid1.SelectedRowID; var depart = BLL.DepartService.GetDepartById(Id); if (depart != null) { this.txtDepartCode.Text = depart.DepartCode; this.txtDepartName.Text = depart.DepartName; this.txtRemark.Text = depart.Remark; if (!string.IsNullOrEmpty(depart.DepartLeader)) { this.drpDepartLeader.SelectedValue = depart.DepartLeader; } hfFormID.Text = Id; this.btnDelete.Enabled = true; } } #endregion #region 保存 /// /// 保存按钮 /// /// /// protected void btnSave_Click(object sender, EventArgs e) { string strRowID = hfFormID.Text; if (!BLL.DepartService.IsExitDepartCode(this.txtDepartCode.Text.Trim(), strRowID) && !BLL.DepartService.IsExitDepartName(this.txtDepartName.Text.Trim(), strRowID)) { Model.Base_Depart depart = new Model.Base_Depart { DepartCode = this.txtDepartCode.Text.Trim(), DepartName = this.txtDepartName.Text.Trim(), Remark = this.txtRemark.Text.Trim() }; if (this.drpDepartLeader.SelectedValue!=BLL.Const._Null) { depart.DepartLeader = this.drpDepartLeader.SelectedValue; } if (string.IsNullOrEmpty(strRowID)) { depart.DepartId = SQLHelper.GetNewID(typeof(Model.Base_Depart)); BLL.DepartService.AddDepart(depart); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Add the department"); ShowNotify("Save successfully!", MessageBoxIcon.Success); } else { depart.DepartId = strRowID; BLL.DepartService.UpdateDepart(depart); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Modify the department"); ShowNotify("Save successfully!", MessageBoxIcon.Success); } //this.SimpleForm1.Reset(); // 重新绑定表格,并点击当前编辑或者新增的行 BindGrid(); //PageContext.RegisterStartupScript(String.Format("F('{0}').selectRow('{1}');", Grid1.ClientID, depart.DepartId)); //PageContext.RegisterStartupScript("onNewButtonClick();"); } else { ShowNotify("The BU.Code or BU.Name entered already exists!", MessageBoxIcon.Warning); } } #endregion #region 权限设置 /// /// 菜单按钮权限 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.DepartmentMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnMenuEdit.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnSave)) { this.btnSave.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnDelete.Hidden = false; } } } #endregion protected void Grid1_RowSelect(object sender, GridRowSelectEventArgs e) { this.EditData(); } protected void btnNew_Click(object sender, EventArgs e) { this.hfFormID.Text = string.Empty; this.txtDepartCode.Text = string.Empty; this.txtDepartName.Text = string.Empty; this.drpDepartLeader.SelectedValue = Const._Null; this.txtRemark.Text = string.Empty; this.btnDelete.Enabled = false; } #region 模板下载 /// /// 模板下载 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Confirm.GetShowReference("Are you sure to download the import template?", String.Empty, MessageBoxIcon.Question, PageManager1.GetCustomEventReference(false, "Confirm_OK"), PageManager1.GetCustomEventReference("Confirm_Cancel"))); } /// /// 下载导入模板 /// /// /// protected void PageManager1_CustomEvent(object sender, CustomEventArgs e) { if (e.EventArgument == "Confirm_OK") { string rootPath = Server.MapPath("~/"); string uploadfilepath = rootPath + Const.DepartmentTemplateUrl; string filePath = Const.DepartmentTemplateUrl; string fileName = Path.GetFileName(filePath); FileInfo info = new FileInfo(uploadfilepath); long fileSize = info.Length; Response.ClearContent(); Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.ContentType = "excel/plain"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Length", fileSize.ToString().Trim()); Response.TransmitFile(uploadfilepath, 0, fileSize); Response.End(); } } #endregion #region 导入定义项 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 错误集合 /// public static string errorInfos = string.Empty; #endregion #region 导入 /// /// 导入 /// /// /// protected void btnImport_Click(object sender, EventArgs e) { string message = string.Empty; errorInfos = string.Empty; List depList = new List(); try { if (this.fuAttachUrl.HasFile == false) { ShowNotify("Please select Excel file!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower(); if (IsXls != ".xls" && IsXls != ".xlsx") { ShowNotify("Only Excel files can be selected!", MessageBoxIcon.Warning); return; } if (depList != null) { depList.Clear(); } if (!string.IsNullOrEmpty(errorInfos)) { errorInfos = string.Empty; } string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } //指定上传文件名称 this.hdFileName.Text = BLL.Funs.GetNewFileName() + IsXls; //上传文件路径 string filePath = initFullPath + this.hdFileName.Text; //文件上传服务器 this.fuAttachUrl.PostedFile.SaveAs(filePath); //文件上传服务器后的名称 string fileName = rootPath + initPath + this.hdFileName.Text; //读取Excel DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true); //验证Excel读取是否有误 if (!string.IsNullOrEmpty(errorInfos)) { ShowNotify(errorInfos, MessageBoxIcon.Warning); return; } //导入数据库 if (ds.Tables.Count > 0) { for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { Base_Depart d = new Base_Depart(); #region 数据验证和赋值 string departmentCode = ds.Tables[0].Rows[i]["DepartmentCode"].ToString(); if (departmentCode != null && !string.IsNullOrEmpty(departmentCode)) { d.DepartCode = departmentCode; } else { errorInfos += (i + 2) + "Line, [DepartmentCode] cannot be empty!
"; } string manager = ds.Tables[0].Rows[i]["Manager"].ToString(); if (manager != null && !string.IsNullOrEmpty(manager)) { d.DepartLeader = manager; } else { errorInfos += (i + 2) + "Line, [Manager] cannot be empty!
"; } depList.Add(d); #endregion } if (!string.IsNullOrEmpty(errorInfos)) { ShowNotify(errorInfos, MessageBoxIcon.Warning, 10000); return; } if (depList.Count > 0) { foreach (var item in depList) { Model.Base_Depart dep = Funs.DB.Base_Depart.FirstOrDefault(x => x.DepartCode == item.DepartCode); if (dep != null) { var user = BLL.Sys_UserService.GetUserByAccount(item.DepartLeader); if (user != null) { dep.DepartLeader = user.UserId; Funs.DB.SubmitChanges(); } } } } ShowNotify("Import success!", MessageBoxIcon.Success); PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference()); } else { ShowAlert("No data!", MessageBoxIcon.Warning); return; } } catch (Exception ex) { ShowAlert("'" + ex.Message + "'", MessageBoxIcon.Warning); } } #endregion } }