450 lines
16 KiB
C#
450 lines
16 KiB
C#
using BLL;
|
|
using FineUIPro.Web.BaseInfo;
|
|
using Model;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Web.Services.Description;
|
|
|
|
namespace FineUIPro.Web.ZHGL.Supervise
|
|
{
|
|
public partial class UnitHazardRegisterDetailIn : PageBase
|
|
{
|
|
#region 定义变量
|
|
|
|
/// <summary>
|
|
/// 上传预设的虚拟路径
|
|
/// </summary>
|
|
private string initPath = Const.ExcelUrl;
|
|
|
|
|
|
/// <summary>
|
|
/// 错误集合
|
|
/// </summary>
|
|
public static List<Model.ErrorInfo> errorInfos = new List<Model.ErrorInfo>();
|
|
|
|
/// <summary>
|
|
/// 检查id
|
|
/// </summary>
|
|
public string UnitHazardRegisterId
|
|
{
|
|
get
|
|
{
|
|
return (string)ViewState["UnitHazardRegisterId"];
|
|
}
|
|
set
|
|
{
|
|
ViewState["UnitHazardRegisterId"] = value;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 类型
|
|
/// </summary>
|
|
public string Type
|
|
{
|
|
get
|
|
{
|
|
return (string)ViewState["Type"];
|
|
}
|
|
set
|
|
{
|
|
ViewState["Type"] = value;
|
|
}
|
|
}
|
|
public static int Cols = 4;//列数
|
|
|
|
public static Dictionary<string, string> imgDic = new Dictionary<string, string>();
|
|
|
|
public static List<string> validLevelStrings = new List<string> { "一般", "较大", "重大" };
|
|
public static List<string> validRegisterTypesStrings = new List<string>();
|
|
|
|
#endregion
|
|
|
|
protected void Page_Load(object sender, EventArgs e)
|
|
{
|
|
if (!IsPostBack)
|
|
{
|
|
this.Type = Request.Params["Type"];
|
|
this.UnitHazardRegisterId = Request.Params["UnitHazardRegisterId"];
|
|
|
|
this.hdFileName.Text = string.Empty;
|
|
if (errorInfos != null)
|
|
{
|
|
errorInfos.Clear();
|
|
}
|
|
if (this.Type == "0")
|
|
{//安全类型
|
|
validRegisterTypesStrings = (from x in Funs.DB.HSSE_Hazard_HazardRegisterTypes where x.HazardRegisterType == "1" orderby x.TypeCode select x.RegisterTypesName).ToList();
|
|
}
|
|
else
|
|
{ //质量类型
|
|
validRegisterTypesStrings = (from x in Funs.DB.Base_QualityQuestionType orderby x.SortIndex select x.QualityQuestionType).ToList();
|
|
}
|
|
//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.UnitHazardRegisterDetailTemplateUrl;
|
|
string filePath = Const.UnitHazardRegisterDetailTemplateUrl;
|
|
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.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.Supervise_UnitHazardRegisterItem entity) ValidateRowAndBuildEntity(DataRow row, string displayRow, bool import)
|
|
{
|
|
List<ErrorInfo> lstError = new List<ErrorInfo>();
|
|
// 新模板字段顺序
|
|
string row0 = row[0]?.ToString().Trim(); // 问题描述
|
|
string row1 = row[1]?.ToString().Trim(); // 问题类型
|
|
string row2 = row[2]?.ToString().Trim(); // 风险级别
|
|
string row3 = row[3]?.ToString().Trim(); // 整改要求
|
|
//string row4 = row[4]?.ToString().Trim(); // 问题图片地址
|
|
|
|
string data0 = string.Empty;
|
|
string data1 = string.Empty;
|
|
string data2 = string.Empty;
|
|
string data3 = string.Empty;
|
|
string data4 = string.Empty;
|
|
|
|
// 校验并转换
|
|
if (!string.IsNullOrEmpty(row0))
|
|
{
|
|
data0 = row0;
|
|
}
|
|
else
|
|
{
|
|
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题描述", Reason = "不能为空" });
|
|
}
|
|
if (!string.IsNullOrEmpty(row1))
|
|
{
|
|
if (validRegisterTypesStrings.Any(x => x == row1))
|
|
{
|
|
data1 = row1;
|
|
}
|
|
else
|
|
{
|
|
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题类型", Reason = $"{(this.Type == "0" ? "安全" : "质量")}问题类型不存在[{row1}]" });
|
|
}
|
|
}
|
|
else
|
|
{
|
|
lstError.Add(new ErrorInfo { Row = displayRow, Column = "问题类型", Reason = "不能为空" });
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(row2))
|
|
{
|
|
lstError.Add(new ErrorInfo { Row = displayRow, Column = "风险级别", Reason = "不能为空" });
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(row2))
|
|
{
|
|
if (validLevelStrings.Any(x => x == row2))
|
|
{
|
|
data2 = row2;
|
|
}
|
|
else
|
|
{
|
|
lstError.Add(new ErrorInfo { Row = displayRow, Column = "风险级别", Reason = $"风险级别[{row2}]不存在" });
|
|
}
|
|
}
|
|
if (!string.IsNullOrWhiteSpace(row3))
|
|
{
|
|
data3 = row3;
|
|
}
|
|
//if (import)
|
|
//{图片待处理
|
|
// try
|
|
// {
|
|
// urls = urls.Substring(urls.IndexOf('"') + 1, urls.LastIndexOf('"') - urls.IndexOf('"') - 1);
|
|
// var sss = imgDic.ContainsKey(urls) ? imgDic[urls].Replace(Funs.RootPath, "") : "";
|
|
// }
|
|
// catch (Exception e) { }
|
|
//}
|
|
|
|
if (lstError.Any())
|
|
{
|
|
return (lstError, null);
|
|
}
|
|
|
|
// 其余字段可根据实际业务映射到实体
|
|
var entity = new Model.Supervise_UnitHazardRegisterItem
|
|
{
|
|
UnitHazardRegisterItemId = SQLHelper.GetNewID(),
|
|
UnitHazardRegisterId = this.UnitHazardRegisterId,
|
|
ProblemDescription = data0,
|
|
ProblemType = data1,
|
|
RiskLevel = data2,
|
|
RectifyRequirement = data3,
|
|
CompleteStatus = 0,
|
|
//CompletedDate = DateTime.Now,
|
|
////SortIndex = 1,
|
|
//RectificationMeasures = null,
|
|
//IsUpdate = null
|
|
};
|
|
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.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.Supervise_UnitHazardRegisterItem>();
|
|
|
|
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.Supervise_UnitHazardRegisterItem.InsertAllOnSubmit(toInsert);
|
|
Funs.DB.SubmitChanges();
|
|
}
|
|
ShowNotify($"成功导入 {toInsert.Count} 条记录。", MessageBoxIcon.Success);
|
|
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrLogInfo.WriteLog("导入 Hazard_HazardRegister_Unit 失败", ex);
|
|
ShowNotify("导入失败:" + ex.Message, MessageBoxIcon.Error);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
#endregion // 导入
|
|
}
|
|
} |