Basf_TCC7/HJGL/FineUIPro.Web/WelderManage/WelderTestInfo.aspx.cs

654 lines
26 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
//定义变量
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 错误集合
/// </summary>
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();//加载树
}
}
/// <summary>
/// 加载树
/// </summary>
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<Model.Welder_Welder> welders = new List<Model.Welder_Welder>();
List<Model.Base_Unit> 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
/// <summary>
/// 绑定树节点
/// </summary>
/// <param name="node"></param>
private void BindNodes(TreeNode node, List<Model.Welder_Welder> 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
/// <summary>
/// 点击TreeView
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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<SqlParameter> listStr = new List<SqlParameter>();
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
/// <summary>
/// Grid行双击事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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);
}
}
/// <summary>
/// 删除
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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
/// <summary>
/// 判断是否可以删除
/// </summary>
/// <returns></returns>
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
/// <summary>
/// 模板下载
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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")));
}
/// <summary>
/// 下载导入模板
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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<Model.Welder_TestInfo> listData = new List<Model.Welder_TestInfo>();
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
/// <summary>
/// 弹出编辑窗体关闭事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Window1_Close(object sender, WindowCloseEventArgs e)
{
BindGrid();
}
#endregion
#region
/// <summary>
/// 分页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
{
Grid1.PageIndex = e.NewPageIndex;
BindGrid();
}
/// <summary>
/// 分页显示条数下拉框
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
BindGrid();
}
/// <summary>
/// 排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
{
Grid1.SortDirection = e.SortDirection;
Grid1.SortField = e.SortField;
BindGrid();
}
#endregion
#region
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void TextBox_TextChanged(object sender, EventArgs e)
{
this.InitTreeMenu();
this.BindGrid();
}
#endregion
#region
/// <summary>
/// 增加
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
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<Model.ViewModels.ImportErrorViewModel> ValidateImportFileds(DataTable dt)
{
var listData = new List<Model.ViewModels.ImportErrorViewModel>();
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;
}
}
}