ZHJA_HJGL/HJGL_ZH/FineUIPro.Web/WeldMat/UsingPlan/UsingPlanImport.aspx.cs

506 lines
23 KiB
C#

using BLL;
using BLL.Common;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
namespace FineUIPro.Web.WeldMat.UsingPlan
{
public partial class UsingPlanImport : PageBase
{
#region
/// <summary>
/// 上传预设的虚拟路径
/// </summary>
private string initPath = Const.ExcelUrl;
/// <summary>
/// 错误集合
/// </summary>
public static string errorInfos = string.Empty;
#endregion
#region
/// <summary>
/// 加载页面
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
}
#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.UsingPlanInTemplateUrl;
string filePath = Const.UsingPlanInTemplateUrl;
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 btnImport_Click(object sender, EventArgs e)
{
string message = string.Empty;
errorInfos = string.Empty;
List<Weld_UsingPlan> usingPlanLists = new List<Weld_UsingPlan>();
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 (usingPlanLists != null)
{
usingPlanLists.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);
//文件上传服务器后的名称
string fileName = rootPath + initPath + this.hdFileName.Text;
//读取Excel
DataSet ds = NPOIHelper.ExcelToDataSet(fileName, out errorInfos, true);
//验证Excel读取是否有误
if (!string.IsNullOrEmpty(errorInfos))
{
ShowNotify(errorInfos, MessageBoxIcon.Warning);
return;
}
//导入数据库
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
Weld_UsingPlan usingPlan = new Weld_UsingPlan();
#region
string projectId = string.Empty;
if (ds.Tables[0].Rows[i]["施工号"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["施工号"].ToString()))
{
var project = from x in Funs.DB.Base_Project where x.ProjectCode == ds.Tables[0].Rows[i]["施工号"].ToString() select x;
if (project.Count() > 0)
{
usingPlan.ProjectId = project.First().ProjectId;
projectId = project.First().ProjectId;
}
else
{
errorInfos += (i + 2) + "行, [施工号] 不存在</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [施工号] 不能为空</br>";
}
string steelType = string.Empty;
string weldTypeId = string.Empty;
if (ds.Tables[0].Rows[i]["焊材牌号"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["焊材牌号"].ToString())
&& ds.Tables[0].Rows[i]["规格"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["规格"].ToString()))
{
var weld = from x in Funs.DB.Weld_WeldInfo
where x.WeldName == ds.Tables[0].Rows[i]["焊材牌号"].ToString() &&
x.WeldSpec == ds.Tables[0].Rows[i]["规格"].ToString()
select x;
if (weld.Count() > 0)
{
usingPlan.WeldId = weld.First().WeldId;
steelType = weld.First().SteelType;
weldTypeId = weld.First().WeldTypeId;
}
else
{
errorInfos += (i + 2) + "行, [焊材牌号和规格] 不存在</br>";
}
}
else
{
errorInfos += (i + 2) + "Line, [焊材牌号和规格] 不能为空</br>";
}
string unitId = string.Empty;
if (ds.Tables[0].Rows[i]["使用单位"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["使用单位"].ToString()))
{
var unit = from x in Funs.DB.Project_Unit
join y in Funs.DB.Base_Unit on x.UnitId equals y.UnitId
where x.ProjectId == projectId && y.UnitName == ds.Tables[0].Rows[i]["使用单位"].ToString()
select x;
if (unit.Count() > 0)
{
usingPlan.UsingUnit = unit.First().UnitId;
unitId = unit.First().UnitId;
}
else
{
errorInfos += (i + 2) + "行, [使用单位] 不存在</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [使用单位] 不能为空</br>";
}
if (ds.Tables[0].Rows[i]["使用位置"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["使用位置"].ToString()))
{
usingPlan.UsePosition = ds.Tables[0].Rows[i]["使用位置"].ToString();
}
else
{
errorInfos += (i + 2) + "行, [使用位置] 不能为空</br>";
}
bool? isSteelStru = null;
if (ds.Tables[0].Rows[i]["是否钢结构"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["是否钢结构"].ToString()))
{
if (ds.Tables[0].Rows[i]["是否钢结构"].ToString() != "是" && ds.Tables[0].Rows[i]["是否钢结构"].ToString() != "否")
{
errorInfos += (i + 2) + "行, [是否钢结构] 请填写是或否</br>";
}
else
{
if (ds.Tables[0].Rows[i]["是否钢结构"].ToString() == "是")
{
usingPlan.IsSteelStru = true;
isSteelStru = true;
}
else
{
usingPlan.IsSteelStru = false;
isSteelStru = false;
}
}
}
else
{
errorInfos += (i + 2) + "行, [是否钢结构] 不能为空</br>";
}
if (ds.Tables[0].Rows[i]["焊工班组"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["焊工班组"].ToString()))
{
var team = from x in Funs.DB.Base_TeamGroup
where x.ProjectId == projectId && x.UnitId == unitId
select x;
if (team.Count() > 0)
{
usingPlan.TeamGroupId = team.First().TeamGroupId;
}
else
{
errorInfos += (i + 2) + "行, [焊工班组] 不存在</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [焊工班组] 不能为空</br>";
}
string usingWelder = string.Empty;
if (ds.Tables[0].Rows[i]["领料人"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["领料人"].ToString()))
{
var welder = from x in Funs.DB.Project_Welder
join y in Funs.DB.HJGL_BS_Welder on x.WED_ID equals y.WED_ID
where x.ProjectId == projectId && y.WED_Name == ds.Tables[0].Rows[i]["领料人"].ToString()
select x;
if (welder.Count() > 0)
{
if (WelderQueIsPass(welder.First().WED_ID, steelType, isSteelStru))
{
usingPlan.UsingManOne = welder.First().WED_ID;
usingWelder = usingPlan.UsingManOne;
}
else
{
errorInfos += (i + 2) + "行, [焊工] 资质不符合</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [领料人] 不存在</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [领料人] 不能为空</br>";
}
if (ds.Tables[0].Rows[i]["数量"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["数量"].ToString()))
{
try
{
decimal amount = Convert.ToDecimal(ds.Tables[0].Rows[i]["数量"].ToString());
usingPlan.Amount = amount;
var spec = from x in Funs.DB.Weld_Specifications where x.WeldTypeId == weldTypeId && x.Specifications == ds.Tables[0].Rows[i]["规格"].ToString() select x;
var weldtype = BLL.WeldTypeService.GetWeldTypeById(weldTypeId);
var welder = BLL.HJGL_PersonManageService.GetWelderByWenId(usingWelder);
if (welder!=null && welder.MaxHanTiao != null && welder.MaxHanTiao != null)
{
if (weldtype.WeldTypeName.Contains("焊条"))
{
if (amount > welder.MaxHanTiao)
{
errorInfos += (i + 2) + "行, [数量] 超过该焊工最大领用数量</br>";
}
}
if (weldtype.WeldTypeName.Contains("焊丝"))
{
if (amount > welder.MaxWeldingWire)
{
errorInfos += (i + 2) + "行, [数量] 超过该焊工最大领用数量</br>";
}
}
}
else if (spec.Count() > 0 && spec.First().MaxUsingNum != null)
{
if (amount > spec.First().MaxUsingNum)
{
errorInfos += (i + 2) + "行, [数量] 超过最大领用数量</br>";
}
}
}
catch (Exception)
{
errorInfos += (i + 2) + "行, [数量] 必须为数据型</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [数量] 不能为空</br>";
}
if (ds.Tables[0].Rows[i]["时间段"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["时间段"].ToString()))
{
string strTime = ds.Tables[0].Rows[i]["时间段"].ToString();
if (BLL.DropListService.OrderTimeList().Any(x => x.Value == strTime))
{
usingPlan.OrderTime = strTime;
}
else
{
errorInfos += (i + 2) + "行, [时间段] 录入不正确</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [时间段] 不能为空</br>";
}
if (ds.Tables[0].Rows[i]["焊件材质"] != null && !string.IsNullOrEmpty(ds.Tables[0].Rows[i]["焊件材质"].ToString()))
{
string[] steList = ds.Tables[0].Rows[i]["焊件材质"].ToString().Split(',');
string steCode = string.Empty;
string steId = string.Empty;
bool isExist = true;
if (steList.Count() > 0)
{
foreach (string item in steList)
{
var ste = from x in Funs.DB.HJGL_BS_Steel where x.STE_Code == item select x;
if (ste.Count() > 0)
{
steId = steId + ste.First().STE_ID + ",";
steCode = steCode + ste.First().STE_Code + ",";
}
else
{
isExist = false;
break;
}
}
}
if (isExist)
{
if (steId.Length > 0 && steCode.Length > 0)
{
usingPlan.STE_ID = steId.Substring(0, steId.Length - 1);
usingPlan.STE_Name = steCode.Substring(0, steCode.Length - 1);
}
}
else
{
errorInfos += (i + 2) + "行, [焊件材质] 不存在</br>";
}
}
else
{
errorInfos += (i + 2) + "行, [焊件材质] 不能为空</br>";
}
usingPlan.UsingPlanId = SQLHelper.GetNewID(typeof(Model.Weld_UsingPlan));
usingPlan.InPutDate = DateTime.Now.Date;
usingPlan.OrderDate = DateTime.Now.Date;
usingPlan.InPutMan = CurrUser.UserId;
usingPlan.IsCancel = false;
usingPlan.IsSubmit = true;
usingPlanLists.Add(usingPlan);
#endregion
}
if (!string.IsNullOrEmpty(errorInfos))
{
ShowAlert(errorInfos, MessageBoxIcon.Warning);
return;
}
// 去除集合中重复的数据
//for (int i = 0; i < usingPlanLists.Count; i++)
//{
// for (int j = usingPlanLists.Count - 1; j > i; j--)
// {
// if (usingPlanLists[i].WeldId == usingPlanLists[j].WeldId && usingPlanLists[i].UsingManOne == usingPlanLists[j].UsingManOne)
// {
// usingPlanLists.RemoveAt(j);
// }
// }
//}
if (usingPlanLists.Count > 0)
{
Funs.DB.Weld_UsingPlan.InsertAllOnSubmit(usingPlanLists);
Funs.DB.SubmitChanges();
foreach (var plan in usingPlanLists)
{
List<Model.Weld_UsingPlan> welderUsingPlan = BLL.UsingPlanService.GetWelderCurTimePlan(plan.UsingManOne, plan.OrderDate, plan.OrderTime);
if (welderUsingPlan != null && welderUsingPlan.Count() > 2)
{
// 超过两条还未确认的记录
var noConfirm = welderUsingPlan.Where(x => x.IsNeedConfirm == null);
foreach (var q in noConfirm)
{
BLL.UsingPlanService.UpdateIsNeedConfirm(q.UsingPlanId, true);
}
}
}
}
ShowNotify("导入成功!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
ShowAlert("没有数据!", MessageBoxIcon.Warning);
return;
}
}
catch (Exception ex)
{
ShowAlert("'" + ex.Message + "'", MessageBoxIcon.Warning);
}
}
#endregion
#region
/// <summary>
/// 判断焊工资质是否合格
/// </summary>
/// <param name="welderId"></param>
/// <param name="steelType"></param>
/// <returns></returns>
private bool WelderQueIsPass(string welderId, string steelType, bool? isSteelStru)
{
bool isPass = false;
List<Model.HJGL_BS_WelderQualifiedProject> qualifys = new List<Model.HJGL_BS_WelderQualifiedProject>();
if (isSteelStru == true)
{
qualifys = (from x in Funs.DB.HJGL_BS_WelderQualifiedProject
where x.WED_ID == welderId && x.LimitDate >= DateTime.Now && x.MaterialType != null
select x).ToList();
}
else
{
qualifys = (from x in Funs.DB.HJGL_BS_WelderQualifiedProject
where x.WED_ID == welderId && x.LimitDate >= DateTime.Now && x.MaterialType != null
&& (x.IsSteelStru == false || x.IsSteelStru == null)
select x).ToList();
}
foreach (var q in qualifys)
{
if (q.MaterialType.Contains(steelType))
{
isPass = true;
break;
}
}
return isPass;
}
#endregion
}
}