using BLL; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; namespace FineUIPro.Web.SES { public partial class CQualityPunish : PageBase { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { GetButtonPower();//权限设置 btnNew.OnClientClick = Window1.GetShowReference("CQualityPunishEdit.aspx") + "return false;"; btnDelete.OnClientClick = Grid1.GetNoSelectionAlertReference("Please select at least one item!"); btnDelete.ConfirmText = String.Format("Are you sure you want to delete the selected   rows?", Grid1.GetSelectedCountReference()); var pun = from x in Funs.DB.View_EMC_Punishment where x.Flag == "2" && x.Contract_AdminId != null orderby x.Contract_Admin select new { x.Contract_AdminId, x.Contract_Admin }; drpContractAdmin.DataValueField = "Contract_AdminId"; drpContractAdmin.DataTextField = "Contract_Admin"; drpContractAdmin.DataSource = pun.Distinct(); drpContractAdmin.DataBind(); Funs.FineUIPleaseSelect(drpContractAdmin); ddlPageSize.SelectedValue = Grid1.PageSize.ToString(); // 绑定表格 BindGrid(); } } private void BindGrid() { string strSql = string.Empty; if (this.CurrUser.UserId == BLL.Const.GlyId) { strSql = @"SELECT * FROM dbo.View_EMC_Que_Punishment WHERE Flag='2' "; } else { strSql = @"SELECT * FROM dbo.View_EMC_Que_Punishment WHERE Flag='2' AND (Requisitioner=@userId or AuditMan=@userId or Violation_Inspector=@userId or Main_CoordinatorId=@userId) "; } List listStr = new List(); listStr.Add(new SqlParameter("@userId", this.CurrUser.UserId)); if (!string.IsNullOrEmpty(this.txtFO_NO.Text.Trim())) { strSql += " AND FO_NO LIKE @FO_NO"; listStr.Add(new SqlParameter("@FO_NO", "%" + this.txtFO_NO.Text.Trim() + "%")); } if (drpContractAdmin.SelectedValue != Const._Null && drpContractAdmin.SelectedValue != null) { strSql += " AND Contract_AdminId = @Contract_AdminId"; listStr.Add(new SqlParameter("@Contract_AdminId", drpContractAdmin.SelectedValue)); } if (!string.IsNullOrEmpty(txtPunishDate.Text)) { DateTime startDate = Convert.ToDateTime(txtPunishDate.Text + "-01"); DateTime endDate = startDate.AddMonths(1); strSql += " AND PunishDate >= @StartDate"; listStr.Add(new SqlParameter("@StartDate", startDate)); strSql += " AND PunishDate < @EndDate"; listStr.Add(new SqlParameter("@EndDate", endDate)); } 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(); for (int i = 0; i < Grid1.Rows.Count; i++) { System.Web.UI.WebControls.LinkButton lbtnUrl = ((System.Web.UI.WebControls.LinkButton)(Grid1.Rows[i].FindControl("lbtnUrl"))); string url = lbtnUrl.CommandArgument.ToString(); if (!string.IsNullOrEmpty(url)) { url = url.Replace('\\', '/'); lbtnUrl.Text = BLL.UploadAttachmentService.ShowAttachment("../", url); } } } #region 编辑 /// /// 编辑 /// /// /// protected void btnEdit_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInParent("Please select at least one record!"); return; } string rowID = Grid1.SelectedRowID; var pun = BLL.PunishmentService.GetPunishmentById(rowID); if (pun != null) { if (pun.IsAudit == true) { if (CurrUser.UserId == pun.AuditMan) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("CQualityPunishEdit.aspx?punishmentId={0}", rowID, "编辑 - "))); } else { ShowNotify("已批准!只有批准人有修改权限!", MessageBoxIcon.Warning); } } else { if (pun.IsFrame == true) { var datas = BLL.SESRelatedDataService.GetSESRelatedDataByFoNo(pun.FO_NO); if (CurrUser.UserId == datas.Main_Coordinator || CurrUser.UserId == pun.Violation_Inspector || CurrUser.UserId == Const.GlyId) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("CQualityPunishEdit.aspx?punishmentId={0}", rowID, "编辑 - "))); } else { ShowNotify("只有主协调员,发起人(违章检查人)有修改权限!", MessageBoxIcon.Warning); } } else { if (CurrUser.UserId == pun.Requisitioner || CurrUser.UserId == pun.Violation_Inspector || CurrUser.UserId == Const.GlyId) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("CQualityPunishEdit.aspx?punishmentId={0}", rowID, "编辑 - "))); } else { ShowNotify("只有合同请购人,发起人(违章检查人)有修改权限!", MessageBoxIcon.Warning); } } } } } protected void btnAudit_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInParent("Please select at least one record!"); return; } string rowID = Grid1.SelectedRowID; var pun = BLL.PunishmentService.GetPunishmentById(rowID); string auditMan=string.Empty; if (pun != null) { if (pun.IsFrame == true) { var fc = BLL.SESRelatedDataService.GetSESRelatedDataByFoNo(pun.FO_NO); string curUser = this.CurrUser.UserId; // 当前用户 string violationInspector = pun.Violation_Inspector; // 发起人 string leader = string.Empty; // 主协调员部门领导 string mainCoordinator = fc.Main_Coordinator; var userInfo = BLL.Sys_UserService.GetUsersByUserId(fc.Main_Coordinator); if (userInfo != null) { var dep = BLL.DepartService.GetDepartById(userInfo.DepartId); if (dep != null) { leader = dep.DepartLeader; } } // 当前用户不是主调协员 if (curUser != mainCoordinator) { // 当前用户是主调协员部门领导 不发邮件 if (curUser == leader) { auditMan = leader; } else if(curUser==Const.GlyId) { auditMan = Const.GlyId; } else { ShowNotify("您没有审核权限,请与管理员联系!", MessageBoxIcon.Warning); return; } } else // 当前用户是主调协员 { auditMan = mainCoordinator; // 发邮件给主调协员部门领导 } } // 非框架合同 else { string curUser = this.CurrUser.UserId; // 当前用户 string violationInspector = pun.Violation_Inspector; // 发起人 string leader = string.Empty; // 请购人部门领导 string requisitioner = pun.Requisitioner; // 请购人 var userInfo = BLL.Sys_UserService.GetUsersByUserId(pun.Requisitioner); if (userInfo != null) { var dep = BLL.DepartService.GetDepartById(userInfo.DepartId); if (dep != null) { leader = dep.DepartLeader; } } // 当前用户不是请购人 if (curUser != requisitioner) { // 当前用户是请购人部门领导 不发邮件 if (curUser == leader) { auditMan = leader; } else if (curUser == Const.GlyId) { auditMan = Const.GlyId; } else { ShowNotify("您没有审核权限,请与管理员联系!", MessageBoxIcon.Warning); return; } } else // 当前用户是请购人 { auditMan = requisitioner; // 发邮件给请购人部门领导 } } if (auditMan != string.Empty) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("CQualityPunishEdit.aspx?punishmentId={0}&auditMan={1}&audit=audit", rowID, auditMan, "编辑 - "))); } else { ShowNotify("您没有审核权限,请与管理员联系!", MessageBoxIcon.Warning); } } } /// /// 右键编辑事件 /// /// /// protected void btnMenuEdit_Click(object sender, EventArgs e) { btnEdit_Click(null, null); } /// /// Grid行双击事件 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { btnEdit_Click(null, null); } #endregion #region 删除数据 /// /// 批量删除数据 /// /// /// protected void btnDelete_Click(object sender, EventArgs e) { this.DeleteData(); } /// /// 右键删除事件 /// /// /// protected void btnMenuDelete_Click(object sender, EventArgs e) { this.DeleteData(); } /// /// 删除方法 /// private void DeleteData() { if (!string.IsNullOrEmpty(Grid1.SelectedRowID)) { string rowID = Grid1.SelectedRowID; var pun = BLL.PunishmentService.GetPunishmentById(rowID); if (pun != null) { if (pun.IsFrame == true) { var datas = BLL.SESRelatedDataService.GetSESRelatedDataByFoNo(pun.FO_NO); if (pun.IsAudit == true) // 审核后只有批准人可删除 { if (CurrUser.UserId == pun.AuditMan) { if (judgementDelete(rowID, false)) { BLL.PunishmentService.DeletePunishmentById(rowID); } } else { ShowNotify("已批准!只有批准人有删除权限!", MessageBoxIcon.Warning); return; } } else { if (CurrUser.UserId == datas.Main_Coordinator || CurrUser.UserId == pun.Violation_Inspector || CurrUser.UserId == Const.GlyId) { if (judgementDelete(rowID, false)) { BLL.PunishmentService.DeletePunishmentById(rowID); } } else { ShowNotify("只有主协调员,发起人(违章检查人)有删除权限!", MessageBoxIcon.Warning); return; } } } else { if (pun.IsAudit == true) { if (CurrUser.UserId == pun.AuditMan) { if (judgementDelete(rowID, false)) { BLL.PunishmentService.DeletePunishmentById(rowID); } } else { ShowNotify("已批准!只有批准人有删除权限!", MessageBoxIcon.Warning); return; } } else { if (CurrUser.UserId == pun.Requisitioner || CurrUser.UserId == pun.Violation_Inspector || CurrUser.UserId == Const.GlyId) { if (judgementDelete(rowID, false)) { BLL.PunishmentService.DeletePunishmentById(rowID); } } else { ShowNotify("只有合同请购人,发起人(违章检查人)有删除权限!", MessageBoxIcon.Warning); return; } } } } BindGrid(); BLL.Sys_LogService.AddLog(this.CurrUser.UserId, "Delete Contractor Safety Punishment"); ShowNotify("Deleted successfully!"); } } #endregion #region 查询 /// /// 查询 /// /// /// protected void btnSearch_Click(object sender, EventArgs e) { BindGrid(); } #endregion #region 查看 protected void btnMenuView_Click(object sender, EventArgs e) { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInParent("Please select at least one record!"); return; } string Id = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("CQualityPunishEdit.aspx?punishmentId={0}&view=1", Id, "查看 - "))); } #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); } return false; } } #endregion #region 关闭弹出窗口 /// /// 关闭窗口 /// /// /// protected void Window1_Close(object sender, EventArgs e) { BindGrid(); drpContractAdmin.Items.Clear(); var pun = from x in Funs.DB.View_EMC_Punishment where x.Flag == "2" && x.Contract_AdminId != null orderby x.Contract_Admin select new { x.Contract_AdminId, x.Contract_Admin }; drpContractAdmin.DataValueField = "Contract_AdminId"; drpContractAdmin.DataTextField = "Contract_Admin"; drpContractAdmin.DataSource = pun.Distinct(); drpContractAdmin.DataBind(); Funs.FineUIPleaseSelect(drpContractAdmin); } #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 btnExport_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/") + Const.ExcelUrl; //模板文件 string TempletFileName = rootPath + "Punishment.xlsx"; //导出文件 string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\"; if (!Directory.Exists(filePath)) { Directory.CreateDirectory(filePath); } string ReportFileName = filePath + "out.xlsx"; FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read); XSSFWorkbook hssfworkbook = new XSSFWorkbook(file); #region FC_List XSSFSheet reportModel = (XSSFSheet)hssfworkbook.GetSheet("Sheet1"); IDataFormat dataformat = hssfworkbook.CreateDataFormat(); ICellStyle styleQfw = hssfworkbook.CreateCellStyle(); styleQfw.DataFormat = dataformat.GetFormat("#,##0.00"); ICellStyle styleDate = hssfworkbook.CreateCellStyle(); styleDate.DataFormat = dataformat.GetFormat("yyyy/m/d"); ICellStyle styleTime = hssfworkbook.CreateCellStyle(); styleTime.DataFormat = dataformat.GetFormat("yyyy/m/d HH:mm:ss"); XSSFFont cs_content_Font = (XSSFFont)hssfworkbook.CreateFont(); //创建字体 cs_content_Font.FontName = "等线";//字体 cs_content_Font.FontHeightInPoints = 10; //字体大小 Grid1.PageSize = 1000000; BindGrid(); if (Grid1.Rows.Count() > 0) { for (int i = 1; i <= Grid1.Rows.Count(); i++) { if (reportModel.GetRow(i) == null) reportModel.CreateRow(i); #region 列赋值 //Date if (reportModel.GetRow(i).GetCell(0) == null) reportModel.GetRow(i).CreateCell(0); if (Grid1.Rows[i - 1].Values[1] != null && Grid1.Rows[i - 1].Values[1].ToString() != "") { DateTime date = Convert.ToDateTime(Grid1.Rows[i - 1].Values[1]); reportModel.GetRow(i).GetCell(0).SetCellValue(date.ToString("yyyy/MM/dd")); // reportModel.GetRow(i).GetCell(0).CellStyle.SetFont(cs_content_Font);//将字体绑定到样式 } //Time if (reportModel.GetRow(i).GetCell(1) == null) reportModel.GetRow(i).CreateCell(1); reportModel.GetRow(i).GetCell(1).SetCellValue(Grid1.Rows[i - 1].Values[2].ToString()); // Contract No. if (reportModel.GetRow(i).GetCell(2) == null) reportModel.GetRow(i).CreateCell(2); reportModel.GetRow(i).GetCell(2).SetCellValue(Grid1.Rows[i - 1].Values[3].ToString()); //是否框架合格 if (reportModel.GetRow(i).GetCell(3) == null) reportModel.GetRow(i).CreateCell(3); reportModel.GetRow(i).GetCell(3).SetCellValue(Grid1.Rows[i - 1].Values[4].ToString() == "True" ? "是" : "否"); // Discipline if (reportModel.GetRow(i).GetCell(4) == null) reportModel.GetRow(i).CreateCell(4); reportModel.GetRow(i).GetCell(4).SetCellValue(Grid1.Rows[i - 1].Values[5].ToString()); // Contractor if (reportModel.GetRow(i).GetCell(5) == null) reportModel.GetRow(i).CreateCell(5); reportModel.GetRow(i).GetCell(5).SetCellValue(Grid1.Rows[i - 1].Values[6].ToString()); // Location if (reportModel.GetRow(i).GetCell(6) == null) reportModel.GetRow(i).CreateCell(6); reportModel.GetRow(i).GetCell(6).SetCellValue(Grid1.Rows[i - 1].Values[7].ToString()); //Violation Person if (reportModel.GetRow(i).GetCell(7) == null) reportModel.GetRow(i).CreateCell(7); reportModel.GetRow(i).GetCell(7).SetCellValue(Grid1.Rows[i - 1].Values[8].ToString()); // Violation Description if (reportModel.GetRow(i).GetCell(8) == null) reportModel.GetRow(i).CreateCell(8); reportModel.GetRow(i).GetCell(8).SetCellValue(Grid1.Rows[i - 1].Values[9].ToString()); //Violation Clause if (reportModel.GetRow(i).GetCell(9) == null) reportModel.GetRow(i).CreateCell(9); reportModel.GetRow(i).GetCell(9).SetCellValue(Grid1.Rows[i - 1].Values[10].ToString()); // Company(RMB) if (reportModel.GetRow(i).GetCell(10) == null) reportModel.GetRow(i).CreateCell(10); reportModel.GetRow(i).GetCell(10).SetCellValue(Grid1.Rows[i - 1].Values[11].ToString()); // Individual(RMB) if (reportModel.GetRow(i).GetCell(11) == null) reportModel.GetRow(i).CreateCell(11); reportModel.GetRow(i).GetCell(11).SetCellValue(Grid1.Rows[i - 1].Values[12].ToString()); // Backcharge(RMB) if (reportModel.GetRow(i).GetCell(12) == null) reportModel.GetRow(i).CreateCell(12); reportModel.GetRow(i).GetCell(12).SetCellValue(Grid1.Rows[i - 1].Values[13].ToString()); // Violation Degree if (reportModel.GetRow(i).GetCell(13) == null) reportModel.GetRow(i).CreateCell(13); reportModel.GetRow(i).GetCell(13).SetCellValue(Grid1.Rows[i - 1].Values[14].ToString()); // Contract Admin if (reportModel.GetRow(i).GetCell(14) == null) reportModel.GetRow(i).CreateCell(14); reportModel.GetRow(i).GetCell(14).SetCellValue(Grid1.Rows[i - 1].Values[15].ToString()); // Main Coordinator if (reportModel.GetRow(i).GetCell(15) == null) reportModel.GetRow(i).CreateCell(15); reportModel.GetRow(i).GetCell(15).SetCellValue(Grid1.Rows[i - 1].Values[16].ToString()); // M.C.Dept if (reportModel.GetRow(i).GetCell(16) == null) reportModel.GetRow(i).CreateCell(16); reportModel.GetRow(i).GetCell(16).SetCellValue(Grid1.Rows[i - 1].Values[17].ToString()); // User Representative if (reportModel.GetRow(i).GetCell(17) == null) reportModel.GetRow(i).CreateCell(17); reportModel.GetRow(i).GetCell(17).SetCellValue(Grid1.Rows[i - 1].Values[18].ToString()); // BYC RU if (reportModel.GetRow(i).GetCell(18) == null) reportModel.GetRow(i).CreateCell(18); reportModel.GetRow(i).GetCell(18).SetCellValue(Grid1.Rows[i - 1].Values[19].ToString()); // Violation Inspector if (reportModel.GetRow(i).GetCell(19) == null) reportModel.GetRow(i).CreateCell(19); reportModel.GetRow(i).GetCell(19).SetCellValue(Grid1.Rows[i - 1].Values[20].ToString()); // Inspection Department if (reportModel.GetRow(i).GetCell(20) == null) reportModel.GetRow(i).CreateCell(20); reportModel.GetRow(i).GetCell(20).SetCellValue(Grid1.Rows[i - 1].Values[21].ToString()); //Requistioner if (reportModel.GetRow(i).GetCell(21) == null) reportModel.GetRow(i).CreateCell(21); reportModel.GetRow(i).GetCell(21).SetCellValue(Grid1.Rows[i - 1].Values[22].ToString()); // Backcharge SES No. if (reportModel.GetRow(i).GetCell(22) == null) reportModel.GetRow(i).CreateCell(22); reportModel.GetRow(i).GetCell(22).SetCellValue(Grid1.Rows[i - 1].Values[23].ToString()); // Backcharge Completion Date if (reportModel.GetRow(i).GetCell(23) == null) reportModel.GetRow(i).CreateCell(23); if (Grid1.Rows[i - 1].Values[24] != null && Grid1.Rows[i - 1].Values[24].ToString() != "") { DateTime date = Convert.ToDateTime(Grid1.Rows[i - 1].Values[24]); reportModel.GetRow(i).GetCell(23).SetCellValue(date.ToString("yyyy/MM/dd")); // reportModel.GetRow(i).GetCell(0).CellStyle.SetFont(cs_content_Font);//将字体绑定到样式 } #endregion } } #endregion reportModel.ForceFormulaRecalculation = true; using (FileStream filess = File.OpenWrite(ReportFileName)) { hssfworkbook.Write(filess); } FileInfo filet = new FileInfo(ReportFileName); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; // 添加头信息,为"文件下载/另存为"对话框指定默认文件名 Response.AddHeader("Content-Disposition", "attachment; filename=CQuality_Punishment_" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx")); // 添加头信息,指定文件大小,让浏览器能够显示下载进度 Response.AddHeader("Content-Length", filet.Length.ToString()); // 指定返回的是一个不能被客户端读取的流,必须被下载 Response.ContentType = "application/ms-excel"; // 把文件流发送到客户端 Response.WriteFile(filet.FullName); // 停止页面的执行 Response.End(); } #endregion #region 权限设置 /// /// 菜单按钮权限 /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.UserId, BLL.Const.CQualityPunishMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnNew.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnEdit.Hidden = false; this.btnMenuEdit.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnDelete.Hidden = false; this.btnMenuDelete.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnOut)) { this.btnExport.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnSave)) { this.Grid1.EnableRowDoubleClickEvent = true; } else { this.Grid1.EnableRowDoubleClickEvent = false; } } } #endregion } }