using BLL; using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Data; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using static System.Windows.Forms.VisualStyles.VisualStyleElement; using System.Web.Configuration; using System.Security.Principal; namespace FineUIPro.Web.WelderManage { public partial class WelderTestInfo : PageBase { //定义变量 /// /// 上传预设的虚拟路径 /// private string initPath = Const.ExcelUrl; /// /// 错误集合 /// public static string errorInfos = string.Empty; #region 加载 protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); this.drpUnitId.DataTextField = "UnitName"; this.drpUnitId.DataValueField = "UnitId"; this.drpUnitId.DataSource = (from x in Funs.DB.Base_Unit select x).ToList(); this.drpUnitId.DataBind(); Funs.FineUIPleaseSelect(this.drpUnitId); this.InitTreeMenu();//加载树 } } /// /// 加载树 /// private void InitTreeMenu() { this.tvControlItem.Nodes.Clear(); TreeNode rootNode = new TreeNode(); rootNode.Text = "单位-焊工"; rootNode.ToolTip = "单位"; rootNode.NodeID = "0"; rootNode.Expanded = true; this.tvControlItem.Nodes.Add(rootNode); List welders = new List(); List units = (from x in Funs.DB.Base_Unit select x).ToList(); if (this.drpUnitId.SelectedValue != BLL.Const._Null) { units = units.Where(e => e.UnitId == this.drpUnitId.SelectedValue).ToList(); } foreach (var item in units) { TreeNode rootProjectNode = new TreeNode();//定义根节点 rootProjectNode.Text = item.UnitName; rootProjectNode.NodeID = item.UnitId; //rootProjectNode.Expanded = true; rootProjectNode.ToolTip = "单位"; rootNode.Nodes.Add(rootProjectNode); welders = (from x in Funs.DB.Welder_Welder where x.UnitId == item.UnitId select x).ToList(); this.BindNodes(rootProjectNode, welders); } } #endregion #region 绑定树节点 /// /// 绑定树节点 /// /// private void BindNodes(TreeNode node, List welders) { if (node.ToolTip == "单位") { if (!string.IsNullOrEmpty(this.txtWelderCodeS.Text.Trim())) { welders = welders.Where(e => e.WelderCode.Contains(this.txtWelderCodeS.Text.Trim())).ToList(); } if (!string.IsNullOrEmpty(txtWelderNameS.Text.Trim())) { welders = welders.Where(e => e.WelderName.Contains(this.txtWelderNameS.Text.Trim())).ToList(); } welders = welders.OrderBy(x => x.WelderCode).ToList(); foreach (var item in welders) { TreeNode newNode = new TreeNode(); newNode.Text = item.WelderName + "(" + item.WelderCode + ")"; newNode.NodeID = item.WelderId; newNode.ToolTip = item.WelderCode; newNode.EnableClickEvent = true; node.Nodes.Add(newNode); } } } #endregion #region 点击TreeView /// /// 点击TreeView /// /// /// protected void tvControlItem_NodeCommand(object sender, TreeCommandEventArgs e) { this.BindGrid(); } #endregion #region Grid 绑定 private void BindGrid() { var welder = BLL.WelderService.GetWelderById(this.tvControlItem.SelectedNodeID); if (welder != null) { this.lblCode.Text = welder.WelderCode; this.lblName.Text = welder.WelderName; } string strSql = @"SELECT A.*,b.WeldingMethodCode, (case when A.IsPass=1 then '是' else '否' end ) As PassName , convert(varchar,A.CreatedDate,23) as TestDate FROM Welder_TestInfo AS A inner join Base_WeldingMethod as B on A.WeldMethodId=B.WeldingMethodId WHERE A.WelderId=@WelderId"; List listStr = new List(); listStr.Add(new SqlParameter("@WelderId", this.tvControlItem.SelectedNodeID)); SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); // 2.获取当前分页数据 //var table = this.GetPagedDataTable(Grid1, tb1); Grid1.RecordCount = tb.Rows.Count; tb = GetFilteredTable(Grid1.FilteredData, tb); var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } #endregion #region 双击事件 /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning); return; } if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.Welder_QualifiedProjectMenuId, Const.BtnAdd)) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WelderTestInfoEdit.aspx?Id={0}", Grid1.SelectedRowID, "维护 - "))); } else { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); } } #endregion #region 右键编辑 protected void btnMenuEdit_Click(object sender, EventArgs e) { if (BLL.CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.WelderTestInfoMenuId, BLL.Const.BtnModify)) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning); return; } PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WelderTestInfoEdit.aspx?Id={0}", Grid1.SelectedRowID, "维护 - "))); } else { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); } } /// /// 删除 /// /// /// protected void btnMenuDelete_Click(object sender, EventArgs e) { if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.WelderTestInfoMenuId, Const.BtnDelete)) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请选择一条记录!", MessageBoxIcon.Warning); return; } bool isShow = true; if (Grid1.SelectedRowIndexArray.Length > 1) { isShow = false; } foreach (int rowIndex in Grid1.SelectedRowIndexArray) { string rowID = Grid1.DataKeys[rowIndex][0].ToString(); if (judgementDelete(rowID, isShow)) { BLL.WelderTestService.Delete(rowID); //BLL.Sys_LogService.AddLog(Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "删除合格项目焊工"); } } ShowNotify("删除成功!", MessageBoxIcon.Success); this.BindGrid(); } else { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); } } #endregion #region 判断是否可删除 /// /// 判断是否可以删除 /// /// private bool judgementDelete(string id, bool isShow) { string content = string.Empty; if (string.IsNullOrEmpty(content)) { return true; } else { if (isShow) { Alert.ShowInTop(content, MessageBoxIcon.Error); } return false; } } #endregion #region 模板下载 /// /// 模板下载 /// /// /// protected void btnDownLoad_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string uploadfilepath = rootPath + Const.WelderTestInfoTemplateUrl; string filePath = Const.WelderTestInfoTemplateUrl; 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(); //PageContext.RegisterStartupScript(Confirm.GetShowReference("确定要下载焊工资质导入模板?", 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.WelderQueTemplateUrl; string filePath = Const.WelderQueTemplateUrl; 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 protected void btnImport_Click(object sender, EventArgs e) { if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.WelderTestInfoMenuId, Const.BtnIn)) { string message = string.Empty; errorInfos = string.Empty; try { if (this.fileUpload.HasFile == false) { ShowNotify("请选择Excel文件!", MessageBoxIcon.Warning); return; } string IsXls = Path.GetExtension(this.fileUpload.FileName).ToString().Trim().ToLower(); if (IsXls != ".xls" && IsXls != ".xlsx") { ShowNotify("只能选择Excel文件!", MessageBoxIcon.Warning); return; } string rootPath = Server.MapPath("~/"); string initFullPath = rootPath + initPath; if (!Directory.Exists(initFullPath)) { Directory.CreateDirectory(initFullPath); } //指定上传文件名称 this.hidFileName.Text = BLL.Funs.GetNewFileName() + IsXls; //上传文件路径 string filePath = initFullPath + this.hidFileName.Text; //文件上传服务器 this.fileUpload.PostedFile.SaveAs(filePath); //文件上传服务器后的名称 string fileName = rootPath + initPath + this.hidFileName.Text; //读取Excel DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true); //验证Excel读取是否有误 if (!string.IsNullOrEmpty(errorInfos)) { ShowNotify(errorInfos, MessageBoxIcon.Warning); return; } DataTable dt = ds.Tables[0]; if (dt.Rows.Count > 0) { List listData = new List(); var validate = ValidateImportFileds(dt); if (validate != null && validate.Count > 0) { Cache["errLog"] = validate; //提示错误信息 PageContext.RegisterStartupScript(Window2.GetShowReference("WelderTestInfoImportError.aspx")); } else { foreach (DataRow dr in dt.Rows) { Model.Welder_TestInfo model = new Model.Welder_TestInfo(); string welderCode = dr[0].ToString(); string weldMethod = dr[1].ToString(); string weldMeatrail = dr[2].ToString(); string isPass = dr[3].ToString(); string testDate = dr[4].ToString(); string remark = dr[5].ToString(); var welderId = Funs.DB.Welder_Welder.FirstOrDefault(t => t.WelderCode == welderCode)?.WelderId; var methodId = Funs.DB.Base_WeldingMethod.FirstOrDefault(t => t.WeldingMethodCode == weldMethod)?.WeldingMethodId; //var materId = Funs.DB.Base_Material.FirstOrDefault(t => t.MaterialCode == weldMeatrail)?.MaterialId; model.Id = SQLHelper.GetNewID(typeof(Model.Welder_TestInfo)); model.WeldMethodId = methodId; model.MaterialId = weldMeatrail; model.IsPass = isPass == "是" ? true : false; model.ProjectId = "0"; model.WelderId = welderId; model.CreatedDate = DateTime.Parse(testDate); model.Remark = remark; listData.Add(model); } } if (listData.Count > 0) { Funs.DB.Welder_TestInfo.InsertAllOnSubmit(listData); Funs.DB.SubmitChanges(); ShowNotify("导入成功", MessageBoxIcon.Success); this.BindGrid(); } } else { ShowAlert("没有数据!", MessageBoxIcon.Warning); return; } } catch (Exception ex) { ShowAlert("'" + ex.Message + "'", MessageBoxIcon.Warning); } } else { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); } } #region 弹出编辑窗口关闭事件 /// /// 弹出编辑窗体关闭事件 /// /// /// protected void Window1_Close(object sender, WindowCloseEventArgs e) { BindGrid(); } #endregion #region 分页 排序 /// /// 分页 /// /// /// protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e) { Grid1.PageIndex = e.NewPageIndex; BindGrid(); } /// /// 分页显示条数下拉框 /// /// /// protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e) { Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue); BindGrid(); } /// /// 排序 /// /// /// protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e) { Grid1.SortDirection = e.SortDirection; Grid1.SortField = e.SortField; BindGrid(); } #endregion #region 查询 /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.InitTreeMenu(); this.BindGrid(); } #endregion #region 增加 /// /// 增加 /// /// /// protected void btnAdd_Click(object sender, EventArgs e) { if (CommonService.GetAllButtonPowerList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, Const.WelderTestInfoMenuId, Const.BtnAdd)) { if (!string.IsNullOrEmpty(this.tvControlItem.SelectedNodeID)) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("WelderTestInfoEdit.aspx?WelderId={0}", this.tvControlItem.SelectedNodeID, "新增 - "))); } else { Alert.ShowInTop("请选择一名焊工!", MessageBoxIcon.Warning); return; } } else { ShowNotify("您没有这个权限,请与管理员联系!", MessageBoxIcon.Warning); } } #endregion protected void Window2_Close(object sender, EventArgs e) { Cache["errLog"] = null; } private List ValidateImportFileds(DataTable dt) { var listData = new List(); int i = 0; foreach (DataRow dr in dt.Rows) { i++; string welderCode = dr[0].ToString(); string weldMethod = dr[1].ToString(); string weldMeatrail = dr[2].ToString(); string isPass = dr[3].ToString(); string testDate = dr[4].ToString(); string remark = dr[5].ToString(); //验证焊工号 if (string.IsNullOrEmpty(welderCode)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "焊工号"; model.errMsg = "焊工号不能为空"; model.isSuccess = false; listData.Add(model); } if (string.IsNullOrEmpty(weldMethod)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "焊接方法"; model.errMsg = "焊接方法不能为空"; model.isSuccess = false; listData.Add(model); } if (string.IsNullOrEmpty(weldMeatrail)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "焊接材质"; model.errMsg = "焊接材质不能为空"; model.isSuccess = false; listData.Add(model); } if (string.IsNullOrEmpty(isPass)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "是否合格"; model.errMsg = "是否合格不能为空"; model.isSuccess = false; listData.Add(model); } if(isPass!="是" && isPass != "否") { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "是否合格"; model.errMsg = "合格字段只能是或者否"; model.isSuccess = false; listData.Add(model); } if (string.IsNullOrEmpty(testDate)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "考试日期"; model.errMsg = "考试日期不能为空"; model.isSuccess = false; listData.Add(model); } DateTime t1 = new DateTime(); if(!DateTime.TryParse(testDate,out t1)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "考试日期"; model.errMsg = "请输入正确的考试日期(yyyy-MM-dd)"; model.isSuccess = false; listData.Add(model); } //验证是否存在 var isAnyWelderCode = Funs.DB.Welder_Welder.Any(t => t.WelderCode == welderCode); if (!isAnyWelderCode) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "焊工号"; model.errMsg = "焊工号不存在"; model.isSuccess = false; listData.Add(model); } var isAnyWeldMethod = Funs.DB.Base_WeldingMethod.Any(t => t.WeldingMethodCode == weldMethod); if (!isAnyWeldMethod) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "焊接方法"; model.errMsg = "焊接方法不存在"; model.isSuccess = false; listData.Add(model); } string type = "FeⅠ,FeⅡ,FeⅢ,FeⅣ,其他"; var isAnyMeaterail = weldMeatrail.Split(','); foreach (var item in isAnyMeaterail) { if (!type.Contains(item)) { var model = new Model.ViewModels.ImportErrorViewModel(); model.rowId = i; model.createdTime = DateTime.Now; model.columnName = "焊接材质"; model.errMsg = "材质应为FeⅠ,FeⅡ,FeⅢ,FeⅣ,其他,多个用','隔开"; model.isSuccess = false; listData.Add(model); break; } } } return listData; } } }