using BLL; using NPOI.POIFS.Properties; using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; namespace FineUIPro.Web.HSSE.EduTrain { public partial class TestTraining : PageBase { /// /// 加载页面 /// /// /// protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Funs.DropDownPageSize(this.ddlPageSize); this.GetButtonPower(); this.InitTreeMenu(); } } /// /// 初始化树 /// private void InitTreeMenu() { tvTestTraining.Nodes.Clear(); tvTestTraining.ShowBorder = false; tvTestTraining.ShowHeader = false; tvTestTraining.EnableIcons = true; tvTestTraining.AutoScroll = true; tvTestTraining.EnableSingleClickExpand = true; TreeNode rootNode = new TreeNode { Text = "考试试题库", NodeID = "0", Expanded = true }; this.tvTestTraining.Nodes.Add(rootNode); BoundTree(rootNode.Nodes, rootNode.NodeID); } private void BoundTree(TreeNodeCollection nodes, string parentId) { var dt = GetNewTraining(parentId); if (dt.Count() > 0) { TreeNode tn = null; foreach (var dr in dt) { string name = dr.TrainingName; if (!string.IsNullOrEmpty(dr.TrainingCode)) { name = "[" + dr.TrainingCode + "]" + dr.TrainingName; } tn = new TreeNode { Text = name, NodeID = dr.TrainingId, EnableClickEvent = true, ToolTip = dr.TrainingName }; nodes.Add(tn); ///是否存在下级节点 var sup = Funs.DB.Training_TestTraining.FirstOrDefault(x => x.SupTrainingId == tn.NodeID); if (sup != null) { BoundTree(tn.Nodes, tn.NodeID); } } } } /// /// 得到培训类型 /// /// /// private List GetNewTraining(string parentId) { return (from x in Funs.DB.Training_TestTraining where x.SupTrainingId == parentId orderby x.TrainingCode select x).ToList(); } //protected void btnNew_Click(object sender, EventArgs e) //{ // PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TrainingSave.aspx", "编辑 - "))); //} protected void btnMenuADD_Click(object sender, EventArgs e) { if (this.tvTestTraining.SelectedNode != null) { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TestTrainingSave.aspx?SupTrainingId={0}", this.tvTestTraining.SelectedNode.NodeID, "新增 - "))); } else { ShowNotify("请选择树节点!", MessageBoxIcon.Warning); } } protected void btnTreeMenuEdit_Click(object sender, EventArgs e) { if (this.tvTestTraining.SelectedNode != null) { if (this.tvTestTraining.SelectedNode.NodeID != "0") //非根节点可以编辑 { PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("TestTrainingSave.aspx?TrainingId={0}", this.tvTestTraining.SelectedNode.NodeID, "编辑 - "))); } else { ShowNotify("根节点无法编辑!", MessageBoxIcon.Warning); } } else { ShowNotify("请选择树节点!", MessageBoxIcon.Warning); } } protected void btnTreeMenuDelete_Click(object sender, EventArgs e) { if (this.tvTestTraining.SelectedNode != null && this.tvTestTraining.SelectedNodeID != "0") { var edu = Funs.DB.Training_TestTraining.FirstOrDefault(x => x.SupTrainingId == this.tvTestTraining.SelectedNode.NodeID); if (edu == null) { BLL.TestTrainingService.DeleteTestTrainingById(this.tvTestTraining.SelectedNode.NodeID); InitTreeMenu(); Grid1.DataSource = null; Grid1.DataBind(); } else { ShowNotify("存在子目录,不能删除!", MessageBoxIcon.Warning); } } else { ShowNotify("请选择删除项!", MessageBoxIcon.Warning); } } protected void tvTestTraining_NodeCommand(object sender, FineUIPro.TreeCommandEventArgs e) { BindGrid(); } #region BindGrid private void BindGrid() { if (this.tvTestTraining.SelectedNode != null && !string.IsNullOrEmpty(this.tvTestTraining.SelectedNode.NodeID)) { string trainingId = this.tvTestTraining.SelectedNode.NodeID; DataTable tb = BindData(trainingId); Grid1.RecordCount = tb.Rows.Count; var table = this.GetPagedDataTable(Grid1, tb); Grid1.DataSource = table; Grid1.DataBind(); } } /// /// 数据加载 /// /// /// public DataTable BindData(string trainingId) { string strSql = @"SELECT TrainingItemId,TrainingId,TrainingItemCode,TrainingItemName,Abstracts,AttachUrl,VersionNum,TestType " + @" ,(CASE WHEN TestType = '1' THEN '单选题' WHEN TestType = '2' THEN '多选题' ELSE '判断题' END) AS TestTypeName " + @" ,(CASE WHEN WorkPostNames IS NULL THEN '通用' ELSE WorkPostNames END) AS WorkPostNames,AItem,BItem,CItem,DItem,EItem,AnswerItems " + @" FROM dbo.Training_TestTrainingItem" + @" WHERE TrainingId=@TrainingId "; List listStr = new List { new SqlParameter("@TrainingId", trainingId) }; if (!string.IsNullOrEmpty(this.txtName.Text.Trim())) { strSql += " AND (TrainingItemCode LIKE @Name OR TrainingItemName LIKE @Name OR Abstracts LIKE @Name OR WorkPostNames LIKE @Name)"; listStr.Add(new SqlParameter("@Name", "%" + this.txtName.Text.Trim() + "%")); } if (this.drptype.SelectedValue != "0") { strSql += " AND TestType=@TestType"; listStr.Add(new SqlParameter("@TestType", this.drptype.SelectedValue)); } if (this.ckIsItem.Checked) { strSql += " AND (AItem IS NULL OR BItem IS NULL OR AItem ='' OR BItem ='')"; } SqlParameter[] parameter = listStr.ToArray(); DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter); return tb; } #endregion #region Events protected void Window1_Close(object sender, EventArgs e) { this.InitTreeMenu(); } protected void Window2_Close(object sender, EventArgs e) { this.BindGrid(); } protected void Window3_Close(object sender, EventArgs e) { this.InitTreeMenu(); this.BindGrid(); } /// /// 右键删除事件 /// /// /// 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(); var getD = BLL.TestTrainingItemService.GetTestTrainingItemById(rowID); if (getD != null) { BLL.LogService.AddSys_Log(this.CurrUser, getD.TrainingItemCode, getD.TrainingItemId, BLL.Const.TestTrainingMenuId, BLL.Const.BtnDelete); BLL.TestTrainingItemService.DeleteTestTrainingItemById(rowID); } } BindGrid(); ShowNotify("删除数据成功!"); } } #endregion #region GV排序页面 /// /// /// /// /// 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 /// /// 编辑试题 /// /// /// protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e) { this.EditData(); } /// /// 右键编辑事件 /// /// /// protected void btnMenuEdit_Click(object sender, EventArgs e) { this.EditData(); } /// /// 编辑数据方法 /// private void EditData() { if (Grid1.SelectedRowIndexArray.Length == 0) { Alert.ShowInTop("请至少选择一条记录!", MessageBoxIcon.Warning); return; } string trainingItemId = Grid1.SelectedRowID; PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("TestTrainingItemSave.aspx?TrainingItemId={0}", trainingItemId, "编辑 - "))); } /// /// 增加 /// /// /// protected void btnNewDetail_Click(object sender, EventArgs e) { if (this.tvTestTraining.SelectedNode != null) { string id = this.tvTestTraining.SelectedNodeID; var testTrain = BLL.TestTrainingService.GetTestTrainingById(id); if (testTrain != null && testTrain.IsEndLever == true) { PageContext.RegisterStartupScript(Window2.GetShowReference(String.Format("TestTrainingItemSave.aspx?TrainingId={0}", this.tvTestTraining.SelectedNode.NodeID, "编辑 - "))); } else { ShowNotify("请选择末级树节点!", MessageBoxIcon.Warning); } } else { ShowNotify("请选择树节点!", MessageBoxIcon.Warning); } } #region 按钮权限 /// /// 获取按钮权限 /// /// /// private void GetButtonPower() { var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.TestTrainingMenuId); if (buttonList.Count() > 0) { if (buttonList.Contains(BLL.Const.BtnAdd)) { this.btnMenuADD.Hidden = false; this.btnImport.Hidden = false; this.btnNewDetail.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnModify)) { this.btnTreeMenuEdit.Hidden = false; this.btnMenuEdit.Hidden = false; } if (buttonList.Contains(BLL.Const.BtnDelete)) { this.btnTreeMenuDelete.Hidden = false; this.btnMenuDelete.Hidden = false; } } //if (this.CurrUser.UserId == BLL.Const.sysglyId) //{ // this.btnRefresh.Hidden = false; // this.btnRefresh1.Hidden = false; //} } #endregion /// /// 查询 /// /// /// protected void TextBox_TextChanged(object sender, EventArgs e) { this.BindGrid(); } #region 导入 /// /// 导入按钮 /// /// /// protected void btnImport_Click(object sender, EventArgs e) { PageContext.RegisterStartupScript(Window3.GetShowReference(String.Format("TestTrainingItemIn.aspx", "导入 - "))); } #endregion #region 导出按钮 /// /// 导出按钮 /// /// /// protected void btnOut_Click(object sender, EventArgs e) { string rootPath = Server.MapPath("~/"); string initTemplatePath = Const.TestTrainingTemplateUrl; string uploadfilepath = string.Empty; string newUrl = string.Empty; uploadfilepath = rootPath + initTemplatePath; string trainingId = string.Empty; if (this.tvTestTraining.SelectedNode != null && !string.IsNullOrEmpty(this.tvTestTraining.SelectedNode.NodeID)) { trainingId = this.tvTestTraining.SelectedNode.NodeID; } else { ShowNotify("请选择试题类型!", MessageBoxIcon.Warning); return; } var testTraining = Funs.DB.Training_TestTraining.FirstOrDefault(x => x.TrainingId == trainingId); var lists = (from x in Funs.DB.Training_TestTrainingItem where x.TrainingId == trainingId select x); string tName = this.txtName.Text.Trim(); if (!string.IsNullOrEmpty(tName)) { lists = lists.Where(x => x.TrainingItemCode.Contains(tName) || x.TrainingItemName.Contains(tName) || x.Abstracts.Contains(tName) || x.WorkPostNames.Contains(tName)); } if (this.drptype.SelectedValue != "0") { lists = lists.Where(x => x.TestType == this.drptype.SelectedValue); } if (this.ckIsItem.Checked) { lists = lists.Where(x => string.IsNullOrWhiteSpace(x.AItem) == true || string.IsNullOrWhiteSpace(x.BItem) == true); } lists = lists.OrderByDescending(x => x.TrainingItemCode); if (lists.Any()) { //string projectName = BLL.ProjectService.GetShortNameByProjectId(this.CurrUser.LoginProjectId); newUrl = uploadfilepath.Replace("考试试题模版", $"{testTraining.TrainingName}考试试题({DateTime.Now.ToString("yyyyMMddHHmmss")})"); if (File.Exists(newUrl)) { File.Delete(newUrl); } File.Copy(uploadfilepath, newUrl); // 第一步:读取文件流 NPOI.SS.UserModel.IWorkbook workbook; using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read)) { //workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream); workbook = new NPOI.HSSF.UserModel.HSSFWorkbook(stream); } // 创建单元格样式 NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle.WrapText = true;//自动换行 var font = workbook.CreateFont(); font.FontHeightInPoints = 11; cellStyle.SetFont(font); // 第二步:创建新数据行 NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); NPOI.SS.UserModel.IRow row = sheet.GetRow(0); NPOI.SS.UserModel.ICell cell; int i = 2; foreach (var item in lists) { // 第二步:创建新数据行 row = sheet.CreateRow(i); // 添加数据 cell = row.CreateCell(0); cell.CellStyle = cellStyle; cell.SetCellValue(testTraining.TrainingCode);//试题类型编号 cell = row.CreateCell(1); cell.CellStyle = cellStyle; cell.SetCellValue(testTraining.TrainingName);//试题类型 cell = row.CreateCell(2); cell.CellStyle = cellStyle; cell.SetCellValue(item.TrainingItemCode);//试题编号 string testTypeName = item.TestType == "1" ? "单选题" : item.TestType == "2" ? "多选题" : "判断题"; cell = row.CreateCell(3); cell.CellStyle = cellStyle; cell.SetCellValue(testTypeName);//题型 cell = row.CreateCell(4); cell.CellStyle = cellStyle; cell.SetCellValue(item.WorkPostNames);//适合岗位 cell = row.CreateCell(5); cell.CellStyle = cellStyle; cell.SetCellValue(item.Abstracts);//试题内容 cell = row.CreateCell(6); cell.CellStyle = cellStyle; cell.SetCellValue(item.AItem);//答案项A cell = row.CreateCell(7); cell.CellStyle = cellStyle; cell.SetCellValue(item.BItem);//答案项B cell = row.CreateCell(8); cell.CellStyle = cellStyle; cell.SetCellValue(item.CItem);//答案项C cell = row.CreateCell(9); cell.CellStyle = cellStyle; cell.SetCellValue(item.DItem);//答案项D cell = row.CreateCell(10); cell.CellStyle = cellStyle; cell.SetCellValue(item.EItem);//答案项E cell = row.CreateCell(11); cell.CellStyle = cellStyle; cell.SetCellValue(item.AnswerItems);//正确答案 i++; } // 第三步:写入文件流 using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write)) { workbook.Write(stream); workbook.Close(); } string fileName = Path.GetFileName(newUrl); FileInfo info = new FileInfo(newUrl); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)); Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(newUrl, 0, fileSize); Response.Flush(); Response.Close(); File.Delete(newUrl); } else { Alert.ShowInTop("当前无记录,无法导出!", MessageBoxIcon.Warning); } } #endregion #region 刷新装置 /// /// 刷新 /// /// /// protected void Refresh_Click(object sender, EventArgs e) { var trainingItems = from x in Funs.DB.Training_TestTrainingItem select x; if (trainingItems.Count() > 0) { foreach (var trainingItem in trainingItems) { if (trainingItem.TestType == "3" && string.IsNullOrEmpty(trainingItem.AItem)) { trainingItem.AItem = "对"; trainingItem.BItem = "错"; } if (!string.IsNullOrEmpty(trainingItem.WorkPostIds)) { string name = string.Empty; var workPostList = Funs.GetStrListByStr(trainingItem.WorkPostIds, ','); if (workPostList.Count() > 0) { foreach (var workPostId in workPostList) { var workPost = BLL.WorkPostService.GetWorkPostById(workPostId).WorkPostName; if (!string.IsNullOrEmpty(workPost)) { name += workPost + ","; } } } if (!string.IsNullOrEmpty(name)) { trainingItem.WorkPostNames = name.Substring(0, name.LastIndexOf(",")); } } BLL.TestTrainingItemService.UpdateTestTrainingItem(trainingItem); } } Alert.ShowInTop("操作完成!", MessageBoxIcon.Success); } /// /// 刷新 /// /// /// protected void Refresh1_Click(object sender, EventArgs e) { var trainingItems = from x in Funs.DB.Training_TestTrainingItem select x; if (trainingItems.Count() > 0) { foreach (var trainingItem in trainingItems) { if (trainingItem.TestType == "3" && string.IsNullOrEmpty(trainingItem.AItem)) { trainingItem.AItem = "对"; trainingItem.BItem = "错"; } if (!string.IsNullOrEmpty(trainingItem.WorkPostNames)) { string getInstallationId = string.Empty; var installList = Funs.GetStrListByStr(trainingItem.WorkPostNames, ','); if (installList.Count() > 0) { foreach (var installItem in installList) { var install = Funs.DB.Base_WorkPost.FirstOrDefault(x => x.WorkPostName == installItem); if (install != null && !string.IsNullOrEmpty(install.WorkPostId)) { getInstallationId += install.WorkPostId + ","; } } } if (!string.IsNullOrEmpty(getInstallationId)) { trainingItem.WorkPostIds = getInstallationId.Substring(0, getInstallationId.LastIndexOf(",")); } } BLL.TestTrainingItemService.UpdateTestTrainingItem(trainingItem); } } Alert.ShowInTop("操作完成!", MessageBoxIcon.Success); } #endregion /// /// 显示选项不全的题目 /// /// /// protected void ckIsItem_CheckedChanged(object sender, CheckedEventArgs e) { this.BindGrid(); } } }