716 lines
28 KiB
C#
716 lines
28 KiB
C#
using BLL;
|
||
using Model;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Configuration;
|
||
using System.Data;
|
||
using System.IO;
|
||
using System.Linq;
|
||
|
||
namespace FineUIPro.Web.Check.HSE
|
||
{
|
||
public partial class CheckProblemIn : PageBase
|
||
{
|
||
#region 定义变量
|
||
|
||
/// <summary>
|
||
/// 上传预设的虚拟路径
|
||
/// </summary>
|
||
private string initPath = Const.ExcelUrl;
|
||
|
||
|
||
/// <summary>
|
||
/// 错误集合
|
||
/// </summary>
|
||
public static List<Model.ErrorInfo> errorInfos = new List<Model.ErrorInfo>();
|
||
/// <summary>
|
||
/// 问题分类
|
||
/// </summary>
|
||
public static List<Model.Base_SafetyProblemClassify> classifys = new List<Model.Base_SafetyProblemClassify>();
|
||
/// <summary>
|
||
/// 问题分类明细
|
||
/// </summary>
|
||
public static List<Model.Base_SafetyProblemClassifyItem> classifyItems = new List<Model.Base_SafetyProblemClassifyItem>();
|
||
/// <summary>
|
||
/// 问题性质
|
||
/// </summary>
|
||
public static List<Model.Base_SafetyProblemNature> natures = new List<Model.Base_SafetyProblemNature>();
|
||
/// <summary>
|
||
/// 问题性质明细
|
||
/// </summary>
|
||
public static List<Model.Base_SafetyProblemNatureItem> natureItems = new List<Model.Base_SafetyProblemNatureItem>();
|
||
|
||
///// <summary>
|
||
///// 项目id
|
||
///// </summary>
|
||
//public string ProjectId
|
||
//{
|
||
// get
|
||
// {
|
||
// return (string)ViewState["ProjectId"];
|
||
// }
|
||
// set
|
||
// {
|
||
// ViewState["ProjectId"] = value;
|
||
// }
|
||
//}
|
||
///// <summary>
|
||
///// 单位id
|
||
///// </summary>
|
||
//public string UnitId
|
||
//{
|
||
// get
|
||
// {
|
||
// return (string)ViewState["UnitId"];
|
||
// }
|
||
// set
|
||
// {
|
||
// ViewState["UnitId"] = value;
|
||
// }
|
||
//}
|
||
|
||
/// <summary>
|
||
/// 操作人
|
||
/// </summary>
|
||
public string CompileMan
|
||
{
|
||
get
|
||
{
|
||
return (string)ViewState["CompileMan"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["CompileMan"] = value;
|
||
}
|
||
}
|
||
public string CompileManName
|
||
{
|
||
get
|
||
{
|
||
return (string)ViewState["CompileManName"];
|
||
}
|
||
set
|
||
{
|
||
ViewState["CompileManName"] = value;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 导入表格列数
|
||
/// </summary>
|
||
public static int Cols = 17;
|
||
|
||
/// <summary>
|
||
/// 单位类型
|
||
/// </summary>
|
||
public static List<string> validUnitTypeStrings = new List<string> { "承包商", "项目部" };
|
||
/// <summary>
|
||
/// 在岗用户
|
||
/// </summary>
|
||
public static List<Sys_User> users = new List<Sys_User>();
|
||
|
||
#endregion
|
||
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack)
|
||
{
|
||
//users = BLL.ProjectUserService.GetProjectUserByProjectId(this.ProjectId,null,null);
|
||
//获取所有在岗用户
|
||
users = BLL.UserService.GetProjectUserListByProjectId(null);
|
||
|
||
classifys = SafetyProblemClassifyService.GetClassify();
|
||
classifyItems = SafetyProblemClassifyItemService.GetClassifyItemList("");
|
||
natures = SafetyProblemNatureService.GetNature();
|
||
natureItems = SafetyProblemNatureItemService.GetNatureItemList("");
|
||
|
||
this.CompileMan = this.CurrUser.UserId;
|
||
this.CompileManName = this.CurrUser.UserName;
|
||
this.hdFileName.Text = string.Empty;
|
||
if (errorInfos != null)
|
||
{
|
||
errorInfos.Clear();
|
||
}
|
||
|
||
//if (this.CurrUser.UserId == Const.AdminId)
|
||
//{
|
||
// this.btnFastImport.Hidden = false;
|
||
//}
|
||
}
|
||
}
|
||
|
||
#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.CheckProblemSafetyDataInTemplateUrl;
|
||
string filePath = Const.CheckProblemSafetyDataInTemplateUrl;
|
||
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 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 != ".xls" && IsXls != ".xlsx")
|
||
{
|
||
ShowNotify("只可以选择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;
|
||
this.fuAttachUrl.PostedFile.SaveAs(filePath);
|
||
AuditExcelAndShowResult(filePath);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ShowNotify("'" + ex.Message + "'", MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 审核Excel并展示校验结果
|
||
/// </summary>
|
||
private void AuditExcelAndShowResult(string fileName)
|
||
{
|
||
try
|
||
{
|
||
string msg = string.Empty;
|
||
var dt = BLL.Common.NPOIHelper.ExcelToDataSet(fileName, out msg, true);
|
||
if (dt != null)
|
||
{
|
||
//string rootPath = Server.MapPath("~/");
|
||
//var localPath = ConfigurationManager.AppSettings["localRoot"];
|
||
//imgDic = NPOIHelper.ExtractDispimgImagesFromXml(rootPath + initPath + this.hdFileName.Text, localPath + "FileUpload/CheckSpecial");
|
||
ValidateExcelData(dt.Tables[0], false);
|
||
hdCheckResult.Text = "1";
|
||
}
|
||
}
|
||
catch (Exception exc)
|
||
{
|
||
BLL.ErrLogInfo.WriteLog("", exc);
|
||
Response.Write(exc);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校验Excel数据(仅校验)
|
||
/// </summary>
|
||
/// <param name="pds"></param>
|
||
/// <param name="import">true:导入;false:审核</param>
|
||
/// <returns></returns>
|
||
private bool ValidateExcelData(DataTable pds, bool import)
|
||
{
|
||
int ic = pds.Columns.Count;
|
||
if (ic < Cols)
|
||
{
|
||
ShowNotify($"导入Excel格式错误!Excel只有{ic}列", MessageBoxIcon.Warning);
|
||
return false;
|
||
}
|
||
int ir = pds.Rows.Count;
|
||
if (pds == null || ir <= 0)
|
||
{
|
||
ShowNotify("导入数据为空!", MessageBoxIcon.Warning);
|
||
return false;
|
||
}
|
||
var errors = new List<ErrorInfo>();
|
||
for (int i = 0; i < ir; i++)
|
||
{
|
||
var (error, _) = ValidateRowAndBuildEntity(pds.Rows[i], (i + 2).ToString(), import);
|
||
if (error.Any()) { errors.AddRange(error); }
|
||
}
|
||
errorInfos.Clear();
|
||
errorInfos.AddRange(errors);
|
||
this.gvErrorInfo.DataSource = errorInfos;
|
||
this.gvErrorInfo.DataBind();
|
||
if (errors.Count > 0)
|
||
{
|
||
ShowNotify("存在校验错误,请查看错误列表并修正后再次导入。", MessageBoxIcon.Warning);
|
||
return false;
|
||
}
|
||
ShowNotify("审核完成,请点击导入!", MessageBoxIcon.Success);
|
||
return true;
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 校验单行并生成实体(字段顺序与新模板一致)
|
||
/// </summary>
|
||
private (List<ErrorInfo> lstError, Model.CheckProblem_Safety entity) ValidateRowAndBuildEntity(DataRow row, string displayRow, bool import)
|
||
{
|
||
List<ErrorInfo> lstError = new List<ErrorInfo>();
|
||
// 新模板字段顺序
|
||
string rData0 = row[0]?.ToString().Trim(); // 受检单位
|
||
string rData1 = row[1]?.ToString().Trim(); // 受检项目
|
||
string rData2 = row[2]?.ToString().Trim(); // 项目所在地(省市)
|
||
string rData3 = row[3]?.ToString().Trim(); // 不符合项描述
|
||
string rData4 = row[4]?.ToString().Trim(); // 单位类型
|
||
string rData5 = row[5]?.ToString().Trim(); // 机组名称
|
||
string rData6 = row[6]?.ToString().Trim(); // 提出人
|
||
string rData7 = row[7]?.ToString().Trim(); // 提出日期
|
||
string rData8 = row[8]?.ToString().Trim(); // 截止日期
|
||
string rData9 = row[9]?.ToString().Trim(); // 整改人
|
||
string rData10 = row[10]?.ToString().Trim(); // 闭合日期
|
||
string rData11 = row[11]?.ToString().Trim(); // 项目经理
|
||
string rData12 = row[12]?.ToString().Trim(); // 问题分类
|
||
string rData13 = row[13]?.ToString().Trim(); // 分类明细
|
||
string rData14 = row[14]?.ToString().Trim(); // 问题性质
|
||
string rData15 = row[15]?.ToString().Trim(); // 性质明细
|
||
string rData16 = row[16]?.ToString().Trim(); // 备注
|
||
|
||
|
||
string UnitId = string.Empty;
|
||
string UnitName = string.Empty;
|
||
string ProjectId = null;
|
||
string ProjectName = null;
|
||
int State = 1;
|
||
string Address = string.Empty;
|
||
string Description = string.Empty;
|
||
string UnitType = string.Empty;
|
||
string AssemblyName = string.Empty;
|
||
//string CheckUserId = string.Empty;
|
||
//string CheckUserName = string.Empty;
|
||
string CheckTeamUser = string.Empty;
|
||
DateTime CheckDate = DateTime.Now;
|
||
DateTime LimitDate = DateTime.Now;
|
||
DateTime? CloseTime = null;
|
||
string RectificationUserId = string.Empty;
|
||
string RectificationUserName = string.Empty;
|
||
string PMUserId = string.Empty;
|
||
string PMUserName = string.Empty;
|
||
string ClassifyId = string.Empty;
|
||
string ClassifyItemId = string.Empty;
|
||
string NatureId = string.Empty;
|
||
string NatureItemId = string.Empty;
|
||
string Remark = string.Empty;
|
||
|
||
// 校验并转换
|
||
if (!string.IsNullOrWhiteSpace(rData0))
|
||
{
|
||
var unit = BLL.UnitService.getUnitByUnitName(rData0);
|
||
if (unit != null)
|
||
{
|
||
UnitId = unit.UnitId;
|
||
UnitName = unit.UnitName;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "受检单位", Reason = $"未找到单位:{rData0};请确认单位名称是否有误!" });
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "受检单位", Reason = "受检单位必填" });
|
||
}
|
||
if (!string.IsNullOrWhiteSpace(rData1))
|
||
{
|
||
var project = BLL.ProjectService.GetProjectByProjectName(rData1);
|
||
if (project != null)
|
||
{
|
||
ProjectId = project.ProjectId;
|
||
ProjectName = project.ProjectName;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "受检项目", Reason = $"未找到项目:{rData1};请确认项目名称是否有误!" });
|
||
}
|
||
}
|
||
//else
|
||
//{
|
||
// lstError.Add(new ErrorInfo { Row = displayRow, Column = "受检项目", Reason = "受检项目必填" });
|
||
//}
|
||
|
||
if (!string.IsNullOrEmpty(rData2))
|
||
{
|
||
Address = rData2;
|
||
}
|
||
if (!string.IsNullOrEmpty(rData3))
|
||
{
|
||
Description = rData3;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "不符合描述", Reason = "不符合描述必填!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData4))
|
||
{
|
||
if (!validUnitTypeStrings.Any(x => x == rData4))
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "单位类型", Reason = "只能是[承包商/项目部]!" });
|
||
}
|
||
else
|
||
{
|
||
UnitType = rData4;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "单位类型", Reason = "单位类型必选!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData5))
|
||
{
|
||
AssemblyName = rData5;
|
||
}
|
||
//else
|
||
//{
|
||
// lstError.Add(new ErrorInfo { Row = displayRow, Column = "机组名称", Reason = "机组名称必填!" });
|
||
//}
|
||
|
||
if (!string.IsNullOrEmpty(rData6))
|
||
{
|
||
CheckTeamUser = rData6;
|
||
//var user = users.Where(x => x.UserName == rData6).FirstOrDefault();
|
||
//if (user != null)
|
||
//{
|
||
// CheckUserId = user.UserId;
|
||
// CheckUserName = rData6;
|
||
//}
|
||
//else
|
||
//{
|
||
// lstError.Add(new ErrorInfo { Row = displayRow, Column = "提出人", Reason = $"系统不存在人员[{rData6}]!" });
|
||
//}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "提出人", Reason = "提出人必填!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData7))
|
||
{
|
||
if (!DateTime.TryParse(rData7, out DateTime ct))
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "提出日期", Reason = "提出日期格式错误[正确格式:yyyy-MM-dd,2026-06-01]" });
|
||
}
|
||
else
|
||
{
|
||
CheckDate = ct;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "提出日期", Reason = "不能为空" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData8))
|
||
{
|
||
if (!DateTime.TryParse(rData8, out DateTime ct))
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "截止日期", Reason = "截止日期格式错误[正确格式:yyyy-MM-dd,2026-06-01]" });
|
||
}
|
||
else
|
||
{
|
||
LimitDate = ct;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "截止日期", Reason = "不能为空" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData9))
|
||
{
|
||
var user = users.Where(x => x.UserName == rData9).FirstOrDefault();
|
||
if (user != null)
|
||
{
|
||
RectificationUserId = user.UserId;
|
||
RectificationUserName = rData9;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "整改人", Reason = $"系统不存在人员[{rData9}]!" });
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "整改人", Reason = "整改人必填!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData10))
|
||
{
|
||
if (!DateTime.TryParse(rData10, out DateTime ct))
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "闭合日期", Reason = "闭合日期格式错误[正确格式:yyyy-MM-dd,2026-06-01]" });
|
||
}
|
||
else
|
||
{
|
||
CloseTime = ct;
|
||
State = (int)CheckProblemSafetyService.StateInt.已闭环;
|
||
}
|
||
}
|
||
//else
|
||
//{
|
||
// lstError.Add(new ErrorInfo { Row = displayRow, Column = "闭合日期", Reason = "不能为空" });
|
||
//}
|
||
if (!string.IsNullOrEmpty(rData11))
|
||
{
|
||
var user = users.Where(x => x.UserName == rData11).FirstOrDefault();
|
||
if (user != null)
|
||
{
|
||
PMUserId = user.UserId;
|
||
PMUserName = rData11;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "项目经理", Reason = $"系统不存在人员[{rData11}]!" });
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "项目经理", Reason = "项目经理必填!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData12))
|
||
{//问题分类
|
||
var classify = classifys.Where(x => x.ClassifyName == rData12).FirstOrDefault();
|
||
if (classify != null)
|
||
{
|
||
ClassifyId = classify.ClassifyId;
|
||
if (!string.IsNullOrEmpty(rData13))
|
||
{//问题分类明细
|
||
var classifyItem = classifyItems.Where(x => x.ClassifyItemName == rData13).FirstOrDefault();
|
||
if (classifyItem != null)
|
||
{
|
||
ClassifyItemId = classifyItem.ClassifyItemId;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题分类明细", Reason = $"系统不存在问题分类明细[{rData13}],请检查名称是否正确!" });
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题分类", Reason = $"系统不存在问题分类[{rData12}],请检查名称是否正确!" });
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题分类", Reason = "问题分类必填!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData14))
|
||
{//问题性质
|
||
var nature = natures.Where(x => x.NatureName == rData14).FirstOrDefault();
|
||
if (nature != null)
|
||
{
|
||
NatureId = nature.NatureId;
|
||
if (!string.IsNullOrEmpty(rData15))
|
||
{//问题性质明细
|
||
var natureItem = natureItems.Where(x => x.NatureItemContent == rData15).FirstOrDefault();
|
||
if (natureItem != null)
|
||
{
|
||
NatureItemId = natureItem.NatureItemId;
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题性质明细", Reason = $"系统不存在问题性质明细[{rData15}],请检查名称是否正确!" });
|
||
}
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题性质", Reason = $"系统不存在问题性质[{rData14}],请检查名称是否正确!" });
|
||
}
|
||
}
|
||
else
|
||
{
|
||
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题性质", Reason = "问题性质必填!" });
|
||
}
|
||
if (!string.IsNullOrEmpty(rData16))
|
||
{
|
||
Remark = rData16;
|
||
}
|
||
// 其余字段可根据实际业务映射到实体
|
||
var entity = new Model.CheckProblem_Safety
|
||
{
|
||
CheckId = SQLHelper.GetNewID(typeof(Model.CheckProblem_Safety)),
|
||
CompileDate = DateTime.Now,
|
||
CompileMan = this.CompileMan,
|
||
CompileManName = this.CompileManName,
|
||
UnitId = UnitId,
|
||
UnitName = UnitName,
|
||
ProjectId = ProjectId,
|
||
ProjectName = ProjectName,
|
||
State = State,//状态(0:已闭环,1:整改中)
|
||
Address = Address,
|
||
Description = Description,
|
||
UnitType = UnitType,
|
||
AssemblyName = AssemblyName,
|
||
CheckTeamUser = CheckTeamUser,
|
||
CheckDate = CheckDate,
|
||
LimitDate = LimitDate,
|
||
CloseTime = CloseTime,
|
||
RectificationUserId = RectificationUserId,
|
||
RectificationUserName = RectificationUserName,
|
||
PMUserId = PMUserId,
|
||
PMUserName = PMUserName,
|
||
ClassifyId = ClassifyId,
|
||
ClassifyItemId = ClassifyItemId,
|
||
NatureId = NatureId,
|
||
NatureItemId = NatureItemId,
|
||
Remark = Remark
|
||
};
|
||
return (lstError, entity);
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 导入
|
||
|
||
/// <summary>
|
||
/// 导入
|
||
/// </summary>
|
||
/// <param name="sender"></param>
|
||
/// <param name="e"></param>
|
||
protected void btnImport_Click(object sender, EventArgs e)
|
||
{
|
||
if (hdCheckResult.Text == "1")
|
||
{
|
||
if (errorInfos.Count <= 0)
|
||
{
|
||
string rootPath = Server.MapPath("~/");
|
||
var localPath = ConfigurationManager.AppSettings["localRoot"];
|
||
//imgDic = NPOIHelper.ExtractDispimgImagesFromXml(rootPath + initPath + this.hdFileName.Text, localPath + "FileUpload/CheckSpecial");
|
||
ImportExcelToDatabase(rootPath + initPath + this.hdFileName.Text, true);
|
||
|
||
hdCheckResult.Text = string.Empty;
|
||
ShowNotify("导入成功!", MessageBoxIcon.Success);
|
||
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
|
||
}
|
||
else
|
||
{
|
||
ShowNotify("请先将错误数据修正,再重新导入提交!", MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
ShowNotify("请先审核要导入的文件!", MessageBoxIcon.Warning);
|
||
}
|
||
}
|
||
/// <summary>
|
||
/// 导入Excel到数据库
|
||
/// </summary>
|
||
private void ImportExcelToDatabase(string fileName, bool import)
|
||
{
|
||
try
|
||
{
|
||
string msg = string.Empty;
|
||
var dt = BLL.Common.NPOIHelper.ExcelToDataSet(fileName, out msg, true);
|
||
BLL.ErrLogInfo.WriteLog(msg);
|
||
SaveExcelDataToDatabase(fileName, dt.Tables[0], import);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrLogInfo.WriteLog("", ex);
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 校验并保存数据到数据库
|
||
/// </summary>
|
||
private bool SaveExcelDataToDatabase(string fileName, DataTable pds, bool import)
|
||
{
|
||
int ic = pds.Columns.Count;
|
||
if (ic < Cols)
|
||
{
|
||
ShowNotify($"导入Excel格式错误!Excel只有{ic}列", MessageBoxIcon.Warning);
|
||
return false;
|
||
}
|
||
int ir = pds.Rows.Count;
|
||
if (pds == null || ir <= 0)
|
||
{
|
||
ShowNotify("导入数据为空!", MessageBoxIcon.Warning);
|
||
return false;
|
||
}
|
||
var errors = new List<ErrorInfo>();
|
||
var toInsert = new List<Model.CheckProblem_Safety>();
|
||
|
||
for (int i = 0; i < ir; i++)
|
||
{
|
||
var (error, entity) = ValidateRowAndBuildEntity(pds.Rows[i], (i + 2).ToString(), import);
|
||
if (error.Any())
|
||
errors.AddRange(error);
|
||
else
|
||
toInsert.Add(entity);
|
||
}
|
||
if (errors.Count > 0)
|
||
{
|
||
errorInfos.Clear();
|
||
errorInfos.AddRange(errors);
|
||
this.gvErrorInfo.DataSource = errorInfos;
|
||
this.gvErrorInfo.DataBind();
|
||
ShowNotify("存在校验错误,请先审核并修正后再导入。", MessageBoxIcon.Warning);
|
||
return false;
|
||
}
|
||
try
|
||
{
|
||
if (toInsert.Any())
|
||
{
|
||
Funs.DB.CheckProblem_Safety.InsertAllOnSubmit(toInsert);
|
||
Funs.DB.SubmitChanges();
|
||
}
|
||
ShowNotify($"成功导入 {toInsert.Count} 条记录。", MessageBoxIcon.Success);
|
||
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
|
||
return true;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
ErrLogInfo.WriteLog("导入 安全不符合项(CheckProblem_Safety) 失败", ex);
|
||
ShowNotify("导入失败:" + ex.Message, MessageBoxIcon.Error);
|
||
return false;
|
||
}
|
||
}
|
||
|
||
#endregion // 导入
|
||
}
|
||
} |