xinjiang/SGGL/FineUIPro.Web/HJGL/PersonManage/TestApplicationDataIn.aspx.cs

696 lines
31 KiB
C#
Raw Normal View History

2024-11-19 09:45:27 +08:00
using BLL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.HJGL.PersonManage
{
public partial class TestApplicationDataIn : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 错误集合
/// </summary>
public static List<Model.ErrorInfo> errorInfos = new List<Model.ErrorInfo>();
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (errorInfos != null)
{
errorInfos.Clear();
}
}
}
#endregion
#region
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAudit_Click(object sender, EventArgs e)
{
try
{
if (this.FileExcel.HasFile == false)
{
Alert.ShowInTop("请您选择Excel文件!", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(FileExcel.FileName).ToString().Trim().ToLower();
if (IsXls != ".xls")
{
Alert.ShowInTop("只可以选择Excel文件!", MessageBoxIcon.Warning);
return;
}
if (errorInfos != null)
{
errorInfos.Clear();
}
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;
FileExcel.PostedFile.SaveAs(filePath);
ImportXlsToData(filePath);
}
catch (Exception ex)
{
Alert.ShowInTop(ex.Message);
}
}
#region Excel提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData(string fileName)
{
try
{
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += fileName;
oleDBConnString += ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = null;
OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataSet ds = new DataSet();
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
}
string sqlMaster;
sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
oleAdMaster.SelectCommand.CommandTimeout = 1200;
oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
AddDatasetToSQL(ds.Tables[0]);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集列数</param>
/// <returns></returns>
private bool AddDatasetToSQL(DataTable pds)
{
string result = string.Empty;
int ic, ir;
ic = pds.Columns.Count;
ir = pds.Rows.Count;
if (pds != null && ir > 0)
{
var conUnits = from x in Funs.DB.Base_Unit
join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId
where y.ProjectId == this.CurrUser.LoginProjectId
&& y.UnitType == BLL.Const.ProjectUnitType_2
select x;//施工单位
var checkUnits = from x in Funs.DB.Base_Unit
join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId
where y.ProjectId == this.CurrUser.LoginProjectId
&& y.UnitType == BLL.Const.ProjectUnitType_5
select x;//检测单位
var users = from x in Funs.DB.Sys_User
join y in Funs.DB.Project_ProjectUser on x.UserId equals y.UserId
where y.ProjectId == this.CurrUser.LoginProjectId
&& y.RoleId.Contains(BLL.Const.SubWeldingEngineer)
select x; //分包商焊接工程师
var workAreas = from x in Funs.DB.ProjectData_WorkArea where x.ProjectId == this.CurrUser.LoginProjectId select x;
var weldingMethods = from x in Funs.DB.Base_WeldingMethod select x;
for (int i = 0; i < ir; i++)
{
string col0 = pds.Rows[i][0].ToString().Trim();
if (!string.IsNullOrEmpty(col0))
{
var unit = conUnits.FirstOrDefault(e => e.UnitName == col0);
if (unit == null)
{
result += (i + 2).ToString() + "," + "施工单位" + "," + "[" + col0 + "]错误!" + "|";
}
}
else
{
result += (i + 2).ToString() + "," + "施工单位" + "," + "此项为必填项!" + "|";
}
string col1 = pds.Rows[i][1].ToString().Trim();
if (!string.IsNullOrEmpty(col1))
{
var user = users.FirstOrDefault(e => e.UserName == col1);
if (user == null)
{
result += (i + 2).ToString() + "," + "施工单位焊接工程师" + "," + "[" + col1 + "]错误!" + "|";
}
}
string col2 = pds.Rows[i][2].ToString().Trim();
if (!string.IsNullOrEmpty(col2))
{
try
{
DateTime d = Convert.ToDateTime(col2);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "证件有效期" + "," + "[" + col2 + "]错误!" + "|";
}
}
else
{
result += (i + 2).ToString() + "," + "证件有效期" + "," + "此项为必填项!" + "|";
}
string col3 = pds.Rows[i][3].ToString().Trim();
if (!string.IsNullOrEmpty(col3))
{
try
{
DateTime d = Convert.ToDateTime(col3);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "预考试日期" + "," + "[" + col3 + "]错误!" + "|";
}
}
else
{
result += (i + 2).ToString() + "," + "预考试日期" + "," + "此项为必填项!" + "|";
}
string col5 = pds.Rows[i][5].ToString().Trim();
if (!string.IsNullOrEmpty(col5))
{
if (col5 != "土建" && col5 != "结构" && col5 != "设备" && col5 != "管道")
{
result += (i + 2).ToString() + "," + "焊工类别" + "," + "[" + col5 + "]错误!" + "|";
}
}
else
{
result += (i + 2).ToString() + "," + "焊工类别" + "," + "此项为必填项!" + "|";
}
if (string.IsNullOrEmpty(pds.Rows[i][6].ToString().Trim()))
{
result += (i + 2).ToString() + "," + "焊工编号" + "," + "此项为必填项!" + "|";
}
if (string.IsNullOrEmpty(pds.Rows[i][7].ToString()))
{
result += (i + 2).ToString() + "," + "姓名" + "," + "此项为必填项!" + "|";
}
string col8 = pds.Rows[i][8].ToString().Trim();
if (!string.IsNullOrEmpty(col8))
{
if (col8.Length > 50)
{
result += (i + 2).ToString() + "," + "身份证号" + "," + "[" + col8 + "]错误!" + "|";
}
if (TestApplicationService.GetTestApplicationByTestDateAndIdentityCard(this.CurrUser.LoginProjectId, col3, col8) != null)
{
result += (i + 2).ToString() + "," + "身份证号" + "," + "[" + col8 + "]已存在!" + "|";
}
if (!IDCardValid.CheckIDCard(col8))
{
result += (i + 2).ToString() + "," + "身份证号" + "," + "[" + col8 + "]非法!" + "|";
}
}
else
{
result += (i + 2).ToString() + "," + "身份证号" + "," + "此项为必填项!" + "|";
}
string col10 = pds.Rows[i][10].ToString().Trim();
if (!string.IsNullOrEmpty(col10))
{
var weldMethod = weldingMethods.FirstOrDefault(e => e.WeldingMethodCode == col10);
if (weldMethod == null)
{
result += (i + 2).ToString() + "," + "焊接方法" + "," + "[" + col10 + "]错误!" + "|";
}
}
string col11 = pds.Rows[i][11].ToString().Trim();
if (!string.IsNullOrEmpty(col11))
{
if (col11 != "300×125×6mm" && col11 != "300×125×8mm" && col11 != "300×125×12mm" && col11 != "Ф22×3×125mm" && col11 != "Ф60.3×5.6×125mm" && col11 != "Ф89×7×125mm" && col11 != "Ф114×7×125mm")
{
result += (i + 2).ToString() + "," + "焊接试件尺寸" + "," + "[" + col11 + "]错误!" + "|";
}
}
string col12 = pds.Rows[i][12].ToString().Trim();
if (!string.IsNullOrEmpty(col12))
{
if (col12 != "FeII" && col12 != "FeIII" && col12 != "FeIV" && col12 != "Ni" && col12 != "Al")
{
result += (i + 2).ToString() + "," + "材质类别" + "," + "[" + col12 + "]错误!" + "|";
}
}
string col14 = pds.Rows[i][14].ToString().Trim();
if (!string.IsNullOrEmpty(col14))
{
try
{
decimal d = Convert.ToDecimal(col14);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "一次考试" + "," + "[" + col14 + "]错误!" + "|";
}
}
string col15 = pds.Rows[i][15].ToString().Trim();
if (!string.IsNullOrEmpty(col15))
{
try
{
decimal d = Convert.ToDecimal(col15);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "二次考试" + "," + "[" + col15 + "]错误!" + "|";
}
}
string col16 = pds.Rows[i][16].ToString().Trim();
if (!string.IsNullOrEmpty(col16))
{
if (col16 != "待考试" && col16 != "合格在岗" && col16 != "合格离岗" && col16 != "考试不合格退场")
{
result += (i + 2).ToString() + "," + "焊工状态" + "," + "[" + col16 + "]错误!" + "|";
}
}
else
{
result += (i + 2).ToString() + "," + "焊工状态" + "," + "此项为必填项!" + "|";
}
string col17 = pds.Rows[i][17].ToString().Trim();
if (!string.IsNullOrEmpty(col17))
{
try
{
DateTime d = Convert.ToDateTime(col17);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "日期" + "," + "[" + col17 + "]错误!" + "|";
}
}
string col19 = pds.Rows[i][19].ToString().Trim();
if (!string.IsNullOrEmpty(col19))
{
var area = workAreas.FirstOrDefault(e => e.WorkAreaCode == col19);
if (area == null)
{
result += (i + 2).ToString() + "," + "负责装置区域" + "," + "[" + col19 + "]错误!" + "|";
}
}
string col20 = pds.Rows[i][20].ToString().Trim();
if (!string.IsNullOrEmpty(col20))
{
try
{
DateTime d = Convert.ToDateTime(col20);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "焊工考试委托日期" + "," + "[" + col20 + "]错误!" + "|";
}
}
string col22 = pds.Rows[i][22].ToString().Trim();
if (!string.IsNullOrEmpty(col22))
{
try
{
DateTime d = Convert.ToDateTime(col22);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "检测日期" + "," + "[" + col22 + "]错误!" + "|";
}
}
string col24 = pds.Rows[i][24].ToString().Trim();
if (!string.IsNullOrEmpty(col24))
{
try
{
int d = Convert.ToInt32(col24);
}
catch (Exception)
{
result += (i + 2).ToString() + "," + "无损检测拍片数量" + "," + "[" + col24 + "]错误!" + "|";
}
}
string col25 = pds.Rows[i][25].ToString().Trim();
if (!string.IsNullOrEmpty(col25))
{
var unit = checkUnits.FirstOrDefault(e => e.UnitName == col25);
if (unit == null)
{
result += (i + 2).ToString() + "," + "检测单位" + "," + "[" + col25 + "]错误!" + "|";
}
}
}
if (!string.IsNullOrEmpty(result))
{
result = result.Substring(0, result.LastIndexOf("|"));
}
errorInfos.Clear();
if (!string.IsNullOrEmpty(result))
{
string results = result;
List<string> errorInfoList = results.Split('|').ToList();
foreach (var item in errorInfoList)
{
string[] errors = item.Split(',');
Model.ErrorInfo errorInfo = new Model.ErrorInfo();
errorInfo.Row = errors[0];
errorInfo.Column = errors[1];
errorInfo.Reason = errors[2];
errorInfos.Add(errorInfo);
}
if (errorInfos.Count > 0)
{
Grid1.DataSource = errorInfos;
Grid1.DataBind();
}
}
else
{
ShowNotify("审核完成,请点击保存!", MessageBoxIcon.Success);
}
}
else
{
throw new Exception("导入数据为空!");
}
return true;
}
#endregion
#region
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
if (errorInfos.Count <= 0)
{
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
if (!Directory.Exists(initFullPath))
{
Directory.CreateDirectory(initFullPath);
}
string filePath = initFullPath + this.hdfileName.Text;
ImportXlsToData2(filePath);
}
else
{
Alert.ShowInTop("请先将错误数据修正,再重新导入保存!", MessageBoxIcon.Warning);
}
}
#region Excel提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData2(string fileName)
{
try
{
string oleDBConnString = String.Empty;
oleDBConnString = "Provider=Microsoft.Jet.OLEDB.4.0;";
oleDBConnString += "Data Source=";
oleDBConnString += fileName;
oleDBConnString += ";Extended Properties=Excel 8.0;";
OleDbConnection oleDBConn = null;
OleDbDataAdapter oleAdMaster = null;
DataTable m_tableName = new DataTable();
DataSet ds = new DataSet();
oleDBConn = new OleDbConnection(oleDBConnString);
oleDBConn.Open();
m_tableName = oleDBConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
if (m_tableName != null && m_tableName.Rows.Count > 0)
{
m_tableName.TableName = m_tableName.Rows[0]["TABLE_NAME"].ToString().Trim();
}
string sqlMaster;
sqlMaster = " SELECT * FROM [" + m_tableName.TableName + "]";
oleAdMaster = new OleDbDataAdapter(sqlMaster, oleDBConn);
oleAdMaster.SelectCommand.CommandTimeout = 1200;
oleAdMaster.Fill(ds, "m_tableName");
oleAdMaster.Dispose();
oleDBConn.Close();
oleDBConn.Dispose();
AddDatasetToSQL2(ds.Tables[0]);
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集列数</param>
/// <returns></returns>
private bool AddDatasetToSQL2(DataTable pds)
{
string result = string.Empty;
var conUnits = from x in Funs.DB.Base_Unit
join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId
where y.ProjectId == this.CurrUser.LoginProjectId
&& y.UnitType == BLL.Const.ProjectUnitType_2
select x;//施工单位
var checkUnits = from x in Funs.DB.Base_Unit
join y in Funs.DB.Project_ProjectUnit on x.UnitId equals y.UnitId
where y.ProjectId == this.CurrUser.LoginProjectId
&& y.UnitType == BLL.Const.ProjectUnitType_5
select x;//检测单位
var users = from x in Funs.DB.Sys_User
join y in Funs.DB.Project_ProjectUser on x.UserId equals y.UserId
where y.ProjectId == this.CurrUser.LoginProjectId
&& y.RoleId.Contains(BLL.Const.SubWeldingEngineer)
select x; //分包商焊接工程师
var workAreas = from x in Funs.DB.ProjectData_WorkArea where x.ProjectId == this.CurrUser.LoginProjectId select x;
var weldingMethods = from x in Funs.DB.Base_WeldingMethod select x;
foreach (DataRow row in pds.Rows)
{
var oldTestApplication = BLL.TestApplicationService.GetTestApplicationByTestDateAndIdentityCard(this.CurrUser.LoginProjectId,row[3].ToString().Trim(), row[7].ToString().Trim());
if (oldTestApplication == null)
{
Model.Welder_TestApplication testApplication = new Model.Welder_TestApplication();
testApplication.ApplicationTestId = Guid.NewGuid().ToString();
testApplication.ProjectId = this.CurrUser.LoginProjectId;
string col0 = row[0].ToString().Trim();
string col1 = row[1].ToString().Trim();
string col2 = row[2].ToString().Trim();
string col3 = row[3].ToString().Trim();
string col4 = row[4].ToString().Trim();
string col5 = row[5].ToString().Trim();
string col6 = row[6].ToString().Trim();
string col7 = row[7].ToString().Trim();
string col8 = row[8].ToString().Trim();
string col9 = row[9].ToString().Trim();
string col10 = row[10].ToString().Trim();
string col11 = row[11].ToString().Trim();
string col12 = row[12].ToString().Trim();
string col13 = row[13].ToString().Trim();
string col14 = row[14].ToString().Trim();
string col15 = row[15].ToString().Trim();
string col16 = row[16].ToString().Trim();
string col17 = row[17].ToString().Trim();
string col18 = row[18].ToString().Trim();
string col19 = row[19].ToString().Trim();
string col20 = row[20].ToString().Trim();
string col21 = row[21].ToString().Trim();
string col22 = row[22].ToString().Trim();
string col23 = row[23].ToString().Trim();
string col24 = row[24].ToString().Trim();
string col25 = row[25].ToString().Trim();
var unit = conUnits.FirstOrDefault(e => e.UnitName == col0);
if (unit != null)
{
testApplication.ConUnit = unit.UnitId;
}
var user = users.FirstOrDefault(e => e.UserName == col1);
if (user != null)
{
testApplication.ConUnitWeldingEngineer = user.UserId;
}
testApplication.CertificateValidity = Funs.GetNewDateTime(col2);
testApplication.PreTestDate = Funs.GetNewDateTime(col3);
testApplication.OwnerNum = col4;
testApplication.WelderClass = col5;
testApplication.WelderCode = col6;
testApplication.WelderName = col7;
testApplication.IdentityCard = col8;
testApplication.WeldingPosition = col9;
var weldMethod = weldingMethods.FirstOrDefault(e => e.WeldingMethodCode == col10);
if (weldMethod != null)
{
testApplication.WeldingMethod = weldMethod.WeldingMethodId;
}
testApplication.SpecimenSize = col11;
testApplication.MaterialCategory = col12;
testApplication.AppearanceEvaluation = col13;
testApplication.FirstExamination = Funs.GetNewDecimal(col14);
testApplication.SecondExamination = Funs.GetNewDecimal(col15);
testApplication.WelderCondition = col16;
testApplication.ExamDate = Funs.GetNewDateTime(col17);
testApplication.InvigilatorConfirmed = col18;
var area = workAreas.FirstOrDefault(e => e.WorkAreaCode == col19);
if (area != null)
{
testApplication.Area = area.WorkAreaId;
}
testApplication.ExamTrustDate = Funs.GetNewDateTime(col20);
testApplication.ExamTrustCode = col21;
testApplication.CheckDate = Funs.GetNewDateTime(col22);
testApplication.CheckResult = col23;
testApplication.NDTFilm = Funs.GetNewInt(col24);
var checkUnit = checkUnits.FirstOrDefault(e => e.UnitName == col25);
if (checkUnit != null)
{
testApplication.CheckUnit = checkUnit.UnitId;
}
Funs.DB.Welder_TestApplication.InsertOnSubmit(testApplication);
}
}
Funs.DB.SubmitChanges();
ShowNotify("导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
return true;
}
#endregion
#region
/// <summary>
/// 导出错误提示
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnOut_Click(object sender, EventArgs e)
{
//string strFileName = DateTime.Now.ToString("yyyyMMdd-hhmmss");
//System.Web.HttpContext HC = System.Web.HttpContext.Current;
//HC.Response.Clear();
//HC.Response.Buffer = true;
//HC.Response.ContentEncoding = System.Text.Encoding.UTF8;//设置输出流为简体中文
////---导出为Excel文件
//HC.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls");
//HC.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
//System.IO.StringWriter sw = new System.IO.StringWriter();
//System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);
//this.gvErrorInfo.RenderControl(htw);
//HC.Response.Write(sw.ToString());
//HC.Response.End();
}
/// <summary>
/// 重载VerifyRenderingInServerForm方法否则运行的时候会出现如下错误提示“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
/// </summary>
/// <param name="control"></param>
public override void VerifyRenderingInServerForm(Control control)
{
}
#endregion
#region
/// <summary>
/// 下载模板按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnDownLoad_Click(object sender, EventArgs e)
{
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.HJGL_TestApplicationTemplateUrl;
string filePath = Const.HJGL_TestApplicationTemplateUrl;
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
}
}