using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web.UI.WebControls;

namespace BLL
{
    public class Project_InstallationService
    {
        /// <summary>
        /// 是否存在装置名称
        /// </summary>
        /// <param name="postName"></param>
        /// <returns>true-存在,false-不存在</returns>
        public static bool IsExistInstallationName(string InstallationName, string projectId)
        {
            var q = from x in Funs.DB.Project_Installation where x.InstallationName == InstallationName && x.ProjectId == projectId select x;
            if (q.Count() > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否可增加子级
        /// </summary>
        /// <param name="postName"></param>
        /// <returns>true-可以,false-不可以</returns>
        public static bool IsCanAddInstallation(string installationId)
        {
            var installation = from x in Funs.DB.Project_Installation
                               join y in Funs.DB.Project_Installation
                               on x.InstallationId equals y.SuperInstallationId
                               join z in Funs.DB.Project_Installation
                               on y.InstallationId equals z.SuperInstallationId
                               where z.InstallationId == installationId
                               select x;
            if (installation.Count() > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 根据名称获取装置
        /// </summary>
        /// <param name="InstallationName">名称</param>
        /// <returns></returns>
        public static Model.Project_Installation GetInstallationByName(string InstallationName)
        {
            return (from x in Funs.DB.Project_Installation where x.InstallationName == InstallationName select x).FirstOrDefault();
        }

        /// <summary>
        /// 获取项目信息
        /// </summary>
        /// <param name="InstallationId"></param>
        /// <returns></returns>
        public static Model.Project_Installation GetInstallationByInstallationId(string InstallationId)
        {
            return Funs.DB.Project_Installation.FirstOrDefault(e => e.InstallationId.ToString() == InstallationId);
        }

        /// <summary>
        /// 是否存在子级装置
        /// </summary>
        /// <param name="superInstallationId"></param>
        /// <returns></returns>
        public static bool IsExitsInstallationsBySuperInstallationId(string superInstallationId)
        {
            return (from x in Funs.DB.Project_Installation where x.SuperInstallationId == superInstallationId select x).Count() > 0;
        }

        /// <summary>
        /// 添加装置
        /// </summary>
        /// <param name="Installation"></param>
        public static void AddInstallation(Model.Project_Installation installation)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Project_Installation newIns = new Model.Project_Installation();
            newIns.InstallationId = installation.InstallationId;
            newIns.ProjectId = installation.ProjectId;
            newIns.InstallationCode = installation.InstallationCode;
            newIns.InstallationName = installation.InstallationName;
            newIns.SuperInstallationId = installation.SuperInstallationId;
            newIns.StartDate = installation.StartDate;
            newIns.EndDate = installation.EndDate;
            newIns.Weights = installation.Weights;
            newIns.WeightsMoney = installation.WeightsMoney;
            newIns.Def = installation.Def;

            db.Project_Installation.InsertOnSubmit(newIns);
            db.SubmitChanges();
        }

        /// <summary>
        /// 修改装置
        /// </summary>
        /// <param name="Installation"></param>
        public static void UpdateInstallation(Model.Project_Installation installation)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Project_Installation newIns = db.Project_Installation.First(e => e.InstallationId == installation.InstallationId);

            newIns.InstallationCode = installation.InstallationCode;
            newIns.InstallationName = installation.InstallationName;
            newIns.SuperInstallationId = installation.SuperInstallationId;
            newIns.StartDate = installation.StartDate;
            newIns.EndDate = installation.EndDate;
            newIns.Weights = installation.Weights;
            newIns.WeightsMoney = installation.WeightsMoney;
            newIns.Def = installation.Def;
            newIns.SortIndex = installation.SortIndex;
            newIns.UnitId = installation.UnitId;

            db.SubmitChanges();
        }

        /// <summary>
        /// 根据装置Id删除一个装置信息
        /// </summary>
        /// <param name="InstallationId"></param>
        public static void DeleteInstallation(string InstallationId)
        {
            Model.SGGLDB db = Funs.DB;
            Model.Project_Installation installation = db.Project_Installation.FirstOrDefault(e => e.InstallationId.ToString() == InstallationId);
            if (installation != null)
            {
                db.Project_Installation.DeleteOnSubmit(installation);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 获取装置/单元名称项
        /// </summary>
        /// <returns></returns>
        public static ListItem[] GetInstallationList(string projectId)
        {
            var q = (from x in Funs.DB.Project_Installation where x.ProjectId == projectId select x).ToList();
            ListItem[] item = new ListItem[q.Count()];
            for (int i = 0; i < q.Count(); i++)
            {
                item[i] = new ListItem(q[i].InstallationName ?? "", q[i].InstallationId.ToString());
            }
            return item;
        }

        /// <summary>
        /// 根据项目Id判断是否存在项目装置信息
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public static bool IsExitProjectInstallation(string projectId)
        {
            return (from x in BLL.Funs.DB.Project_Installation where x.ProjectId == projectId select x).Count() > 0;
        }

        /// <summary>
        /// 更新所有父级开始结束日期
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        public static void UpdateParentWBSDate(string id, string type, string dateType, DateTime? date)
        {
            if (type == "cnProfession")
            {
                Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(id);
                if (cnProfession != null)
                {
                    Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(cnProfession.InstallationId);
                    if (installation != null)
                    {
                        bool change = false;
                        if (cnProfession.StartDate < installation.StartDate)
                        {
                            change = true;
                            installation.StartDate = cnProfession.StartDate;
                        }
                        if (cnProfession.EndDate > installation.EndDate)
                        {
                            change = true;
                            installation.EndDate = cnProfession.EndDate;
                        }
                        if (change)
                        {
                            BLL.Project_InstallationService.UpdateInstallation(installation);
                        }
                    }
                }
            }
            else if (type == "unitProject")
            {
                Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                if (unitProject != null)
                {
                    Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(unitProject.CnProfessionId);
                    if (cnProfession != null)
                    {
                        bool change = false;
                        if (unitProject.StartDate < cnProfession.StartDate)
                        {
                            change = true;
                            cnProfession.StartDate = unitProject.StartDate;
                        }
                        if (unitProject.EndDate > cnProfession.EndDate)
                        {
                            change = true;
                            cnProfession.EndDate = unitProject.EndDate;
                        }
                        if (change)
                        {
                            BLL.CnProfessionService.UpdateCnProfession(cnProfession);
                            UpdateParentWBSDate(cnProfession.CnProfessionId, "cnProfession", dateType, date);
                        }
                    }
                }
            }
            else if (type == "childUnitProject")
            {
                Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                if (unitProject != null)
                {
                    Model.Wbs_UnitProject parunitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(unitProject.SuperUnitProjectId);
                    if (parunitProject != null)
                    {
                        bool change = false;
                        if (unitProject.StartDate < parunitProject.StartDate)
                        {
                            change = true;
                            parunitProject.StartDate = unitProject.StartDate;
                        }
                        if (unitProject.EndDate > parunitProject.EndDate)
                        {
                            change = true;
                            parunitProject.EndDate = unitProject.EndDate;
                        }
                        if (change)
                        {
                            BLL.UnitProjectService.UpdateUnitProject(parunitProject);
                            UpdateParentWBSDate(parunitProject.UnitProjectId, "unitProject", dateType, date);
                        }
                    }
                }
            }
            else if (type == "wbsSet")
            {
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                if (wbsSet != null)
                {
                    Model.Wbs_WbsSet parWbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSet.SuperWbsSetId);
                    if (parWbsSet1 != null)   //当前不是分部
                    {
                        bool change = false;
                        if (wbsSet.StartDate < parWbsSet1.StartDate)
                        {
                            change = true;
                            parWbsSet1.StartDate = wbsSet.StartDate;
                        }
                        if (wbsSet.EndDate > parWbsSet1.EndDate)
                        {
                            change = true;
                            parWbsSet1.EndDate = wbsSet.EndDate;
                        }
                        if (change)
                        {
                            BLL.WbsSetService.UpdateWbsSet(parWbsSet1);
                            UpdateParentWBSDate(parWbsSet1.WbsSetId, "wbsSet", dateType, date);
                        }
                    }
                    else   //当前是分部
                    {
                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(wbsSet.UnitProjectId);
                        if (unitProject != null)
                        {
                            bool change = false;
                            if (wbsSet.StartDate < unitProject.StartDate)
                            {
                                change = true;
                                unitProject.StartDate = wbsSet.StartDate;
                            }
                            if (wbsSet.EndDate > unitProject.EndDate)
                            {
                                change = true;
                                unitProject.EndDate = wbsSet.EndDate;
                            }
                            if (change)
                            {
                                BLL.UnitProjectService.UpdateUnitProject(unitProject);
                            }
                            if (string.IsNullOrEmpty(unitProject.SuperUnitProjectId))   //当前为单位工程
                            {
                                UpdateParentWBSDate(unitProject.UnitProjectId, "unitProject", dateType, date);
                            }
                            else   //当前为子单位工程
                            {
                                UpdateParentWBSDate(unitProject.UnitProjectId, "childUnitProject", dateType, date);
                            }
                        }
                    }


                    //更新处理计划值
                    List<Model.Wbs_WbsSet> childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(id);
                    if (childWbsSets.Count == 0 && wbsSet.WeightsMoney != null && wbsSet.WeightsMoney != 0)   //当前为末级,且已分配权重金额
                    {
                        List<Model.WbsDetail> details = BLL.WbsDetailService.GetTotalWbsDetailsByToWbs(id);
                        if (details.Count > 0)   //存在计划值
                        {
                            DateTime startDate, endDate, startMonth, endMonth;
                            decimal totalValue = 0;
                            List<DateTime> months = new List<DateTime>();
                            startDate = Convert.ToDateTime(wbsSet.StartDate);
                            endDate = Convert.ToDateTime(wbsSet.EndDate);
                            startMonth = Convert.ToDateTime(startDate.Year + "-" + startDate.Month + "-01");
                            endMonth = Convert.ToDateTime(endDate.Year + "-" + endDate.Month + "-01");
                            do
                            {
                                months.Add(startMonth);
                                startMonth = startMonth.AddMonths(1);
                            } while (startMonth <= endMonth);
                            if (wbsSet.IsPlanApprove == true)
                            {
                                wbsSet.IsPlanApprove = null;
                                BLL.WbsSetService.UpdateWbsSet(wbsSet);
                            }
                            //保存之前计划值版本
                            foreach (var detail in details)
                            {
                                Model.WbsDetailHistory detailHistory = new Model.WbsDetailHistory();
                                detailHistory.WbsDetailHistoryId = SQLHelper.GetNewID(typeof(Model.WbsDetailHistory));
                                detailHistory.ToWbs = detail.ToWbs;
                                detailHistory.ToFlag = detail.ToFlag;
                                detailHistory.Way = detail.Way;
                                detailHistory.Months = detail.Months;
                                detailHistory.PlanValue = detail.PlanValue;
                                detailHistory.PlanValueRate = detail.PlanValueRate;
                                detailHistory.VersionNum = wbsSet.VersionNum;
                                BLL.WbsDetailHistoryService.AddWbsDetailHistory(detailHistory);
                            }
                            List<Model.WbsDetail> beforeDetails = BLL.WbsDetailService.GetTotalWbsDetailsByYearMonth2(id, DateTime.Now);
                            if (beforeDetails.Count > 0)   //当月(含)及之前存在计划值
                            {
                                decimal beforeCompleteValueTotal = 0;   //之前月份累计完成值
                                List<DateTime> beforeMonths = months.Where(x => x <= DateTime.Now).ToList();  //获取当月及之前月份集合
                                List<DateTime> removeMonths = new List<DateTime>();   //需要移除的月份集合
                                for (int i = 0; i < beforeMonths.Count; i++)
                                {
                                    Model.WbsDetail detail = details.FirstOrDefault(x => x.Months == months[i]);
                                    if (detail != null)
                                    {
                                        if (detail.CompleteValue != null)
                                        {
                                            beforeCompleteValueTotal += Convert.ToDecimal(detail.CompleteValue);
                                            removeMonths.Add(months[i]);
                                        }
                                    }
                                }
                                foreach (var item in removeMonths)
                                {
                                    months.Remove(item);    //得到需要重新分配计划值的月份集合
                                }
                                decimal lastWeightMoneys = Convert.ToDecimal(wbsSet.WeightsMoney) - beforeCompleteValueTotal;    //剩余未完成计划值
                                for (int i = 0; i < months.Count; i++)
                                {
                                    //对应月份已有的记录
                                    Model.WbsDetail oldDetail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(id, 4, months[i]);

                                    Model.WbsDetail detail = new Model.WbsDetail();
                                    detail.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                    detail.ToWbs = id;
                                    detail.ToFlag = 4;
                                    detail.Way = wbsSet.Way;
                                    detail.Months = months[i];
                                    if (i == months.Count - 1)
                                    {
                                        detail.PlanValue = decimal.Round(Convert.ToDecimal(lastWeightMoneys - totalValue), 2);
                                    }
                                    else
                                    {
                                        detail.PlanValue = decimal.Round(lastWeightMoneys / months.Count, 2);
                                    }
                                    detail.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValue) / Convert.ToDecimal(wbsSet.WeightsMoney) * 100, 2);
                                    totalValue += decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                    if (oldDetail == null)
                                    {
                                        #region  新增记录
                                        BLL.WbsDetailService.AddWbsDetail(detail);
                                        //循环保存所有上级分部分项对应记录
                                        AddWbsParentDetail(Convert.ToDateTime(detail.Months), detail.PlanValue, wbsSet.SuperWbsSetId);
                                        //保存对应单位工程及子单位工程
                                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(wbsSet.UnitProjectId);
                                        Model.WbsDetail detailUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                        if (unitProject != null)
                                        {
                                            if (detailUnitProject == null)
                                            {
                                                Model.WbsDetail newDetailUnitProject = new Model.WbsDetail();
                                                newDetailUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailUnitProject.ToWbs = wbsSet.UnitProjectId;
                                                newDetailUnitProject.ToFlag = 3;    //单位工程
                                                newDetailUnitProject.Way = "A";
                                                newDetailUnitProject.Months = detail.Months;
                                                newDetailUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                newDetailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailUnitProject);
                                            }
                                            else
                                            {
                                                detailUnitProject.PlanValue += detail.PlanValue ?? 0;
                                                detailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailUnitProject);
                                            }
                                            if (unitProject.SuperUnitProjectId != null)   //存在单位工程
                                            {
                                                Model.Wbs_UnitProject parentUnitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(unitProject.SuperUnitProjectId);
                                                Model.WbsDetail detailParentUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentUnitProject.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                                if (detailParentUnitProject == null)
                                                {
                                                    Model.WbsDetail newDetailParentUnitProject = new Model.WbsDetail();
                                                    newDetailParentUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailParentUnitProject.ToWbs = parentUnitProject.UnitProjectId;
                                                    newDetailParentUnitProject.ToFlag = 3;   //单位工程
                                                    newDetailParentUnitProject.Way = "A";
                                                    newDetailParentUnitProject.Months = detail.Months;
                                                    newDetailParentUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailParentUnitProject);
                                                }
                                                else
                                                {
                                                    detailParentUnitProject.PlanValue += detail.PlanValue ?? 0;
                                                    detailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailParentUnitProject);
                                                }
                                            }
                                        }
                                        //保存对应专业
                                        Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(wbsSet.CnProfessionId);
                                        if (cnProfession != null)
                                        {
                                            Model.WbsDetail detailCnProfession = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(cnProfession.CnProfessionId, 2, Convert.ToDateTime(detail.Months));
                                            if (detailCnProfession == null)
                                            {
                                                Model.WbsDetail newDetailCnProfession = new Model.WbsDetail();
                                                newDetailCnProfession.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailCnProfession.ToWbs = wbsSet.CnProfessionId;
                                                newDetailCnProfession.ToFlag = 2;  //专业
                                                newDetailCnProfession.Way = wbsSet.Way;
                                                newDetailCnProfession.Months = detail.Months;
                                                newDetailCnProfession.PlanValue = detail.PlanValue ?? 0;
                                                newDetailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailCnProfession);
                                            }
                                            else
                                            {
                                                detailCnProfession.PlanValue += detail.PlanValue ?? 0;
                                                detailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(detailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailCnProfession);
                                            }
                                            //保存对应装置
                                            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(cnProfession.InstallationId);
                                            if (installation != null)
                                            {
                                                Model.WbsDetail detailInstallation = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(installation.InstallationId, 1, Convert.ToDateTime(detail.Months));
                                                if (detailInstallation == null)
                                                {
                                                    Model.WbsDetail newDetailInstallation = new Model.WbsDetail();
                                                    newDetailInstallation.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailInstallation.ToWbs = installation.InstallationId;
                                                    newDetailInstallation.ToFlag = 1;  //装置
                                                    newDetailInstallation.Way = wbsSet.Way;
                                                    newDetailInstallation.Months = detail.Months;
                                                    newDetailInstallation.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailInstallation);
                                                }
                                                else
                                                {
                                                    detailInstallation.PlanValue += detail.PlanValue ?? 0;
                                                    detailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(detailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailInstallation);
                                                }
                                            }
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region   更新记录
                                        decimal? oldValue = oldDetail.PlanValue;
                                        detail.WbsDetailId = oldDetail.WbsDetailId;
                                        detail.CompleteValue = oldDetail.CompleteValue;
                                        detail.CompleteValueRate = oldDetail.CompleteValueRate;
                                        detail.RealValue = oldDetail.RealValue;
                                        detail.RealValueRate = oldDetail.RealValueRate;
                                        BLL.WbsDetailService.UpdateWbsDetail(detail);
                                        //循环保存所有上级分部分项对应记录
                                        UpdateWbsParentDetail(Convert.ToDateTime(detail.Months), detail.PlanValue, oldValue, wbsSet.SuperWbsSetId);
                                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(wbsSet.UnitProjectId);
                                        Model.WbsDetail detailUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                        if (unitProject != null)
                                        {
                                            if (detailUnitProject != null)
                                            {
                                                detailUnitProject.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                detailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailUnitProject);
                                            }
                                            else
                                            {
                                                Model.WbsDetail newDetailUnitProject = new Model.WbsDetail();
                                                newDetailUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailUnitProject.ToWbs = wbsSet.UnitProjectId;
                                                newDetailUnitProject.ToFlag = 3;    //单位工程
                                                newDetailUnitProject.Way = "A";
                                                newDetailUnitProject.Months = detail.Months;
                                                newDetailUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                newDetailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailUnitProject);
                                            }
                                            if (unitProject.SuperUnitProjectId != null)   //存在单位工程
                                            {
                                                Model.Wbs_UnitProject parentUnitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(unitProject.SuperUnitProjectId);
                                                Model.WbsDetail detailParentUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentUnitProject.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                                if (detailParentUnitProject != null)
                                                {
                                                    detailParentUnitProject.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                    detailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailParentUnitProject);
                                                }
                                                else
                                                {
                                                    Model.WbsDetail newDetailParentUnitProject = new Model.WbsDetail();
                                                    newDetailParentUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailParentUnitProject.ToWbs = parentUnitProject.UnitProjectId;
                                                    newDetailParentUnitProject.ToFlag = 3;   //单位工程
                                                    newDetailParentUnitProject.Way = "A";
                                                    newDetailParentUnitProject.Months = detail.Months;
                                                    newDetailParentUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailParentUnitProject);
                                                }
                                            }
                                        }
                                        //保存对应专业
                                        Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(wbsSet.CnProfessionId);
                                        if (cnProfession != null)
                                        {
                                            Model.WbsDetail detailCnProfession = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(cnProfession.CnProfessionId, 2, Convert.ToDateTime(detail.Months));
                                            if (detailCnProfession != null)
                                            {
                                                detailCnProfession.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                detailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(detailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailCnProfession);
                                            }
                                            else
                                            {
                                                Model.WbsDetail newDetailCnProfession = new Model.WbsDetail();
                                                newDetailCnProfession.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailCnProfession.ToWbs = wbsSet.CnProfessionId;
                                                newDetailCnProfession.ToFlag = 2;  //专业
                                                newDetailCnProfession.Way = wbsSet.Way;
                                                newDetailCnProfession.Months = detail.Months;
                                                newDetailCnProfession.PlanValue = detail.PlanValue ?? 0;
                                                newDetailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailCnProfession);
                                            }
                                            //保存对应装置
                                            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(cnProfession.InstallationId);
                                            if (installation != null)
                                            {
                                                Model.WbsDetail detailInstallation = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(installation.InstallationId, 1, Convert.ToDateTime(detail.Months));
                                                if (detailInstallation != null)
                                                {
                                                    detailInstallation.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                    detailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(detailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailInstallation);
                                                }
                                                else
                                                {
                                                    Model.WbsDetail newDetailInstallation = new Model.WbsDetail();
                                                    newDetailInstallation.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailInstallation.ToWbs = installation.InstallationId;
                                                    newDetailInstallation.ToFlag = 1;  //装置
                                                    newDetailInstallation.Way = wbsSet.Way;
                                                    newDetailInstallation.Months = detail.Months;
                                                    newDetailInstallation.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailInstallation);
                                                }
                                            }
                                        }
                                        #endregion
                                    }
                                }
                            }
                            else    //计划开始日期在当月之后
                            {
                                for (int i = 0; i < months.Count; i++)
                                {
                                    //对应月份已有的记录
                                    Model.WbsDetail oldDetail = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(id, 4, months[i]);

                                    Model.WbsDetail detail = new Model.WbsDetail();
                                    detail.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                    detail.ToWbs = id;
                                    detail.ToFlag = 4;
                                    detail.Way = wbsSet.Way;
                                    detail.Months = months[i];
                                    if (i == months.Count - 1)
                                    {
                                        detail.PlanValue = decimal.Round(Convert.ToDecimal(wbsSet.WeightsMoney - totalValue), 2);
                                    }
                                    else
                                    {
                                        detail.PlanValue = WbsDetailService.GetMonthPlanValueByMonthCountAndMonthNumAndWeightMonth(months.Count, i + 1, Convert.ToDecimal(wbsSet.WeightsMoney));
                                    }
                                    detail.PlanValueRate = decimal.Round(Convert.ToDecimal(detail.PlanValue) / Convert.ToDecimal(wbsSet.WeightsMoney) * 100, 2);
                                    totalValue += decimal.Round(Convert.ToDecimal(detail.PlanValue), 2);
                                    if (oldDetail == null)
                                    {
                                        #region  新增记录
                                        BLL.WbsDetailService.AddWbsDetail(detail);
                                        //循环保存所有上级分部分项对应记录
                                        AddWbsParentDetail(Convert.ToDateTime(detail.Months), detail.PlanValue, wbsSet.SuperWbsSetId);
                                        //保存对应单位工程及子单位工程
                                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(wbsSet.UnitProjectId);
                                        Model.WbsDetail detailUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                        if (unitProject != null)
                                        {
                                            if (detailUnitProject == null)
                                            {
                                                Model.WbsDetail newDetailUnitProject = new Model.WbsDetail();
                                                newDetailUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailUnitProject.ToWbs = wbsSet.UnitProjectId;
                                                newDetailUnitProject.ToFlag = 3;    //单位工程
                                                newDetailUnitProject.Way = "A";
                                                newDetailUnitProject.Months = detail.Months;
                                                newDetailUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                newDetailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailUnitProject);
                                            }
                                            else
                                            {
                                                detailUnitProject.PlanValue += detail.PlanValue ?? 0;
                                                detailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailUnitProject);
                                            }
                                            if (unitProject.SuperUnitProjectId != null)   //存在单位工程
                                            {
                                                Model.Wbs_UnitProject parentUnitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(unitProject.SuperUnitProjectId);
                                                Model.WbsDetail detailParentUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentUnitProject.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                                if (detailParentUnitProject == null)
                                                {
                                                    Model.WbsDetail newDetailParentUnitProject = new Model.WbsDetail();
                                                    newDetailParentUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailParentUnitProject.ToWbs = parentUnitProject.UnitProjectId;
                                                    newDetailParentUnitProject.ToFlag = 3;   //单位工程
                                                    newDetailParentUnitProject.Way = "A";
                                                    newDetailParentUnitProject.Months = detail.Months;
                                                    newDetailParentUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailParentUnitProject);
                                                }
                                                else
                                                {
                                                    detailParentUnitProject.PlanValue += detail.PlanValue ?? 0;
                                                    detailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailParentUnitProject);
                                                }
                                            }
                                        }
                                        //保存对应专业
                                        Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(wbsSet.CnProfessionId);
                                        if (cnProfession != null)
                                        {
                                            Model.WbsDetail detailCnProfession = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(cnProfession.CnProfessionId, 2, Convert.ToDateTime(detail.Months));
                                            if (detailCnProfession == null)
                                            {
                                                Model.WbsDetail newDetailCnProfession = new Model.WbsDetail();
                                                newDetailCnProfession.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailCnProfession.ToWbs = wbsSet.CnProfessionId;
                                                newDetailCnProfession.ToFlag = 2;  //专业
                                                newDetailCnProfession.Way = wbsSet.Way;
                                                newDetailCnProfession.Months = detail.Months;
                                                newDetailCnProfession.PlanValue = detail.PlanValue ?? 0;
                                                newDetailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailCnProfession);
                                            }
                                            else
                                            {
                                                detailCnProfession.PlanValue += detail.PlanValue ?? 0;
                                                detailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(detailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailCnProfession);
                                            }
                                            //保存对应装置
                                            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(cnProfession.InstallationId);
                                            if (installation != null)
                                            {
                                                Model.WbsDetail detailInstallation = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(installation.InstallationId, 1, Convert.ToDateTime(detail.Months));
                                                if (detailInstallation == null)
                                                {
                                                    Model.WbsDetail newDetailInstallation = new Model.WbsDetail();
                                                    newDetailInstallation.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailInstallation.ToWbs = installation.InstallationId;
                                                    newDetailInstallation.ToFlag = 1;  //装置
                                                    newDetailInstallation.Way = wbsSet.Way;
                                                    newDetailInstallation.Months = detail.Months;
                                                    newDetailInstallation.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailInstallation);
                                                }
                                                else
                                                {
                                                    detailInstallation.PlanValue += detail.PlanValue ?? 0;
                                                    detailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(detailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailInstallation);
                                                }
                                            }
                                        }
                                        #endregion
                                    }
                                    else
                                    {
                                        #region   更新记录
                                        decimal? oldValue = oldDetail.PlanValue;
                                        detail.WbsDetailId = oldDetail.WbsDetailId;
                                        detail.CompleteValue = oldDetail.CompleteValue;
                                        detail.CompleteValueRate = oldDetail.CompleteValueRate;
                                        detail.RealValue = oldDetail.RealValue;
                                        detail.RealValueRate = oldDetail.RealValueRate;
                                        BLL.WbsDetailService.UpdateWbsDetail(detail);
                                        //循环保存所有上级分部分项对应记录
                                        UpdateWbsParentDetail(Convert.ToDateTime(detail.Months), detail.PlanValue, oldValue, wbsSet.SuperWbsSetId);
                                        Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(wbsSet.UnitProjectId);
                                        Model.WbsDetail detailUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(wbsSet.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                        if (unitProject != null)
                                        {
                                            if (detailUnitProject != null)
                                            {
                                                detailUnitProject.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                detailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailUnitProject);
                                            }
                                            else
                                            {
                                                Model.WbsDetail newDetailUnitProject = new Model.WbsDetail();
                                                newDetailUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailUnitProject.ToWbs = wbsSet.UnitProjectId;
                                                newDetailUnitProject.ToFlag = 3;    //单位工程
                                                newDetailUnitProject.Way = "A";
                                                newDetailUnitProject.Months = detail.Months;
                                                newDetailUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                newDetailUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailUnitProject.PlanValue) / Convert.ToDecimal(unitProject.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailUnitProject);
                                            }
                                            if (unitProject.SuperUnitProjectId != null)   //存在单位工程
                                            {
                                                Model.Wbs_UnitProject parentUnitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(unitProject.SuperUnitProjectId);
                                                Model.WbsDetail detailParentUnitProject = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentUnitProject.UnitProjectId, 3, Convert.ToDateTime(detail.Months));
                                                if (detailParentUnitProject != null)
                                                {
                                                    detailParentUnitProject.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                    detailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(detailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailParentUnitProject);
                                                }
                                                else
                                                {
                                                    Model.WbsDetail newDetailParentUnitProject = new Model.WbsDetail();
                                                    newDetailParentUnitProject.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailParentUnitProject.ToWbs = parentUnitProject.UnitProjectId;
                                                    newDetailParentUnitProject.ToFlag = 3;   //单位工程
                                                    newDetailParentUnitProject.Way = "A";
                                                    newDetailParentUnitProject.Months = detail.Months;
                                                    newDetailParentUnitProject.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailParentUnitProject.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailParentUnitProject.PlanValue) / Convert.ToDecimal(parentUnitProject.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailParentUnitProject);
                                                }
                                            }
                                        }
                                        //保存对应专业
                                        Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(wbsSet.CnProfessionId);
                                        if (cnProfession != null)
                                        {
                                            Model.WbsDetail detailCnProfession = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(cnProfession.CnProfessionId, 2, Convert.ToDateTime(detail.Months));
                                            if (detailCnProfession != null)
                                            {
                                                detailCnProfession.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                detailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(detailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.UpdateWbsDetail(detailCnProfession);
                                            }
                                            else
                                            {
                                                Model.WbsDetail newDetailCnProfession = new Model.WbsDetail();
                                                newDetailCnProfession.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                newDetailCnProfession.ToWbs = wbsSet.CnProfessionId;
                                                newDetailCnProfession.ToFlag = 2;  //专业
                                                newDetailCnProfession.Way = wbsSet.Way;
                                                newDetailCnProfession.Months = detail.Months;
                                                newDetailCnProfession.PlanValue = detail.PlanValue ?? 0;
                                                newDetailCnProfession.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailCnProfession.PlanValue) / Convert.ToDecimal(cnProfession.WeightsMoney) * 100, 2);
                                                BLL.WbsDetailService.AddWbsDetail(newDetailCnProfession);
                                            }
                                            //保存对应装置
                                            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(cnProfession.InstallationId);
                                            if (installation != null)
                                            {
                                                Model.WbsDetail detailInstallation = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(installation.InstallationId, 1, Convert.ToDateTime(detail.Months));
                                                if (detailInstallation != null)
                                                {
                                                    detailInstallation.PlanValue += ((detail.PlanValue ?? 0) - (oldValue ?? 0));
                                                    detailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(detailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.UpdateWbsDetail(detailInstallation);
                                                }
                                                else
                                                {
                                                    Model.WbsDetail newDetailInstallation = new Model.WbsDetail();
                                                    newDetailInstallation.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                                                    newDetailInstallation.ToWbs = installation.InstallationId;
                                                    newDetailInstallation.ToFlag = 1;  //装置
                                                    newDetailInstallation.Way = wbsSet.Way;
                                                    newDetailInstallation.Months = detail.Months;
                                                    newDetailInstallation.PlanValue = detail.PlanValue ?? 0;
                                                    newDetailInstallation.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetailInstallation.PlanValue) / Convert.ToDecimal(installation.WeightsMoney) * 100, 2);
                                                    BLL.WbsDetailService.AddWbsDetail(newDetailInstallation);
                                                }
                                            }
                                        }
                                        #endregion
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 更新所有子级开始结束日期
        /// </summary>
        /// <param name="id"></param>
        /// <param name="type"></param>
        /// <param name="startDate"></param>
        /// <param name="endDate"></param>
        public static void UpdateChildWBSDate(string id, string type, string dateType, DateTime? date)
        {
            if (type == "installation")
            {
                Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(id);
                if (installation != null)
                {
                    if (dateType == "StartDate")
                    {
                        installation.StartDate = date;
                    }
                    else
                    {
                        installation.EndDate = date;
                    }
                    BLL.Project_InstallationService.UpdateInstallation(installation);
                    var cnProfessions = from x in Funs.DB.WBS_CnProfession where x.InstallationId == id select x;
                    foreach (var cnProfession in cnProfessions)
                    {
                        UpdateChildWBSDate(cnProfession.CnProfessionId, "cnProfession", dateType, date);
                    }
                }
            }
            else if (type == "cnProfession")
            {
                Model.WBS_CnProfession cnProfession = BLL.CnProfessionService.GetCnProfessionByCnProfessionId(id);
                if (cnProfession != null)
                {
                    if (dateType == "StartDate")
                    {
                        cnProfession.StartDate = date;
                    }
                    else
                    {
                        cnProfession.EndDate = date;
                    }
                    BLL.CnProfessionService.UpdateCnProfession(cnProfession);
                    var unitProjects = from x in Funs.DB.Wbs_UnitProject where x.CnProfessionId == cnProfession.CnProfessionId && x.SuperUnitProjectId == null orderby x.SortIndex, x.UnitProjectCode select x;
                    foreach (var unitProject in unitProjects)
                    {
                        UpdateChildWBSDate(unitProject.UnitProjectId, "unitProject", dateType, date);
                    }
                }
            }
            else if (type == "unitProject")
            {
                Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                if (unitProject != null)
                {
                    if (dateType == "StartDate")
                    {
                        unitProject.StartDate = date;
                    }
                    else
                    {
                        unitProject.EndDate = date;
                    }
                    BLL.UnitProjectService.UpdateUnitProject(unitProject);
                    var childUnitProjects = from x in Funs.DB.Wbs_UnitProject where x.SuperUnitProjectId == unitProject.UnitProjectId orderby x.SortIndex, x.UnitProjectCode select x;
                    if (childUnitProjects.Count() > 0)   //存在子单位工程
                    {
                        foreach (var childUnitProject in childUnitProjects)
                        {
                            UpdateChildWBSDate(childUnitProject.UnitProjectId, "unitProject", dateType, date);
                        }
                    }
                    else   //不存在子单位工程,加载分部工程
                    {
                        var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == unitProject.UnitProjectId orderby x.SortIndex, x.WbsSetCode select x;
                        if (wbsSet1s.Count() > 0)
                        {
                            foreach (var wbsSet in wbsSet1s)
                            {
                                UpdateChildWBSDate(wbsSet.WbsSetId, "wbsSet", dateType, date);
                            }
                        }
                        else    //单位工程下直接是分项内容,如质量行为
                        {
                            var wbsSet3s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 3 && x.UnitProjectId == unitProject.UnitProjectId orderby x.SortIndex, x.WbsSetCode select x;
                            if (wbsSet3s.Count() > 0)
                            {
                                foreach (var wbsSet in wbsSet3s)
                                {
                                    UpdateChildWBSDate(wbsSet.WbsSetId, "wbsSet", dateType, date);
                                }
                            }
                        }
                    }
                }
            }
            else if (type == "childUnitProject")
            {
                Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                if (unitProject != null)
                {
                    if (dateType == "StartDate")
                    {
                        unitProject.StartDate = date;
                    }
                    else
                    {
                        unitProject.EndDate = date;
                    }
                    BLL.UnitProjectService.UpdateUnitProject(unitProject);
                    var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == unitProject.UnitProjectId orderby x.SortIndex, x.WbsSetCode select x;
                    foreach (var wbsSet in wbsSet1s)
                    {
                        UpdateChildWBSDate(wbsSet.WbsSetId, "wbsSet", dateType, date);
                    }
                }
            }
            else if (type == "wbsSet")
            {
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                if (wbsSet != null)
                {
                    var childWbsSets = BLL.WbsSetService.GetWbsSetsBySuperWbsSetId(id);
                    if (childWbsSets.Count == 0)   //当前为末级
                    {
                        if (wbsSet.IsPlanApprove == true)
                        {
                            wbsSet.IsPlanApprove = null;    //末级计划值审核状态变为未审核
                        }
                    }
                    if (dateType == "StartDate")
                    {
                        wbsSet.StartDate = date;
                    }
                    else
                    {
                        wbsSet.EndDate = date;
                    }
                    BLL.WbsSetService.UpdateWbsSet(wbsSet);
                    if (childWbsSets.Count > 0)    //当前不为末级
                    {
                        foreach (var childwbsSet in childWbsSets)
                        {
                            UpdateChildWBSDate(childwbsSet.WbsSetId, "wbsSet", dateType, date);
                        }
                    }
                }
            }
        }

        #region  增加分部分项
        /// <summary>
        /// 增加分部分项
        /// </summary>
        /// <param name="years"></param>
        /// <param name="months"></param>
        /// <param name="planValue"></param>
        /// <param name="parentId"></param>
        public static void AddWbsParentDetail(DateTime months, decimal? planValue, string parentId)
        {
            Model.Wbs_WbsSet parentWbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(parentId);
            if (parentWbsSet1 != null)
            {
                if (parentWbsSet1.SuperWbsSetId != null)   //父节点不是分部节点
                {
                    Model.WbsDetail detail1 = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentWbsSet1.WbsSetId, 4, months);
                    if (detail1 == null)
                    {
                        Model.WbsDetail newDetail1 = new Model.WbsDetail();
                        newDetail1.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                        newDetail1.ToWbs = parentWbsSet1.WbsSetId;
                        newDetail1.ToFlag = 4;
                        newDetail1.Way = parentWbsSet1.Way;
                        newDetail1.Months = months;
                        newDetail1.PlanValue = planValue ?? 0;
                        newDetail1.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.AddWbsDetail(newDetail1);
                    }
                    else
                    {
                        detail1.PlanValue += planValue ?? 0;
                        detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.UpdateWbsDetail(detail1);
                    }
                    AddWbsParentDetail(months, planValue, parentWbsSet1.SuperWbsSetId);
                }
                else
                {
                    Model.WbsDetail detail1 = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentWbsSet1.WbsSetId, 4, months);
                    if (detail1 == null)
                    {
                        Model.WbsDetail newDetail1 = new Model.WbsDetail();
                        newDetail1.WbsDetailId = SQLHelper.GetNewID(typeof(Model.WbsDetail));
                        newDetail1.ToWbs = parentWbsSet1.WbsSetId;
                        newDetail1.ToFlag = 4;
                        newDetail1.Way = parentWbsSet1.Way;
                        newDetail1.Months = months;
                        newDetail1.PlanValue = planValue;
                        newDetail1.PlanValueRate = decimal.Round(Convert.ToDecimal(newDetail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.AddWbsDetail(newDetail1);
                    }
                    else
                    {
                        detail1.PlanValue += planValue;
                        detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.UpdateWbsDetail(detail1);
                    }
                }
            }
        }
        #endregion

        #region  更新分部分项
        /// <summary>
        /// 更新分部分项
        /// </summary>
        /// <param name="years"></param>
        /// <param name="months"></param>
        /// <param name="planValue"></param>
        /// <param name="parentId"></param>
        public static void UpdateWbsParentDetail(DateTime months, decimal? planValue, decimal? oldPlanValue, string parentId)
        {
            Model.Wbs_WbsSet parentWbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(parentId);
            if (parentWbsSet1 != null)
            {
                Model.WbsDetail detail1 = BLL.WbsDetailService.GetWbsDetailByWbsFlagYearMonth(parentWbsSet1.WbsSetId, 4, months);
                if (detail1 != null)
                {
                    if (parentWbsSet1.SuperWbsSetId != null)   //父节点不是分部节点
                    {
                        detail1.PlanValue += ((planValue ?? 0) - (oldPlanValue ?? 0));
                        detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.UpdateWbsDetail(detail1);
                        UpdateWbsParentDetail(months, planValue, oldPlanValue, parentWbsSet1.SuperWbsSetId);
                    }
                    else
                    {
                        detail1.PlanValue += ((planValue ?? 0) - (oldPlanValue ?? 0));
                        detail1.PlanValueRate = decimal.Round(Convert.ToDecimal(detail1.PlanValue) / Convert.ToDecimal(parentWbsSet1.WeightsMoney) * 100, 2);
                        BLL.WbsDetailService.UpdateWbsDetail(detail1);
                    }
                }
            }
        }
        #endregion


        /// <summary>
        /// 装置下拉项
        /// </summary>
        /// <param name="dropName">下拉框名称</param>
        /// <param name="isShowPlease">是否显示请选择</param>
        public static void InitInstallationDropDownList(FineUIPro.DropDownList dropName,string projectId, bool isShowPlease)
        {
            dropName.DataValueField = "Value";
            dropName.DataTextField = "Text";
            dropName.DataSource = GetInstallationList(projectId);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }

        public static ListItem[] GetInstallationBySupervisorUnitIdList(string projectId, string unitId, string supervisorUnitId)
        {
            var q = (from x in BLL.Funs.DB.Project_Installation
                     join y in BLL.Funs.DB.ProjectData_WorkArea on x.InstallationId equals y.InstallationId
                     where x.ProjectId == projectId && y.UnitId == unitId && y.SupervisorUnitId == supervisorUnitId
                     orderby x.InstallationId
                     select x).Distinct().ToList();
            ListItem[] item = new ListItem[q.Count()];
            for (int i = 0; i < q.Count(); i++)
            {
                item[i] = new ListItem(q[i].InstallationName ?? "", q[i].InstallationId.ToString());
            }
            return item;
        }
        public static void InitInstallationBySupervisorUnitIdListDownList(FineUIPro.DropDownList dropName, string projectId, string unitId, string supervisorUnitId,  bool isShowPlease)
        {
            dropName.DataValueField = "Value";
            dropName.DataTextField = "Text";
            dropName.DataSource = GetInstallationBySupervisorUnitIdList(projectId, unitId,supervisorUnitId);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }

        public static ListItem[] GetInstallationList(string projectId, string unitId)
        {
            var q = (from x in BLL.Funs.DB.Project_Installation
                     join y in BLL.Funs.DB.ProjectData_WorkArea on x.InstallationId equals y.InstallationId
                     where x.ProjectId == projectId && y.UnitId == unitId
                     orderby x.InstallationId
                     select x).Distinct().ToList();
            ListItem[] item = new ListItem[q.Count()];
            for (int i = 0; i < q.Count(); i++)
            {
                item[i] = new ListItem(q[i].InstallationName ?? "", q[i].InstallationId.ToString());
            }
            return item;
        }
     
        public static void InitInstallationListDownList(FineUIPro.DropDownList dropName, string projectId, string unitId, bool isShowPlease)
        {
            dropName.DataValueField = "Value";
            dropName.DataTextField = "Text";
            dropName.DataSource = GetInstallationList(projectId, unitId);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }

        /// <summary>
        /// 根据项目Id、单位id获取装置名称下拉选择项
        /// </summary>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public static List<Model.Project_Installation> GetInstallationsByProjectId(string projectId)
        {
            using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
            {
                var q = (from x in db.Project_Installation
                         where x.ProjectId == projectId
                         orderby x.InstallationCode
                         select x).ToList();
                return q;
            }
        }

        /// <summary>
        /// 下拉框
        /// </summary>
        /// <param name="dropName">下拉框名字</param>
        /// <param name="projectId">项目id</param>
        /// <param name="unitId">单位id</param>
        /// <param name="isShowPlease">是否显示请选择</param>
        public static void InitInstallationsDropDownList(FineUIPro.DropDownList dropName, string projectId, bool isShowPlease)
        {
            dropName.DataValueField = "InstallationId";
            dropName.DataTextField = "InstallationName";
            dropName.DataSource = GetInstallationsByProjectId(projectId);
            dropName.DataBind();
            if (isShowPlease)
            {
                Funs.FineUIPleaseSelect(dropName);
            }
        }
    }
}