Basf_EProject/EProject/FineUIPro.Web/EditorManage/CostReportIn.aspx.cs

359 lines
15 KiB
C#
Raw Normal View History

2024-05-08 11:01:54 +08:00
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.EditorManage
{
public partial class CostReportIn : 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("Are you sure to download the import template?", 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.CostReportTemplateUrl;
string filePath = Const.CostReportTemplateUrl;
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<Editor_CostReport> costReportLists = new List<Editor_CostReport>();
try
{
if (this.fuAttachUrl.HasFile == false)
{
ShowNotify("Please select Excel file!", MessageBoxIcon.Warning);
return;
}
string IsXls = Path.GetExtension(this.fuAttachUrl.FileName).ToString().Trim().ToLower();
if (IsXls != ".xls" && IsXls != ".xlsx")
{
ShowNotify("Only Excel files can be selected!", MessageBoxIcon.Warning);
return;
}
if (costReportLists != null)
{
costReportLists.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;
}
DataRow[] dv =ds.Tables[0].Select("Month <>'' and JOB_NO <>''");
//导入数据库
if (dv.Length> 0)
{
for (int i = 0; i < dv.Length; i++)
{
Editor_CostReport costReport = new Editor_CostReport();
#region
if (dv[i]["Month"] != null && !string.IsNullOrEmpty(dv[i]["Month"].ToString()))
{
try
{
costReport.Monthly = DateTime.ParseExact(dv[i]["Month"].ToString(), "yyyyMM", null).ToString("yyyyMM");
}
catch (Exception)
{
errorInfos += (i + 2) + "line[Month]Must be in datetime format</br>";
}
}
else
{
errorInfos += (i + 2) + "Line, [Month] cannot be empty</br>";
}
if (dv[i]["JOB_NO"] != null && !string.IsNullOrEmpty(dv[i]["JOB_NO"].ToString()))
{
var eproject = BLL.EProjectService.GeteProjectByJobNO(dv[i]["JOB_NO"].ToString());
if (eproject != null)
{
costReport.EProjectId = eproject.EProjectId;
}
else
{
errorInfos += (i + 2) + "Line, [JOB_NO] not exist</br>";
}
}
else
{
errorInfos += (i + 2) + "Line, [JOB_NO] cannot be empty</br>";
}
if (dv[i]["BUDGET_ORG"] != null && !string.IsNullOrEmpty(dv[i]["BUDGET_ORG"].ToString()))
{
try
{
costReport.OrginalBudget = Funs.GetNewDecimalOrZero(dv[i]["BUDGET_ORG"].ToString());
}
catch (Exception)
{
errorInfos += (i + 2) + "line[BUDGET_ORG] Must be in numeric format</br>";
}
}
else
{
costReport.OrginalBudget = 0;
}
if (dv[i]["Budget_CHK"] != null && !string.IsNullOrEmpty(dv[i]["Budget_CHK"].ToString()))
{
try
{
costReport.ChangedBudget = Funs.GetNewDecimalOrZero(dv[i]["Budget_CHK"].ToString());
}
catch (Exception)
{
errorInfos += (i + 2) + "line[Budget_CHK]Must be in numeric format</br>";
}
}
else
{
costReport.ChangedBudget = 0;
}
if (dv[i]["ACTUAL"] != null && !string.IsNullOrEmpty(dv[i]["ACTUAL"].ToString()))
{
try
{
costReport.ActualCost = Funs.GetNewDecimalOrZero(dv[i]["ACTUAL"].ToString());
}
catch (Exception)
{
errorInfos += (i + 2) + "line[ACTUAL]Must be in numeric format</br>";
}
}
else
{
costReport.ActualCost = 0;
}
if (dv[i]["Commit_PO"] != null && !string.IsNullOrEmpty(dv[i]["Commit_PO"].ToString()))
{
try
{
costReport.CommittedPRPO = Funs.GetNewDecimalOrZero(dv[i]["Commit_PO"].ToString());
}
catch (Exception)
{
errorInfos += (i + 2) + "line[Commit_PO]Must be in numeric format</br>";
}
}
else
{
costReport.CommittedPRPO = 0;
}
if (dv[i]["Commit_SSR"] != null && !string.IsNullOrEmpty(dv[i]["Commit_SSR"].ToString()))
{
try
{
costReport.CommittedSSRs = Funs.GetNewDecimalOrZero(dv[i]["Commit_SSR"].ToString());
}
catch (Exception)
{
errorInfos += (i + 2) + "line[Commit_SSR] Must be in numeric format</br>";
}
}
else
{
costReport.CommittedSSRs = 0;
}
if (dv[i]["Cost_To_CMPT"] != null && !string.IsNullOrEmpty(dv[i]["Cost_To_CMPT"].ToString()))
{
try
{
costReport.CostToComplete = Funs.GetNewDecimalOrZero(dv[i]["Cost_To_CMPT"].ToString());
}
catch (Exception)
{
errorInfos += (i + 2) + "line[Cost_To_CMPT] Must be in numeric format</br>";
}
}
else
{
costReport.CostToComplete = 0;
}
costReport.CostReportId = SQLHelper.GetNewID(typeof(Model.Editor_CostReport));
costReportLists.Add(costReport);
#endregion
}
if (!string.IsNullOrEmpty(errorInfos))
{
ShowAlert(errorInfos, MessageBoxIcon.Warning);
return;
}
//去除集合中重复的数据
//for (int i = 0; i < costReportLists.Count; i++)
//{
// for (int j = costReportLists.Count - 1; j > i; j--)
// {
// if (costReportLists[i].Monthly == costReportLists[j].Monthly && costReportLists[i].EProjectId == costReportLists[j].EProjectId)
// {
// costReportLists.RemoveAt(j);
// }
// }
//}
//判断数据库是否有相同日期和项目的数据
List<Editor_CostReport> insertlists = new List<Editor_CostReport>();
//List<Editor_CostReport> updatelists = new List<Editor_CostReport>();
foreach (var item in costReportLists)
{
var mewCostReport = BLL.CostReportService.GetCostReportByEProjectIdAndMonth(item.EProjectId, item.Monthly);
if (mewCostReport == null)
{
insertlists.Add(item);
}
else
{
Editor_CostReport update = Funs.DB.Editor_CostReport.FirstOrDefault(x => x.CostReportId == mewCostReport.CostReportId);
if (update != null)
{
update.EProjectId = item.EProjectId;
update.Monthly = item.Monthly;
update.OrginalBudget = item.OrginalBudget;
update.ChangedBudget = item.ChangedBudget;
update.ActualCost = item.ActualCost;
update.CommittedPRPO = item.CommittedPRPO;
update.CommittedSSRs = item.CommittedSSRs;
update.CostToComplete = item.CostToComplete;
Funs.DB.SubmitChanges();
}
}
}
if (insertlists.Count > 0)
{
Funs.DB.Editor_CostReport.InsertAllOnSubmit(insertlists);
Funs.DB.SubmitChanges();
}
//更新Control中的OrginalBudget和CM中Cost to complete
foreach (var item in costReportLists)
{
var eproject = BLL.EProjectService.GeteProjectById(item.EProjectId);
eproject.EProjectId = item.EProjectId;
eproject.ProjectControl_OrginalBudget = item.OrginalBudget;
eproject.ProjectControl_ChangedBudget = item.ChangedBudget;
eproject.ProjectControl_Actual = item.ActualCost;
eproject.ProjectControl_CommittedPRPO = item.CommittedPRPO;
eproject.ProjectControl_CommittedSSRs = item.CommittedSSRs;
eproject.PM_General_CostToComplete = item.CostToComplete;
eproject.ModifyDate = DateTime.Now;
eproject.ModifyPerson = CurrUser.UserId;
BLL.EProjectService.UpdateOrginalBudget(eproject);
}
ShowNotify("Import success!", MessageBoxIcon.Success);
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
else
{
ShowAlert("No data!", MessageBoxIcon.Warning);
return;
}
}
catch (Exception ex)
{
ShowAlert("'" + ex.Message + "'", MessageBoxIcon.Warning);
}
}
#endregion
}
}