SGGL_SHJ/SGGL/FineUIPro.Web/HJGL/PreDesign/PrePipelineQRCodeIn.aspx.cs

448 lines
18 KiB
C#
Raw Normal View History

2022-09-05 16:36:31 +08:00
using BLL;
using MiniExcelLibs;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.HJGL.PreDesign
{
public partial class PrePipelineQRCodeIn : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 安装组件集合
/// </summary>
public static List<Model.HJGL_Pipeline> PipelineList = new List<Model.HJGL_Pipeline>();
public static List<Model.HJGL_Pipeline_Component> Pipeline_ComponentList = new List<Model.HJGL_Pipeline_Component>();
/// <summary>
/// 错误集合
/// </summary>
public static string errorInfos = string.Empty;
public string UnitWorkId
{
get
{
return (string)ViewState["UnitWorkId"];
}
set
{
ViewState["UnitWorkId"] = value;
}
}
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.hdFileName.Text = string.Empty;
UnitWorkId = Request.Params["UnitWorkId"];
if (PipelineList != null)
{
PipelineList.Clear();
}
if (Pipeline_ComponentList != null)
{
Pipeline_ComponentList.Clear();
}
errorInfos = string.Empty;
2022-09-05 16:36:31 +08:00
}
}
#endregion
#region
/// <summary>
/// 审核
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnAudit_Click(object sender, EventArgs e)
{
try
{
if (this.fuAttachUrl.HasFile == false)
{
ShowNotify("请您选择Excel文件", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
if (IsXls != ".xlsx")
{
ShowNotify("只可以选择Excel文件", MessageBoxIcon.Warning);
return;
}
if (PipelineList != null)
{
PipelineList.Clear();
}
if (!string.IsNullOrEmpty(errorInfos))
{
errorInfos = string.Empty;
}
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;
this.fuAttachUrl.PostedFile.SaveAs(filePath);
ImportXlsToData(rootPath + initPath + this.hdFileName.Text);
}
catch (Exception ex)
{
2022-12-18 22:40:49 +08:00
Alert alert = new Alert
{
Message = "'" + ex.Message + "'",
Target = Target.Self
};
alert.Show();
//ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning);
2022-09-05 16:36:31 +08:00
}
}
#region Excel提取数据
/// <summary>
/// 从Excel提取数据--》Dataset
/// </summary>
/// <param name="filename">Excel文件路径名</param>
private void ImportXlsToData(string fileName)
{
// var rows =Funs.LINQToDataTable(MiniExcel.Query(fileName).ToList()) ;
2022-09-05 16:36:31 +08:00
var rows = MiniExcel.QueryAsDataTable(fileName, useHeaderRow: true);
Model.ResponeData responeData = new Model.ResponeData();
responeData = AddDatasetToSQL(rows, 3);
if (responeData.code == 1)
2022-09-05 16:36:31 +08:00
{
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
else
{
2022-12-18 22:40:49 +08:00
Alert alert = new Alert
{
Message = responeData.message,
Target = Target.Self
};
alert.Show();
//ShowNotify(responeData.message, MessageBoxIcon.Success);
2022-09-05 16:36:31 +08:00
}
}
#endregion
#region Dataset的数据导入数据库
/// <summary>
/// 将Dataset的数据导入数据库
/// </summary>
/// <param name="pds">数据集</param>
/// <param name="Cols">数据集行数</param>
/// <returns></returns>
private Model.ResponeData AddDatasetToSQL(DataTable pds, int Cols)
{
Model.ResponeData responeData = new Model.ResponeData();
//string result = string.Empty;
List<string> result = new List<string>();
2022-09-05 16:36:31 +08:00
int ic, ir;
ic = pds.Columns.Count;
ir = pds.Rows.Count;
if (ic < Cols)
{
responeData.code = 0;
responeData.message = "导入Excel格式错误Excel只有" + ic.ToString().Trim() + "列";
return responeData;
}
if (pds != null && ir > 0)
{
var getPipeline = from x in Funs.DB.HJGL_Pipeline where x.ProjectId == this.CurrUser.LoginProjectId select x;
for (int i = 0; i < ir; i++)
{
Model.HJGL_Pipeline pipeline = new Model.HJGL_Pipeline();
Model.HJGL_Pipeline_Component hJGL_Pipeline_Component = new Model.HJGL_Pipeline_Component();
string pipelinecode = pds.Rows[i]["管线号"].ToString();
2022-09-05 16:36:31 +08:00
string PipelineComponentCode = pds.Rows[i]["预制组件"].ToString();
string QRCode = pds.Rows[i]["二维码信息"].ToString();
if (string.IsNullOrEmpty(pipelinecode))
2022-09-05 16:36:31 +08:00
{
//result += "第" + (i + 2).ToString() + "行," + "管线号" + "," + "此项为必填项!" + "|";
result.Add("第" + (i + 2).ToString() + "行," + "管线号" + "," + "此项为必填项!");
2022-09-05 16:36:31 +08:00
}
else
{
pipeline = getPipeline.FirstOrDefault(x => x.PipelineCode == pipelinecode);
2022-09-05 16:36:31 +08:00
if (pipeline == null)
{
//result += "第" + (i + 2).ToString() + "行," + "不存在此管线号" + "|";
result.Add("不存在此管线号-" + pipelinecode);
2022-09-05 16:36:31 +08:00
continue;
}
}
if (string.IsNullOrEmpty(PipelineComponentCode))
{
//result += "第" + (i + 2).ToString() + "行," + "预制组件" + "," + "此项为必填项!" + "|";
result.Add("第" + (i + 2).ToString() + "行," + "预制组件" + "," + "此项为必填项!");
2022-09-05 16:36:31 +08:00
}
else
{
hJGL_Pipeline_Component = HJGL_PipelineComponentService.GetPipelineComponentByCodeandpipelineId(PipelineComponentCode, pipeline.PipelineId);
if (hJGL_Pipeline_Component == null)
2022-09-05 16:36:31 +08:00
{
//result += "第" + (i + 2).ToString() + "行," + "不存在此预制组件 " + PipelineComponentCode + " |";
result.Add("不存在此预制组件-" + PipelineComponentCode);
2022-09-05 16:36:31 +08:00
continue;
}
}
}
if (result.Count > 0)
2022-09-05 16:36:31 +08:00
{
PipelineList.Clear();
// result = result.Substring(0, result.LastIndexOf("|"));
errorInfos = string.Join("|", result.Distinct());
2022-09-05 16:36:31 +08:00
//Alert alert = new Alert();
//alert.Message = result;
//alert.Target = Target.Self;
//alert.Show();
responeData.code = 0;
responeData.message = errorInfos;
}
else
{
var dt = pds.AsEnumerable()
.GroupBy(x => new
{
2022-09-05 16:36:31 +08:00
pipelinecode = x.Field<string>("管线号"),
2022-09-23 22:29:48 +08:00
PipelineComponentCode = x.Field<string>("预制组件"),
// DrawingName = x.Field<string>("预制图纸名称"),
2022-09-05 16:36:31 +08:00
})
.Select(x => new
{
pipelinecode = x.Key.pipelinecode,
PipelineComponentCode = x.Key.PipelineComponentCode,
// DrawingName = x.Key.DrawingName,
2022-11-09 16:57:01 +08:00
QRCode = String.Join(";", x.Select(z => z.Field<string>("二维码信息")))
2022-09-05 16:36:31 +08:00
});
foreach (var item in dt)
{
var pipeline = PipelineService.GetPipelineByPipelineCode(this.CurrUser.LoginProjectId, item.pipelinecode.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""));
var hJGL_Pipeline_Component = HJGL_PipelineComponentService.GetPipelineComponentByCodeandpipelineId(item.PipelineComponentCode.Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", ""), pipeline.PipelineId);
if (hJGL_Pipeline_Component != null)
2022-09-05 16:36:31 +08:00
{
2022-11-09 16:57:01 +08:00
//model.Pipeline_ComponentQRCodeRootName rootName = new model.Pipeline_ComponentQRCodeRootName();
//rootName.QRCode = new List<model.Pipeline_ComponentQRCodeQRCodeItem>();
//var elementlist = item.QRCode.Split(';');
//foreach (var element in elementlist)
//{
// model.Pipeline_ComponentQRCodeQRCodeItem qRCodeQRCodeItem = new model.Pipeline_ComponentQRCodeQRCodeItem();
// qRCodeQRCodeItem.element = element;
// rootName.QRCode.Add(qRCodeQRCodeItem);
//}
//hJGL_Pipeline_Component.QRCode = JsonConvert.SerializeObject(rootName);
hJGL_Pipeline_Component.QRCode = item.QRCode;
2023-02-20 10:49:51 +08:00
Pipeline_ComponentList.Add(hJGL_Pipeline_Component);
2022-09-05 16:36:31 +08:00
}
2022-09-05 16:36:31 +08:00
}
errorInfos = string.Empty;
// ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
}
else
{
responeData.code = 0;
responeData.message = "导入数据为空!";
}
return responeData;
}
#endregion
#endregion
#region
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnImport_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(errorInfos))
{
if (!string.IsNullOrEmpty(this.hdFileName.Text))
{
if (Pipeline_ComponentList.Count > 0)
{
this.Grid1.Hidden = false;
this.Grid1.DataIDField = "PipelineComponentId";
this.Grid1.DataSource = Pipeline_ComponentList;
this.Grid1.DataBind();
Grid1.RecordCount = Pipeline_ComponentList.Count;
}
}
else
{
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
#endregion
#region
/// <summary>
/// 提交
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(errorInfos))
{
if (Pipeline_ComponentList.Count > 0)
{
foreach (var item in Pipeline_ComponentList)
{
BLL.HJGL_PipelineComponentService.UpdatePipelineComponent(item);
2023-02-20 10:49:51 +08:00
HJGL_PipelineComponentjointService.DeleteHJGL_Pipeline_ComponentJointByPipelineComponentId(item.PipelineComponentId);
var QRCodeList = item.QRCode.Split(';').ToList();
foreach (var QRCodeitem in QRCodeList)
{
var joint = BLL.WeldJointService.GetWeldJointsByWeldJointCode(item.PipelineId, QRCodeitem.Substring(1));
if (joint != null)
{
var ComponetJoint = BLL.HJGL_PipelineComponentjointService.GetHJGL_Pipeline_ComponentJointByWeldJointId(joint.WeldJointId);
if (ComponetJoint != null)
2023-02-20 10:49:51 +08:00
{
ComponetJoint.PipelineComponentCode = item.PipelineComponentCode;
2023-02-20 10:49:51 +08:00
ComponetJoint.PipelineComponentId = item.PipelineComponentId;
HJGL_PipelineComponentjointService.UpdateHJGL_Pipeline_ComponentJoint(ComponetJoint);
}
else
{
Model.HJGL_Pipeline_ComponentJoint hJGL_Pipeline_ComponentJoint = new Model.HJGL_Pipeline_ComponentJoint();
hJGL_Pipeline_ComponentJoint.Id = SQLHelper.GetNewID();
2023-06-19 18:02:04 +08:00
hJGL_Pipeline_ComponentJoint.PipelineComponentCode = item.PipelineComponentCode;
hJGL_Pipeline_ComponentJoint.PipelineComponentId = item.PipelineComponentId;
hJGL_Pipeline_ComponentJoint.WeldJointCode = joint.WeldJointCode;
hJGL_Pipeline_ComponentJoint.WeldJointId = joint.WeldJointId;
2025-02-18 17:38:49 +08:00
hJGL_Pipeline_ComponentJoint.State = 0;
2023-06-19 18:02:04 +08:00
HJGL_PipelineComponentjointService.AddHJGL_Pipeline_ComponentJoint(hJGL_Pipeline_ComponentJoint);
2023-02-20 10:49:51 +08:00
}
2023-02-20 10:49:51 +08:00
}
}
2022-09-05 16:36:31 +08:00
}
}
int a = PipelineList.Count();
string rootPath = Server.MapPath("~/");
string initFullPath = rootPath + initPath;
string filePath = initFullPath + this.hdFileName.Text;
if (filePath != string.Empty && System.IO.File.Exists(filePath))
{
System.IO.File.Delete(filePath);//删除上传的XLS文件
}
ShowNotify("导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
#endregion
#region
public string ConvertPipelineId(object PipelineId)
{
string StateName = string.Empty;
if (!string.IsNullOrEmpty(PipelineId.ToString()))
{
if (PipelineId != null)
{
string txt = PipelineService.GetPipelineByPipelineId(PipelineId.ToString()).PipelineCode;
return txt;
}
return "";
}
return StateName;
}
#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.Pipeline_ComponentPath;
string filePath = Const.Pipeline_ComponentPath;
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
#region
/// <summary>
/// 分页选择下拉改变事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
this.Grid1.DataSource = PipelineList;
this.Grid1.DataBind();
Grid1.RecordCount = PipelineList.Count;
}
#endregion
}
}