using BLL;
using FineUIPro.Web.HJGL.WeldingManage;
using MiniExcelLibs;
using Model;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.PHTGL.Filing
{
public partial class BidDocumentsStandingBookIn : PageBase
{
#region 定义变量
///
/// 上传预设的虚拟路径
///
private string initPath = Const.ExcelUrl;
///
/// 安装组件集合
///
public static List BidDocStandingBookList = new List();
///
/// 错误集合
///
public static string errorInfos = string.Empty;
public string Type
{
get
{
return (string)ViewState["Type"];
}
set
{
ViewState["Type"] = value;
}
}
#endregion
#region 加载
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.hdFileName.Text = string.Empty;
if (BidDocStandingBookList != null)
{
BidDocStandingBookList.Clear();
}
errorInfos = string.Empty;
Type = Request.Params["Type"];
}
}
#endregion
#region 审核
///
/// 审核
///
///
///
protected void btnAudit_Click(object sender, EventArgs e)
{
try
{
Grid1.DataSource = null;
Grid1.DataBind();
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 (BidDocStandingBookList != null)
{
BidDocStandingBookList.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);
if (string.IsNullOrEmpty(errorInfos))
{
if (!string.IsNullOrEmpty(this.hdFileName.Text))
{
if (BidDocStandingBookList.Count > 0)
{
this.Grid1.Hidden = false;
this.Grid1.DataIDField = "BidDocumentsStandingBookId";
this.Grid1.DataSource = BidDocStandingBookList;
this.Grid1.DataBind();
Grid1.RecordCount = BidDocStandingBookList.Count;
}
}
else
{
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
catch (Exception ex)
{
Alert alert = new Alert
{
Message = "'" + ex.Message + "'",
Target = Target.Self
};
alert.Show();
//ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning);
}
}
#region 读Excel提取数据
///
/// 从Excel提取数据--》Dataset
///
/// Excel文件路径名
private void ImportXlsToData(string fileName)
{
// var rows =Funs.LINQToDataTable(MiniExcel.Query(fileName).ToList()) ;
var ds = MiniExcel.Query(fileName).ToList();
var columns = MiniExcel.GetColumns(fileName);
var cnt = columns.Count;
var reposedata = AddDatasetToSQL(ds, cnt);
if (reposedata.code == 1)
{
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
}
else
{
Alert alert = new Alert
{
Message = reposedata.message,
Target = Target.Self
};
alert.Show();
//ShowNotify(responeData.message, MessageBoxIcon.Success);
}
}
#endregion
#region 将Dataset的数据导入数据库
///
/// 将Dataset的数据导入数据库
///
/// 数据集
/// 数据集行数
///
private Model.ResponeData AddDatasetToSQL(List pds, int count)
{
Model.ResponeData responeData = new Model.ResponeData();
// string result = string.Empty;
List result = new List();
//pds = BLL.Funs.FilterBlankLines(pds);
if (count < 15)
{
responeData.code = 0;
responeData.message = "导入Excel格式错误!Excel只有" + count.ToString().Trim() + "列";
return responeData;
}
if (pds.Count > 0 && pds != null)
{
for (int i = 1; i < pds.Count; i++)
{
Model.PHTGL_BidDocumentsStandingBook _BidDocumentsStandingBook = new PHTGL_BidDocumentsStandingBook();
_BidDocumentsStandingBook.BidDocumentsStandingBookId = SQLHelper.GetNewID();
_BidDocumentsStandingBook.State = 0;
if (Type=="1")
{
_BidDocumentsStandingBook.ProjectId = this.CurrUser.LoginProjectId;
}
_BidDocumentsStandingBook.EPCCode = pds[i].A;
_BidDocumentsStandingBook.ProjectShortName = pds[i].B;
_BidDocumentsStandingBook.ProjectCode = pds[i].C;
_BidDocumentsStandingBook.IsOnLine = pds[i].D;
_BidDocumentsStandingBook.BidType = pds[i].E;
_BidDocumentsStandingBook.ActionPlanCode = pds[i].F;
_BidDocumentsStandingBook.BidDocumentsCode = pds[i].G;
_BidDocumentsStandingBook.BidProject = pds[i].H;
_BidDocumentsStandingBook.ShortListApprovalCode = pds[i].I;
_BidDocumentsStandingBook.ProposedInviter = pds[i].J;
_BidDocumentsStandingBook.Bidding_SendTime = pds[i].K;
_BidDocumentsStandingBook.Bidding_StartTime = pds[i].L;
_BidDocumentsStandingBook.ApprovePersonFormCode = pds[i].M;
_BidDocumentsStandingBook.BidWinner = pds[i].N;
_BidDocumentsStandingBook.SetSubReviewCode = pds[i].O;
_BidDocumentsStandingBook.BidNoticeCode= pds[i].P;
_BidDocumentsStandingBook.BidUnitFileCode= pds[i].Q;
BidDocStandingBookList.Add(_BidDocumentsStandingBook);
//if (col24 != null)
//{
//}
//else
//{
// result.Add("第" + (i + 1).ToString() + "行," + "壁厚" + "," + "此项为必填项!" + "|");
//}
}
if (result.Count > 0)
{
BidDocStandingBookList.Clear();
// result = result.Substring(0, result.LastIndexOf("|"));
errorInfos = string.Join("|", result.Distinct());
//Alert alert = new Alert();
//alert.Message = result;
//alert.Target = Target.Self;
//alert.Show();
responeData.code = 0;
responeData.message = errorInfos;
}
else
{
errorInfos = string.Empty;
}
}
else
{
responeData.code = 0;
responeData.message = "导入数据为空!";
}
return responeData;
}
#endregion
#endregion
#region 导入
///
/// 导入
///
///
///
protected void btnImport_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(errorInfos))
{
if (!string.IsNullOrEmpty(this.hdFileName.Text))
{
if (BidDocStandingBookList.Count > 0)
{
this.Grid1.Hidden = false;
this.Grid1.DataIDField = "BidDocumentsStandingBookId";
this.Grid1.DataSource = BidDocStandingBookList;
this.Grid1.DataBind();
Grid1.RecordCount = BidDocStandingBookList.Count;
}
}
else
{
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
}
}
else
{
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
}
}
#endregion
#region 提交
///
/// 提交
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(errorInfos))
{
if (BidDocStandingBookList.Count > 0)
{
BidDocStandingBookService.AddBulkPHTGL_BidDocumentsStandingBook(BidDocStandingBookList);
}
int a = BidDocStandingBookList.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 下载模板
///
/// 下载模板按钮
///
///
///
protected void btnDownLoad_Click(object sender, EventArgs e)
{
string rootPath = Server.MapPath("~/");
string uploadfilepath = rootPath + Const.BidDocumentsStandingBookIn;
string filePath = Const.BidDocumentsStandingBookIn;
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 分页选择下拉改变事件
///
/// 分页选择下拉改变事件
///
///
///
protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
this.Grid1.DataSource = BidDocStandingBookList;
this.Grid1.DataBind();
Grid1.RecordCount = BidDocStandingBookList.Count;
}
#endregion
}
}