using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Newtonsoft.Json.Linq;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WorkloadInputEdit : PageBase
    {
        /// 
        /// 控制项主键
        /// 
        public string Id
        {
            get
            {
                return (string)ViewState["Id"];
            }
            set
            {
                ViewState["Id"] = value;
            }
        }
        /// 
        /// 加载表头
        /// 
        protected void Page_Init(object sender, EventArgs e)
        {
            InitGrid();
        }
        #region 表头
        /// 
        /// 表头
        /// 
        private void InitGrid()
        {
            this.Id = Request.Params["Id"];
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(Id);
            DateTime startDate, endDate, startMonth, endMonth;
            List months = new List();
            if (wbsSet != null && wbsSet.StartDate != null && wbsSet.EndDate != null)
            {
                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);
            }
            for (int i = 0; i < months.Count; i++)
            {
                //GroupField gd = new GroupField();
                //gd.HeaderText = string.Format("{0:yyyy-MM}", months[i]);
                //gd.HeaderTextAlign = TextAlign.Center;
                RenderField rdPlan = new RenderField();
                rdPlan.ColumnID = string.Format("{0:yyyy-MM}", months[i]) + "Plan";
                rdPlan.Width = Unit.Pixel(100);
                rdPlan.DataField = "PlanNum" + (i + 1).ToString();
                rdPlan.FieldType = FieldType.Double;
                rdPlan.HeaderText = string.Format("{0:yyyy年MM月}", months[i]) + "
计划量";
                rdPlan.HeaderTextAlign = TextAlign.Center;
                NumberBox numPlan = new NumberBox();
                numPlan.NoNegative = true;
                numPlan.NoDecimal = false;
                rdPlan.Editor.Add(numPlan);
                Grid1.Columns.Add(rdPlan);
                RenderField rdThis = new RenderField();
                rdThis.ColumnID = string.Format("{0:yyyy-MM}", months[i]) + "This";
                rdThis.Width = Unit.Pixel(100);
                rdThis.DataField = "ThisNum" + (i + 1).ToString();
                rdThis.FieldType = FieldType.Double;
                rdThis.HeaderText = string.Format("{0:yyyy年MM月}", months[i]) + "
完成量";
                rdThis.HeaderTextAlign = TextAlign.Center;
                NumberBox numReal = new NumberBox();
                numReal.NoNegative = true;
                numReal.NoDecimal = false;
                rdThis.Editor.Add(numReal);
                Grid1.Columns.Add(rdThis);
                //Grid1.Columns.Add(gd);
            }
        }
        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.Id = Request.Params["Id"];
                List itemList = new List();
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(Id);
                DateTime startDate, endDate, startMonth, endMonth;
                List months = new List();
                List notEditIds = new List();
                notEditIds.Add(this.Id);
                if (wbsSet != null && wbsSet.StartDate != null && wbsSet.EndDate != null)
                {
                    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);
                }
                Model.CostControlDetailItem item1 = new Model.CostControlDetailItem();
                item1.Id = Id;
                item1.Name1 = wbsSet.WbsSetName;
                itemList.Add(item1);
                var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(Id);
                foreach (var childWbsSet in childWbsSets)
                {
                    notEditIds.Add(childWbsSet.WbsSetId);
                    Model.CostControlDetailItem item2 = new Model.CostControlDetailItem();
                    item2.Id = childWbsSet.WbsSetId;
                    item2.Name2 = childWbsSet.WbsSetName;
                    itemList.Add(item2);
                    var costControls = BLL.CostControlService.GetSelectedCostControlsByWbsSetId(childWbsSet.WbsSetId);
                    foreach (var costControl in costControls)
                    {
                        Model.CostControlDetailItem item3 = new Model.CostControlDetailItem();
                        item3.Id = costControl.CostControlId;
                        item3.Name3 = costControl.CostControlName;
                        item3.TotalNum = costControl.TotalNum;
                        item3.PlanPrice = costControl.PlanPrice;
                        item3.RealPrice = costControl.RealPrice;
                        for (int j = 0; j < months.Count; j++)
                        {
                            Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlIdAndMonths(costControl.CostControlId, months[j]);
                            if (costControlDetail != null)
                            {
                                if (j == 0)
                                {
                                    item3.PlanNum1 = costControlDetail.PlanNum;
                                    item3.ThisNum1 = costControlDetail.ThisNum;
                                }
                                else if (j == 1)
                                {
                                    item3.PlanNum2 = costControlDetail.PlanNum;
                                    item3.ThisNum2 = costControlDetail.ThisNum;
                                }
                                else if (j == 2)
                                {
                                    item3.PlanNum3 = costControlDetail.PlanNum;
                                    item3.ThisNum3 = costControlDetail.ThisNum;
                                }
                                else if (j == 3)
                                {
                                    item3.PlanNum4 = costControlDetail.PlanNum;
                                    item3.ThisNum4 = costControlDetail.ThisNum;
                                }
                                else if (j == 4)
                                {
                                    item3.PlanNum5 = costControlDetail.PlanNum;
                                    item3.ThisNum5 = costControlDetail.ThisNum;
                                }
                                else if (j == 5)
                                {
                                    item3.PlanNum6 = costControlDetail.PlanNum;
                                    item3.ThisNum6 = costControlDetail.ThisNum;
                                }
                                else if (j == 6)
                                {
                                    item3.PlanNum7 = costControlDetail.PlanNum;
                                    item3.ThisNum7 = costControlDetail.ThisNum;
                                }
                                else if (j == 7)
                                {
                                    item3.PlanNum8 = costControlDetail.PlanNum;
                                    item3.ThisNum8 = costControlDetail.ThisNum;
                                }
                                else if (j == 8)
                                {
                                    item3.PlanNum9 = costControlDetail.PlanNum;
                                    item3.ThisNum9 = costControlDetail.ThisNum;
                                }
                                else if (j == 9)
                                {
                                    item3.PlanNum10 = costControlDetail.PlanNum;
                                    item3.ThisNum10 = costControlDetail.ThisNum;
                                }
                                else if (j == 10)
                                {
                                    item3.PlanNum11 = costControlDetail.PlanNum;
                                    item3.ThisNum11 = costControlDetail.ThisNum;
                                }
                                else if (j == 11)
                                {
                                    item3.PlanNum12 = costControlDetail.PlanNum;
                                    item3.ThisNum12 = costControlDetail.ThisNum;
                                }
                                else if (j == 12)
                                {
                                    item3.PlanNum13 = costControlDetail.PlanNum;
                                    item3.ThisNum13 = costControlDetail.ThisNum;
                                }
                                else if (j == 13)
                                {
                                    item3.PlanNum14 = costControlDetail.PlanNum;
                                    item3.ThisNum14 = costControlDetail.ThisNum;
                                }
                                else if (j == 14)
                                {
                                    item3.PlanNum15 = costControlDetail.PlanNum;
                                    item3.ThisNum15 = costControlDetail.ThisNum;
                                }
                                else if (j == 15)
                                {
                                    item3.PlanNum16 = costControlDetail.PlanNum;
                                    item3.ThisNum16 = costControlDetail.ThisNum;
                                }
                                else if (j == 16)
                                {
                                    item3.PlanNum17 = costControlDetail.PlanNum;
                                    item3.ThisNum17 = costControlDetail.ThisNum;
                                }
                                else if (j == 17)
                                {
                                    item3.PlanNum18 = costControlDetail.PlanNum;
                                    item3.ThisNum18 = costControlDetail.ThisNum;
                                }
                                else if (j == 18)
                                {
                                    item3.PlanNum19 = costControlDetail.PlanNum;
                                    item3.ThisNum19 = costControlDetail.ThisNum;
                                }
                                else if (j == 19)
                                {
                                    item3.PlanNum20 = costControlDetail.PlanNum;
                                    item3.ThisNum20 = costControlDetail.ThisNum;
                                }
                                else if (j == 20)
                                {
                                    item3.PlanNum21 = costControlDetail.PlanNum;
                                    item3.ThisNum21 = costControlDetail.ThisNum;
                                }
                                else if (j == 21)
                                {
                                    item3.PlanNum22 = costControlDetail.PlanNum;
                                    item3.ThisNum22 = costControlDetail.ThisNum;
                                }
                                else if (j == 22)
                                {
                                    item3.PlanNum23 = costControlDetail.PlanNum;
                                    item3.ThisNum23 = costControlDetail.ThisNum;
                                }
                                else if (j == 23)
                                {
                                    item3.PlanNum24 = costControlDetail.PlanNum;
                                    item3.ThisNum24 = costControlDetail.ThisNum;
                                }
                            }
                        }
                        itemList.Add(item3);
                    }
                }
                this.Grid1.DataSource = itemList;
                this.Grid1.DataBind();
                for (int i = 0; i < this.Grid1.Rows.Count; i++)
                {
                    if (notEditIds.Contains(this.Grid1.Rows[i].RowID))
                    {
                        this.Grid1.Rows[i].RowCssClass = "noEdit";
                    }
                }
            }
        }
        /// 
        /// 增加计划月份
        /// 
        /// 
        /// 
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Save();
            Model.Wbs_WbsSet wbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(Id);
            if (wbsSet1.EndDate != null)
            {
                wbsSet1.EndDate = wbsSet1.EndDate.Value.AddMonths(1);
                BLL.WbsSetService.UpdateWbsSet(wbsSet1);
            }
            PageContext.RegisterStartupScript(String.Format("window.location.reload();"));
        }
        /// 
        /// 减少计划月份
        /// 
        /// 
        /// 
        protected void btnDel_Click(object sender, EventArgs e)
        {
            Save();
            Model.Wbs_WbsSet wbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(Id);
            if (wbsSet1.EndDate != null)
            {
                wbsSet1.EndDate = wbsSet1.EndDate.Value.AddMonths(-1);
                BLL.WbsSetService.UpdateWbsSet(wbsSet1);
            }
            PageContext.RegisterStartupScript(String.Format("window.location.reload();"));
        }
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            Save();
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHideReference());
        }
        private void Save()
        {
            Model.Wbs_WbsSet wbsSet1 = BLL.WbsSetService.GetWbsSetByWbsSetId(Id);
            DateTime startDate, endDate, startMonth, endMonth;
            List months = new List();
            if (wbsSet1 != null && wbsSet1.StartDate != null && wbsSet1.EndDate != null)
            {
                startDate = Convert.ToDateTime(wbsSet1.StartDate);
                endDate = Convert.ToDateTime(wbsSet1.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);
            }
            decimal changeThisPlanValue = 0, changeThisRealCost = 0, changeThisPlanCost = 0,  //当月总变化完成成本、完成预算
                        oldThisPlanValue = 0, oldThisRealCost = 0, oldThisPlanCost = 0,
                        thisPlanValue = 0, thisRealCost = 0, thisPlanCost = 0;
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                string costControlId = Grid1.Rows[i].RowID;
                Model.WBS_CostControl costControl = BLL.CostControlService.GetCostControlByCostControlId(costControlId);
                if (costControl != null)
                {
                    decimal oldPlanPrice = costControl.PlanPrice ?? 0;
                    decimal oldRealPrice = costControl.RealPrice ?? 0;
                    string totalNum = values.Value("TotalNum");
                    string planPrice = values.Value("PlanPrice");
                    string realPrice = values.Value("RealPrice");
                    costControl.TotalNum = Funs.GetNewDecimal(totalNum);
                    costControl.PlanPrice = Funs.GetNewDecimal(planPrice);
                    costControl.RealPrice = Funs.GetNewDecimal(realPrice);
                    BLL.CostControlService.UpdateCostControl(costControl);
                    for (int j = 0; j < months.Count; j++)
                    {
                        oldThisPlanValue = 0;
                        oldThisRealCost = 0;
                        oldThisPlanCost = 0;
                        changeThisPlanValue = 0;
                        changeThisRealCost = 0;
                        changeThisPlanCost = 0;
                        string planNum = values.Value(string.Format("{0:yyyy-MM}", months[j]) + "Plan");
                        string thisNum = values.Value(string.Format("{0:yyyy-MM}", months[j]) + "This");
                        Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlIdAndMonths(costControlId, months[j]);
                        if (costControlDetail != null)
                        {
                            oldThisPlanValue = (costControlDetail.PlanNum ?? 0) * oldPlanPrice;
                            oldThisRealCost = (costControlDetail.ThisNum ?? 0) * oldRealPrice;
                            oldThisPlanCost = (costControlDetail.ThisNum ?? 0) * oldPlanPrice;
                            costControlDetail.PlanNum = Funs.GetNewDecimalOrZero(planNum);
                            costControlDetail.ThisNum = Funs.GetNewDecimalOrZero(thisNum);
                            thisPlanValue = (costControlDetail.PlanNum ?? 0) * (costControl.PlanPrice ?? 0);
                            thisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                            thisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                            BLL.CostControlDetailService.UpdateCostControlDetail(costControlDetail);
                        }
                        else
                        {
                            costControlDetail = new Model.WBS_CostControlDetail();
                            costControlDetail.CostControlDetailId = SQLHelper.GetNewID();
                            costControlDetail.CostControlId = costControlId;
                            costControlDetail.Months = months[j];
                            costControlDetail.PlanNum = Funs.GetNewDecimalOrZero(planNum);
                            costControlDetail.ThisNum = Funs.GetNewDecimalOrZero(thisNum);
                            thisPlanValue = (costControlDetail.PlanNum ?? 0) * (costControl.PlanPrice ?? 0);
                            thisRealCost = (costControlDetail.ThisNum ?? 0) * (costControl.RealPrice ?? 0);
                            thisPlanCost = (costControlDetail.ThisNum ?? 0) * (costControl.PlanPrice ?? 0);
                            BLL.CostControlDetailService.AddCostControlDetail(costControlDetail);
                        }
                        //累加变化值,计算总的变化值
                        changeThisPlanValue += thisPlanValue - oldThisPlanValue;
                        changeThisRealCost += thisRealCost - oldThisRealCost;
                        changeThisPlanCost += thisPlanCost - oldThisPlanCost;
                        //更新工作包、工作项
                        Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(costControl.WbsSetId);
                        UpdateWbsSetDetail(costControl.WbsSetId, months[j], changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                        //更新分部
                        Model.WBS_CostControlParentDetail unitProjectDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(wbsSet.UnitProjectId, months[j]);
                        if (unitProjectDetail != null)
                        {
                            unitProjectDetail.ThisPlanValue += changeThisPlanValue;
                            unitProjectDetail.ThisRealCost += changeThisRealCost;
                            unitProjectDetail.ThisPlanCost += changeThisPlanCost;
                            BLL.CostControlParentDetailService.UpdateCostControlParentDetail(unitProjectDetail);
                        }
                        else
                        {
                            unitProjectDetail = new Model.WBS_CostControlParentDetail();
                            unitProjectDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                            unitProjectDetail.ParentId = wbsSet.UnitProjectId;
                            unitProjectDetail.Months = months[j];
                            unitProjectDetail.ThisPlanValue = changeThisPlanValue;
                            unitProjectDetail.ThisRealCost = changeThisRealCost;
                            unitProjectDetail.ThisPlanCost = changeThisPlanCost;
                            BLL.CostControlParentDetailService.AddCostControlParentDetail(unitProjectDetail);
                        }
                        //更新专业
                        if (!string.IsNullOrEmpty(wbsSet.CnProfessionId))
                        {
                            Model.WBS_CostControlParentDetail cnProfessionDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(wbsSet.CnProfessionId, months[j]);
                            if (cnProfessionDetail != null)
                            {
                                cnProfessionDetail.ThisPlanValue += changeThisPlanValue;
                                cnProfessionDetail.ThisRealCost += changeThisRealCost;
                                cnProfessionDetail.ThisPlanCost += changeThisPlanCost;
                                BLL.CostControlParentDetailService.UpdateCostControlParentDetail(cnProfessionDetail);
                            }
                            else
                            {
                                cnProfessionDetail = new Model.WBS_CostControlParentDetail();
                                cnProfessionDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                                cnProfessionDetail.ParentId = wbsSet.CnProfessionId;
                                cnProfessionDetail.Months = months[j];
                                cnProfessionDetail.ThisPlanValue = changeThisPlanValue;
                                cnProfessionDetail.ThisRealCost = changeThisRealCost;
                                cnProfessionDetail.ThisPlanCost = changeThisPlanCost;
                                BLL.CostControlParentDetailService.AddCostControlParentDetail(cnProfessionDetail);
                            }
                        }
                        //更新装置
                        UpdateInstallationDetail(wbsSet.InstallationId, months[j], changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                    }
                }
            }
        }
        #region  更新工作包、工作项
        /// 
        /// 更新月工作包、工作项
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWbsSetDetail(string wbsSetId, DateTime months, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSetId);
            if (wbsSet != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(wbsSetId, months);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = wbsSetId;
                    parentDetail.Months = months;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (wbsSet.SuperWbsSetId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateWbsSetDetail(wbsSet.SuperWbsSetId, months, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        /// 
        /// 更新周工作包、工作项
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWeekWbsSetDetail(string wbsSetId, DateTime months, DateTime startDate, DateTime endDate, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(wbsSetId);
            if (wbsSet != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonthsWeek(wbsSetId, months, startDate);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = wbsSetId;
                    parentDetail.Months = months;
                    parentDetail.StartDate = startDate;
                    parentDetail.EndDate = endDate;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (wbsSet.SuperWbsSetId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateWeekWbsSetDetail(wbsSet.SuperWbsSetId, months, startDate, endDate, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        #endregion
        #region  更新装置
        /// 
        /// 更新月装置
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateInstallationDetail(string installationId, DateTime months, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(installationId);
            if (installation != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(installationId, months);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = installationId;
                    parentDetail.Months = months;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (installation.SuperInstallationId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateInstallationDetail(installation.SuperInstallationId, months, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        /// 
        /// 更新周装置
        /// 
        /// 
        /// 
        /// 
        /// 
        private void UpdateWeekInstallationDetail(string installationId, DateTime months, DateTime startDate, DateTime endDate, decimal changeThisPlanValue, decimal changeThisRealCost, decimal changeThisPlanCost)
        {
            Model.Project_Installation installation = BLL.Project_InstallationService.GetInstallationByInstallationId(installationId);
            if (installation != null)
            {
                Model.WBS_CostControlParentDetail parentDetail = BLL.CostControlParentDetailService.GetCostControlParentDetailByParentIdAndMonths(installationId, months);
                if (parentDetail != null)
                {
                    parentDetail.ThisPlanValue += changeThisPlanValue;
                    parentDetail.ThisRealCost += changeThisRealCost;
                    parentDetail.ThisPlanCost += changeThisPlanCost;
                    BLL.CostControlParentDetailService.UpdateCostControlParentDetail(parentDetail);
                }
                else
                {
                    parentDetail = new Model.WBS_CostControlParentDetail();
                    parentDetail.CostControlParentDetailId = SQLHelper.GetNewID();
                    parentDetail.ParentId = installationId;
                    parentDetail.Months = months;
                    parentDetail.StartDate = startDate;
                    parentDetail.EndDate = endDate;
                    parentDetail.ThisPlanValue = changeThisPlanValue;
                    parentDetail.ThisRealCost = changeThisRealCost;
                    parentDetail.ThisPlanCost = changeThisPlanCost;
                    BLL.CostControlParentDetailService.AddCostControlParentDetail(parentDetail);
                }
                if (installation.SuperInstallationId != null)   //还存在上级节点,需要继续循环
                {
                    UpdateWeekInstallationDetail(installation.SuperInstallationId, months, startDate, endDate, changeThisPlanValue, changeThisRealCost, changeThisPlanCost);
                }
            }
        }
        #endregion
        #endregion
        //#region 导出按钮
        ///// 导出按钮
        ///// 
        ///// 
        ///// 
        //protected void btnOut_Click(object sender, EventArgs e)
        //{
        //    Response.ClearContent();
        //    string filename = Funs.GetNewFileName();
        //   Response.AddHeader("content-disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode("工作量录入" + filename, System.Text.Encoding.UTF8) + ".xls");
        //    Response.ContentType = "application/excel";
        //    Response.ContentEncoding = Encoding.UTF8;
        //    this.Grid1.PageSize = Grid1.RecordCount;
        //    Response.Write(GetGridTableHtml2(Grid1));
        //    Response.End();
        //}
        //#endregion
        protected void btnOut_Click(object sender, EventArgs e)
        {
            string varValue = string.Empty;
            Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(Id);
            DateTime startDate, endDate, startMonth, endMonth;
            List months = new List();
            if (wbsSet != null && wbsSet.StartDate != null && wbsSet.EndDate != null)
            {
                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);
            }
            string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
            //模板文件
            string TempletFileName = Server.MapPath("~/") + "File/Excel/JDGL/工作量录入.xls";
            //导出文件
            string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            string ReportFileName = filePath + "out.xlsx";
            FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
            HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
            ICellStyle styleCenter = hssfworkbook.CreateCellStyle();
            styleCenter.VerticalAlignment = VerticalAlignment.Center;
            styleCenter.Alignment = HorizontalAlignment.Center;
            styleCenter.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            styleCenter.WrapText = true;
            IFont font = styleCenter.GetFont(hssfworkbook);
            font.FontHeightInPoints = 10;//字体高度(与excel中的字号一致)                
            styleCenter.SetFont(font);
            HSSFSheet recordSheet = (HSSFSheet)hssfworkbook.GetSheet("工作量录入");
            HSSFRow row0 = (HSSFRow)recordSheet.CreateRow(0);
            HSSFCell cell0 = (HSSFCell)row0.CreateCell(0);
            cell0.CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            cell0.CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            cell0.CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            cell0.CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            cell0.CellStyle.WrapText = true;
            cell0.SetCellValue(new HSSFRichTextString("序号"));
            HSSFCell cell1 = (HSSFCell)row0.CreateCell(1);
            cell1.SetCellValue(new HSSFRichTextString("分部"));
            cell1.CellStyle.WrapText = true;
            HSSFCell cell2 = (HSSFCell)row0.CreateCell(2);
            cell2.SetCellValue(new HSSFRichTextString("分项"));
            cell2.CellStyle.WrapText = true;
            HSSFCell cell3 = (HSSFCell)row0.CreateCell(3);
            cell3.SetCellValue(new HSSFRichTextString("费控项内容"));
            cell3.CellStyle.WrapText = true;
            HSSFCell cell4 = (HSSFCell)row0.CreateCell(4);
            cell4.SetCellValue(new HSSFRichTextString("合同工作量"));
            HSSFCell cell5 = (HSSFCell)row0.CreateCell(5);
            cell5.SetCellValue(new HSSFRichTextString("预算单价"));
            HSSFCell cell6 = (HSSFCell)row0.CreateCell(6);
            cell6.SetCellValue(new HSSFRichTextString("实际单价"));
            for (int a = 0; a < months.Count; a++)
            {
                HSSFCell cellPlan = (HSSFCell)row0.CreateCell(7 + a * 2);
                cellPlan.SetCellValue(new HSSFRichTextString(string.Format("{0:yyyy年MM月}", months[a]) + "计划量"));
                HSSFCell cellThis = (HSSFCell)row0.CreateCell(8 + a * 2);
                cellThis.SetCellValue(new HSSFRichTextString(string.Format("{0:yyyy年MM月}", months[a]) + "完成量"));
            }
            int i = 1;
            HSSFRow row1 = (HSSFRow)recordSheet.CreateRow(1);
            HSSFCell r1cell0 = (HSSFCell)row1.CreateCell(0);
            r1cell0.SetCellValue(new HSSFRichTextString(i.ToString()));
            HSSFCell r1cell1 = (HSSFCell)row1.CreateCell(1);
            r1cell1.SetCellValue(new HSSFRichTextString(wbsSet.WbsSetName));
            r1cell1.CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
            r1cell1.CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
            r1cell1.CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
            r1cell1.CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
            r1cell1.CellStyle.WrapText = true;
            var childWbsSets = BLL.WbsSetService.GetApproveWbsSetsBySuperWbsSetId(Id);
            foreach (var childWbsSet in childWbsSets)
            {
                i++;
                HSSFRow rowW = (HSSFRow)recordSheet.CreateRow(i);
                HSSFCell rWcell0 = (HSSFCell)rowW.CreateCell(0);
                rWcell0.SetCellValue(new HSSFRichTextString(i.ToString()));
                HSSFCell rWcell2 = (HSSFCell)rowW.CreateCell(2);
                rWcell2.SetCellValue(new HSSFRichTextString(childWbsSet.WbsSetName));
                rWcell2.CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                rWcell2.CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                rWcell2.CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                rWcell2.CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                rWcell2.CellStyle.WrapText = true;
                var costControls = BLL.CostControlService.GetSelectedCostControlsByWbsSetId(childWbsSet.WbsSetId);
                foreach (var costControl in costControls)
                {
                    i++;
                    HSSFRow rowC = (HSSFRow)recordSheet.CreateRow(i);
                    HSSFCell rCcell0 = (HSSFCell)rowC.CreateCell(0);
                    rCcell0.SetCellValue(new HSSFRichTextString(i.ToString()));
                    HSSFCell rCcell3 = (HSSFCell)rowC.CreateCell(3);
                    rCcell3.SetCellValue(new HSSFRichTextString(costControl.CostControlName));
                    rCcell3.CellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
                    rCcell3.CellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
                    rCcell3.CellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
                    rCcell3.CellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
                    rCcell3.CellStyle.WrapText = true;
                    HSSFCell rCcell4 = (HSSFCell)rowC.CreateCell(4);
                    if (costControl.TotalNum != null)
                    {
                        rCcell4.SetCellValue(new HSSFRichTextString(costControl.TotalNum.Value.ToString("0.##")));
                    }
                    HSSFCell rCcell5 = (HSSFCell)rowC.CreateCell(5);
                    if (costControl.PlanPrice != null)
                    {
                        rCcell5.SetCellValue(new HSSFRichTextString(costControl.PlanPrice.Value.ToString("0.##")));
                    }
                    HSSFCell rCcell6 = (HSSFCell)rowC.CreateCell(6);
                    if (costControl.RealPrice != null)
                    {
                        rCcell6.SetCellValue(new HSSFRichTextString(costControl.RealPrice.Value.ToString("0.##")));
                    }
                    for (int j = 0; j < months.Count; j++)
                    {
                        Model.WBS_CostControlDetail costControlDetail = BLL.CostControlDetailService.GetCostControlDetailByCostControlIdAndMonths(costControl.CostControlId, months[j]);
                        if (costControlDetail != null)
                        {
                            HSSFCell cellDetailPlan = (HSSFCell)rowC.CreateCell(7 + j * 2);
                            if (costControlDetail.PlanNum != null)
                            {
                                cellDetailPlan.SetCellValue(new HSSFRichTextString(costControlDetail.PlanNum.Value.ToString("0.##")));
                            }
                            HSSFCell cellDetailThis = (HSSFCell)rowC.CreateCell(8 + j * 2);
                            if (costControlDetail.ThisNum != null)
                            {
                                cellDetailThis.SetCellValue(new HSSFRichTextString(costControlDetail.ThisNum.Value.ToString("0.##")));
                            }
                        }
                    }
                }
            }
            using (FileStream filess = File.OpenWrite(ReportFileName))
            {
                hssfworkbook.Write(filess);
            }
            FileInfo filet = new FileInfo(ReportFileName);
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode("工作量录入_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls"));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            Response.AddHeader("Content-Length", filet.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载
            Response.ContentType = "application/ms-excel";
            // 把文件流发送到客户端
            Response.WriteFile(filet.FullName);
            // 停止页面的执行
            Response.End();
        }
    }
}