using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
namespace BLL
{
    public static class WBSReportService
    {
        /// 
        /// 获取模拟树表格
        /// 
        /// 
        public static DataTable GetTreeDataTable(string projectId, int year, int month, string[] installationId, string[] cnProfessionId, int marking2, string rectificationMeasureType)
        {
            List WBSReportList = new List();
            List newInstallationList = new List();
            List newCNProfessionList = 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("Weights", typeof(String)));
            table.Columns.Add(new DataColumn("WeightsMoneyTotal", typeof(String)));
            table.Columns.Add(new DataColumn("CompleteValue", typeof(String)));
            table.Columns.Add(new DataColumn("CompleteValueTotal", typeof(String)));
            table.Columns.Add(new DataColumn("CompleteValuePercentage", typeof(String)));
            table.Columns.Add(new DataColumn("RealValue", typeof(String)));
            table.Columns.Add(new DataColumn("RealValueTotal", typeof(String)));
            table.Columns.Add(new DataColumn("SVCost", typeof(String)));
            table.Columns.Add(new DataColumn("CVCost", typeof(String)));
            table.Columns.Add(new DataColumn("StartDate", typeof(String)));
            table.Columns.Add(new DataColumn("EndDate", typeof(String)));
            table.Columns.Add(new DataColumn("PlanValue", typeof(String)));
            table.Columns.Add(new DataColumn("PlanValueTotal", typeof(String)));
            table.Columns.Add(new DataColumn("PlanValuePercentage", typeof(String)));
            table.Columns.Add(new DataColumn("PlanValueRateTotal", typeof(String)));
            table.Columns.Add(new DataColumn("IsLeaf", typeof(String)));
            table.Columns.Add(new DataColumn("RectificationMeasureNames", typeof(String)));
            //时间筛选
            WBSReportList = (from x in Funs.DB.View_WBSReport where x.ProjectId == projectId && x.Months.Value.Year == year && x.Months.Value.Month == month select x).Distinct().ToList();
            //单位工程、子单位工程、分部工程、子分部工程、分项工程、子分项工程  筛选
            if (marking2 != 0)
            {
                WBSReportList = (from x in WBSReportList where x.Marking2 <= marking2 select x).Distinct().ToList();
            }
            #region 装置筛选
            if (installationId != null)
            {
                if (installationId.Length > 0)
                {
                    List allId1 = new List();
                    List allId2 = new List();
                    List allId3 = new List();
                    List allId4 = new List();
                    List allId5 = new List();
                    List allId6 = new List();
                    List allId7 = new List();
                    foreach (var item in installationId)  //在集合中获取Id或SupId是所选中装置Id的记录的Id
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId1.Contains(r.Id))
                                {
                                    allId1.Add(r.Id);//装置和专业记录的Id
                                }
                            }
                        }
                    }
                    foreach (string item in allId1)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId2.Contains(r.Id))
                                {
                                    allId2.Add(r.Id);//装置、专业、单位工程所在记录的Id
                                }
                            }
                        }
                    }
                    foreach (string item in allId2)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId3.Contains(r.Id))
                                {
                                    allId3.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allId3)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId4.Contains(r.Id))
                                {
                                    allId4.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allId4)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId5.Contains(r.Id))
                                {
                                    allId5.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allId5)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId6.Contains(r.Id))
                                {
                                    allId6.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allId6)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allId7.Contains(r.Id))
                                {
                                    allId7.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allId7)
                    {
                        Model.View_WBSReport report = (from x in WBSReportList where x.Id == item select x).FirstOrDefault();
                        newInstallationList.Add(report);//根据所选最终Id获取对应记录
                    }
                    WBSReportList = newInstallationList;//最终集合
                }
            }
            #endregion
            #region 专业筛选
            if (cnProfessionId != null)
            {
                if (cnProfessionId.Length > 0)
                {
                    List allIdA = new List();
                    List allIdB = new List();
                    List allIdC = new List();
                    List allIdD = new List();
                    List allIdE = new List();
                    List allIdF = new List();
                    foreach (string item in cnProfessionId)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)//WBSReportList为以上条件筛选后的集合
                        {
                            Model.WBS_CnProfession cnp = (from x in Funs.DB.WBS_CnProfession where x.CnProfessionId == r.Id select x).FirstOrDefault();
                            if (cnp != null)
                            {
                                if (cnp.OldId.ToString() == item)//判断集合中的专业记录是否筛选条件
                                {
                                    allIdA.Add(r.Id);//所有装置下符合所选专业条件记录的Id
                                }
                            }
                        }
                    }
                    foreach (string item in allIdA)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allIdB.Contains(r.Id))
                                {
                                    allIdB.Add(r.Id);//符合条件的专业Id、和对应专业下单位工程的Id
                                }
                            }
                        }
                    }
                    foreach (string item in allIdB)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allIdC.Contains(r.Id))
                                {
                                    allIdC.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allIdC)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allIdD.Contains(r.Id))
                                {
                                    allIdD.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allIdD)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allIdE.Contains(r.Id))
                                {
                                    allIdE.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allIdE)
                    {
                        foreach (Model.View_WBSReport r in WBSReportList)
                        {
                            if (item == r.Id || item == r.SupId)
                            {
                                if (!allIdF.Contains(r.Id))
                                {
                                    allIdF.Add(r.Id);
                                }
                            }
                        }
                    }
                    foreach (string item in allIdF)//allIdF  最终符合条件的记录主键
                    {
                        Model.View_WBSReport report = (from x in WBSReportList where x.Id == item select x).FirstOrDefault();
                        if (report != null)
                        {
                            newCNProfessionList.Add(report);//在集合中获取记录
                        }
                    }
                    if (installationId != null)
                    {
                        if (installationId.Length > 0)
                        {
                            foreach (string item in installationId)
                            {
                                Model.View_WBSReport report = (from x in WBSReportList where x.Id == item select x).FirstOrDefault();
                                if (report != null)
                                {
                                    newCNProfessionList.Add(report);//当装置空时添加所选装置对应记录
                                }
                            }
                        }
                        else
                        {
                            List InstallationList = (from x in Funs.DB.WBS_UnitWork where x.ProjectId == projectId && (x.SuperUnitWork == null || x.SuperUnitWork == "0") select x).ToList();//装置条件为空时获取对应操作表中所有的装置
                            foreach (Model.WBS_UnitWork item in InstallationList)
                            {
                                Model.View_WBSReport InstallationInit = (from x in WBSReportList where x.Id == item.UnitWorkId select x).FirstOrDefault();
                                if (InstallationInit != null)//判断这些装置是否在筛选集合中,符合的添加
                                {
                                    newCNProfessionList.Add(InstallationInit);
                                }
                            }
                        }
                    }
                    else
                    {
                        List InstallationList = (from x in Funs.DB.WBS_UnitWork where x.ProjectId==projectId && (x.SuperUnitWork == null || x.SuperUnitWork == "0") select x).ToList();
                        foreach (Model.WBS_UnitWork item in InstallationList)
                        {
                            Model.View_WBSReport InstallationInit = (from x in WBSReportList where x.Id == item.UnitWorkId select x).FirstOrDefault();
                            if (InstallationInit != null)
                            {
                                newCNProfessionList.Add(InstallationInit);
                            }
                        }
                    }
                    WBSReportList = newCNProfessionList;
                }
            }
            #endregion
            DataRow row;
            foreach (Model.View_WBSReport item in WBSReportList)
            {
                row = table.NewRow();
                row[0] = item.Id;
                row[1] = item.SupId;
                row[2] = item.Name;
                row[3] = item.Weights;
                row[4] = decimal.Round(Convert.ToDecimal(item.WeightsMoney), 2);
                row[5] = decimal.Round(Convert.ToDecimal(item.CompleteValue), 2);//完成值
                DateTime date = Convert.ToDateTime(year + "-" + month + "-01");
                List totalDetails = BLL.WbsDetailService.GetTotalWbsDetailsByYearMonth2(item.Id, date);
                Decimal CompleteValueTotal = decimal.Round(Convert.ToDecimal(totalDetails.Sum(x => x.CompleteValue ?? 0)), 2);
                row[6] = CompleteValueTotal;//完成值累计费用
                row[7] = decimal.Round(Convert.ToDecimal(CompleteValueTotal) / Convert.ToDecimal(item.WeightsMoney) * 100, 2);//累计%
                row[8] = decimal.Round(Convert.ToDecimal(item.RealValue), 2);//实耗值
                Decimal RealValueTotal = decimal.Round(Convert.ToDecimal(totalDetails.Sum(x => x.RealValue ?? 0)), 2);
                row[9] = RealValueTotal;//累计实耗
                Decimal PlanValueTotal = decimal.Round(Convert.ToDecimal(totalDetails.Sum(x => x.PlanValue ?? 0)), 2);//累计计划
                row[10] = CompleteValueTotal - PlanValueTotal;//进度SV费用(万元)=完成值累计费用-计划值累计费用
                row[11] = CompleteValueTotal - RealValueTotal;//费用CV费用(万元)=完成值累计费用-实耗值累计实耗
                row[12] = string.Format("{0:yyyy-MM-dd}", item.StartDate);
                row[13] = string.Format("{0:yyyy-MM-dd}", item.EndDate);
                row[14] = decimal.Round(Convert.ToDecimal(item.PlanValue), 2);//计划值
                row[15] = PlanValueTotal;//累计计划
                row[16] = decimal.Round(Convert.ToDecimal(item.PlanValue) / Convert.ToDecimal(item.WeightsMoney) * 100, 2);// //月计划%
                row[17] = decimal.Round(Convert.ToDecimal(PlanValueTotal) / Convert.ToDecimal(item.WeightsMoney) * 100, 2);// //累计计划%
                int count = (from x in WBSReportList where x.SupId == item.Id select x).Count();
                if (count == 0)
                {
                    row[18] = "true"; //末级节点
                }
                else
                {
                    row[18] = "false"; //非末级节点
                }
                string str = string.Empty;
                Model.WBSRectificationMeasureSet wBSRectificationMeasureSet = BLL.WBSRectificationMeasureSetService.GetWBSRectificationMeasureSetByDateAndToWbs(item.Id, date, rectificationMeasureType);
                if (wBSRectificationMeasureSet != null)
                {
                    if (!string.IsNullOrEmpty(wBSRectificationMeasureSet.RectificationMeasureIds))
                    {
                        if (rectificationMeasureType == "1")
                        {
                            str += "进度纠偏措施:";
                        }
                        else
                        {
                            str += "费用纠偏措施:";
                        }
                        string[] ids = wBSRectificationMeasureSet.RectificationMeasureIds.Split(',');
                        foreach (var id in ids)
                        {
                            Model.JDGL_Base_RectificationMeasure rectificationMeasure = BLL.RectificationMeasureService.GetRectificationMeasureByRectificationMeasureId(id);
                            if (rectificationMeasure != null)
                            {
                                str += rectificationMeasure.RectificationMeasureName + ",";
                            }
                        }
                    }
                }
                if (!string.IsNullOrEmpty(str))
                {
                    row[19] = str.Substring(0, str.LastIndexOf(",")) + (wBSRectificationMeasureSet.IsComplete == true ? "(措施已落实)" : "(尚在落实中)");
                }
                table.Rows.Add(row);
            }
            return table;
        }
    }
}