using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace BLL
{
    public static class WorkloadStatisticsService
    {
        /// 
        /// 获取模拟树表格
        /// 
        /// 
        public static DataTable GetTreeDataTable(string projectId, DateTime months, string cnProfessionId, string unitProjectCode, string wbsSetCode)
        {
            List CostControlDetailStatisticsList = new List();
            DataTable table = new DataTable();
            table.Columns.Add(new DataColumn("Id", typeof(String)));
            table.Columns.Add(new DataColumn("SupId", typeof(String)));
            table.Columns.Add(new DataColumn("Name", typeof(String)));
            table.Columns.Add(new DataColumn("ProjectId", typeof(String)));
            table.Columns.Add(new DataColumn("Unit", typeof(String)));
            table.Columns.Add(new DataColumn("TotalNum", typeof(String)));
            table.Columns.Add(new DataColumn("RealPrice", typeof(String)));
            table.Columns.Add(new DataColumn("PlanNum", typeof(String)));
            table.Columns.Add(new DataColumn("ThisNum", typeof(String)));
            table.Columns.Add(new DataColumn("PlanPrice", typeof(String)));
            table.Columns.Add(new DataColumn("ThisRealCost", typeof(String)));
            table.Columns.Add(new DataColumn("ThisPlanValue", typeof(String)));
            table.Columns.Add(new DataColumn("ThisPlanCost", typeof(String)));
            table.Columns.Add(new DataColumn("TotalPlanNum", typeof(String)));
            table.Columns.Add(new DataColumn("TotalThisNum", typeof(String)));
            table.Columns.Add(new DataColumn("TotalRealCost", typeof(String)));
            table.Columns.Add(new DataColumn("TotalPlanValue", typeof(String)));
            table.Columns.Add(new DataColumn("TotalPlanCost", typeof(String)));
            Model.SGGLDB db = Funs.DB;
            var costControls = from x in db.WBS_CostControl where x.ProjectId == projectId select x;
            var details = from x in db.View_WBS_CostControlDetail where x.ProjectId == projectId select x;
            var parentDetails = from x in db.View_WBS_CostControlParentDetail select x;
            var wbsSets = from x in db.Wbs_WbsSet where x.ProjectId == projectId select x;
            var wbsSetInits = from x in db.WBS_WbsSetInit select x;
            var unitProjects = from x in db.Wbs_UnitProject where x.ProjectId == projectId select x;
            var unitProjectInits = from x in db.Wbs_UnitProjectInit select x;
            var cnProfessions = from x in db.WBS_CnProfession where x.ProjectId == projectId select x;
            var cnProfessionInits = from x in db.WBS_CnProfessionInit select x;
            var installations = from x in db.Project_Installation where x.ProjectId == projectId select x;
            CostControlDetailStatisticsList = (from x in db.View_WBS_CostControlDetailStatistics where x.ProjectId == projectId select x).Distinct().ToList();
            DataRow row; DataRow parentRow;
            #region 全部统计
            if (cnProfessionId == BLL.Const._Null)   //全部统计
            {
                foreach (Model.View_WBS_CostControlDetailStatistics item in CostControlDetailStatisticsList)
                {
                    row = table.NewRow();
                    row[0] = item.Id;
                    row[1] = item.SupId;
                    row[2] = item.Name;
                    row[3] = item.ProjectId;
                    Model.WBS_CostControl costControl = costControls.FirstOrDefault(x => x.CostControlId == item.Id);
                    if (costControl != null)
                    {
                        Model.View_WBS_CostControlDetail detail = details.FirstOrDefault(x => x.CostControlId == item.Id && x.Months == months);
                        if (detail != null)
                        {
                            row[4] = detail.Unit;//单位
                            row[5] = decimal.Round(Convert.ToDecimal(detail.TotalNum), 2);//合同工作量
                            row[6] = decimal.Round(Convert.ToDecimal(detail.RealPrice), 2);//成本单价
                            row[7] = decimal.Round(Convert.ToDecimal(detail.PlanNum), 2);//本月计划完成量
                            row[8] = decimal.Round(Convert.ToDecimal(detail.ThisNum), 2);//本月完成量
                            row[9] = decimal.Round(Convert.ToDecimal(detail.PlanPrice), 2);//控制预算单价
                            row[10] = decimal.Round(Convert.ToDecimal(detail.ThisRealCost), 2);//本月实际成本
                            row[11] = decimal.Round(Convert.ToDecimal(detail.ThisPlanValue), 2);//本月计划完成预算
                            row[12] = decimal.Round(Convert.ToDecimal(detail.ThisPlanCost), 2);//本月完成预算
                            row[13] = decimal.Round(Convert.ToDecimal(detail.TotalPlanNum), 2);//累计计划完成量
                            row[14] = decimal.Round(Convert.ToDecimal(detail.TotalThisNum), 2);//累计完成量
                            row[15] = decimal.Round(Convert.ToDecimal(detail.TotalRealCost), 2);//累计完成成本
                            row[16] = decimal.Round(Convert.ToDecimal(detail.TotalPlanValue), 2);//累计计划完成预算
                            row[17] = decimal.Round(Convert.ToDecimal(detail.TotalPlanCost), 2);//累计完成预算
                        }
                        else
                        {
                            Model.View_WBS_CostControlDetail lastDetail = details.OrderByDescending(x => x.Months).FirstOrDefault(x => x.CostControlId == item.Id);
                            if (lastDetail != null && lastDetail.Months <= months)
                            {
                                row[4] = lastDetail.Unit;//单位
                                row[5] = decimal.Round(Convert.ToDecimal(lastDetail.TotalNum), 2);//合同工作量
                                row[6] = decimal.Round(Convert.ToDecimal(lastDetail.RealPrice), 2);//成本单价
                                row[7] = decimal.Round(Convert.ToDecimal(lastDetail.PlanNum), 2);//本月计划完成量
                                row[8] = decimal.Round(Convert.ToDecimal(lastDetail.ThisNum), 2);//本月完成量
                                row[9] = decimal.Round(Convert.ToDecimal(lastDetail.PlanPrice), 2);//控制预算单价
                                row[13] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanNum), 2);//累计计划完成量
                                row[14] = decimal.Round(Convert.ToDecimal(lastDetail.TotalThisNum), 2);//累计完成量
                                row[15] = decimal.Round(Convert.ToDecimal(lastDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanCost), 2);//累计完成预算
                            }
                        }
                    }
                    else
                    {
                        Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                        if (parentDetail != null)
                        {
                            row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                            row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                            row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                            row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                            row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                            row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                        }
                        else
                        {
                            Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                            if (parentLastDetail != null && parentLastDetail.Months <= months)
                            {
                                row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                            }
                        }
                    }
                    table.Rows.Add(row);
                }
            }
            #endregion
            else
            {
                decimal d10 = 0, d11 = 0, d12 = 0, d15 = 0, d16 = 0, d17 = 0;
                #region  按专业
                if (unitProjectCode == BLL.Const._Null)
                {
                    CostControlDetailStatisticsList = (from x in CostControlDetailStatisticsList where (x.WBSType == "Installation" && x.Name != "总图") || (x.WBSType != "Installation" && x.OldCnProfessionId.ToString() == cnProfessionId) select x).ToList();
                    var cn = cnProfessionInits.FirstOrDefault(x => x.CnProfessionId.ToString() == cnProfessionId);
                    parentRow = table.NewRow();
                    parentRow[0] = cn.CnProfessionId.ToString();
                    parentRow[1] = "0";
                    parentRow[2] = cn.CnProfessionName;
                    parentRow[3] = projectId;
                    foreach (Model.View_WBS_CostControlDetailStatistics item in CostControlDetailStatisticsList)
                    {
                        row = table.NewRow();
                        row[0] = item.Id;
                        row[2] = item.Name;
                        row[3] = item.ProjectId;
                        if (item.WBSType == "Installation")
                        {
                            if (item.SupId == "0")
                            {
                                row[1] = cn.CnProfessionId.ToString();
                            }
                            else
                            {
                                row[1] = item.SupId;
                            }
                            string cnId = BLL.CnProfessionService.GetCnProfessionIdByInstallationIdAndOldId(item.Id, cnProfessionId);
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == cnId && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == cnId);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "CnProfession")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                                d10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                d11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                d12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                d15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                d16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                d17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                    d15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                    d16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                    d17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                }
                            }
                        }
                        else if (item.WBSType == "UnitProject")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "WbsSet")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "CostControl")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlDetail detail = details.FirstOrDefault(x => x.CostControlId == item.Id && x.Months == months);
                            if (detail != null)
                            {
                                row[4] = detail.Unit;//单位
                                row[5] = decimal.Round(Convert.ToDecimal(detail.TotalNum), 2);//合同工作量
                                row[6] = decimal.Round(Convert.ToDecimal(detail.RealPrice), 2);//成本单价
                                row[7] = decimal.Round(Convert.ToDecimal(detail.PlanNum), 2);//本月计划完成量
                                row[8] = decimal.Round(Convert.ToDecimal(detail.ThisNum), 2);//本月完成量
                                row[9] = decimal.Round(Convert.ToDecimal(detail.PlanPrice), 2);//控制预算单价
                                row[10] = decimal.Round(Convert.ToDecimal(detail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(detail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(detail.ThisPlanCost), 2);//本月完成预算
                                row[13] = decimal.Round(Convert.ToDecimal(detail.TotalPlanNum), 2);//累计计划完成量
                                row[14] = decimal.Round(Convert.ToDecimal(detail.TotalThisNum), 2);//累计完成量
                                row[15] = decimal.Round(Convert.ToDecimal(detail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(detail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(detail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlDetail lastDetail = details.OrderByDescending(x => x.Months).FirstOrDefault(x => x.CostControlId == item.Id);
                                if (lastDetail != null && lastDetail.Months <= months)
                                {
                                    row[4] = lastDetail.Unit;//单位
                                    row[5] = decimal.Round(Convert.ToDecimal(lastDetail.TotalNum), 2);//合同工作量
                                    row[6] = decimal.Round(Convert.ToDecimal(lastDetail.RealPrice), 2);//成本单价
                                    row[7] = decimal.Round(Convert.ToDecimal(lastDetail.PlanNum), 2);//本月计划完成量
                                    row[8] = decimal.Round(Convert.ToDecimal(lastDetail.ThisNum), 2);//本月完成量
                                    row[9] = decimal.Round(Convert.ToDecimal(lastDetail.PlanPrice), 2);//控制预算单价
                                    row[13] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanNum), 2);//累计计划完成量
                                    row[14] = decimal.Round(Convert.ToDecimal(lastDetail.TotalThisNum), 2);//累计完成量
                                    row[15] = decimal.Round(Convert.ToDecimal(lastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        table.Rows.Add(row);
                    }
                    if (d10 != 0)
                    {
                        parentRow[10] = decimal.Round(d10, 2);//本月实际成本
                        parentRow[11] = decimal.Round(d11, 2);//本月计划完成预算
                        parentRow[12] = decimal.Round(d12, 2);//本月完成预算
                        parentRow[15] = decimal.Round(d15, 2);//累计完成成本
                        parentRow[16] = decimal.Round(d16, 2);//累计计划完成预算
                        parentRow[17] = decimal.Round(d17, 2);//累计完成预算
                    }
                    else
                    {
                        parentRow[15] = decimal.Round(d15, 2);//累计完成成本
                        parentRow[16] = decimal.Round(d16, 2);//累计计划完成预算
                        parentRow[17] = decimal.Round(d17, 2);//累计完成预算
                    }
                    table.Rows.Add(parentRow);
                }
                #endregion
                #region   按分部工程
                else if (wbsSetCode == BLL.Const._Null)
                {
                    CostControlDetailStatisticsList = (from x in CostControlDetailStatisticsList where (x.WBSType == "Installation" && x.Name != "总图") || (x.WBSType == "CnProfession" && x.OldCnProfessionId.ToString() == cnProfessionId) || (x.WBSType != "Installation" && x.WBSType != "CnProfession" && x.OldCnProfessionId.ToString() == cnProfessionId && x.OldUnitProjectCode == unitProjectCode) select x).ToList();
                    var up = unitProjectInits.FirstOrDefault(x => x.UnitProjectCode == unitProjectCode);
                    parentRow = table.NewRow();
                    parentRow[0] = up.UnitProjectCode;
                    parentRow[1] = "0";
                    parentRow[2] = up.UnitProjectName;
                    parentRow[3] = projectId;
                    foreach (Model.View_WBS_CostControlDetailStatistics item in CostControlDetailStatisticsList)
                    {
                        row = table.NewRow();
                        row[0] = item.Id;
                        row[2] = item.Name;
                        row[3] = item.ProjectId;
                        if (item.WBSType == "Installation")
                        {
                            decimal ind10 = 0, ind11 = 0, ind12 = 0, ind15 = 0, ind16 = 0, ind17 = 0;
                            if (item.SupId == "0")
                            {
                                row[1] = up.UnitProjectCode;
                            }
                            else
                            {
                                row[1] = item.SupId;
                            }
                            List unitProjectIds = BLL.UnitProjectService.GetUnitProjectIdByInstallationIdAndCNCodeAndUnitProjectCode(item.Id, cnProfessionId, unitProjectCode);
                            foreach (var upId in unitProjectIds)
                            {
                                Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == upId && x.Months == months);
                                if (parentDetail != null)
                                {
                                    ind10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                    ind11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                    ind12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                    ind15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                    ind16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                    ind17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                                }
                                else
                                {
                                    Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == upId);
                                    if (parentLastDetail != null && parentLastDetail.Months <= months)
                                    {
                                        ind15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                        ind16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                        ind17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                    }
                                }
                            }
                            if (ind10 != 0)
                            {
                                row[10] = decimal.Round(ind10, 2);//本月实际成本
                                row[11] = decimal.Round(ind11, 2);//本月计划完成预算
                                row[12] = decimal.Round(ind12, 2);//本月完成预算
                                row[15] = decimal.Round(ind15, 2);//累计完成成本
                                row[16] = decimal.Round(ind16, 2);//累计计划完成预算
                                row[17] = decimal.Round(ind17, 2);//累计完成预算
                            }
                            else
                            {
                                if (ind15 > 0)
                                {
                                    row[15] = decimal.Round(ind15, 2);//累计完成成本
                                }
                                if (ind16 > 0)
                                {
                                    row[16] = decimal.Round(ind16, 2);//累计计划完成预算
                                }
                                if (ind17 > 0)
                                {
                                    row[17] = decimal.Round(ind17, 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "CnProfession")
                        {
                            decimal cnd10 = 0, cnd11 = 0, cnd12 = 0, cnd15 = 0, cnd16 = 0, cnd17 = 0;
                            row[1] = item.SupId;
                            List unitProjectIds = BLL.UnitProjectService.GetUnitProjectIdByCnProfessionIdAndUnitProjectCode(item.Id, unitProjectCode);
                            foreach (var upId in unitProjectIds)
                            {
                                Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == upId && x.Months == months);
                                if (parentDetail != null)
                                {
                                    cnd10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                    cnd11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                    cnd12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                    cnd15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                    cnd16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                    cnd17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                                }
                                else
                                {
                                    Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == upId);
                                    if (parentLastDetail != null && parentLastDetail.Months <= months)
                                    {
                                        cnd15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                        cnd16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                        cnd17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                    }
                                }
                            }
                            if (cnd10 != 0)
                            {
                                row[10] = decimal.Round(cnd10, 2);//本月实际成本
                                row[11] = decimal.Round(cnd11, 2);//本月计划完成预算
                                row[12] = decimal.Round(cnd12, 2);//本月完成预算
                                row[15] = decimal.Round(cnd15, 2);//累计完成成本
                                row[16] = decimal.Round(cnd16, 2);//累计计划完成预算
                                row[17] = decimal.Round(cnd17, 2);//累计完成预算
                            }
                            else
                            {
                                if (cnd15 > 0)
                                {
                                    row[15] = decimal.Round(cnd15, 2);//累计完成成本
                                }
                                if (cnd16 > 0)
                                {
                                    row[16] = decimal.Round(cnd16, 2);//累计计划完成预算
                                }
                                if (cnd17 > 0)
                                {
                                    row[17] = decimal.Round(cnd17, 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "UnitProject")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                                d10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                d11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                d12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                d15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                d16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                d17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                    d15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                    d16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                    d17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                }
                            }
                        }
                        else if (item.WBSType == "WbsSet")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "CostControl")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlDetail detail = details.FirstOrDefault(x => x.CostControlId == item.Id && x.Months == months);
                            if (detail != null)
                            {
                                row[4] = detail.Unit;//单位
                                row[5] = decimal.Round(Convert.ToDecimal(detail.TotalNum), 2);//合同工作量
                                row[6] = decimal.Round(Convert.ToDecimal(detail.RealPrice), 2);//成本单价
                                row[7] = decimal.Round(Convert.ToDecimal(detail.PlanNum), 2);//本月计划完成量
                                row[8] = decimal.Round(Convert.ToDecimal(detail.ThisNum), 2);//本月完成量
                                row[9] = decimal.Round(Convert.ToDecimal(detail.PlanPrice), 2);//控制预算单价
                                row[10] = decimal.Round(Convert.ToDecimal(detail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(detail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(detail.ThisPlanCost), 2);//本月完成预算
                                row[13] = decimal.Round(Convert.ToDecimal(detail.TotalPlanNum), 2);//累计计划完成量
                                row[14] = decimal.Round(Convert.ToDecimal(detail.TotalThisNum), 2);//累计完成量
                                row[15] = decimal.Round(Convert.ToDecimal(detail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(detail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(detail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlDetail lastDetail = details.OrderByDescending(x => x.Months).FirstOrDefault(x => x.CostControlId == item.Id);
                                if (lastDetail != null && lastDetail.Months <= months)
                                {
                                    row[4] = lastDetail.Unit;//单位
                                    row[5] = decimal.Round(Convert.ToDecimal(lastDetail.TotalNum), 2);//合同工作量
                                    row[6] = decimal.Round(Convert.ToDecimal(lastDetail.RealPrice), 2);//成本单价
                                    row[7] = decimal.Round(Convert.ToDecimal(lastDetail.PlanNum), 2);//本月计划完成量
                                    row[8] = decimal.Round(Convert.ToDecimal(lastDetail.ThisNum), 2);//本月完成量
                                    row[9] = decimal.Round(Convert.ToDecimal(lastDetail.PlanPrice), 2);//控制预算单价
                                    row[13] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanNum), 2);//累计计划完成量
                                    row[14] = decimal.Round(Convert.ToDecimal(lastDetail.TotalThisNum), 2);//累计完成量
                                    row[15] = decimal.Round(Convert.ToDecimal(lastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        table.Rows.Add(row);
                    }
                    if (d10 != 0)
                    {
                        parentRow[10] = decimal.Round(d10, 2);//本月实际成本
                        parentRow[11] = decimal.Round(d11, 2);//本月计划完成预算
                        parentRow[12] = decimal.Round(d12, 2);//本月完成预算
                        parentRow[15] = decimal.Round(d15, 2);//累计完成成本
                        parentRow[16] = decimal.Round(d16, 2);//累计计划完成预算
                        parentRow[17] = decimal.Round(d17, 2);//累计完成预算
                    }
                    else
                    {
                        parentRow[15] = decimal.Round(d15, 2);//累计完成成本
                        parentRow[16] = decimal.Round(d16, 2);//累计计划完成预算
                        parentRow[17] = decimal.Round(d17, 2);//累计完成预算
                    }
                    table.Rows.Add(parentRow);
                }
                #endregion
                #region  按工作包
                else
                {
                    CostControlDetailStatisticsList = (from x in CostControlDetailStatisticsList where (x.WBSType == "Installation" && x.Name != "总图") || (x.WBSType == "CnProfession" && x.OldCnProfessionId.ToString() == cnProfessionId) || (x.WBSType == "UnitProject" && x.OldCnProfessionId.ToString() == cnProfessionId && x.OldUnitProjectCode == unitProjectCode) || (x.WBSType != "Installation" && x.WBSType != "CnProfession" && x.WBSType != "UnitProject" && x.OldCnProfessionId.ToString() == cnProfessionId && x.OldUnitProjectCode == unitProjectCode && x.OldWbsSetCode == wbsSetCode) select x).ToList();
                    var wb = wbsSetInits.FirstOrDefault(x => x.WbsSetCode == wbsSetCode);
                    parentRow = table.NewRow();
                    parentRow[0] = wb.WbsSetCode;
                    parentRow[1] = "0";
                    parentRow[2] = wb.WbsSetName;
                    parentRow[3] = projectId;
                    foreach (Model.View_WBS_CostControlDetailStatistics item in CostControlDetailStatisticsList)
                    {
                        row = table.NewRow();
                        row[0] = item.Id;
                        row[2] = item.Name;
                        row[3] = item.ProjectId;
                        if (item.WBSType == "Installation")
                        {
                            decimal ind10 = 0, ind11 = 0, ind12 = 0, ind15 = 0, ind16 = 0, ind17 = 0;
                            if (item.SupId == "0")
                            {
                                row[1] = wb.WbsSetCode;
                            }
                            else
                            {
                                row[1] = item.SupId;
                            }
                            List wbsSetIds = BLL.WbsSetService.GetWbsSetIdByInstallationIdAndCNCodeAndUnitProjectCodeAndWbsSetCode(item.Id, cnProfessionId, unitProjectCode, wbsSetCode);
                            foreach (var upId in wbsSetIds)
                            {
                                Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == upId && x.Months == months);
                                if (parentDetail != null)
                                {
                                    ind10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                    ind11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                    ind12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                    ind15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                    ind16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                    ind17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                                }
                                else
                                {
                                    Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == upId);
                                    if (parentLastDetail != null && parentLastDetail.Months <= months)
                                    {
                                        ind15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                        ind16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                        ind17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                    }
                                }
                            }
                            if (ind10 != 0)
                            {
                                row[10] = decimal.Round(ind10, 2);//本月实际成本
                                row[11] = decimal.Round(ind11, 2);//本月计划完成预算
                                row[12] = decimal.Round(ind12, 2);//本月完成预算
                                row[15] = decimal.Round(ind15, 2);//累计完成成本
                                row[16] = decimal.Round(ind16, 2);//累计计划完成预算
                                row[17] = decimal.Round(ind17, 2);//累计完成预算
                            }
                            else
                            {
                                if (ind15 > 0)
                                {
                                    row[15] = decimal.Round(ind15, 2);//累计完成成本
                                }
                                if (ind16 > 0)
                                {
                                    row[16] = decimal.Round(ind16, 2);//累计计划完成预算
                                }
                                if (ind17 > 0)
                                {
                                    row[17] = decimal.Round(ind17, 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "CnProfession")
                        {
                            decimal cnd10 = 0, cnd11 = 0, cnd12 = 0, cnd15 = 0, cnd16 = 0, cnd17 = 0;
                            row[1] = item.SupId;
                            List wbsSetIds = BLL.WbsSetService.GetWbsSetIdByCnProfessionIdAndUnitProjectCodeAndWbsSetCode(item.Id, unitProjectCode, wbsSetCode);
                            foreach (var upId in wbsSetIds)
                            {
                                Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == upId && x.Months == months);
                                if (parentDetail != null)
                                {
                                    cnd10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                    cnd11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                    cnd12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                    cnd15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                    cnd16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                    cnd17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                                }
                                else
                                {
                                    Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == upId);
                                    if (parentLastDetail != null && parentLastDetail.Months <= months)
                                    {
                                        cnd15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                        cnd16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                        cnd17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                    }
                                }
                            }
                            if (cnd10 != 0)
                            {
                                row[10] = decimal.Round(cnd10, 2);//本月实际成本
                                row[11] = decimal.Round(cnd11, 2);//本月计划完成预算
                                row[12] = decimal.Round(cnd12, 2);//本月完成预算
                                row[15] = decimal.Round(cnd15, 2);//累计完成成本
                                row[16] = decimal.Round(cnd16, 2);//累计计划完成预算
                                row[17] = decimal.Round(cnd17, 2);//累计完成预算
                            }
                            else
                            {
                                if (cnd15 > 0)
                                {
                                    row[15] = decimal.Round(cnd15, 2);//累计完成成本
                                }
                                if (cnd16 > 0)
                                {
                                    row[16] = decimal.Round(cnd16, 2);//累计计划完成预算
                                }
                                if (cnd17 > 0)
                                {
                                    row[17] = decimal.Round(cnd17, 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "UnitProject")
                        {
                            decimal upd10 = 0, upd11 = 0, upd12 = 0, upd15 = 0, upd16 = 0, upd17 = 0;
                            row[1] = item.SupId;
                            List wbsSetIds = BLL.WbsSetService.GetWbsSetIdByUnitProjectIdAndWbsSetCode(item.Id, wbsSetCode);
                            foreach (var upId in wbsSetIds)
                            {
                                Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == upId && x.Months == months);
                                if (parentDetail != null)
                                {
                                    upd10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                    upd11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                    upd12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                    upd15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                    upd16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                    upd17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                                }
                                else
                                {
                                    Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == upId);
                                    if (parentLastDetail != null && parentLastDetail.Months <= months)
                                    {
                                        upd15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                        upd16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                        upd17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                    }
                                }
                            }
                            if (upd10 != 0)
                            {
                                row[10] = decimal.Round(upd10, 2);//本月实际成本
                                row[11] = decimal.Round(upd11, 2);//本月计划完成预算
                                row[12] = decimal.Round(upd12, 2);//本月完成预算
                                row[15] = decimal.Round(upd15, 2);//累计完成成本
                                row[16] = decimal.Round(upd16, 2);//累计计划完成预算
                                row[17] = decimal.Round(upd17, 2);//累计完成预算
                            }
                            else
                            {
                                if (upd15 > 0)
                                {
                                    row[15] = decimal.Round(upd15, 2);//累计完成成本
                                }
                                if (upd16 > 0)
                                {
                                    row[16] = decimal.Round(upd16, 2);//累计计划完成预算
                                }
                                if (upd17 > 0)
                                {
                                    row[17] = decimal.Round(upd17, 2);//累计完成预算
                                }
                            }
                        }
                        else if (item.WBSType == "WbsSet")
                        {
                            row[1] = item.SupId;
                            var wbsSet = wbsSets.FirstOrDefault(x => x.WbsSetId == item.Id);
                            Model.View_WBS_CostControlParentDetail parentDetail = parentDetails.FirstOrDefault(x => x.ParentId == item.Id && x.Months == months);
                            if (parentDetail != null)
                            {
                                row[10] = decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);//本月完成预算
                                row[15] = decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);//累计完成预算
                                if (wbsSet.SuperWbsSetId == null)  //工作包节点,累加数据
                                {
                                    d10 += decimal.Round(Convert.ToDecimal(parentDetail.ThisRealCost), 2);
                                    d11 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanValue), 2);
                                    d12 += decimal.Round(Convert.ToDecimal(parentDetail.ThisPlanCost), 2);
                                    d15 += decimal.Round(Convert.ToDecimal(parentDetail.TotalRealCost), 2);
                                    d16 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanValue), 2);
                                    d17 += decimal.Round(Convert.ToDecimal(parentDetail.TotalPlanCost), 2);
                                }
                            }
                            else
                            {
                                Model.View_WBS_CostControlParentDetail parentLastDetail = parentDetails.OrderByDescending(x => x.Months).FirstOrDefault(x => x.ParentId == item.Id);
                                if (parentLastDetail != null && parentLastDetail.Months <= months)
                                {
                                    row[15] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);//累计完成预算
                                    if (wbsSet.SuperWbsSetId == null)  //工作包节点,累加数据
                                    {
                                        d15 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalRealCost), 2);
                                        d16 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanValue), 2);
                                        d17 += decimal.Round(Convert.ToDecimal(parentLastDetail.TotalPlanCost), 2);
                                    }
                                }
                            }
                        }
                        else if (item.WBSType == "CostControl")
                        {
                            row[1] = item.SupId;
                            Model.View_WBS_CostControlDetail detail = details.FirstOrDefault(x => x.CostControlId == item.Id && x.Months == months);
                            if (detail != null)
                            {
                                row[4] = detail.Unit;//单位
                                row[5] = decimal.Round(Convert.ToDecimal(detail.TotalNum), 2);//合同工作量
                                row[6] = decimal.Round(Convert.ToDecimal(detail.RealPrice), 2);//成本单价
                                row[7] = decimal.Round(Convert.ToDecimal(detail.PlanNum), 2);//本月计划完成量
                                row[8] = decimal.Round(Convert.ToDecimal(detail.ThisNum), 2);//本月完成量
                                row[9] = decimal.Round(Convert.ToDecimal(detail.PlanPrice), 2);//控制预算单价
                                row[10] = decimal.Round(Convert.ToDecimal(detail.ThisRealCost), 2);//本月实际成本
                                row[11] = decimal.Round(Convert.ToDecimal(detail.ThisPlanValue), 2);//本月计划完成预算
                                row[12] = decimal.Round(Convert.ToDecimal(detail.ThisPlanCost), 2);//本月完成预算
                                row[13] = decimal.Round(Convert.ToDecimal(detail.TotalPlanNum), 2);//累计计划完成量
                                row[14] = decimal.Round(Convert.ToDecimal(detail.TotalThisNum), 2);//累计完成量
                                row[15] = decimal.Round(Convert.ToDecimal(detail.TotalRealCost), 2);//累计完成成本
                                row[16] = decimal.Round(Convert.ToDecimal(detail.TotalPlanValue), 2);//累计计划完成预算
                                row[17] = decimal.Round(Convert.ToDecimal(detail.TotalPlanCost), 2);//累计完成预算
                            }
                            else
                            {
                                Model.View_WBS_CostControlDetail lastDetail = details.OrderByDescending(x => x.Months).FirstOrDefault(x => x.CostControlId == item.Id);
                                if (lastDetail != null && lastDetail.Months <= months)
                                {
                                    row[4] = lastDetail.Unit;//单位
                                    row[5] = decimal.Round(Convert.ToDecimal(lastDetail.TotalNum), 2);//合同工作量
                                    row[6] = decimal.Round(Convert.ToDecimal(lastDetail.RealPrice), 2);//成本单价
                                    row[7] = decimal.Round(Convert.ToDecimal(lastDetail.PlanNum), 2);//本月计划完成量
                                    row[8] = decimal.Round(Convert.ToDecimal(lastDetail.ThisNum), 2);//本月完成量
                                    row[9] = decimal.Round(Convert.ToDecimal(lastDetail.PlanPrice), 2);//控制预算单价
                                    row[13] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanNum), 2);//累计计划完成量
                                    row[14] = decimal.Round(Convert.ToDecimal(lastDetail.TotalThisNum), 2);//累计完成量
                                    row[15] = decimal.Round(Convert.ToDecimal(lastDetail.TotalRealCost), 2);//累计完成成本
                                    row[16] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanValue), 2);//累计计划完成预算
                                    row[17] = decimal.Round(Convert.ToDecimal(lastDetail.TotalPlanCost), 2);//累计完成预算
                                }
                            }
                        }
                        table.Rows.Add(row);
                    }
                    if (d10 != 0)
                    {
                        parentRow[10] = decimal.Round(d10, 2);//本月实际成本
                        parentRow[11] = decimal.Round(d11, 2);//本月计划完成预算
                        parentRow[12] = decimal.Round(d12, 2);//本月完成预算
                        parentRow[15] = decimal.Round(d15, 2);//累计完成成本
                        parentRow[16] = decimal.Round(d16, 2);//累计计划完成预算
                        parentRow[17] = decimal.Round(d17, 2);//累计完成预算
                    }
                    else
                    {
                        parentRow[15] = decimal.Round(d15, 2);//累计完成成本
                        parentRow[16] = decimal.Round(d16, 2);//累计计划完成预算
                        parentRow[17] = decimal.Round(d17, 2);//累计完成预算
                    }
                    table.Rows.Add(parentRow);
                }
                #endregion
            }
            return table;
        }
    }
}