using BLL;
using System;
using System.Linq;
namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WBSSetCopy : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                string id = Request.Params["Id"];
                string type = Request.Params["Type"];
                if (type == "wbsSet")
                {
                    Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                    if (wbsSet != null)
                    {
                        txtCode.Text = wbsSet.WbsSetCode;
                        if (wbsSet.StartDate != null)
                        {
                            txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", wbsSet.StartDate);
                            txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", wbsSet.EndDate);
                        }
                        txtRemark.Text = wbsSet.Remark;
                    }
                }
            }
        }
        private void LoadData()
        {
            btnClose.OnClientClick = ActiveWindow.GetHideReference();
        }
        #endregion
        #region 获取新编号
        /// 
        /// 获取新编号
        /// 
        /// 
        /// 
        /// 
        private string GetNewCode(string code, int i)
        {
            string newCode = string.Empty;
            int codeLast = Convert.ToInt32(code.Substring(code.Length - 1, 1));
            if (codeLast == 1)
            {
                newCode = code.Substring(0, code.Length - 1) + ((i + 2) < 10 ? (i + 2) : 0);
            }
            else
            {
                newCode = code.Substring(0, code.Length - 1) + ((codeLast + 1) < 10 ? (codeLast + 1) : 0);
            }
            return newCode;
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string updateId = string.Empty;
            string id = Request.Params["Id"];
            string type = Request.Params["Type"];
            if (type == "wbsSet")
            {
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                //wbsSet.IsSelected = true;
                //wbsSet.IsApprove = true;
                //BLL.WbsSetService.UpdateWbsSet(wbsSet);
                var wbsSets = from x in Funs.DB.Wbs_WbsSet where x.SuperWbsSetId == id orderby x.WbsSetCode select x;
                Model.Wbs_WbsSet newWbsSet1 = new Model.Wbs_WbsSet();
                newWbsSet1.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                updateId = newWbsSet1.WbsSetId;
                newWbsSet1.WbsSetCode = this.txtCode.Text.Trim();
                newWbsSet1.WbsSetName = this.txtName.Text.Trim();
                newWbsSet1.CnProfessionId = wbsSet.CnProfessionId;
                newWbsSet1.UnitProjectId = wbsSet.UnitProjectId;
                newWbsSet1.InstallationId = wbsSet.InstallationId;
                newWbsSet1.SuperWbsSetId = wbsSet.SuperWbsSetId;
                newWbsSet1.ProjectId = wbsSet.ProjectId;
                newWbsSet1.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                newWbsSet1.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                {
                    newWbsSet1.SortIndex = Convert.ToInt32(this.txtSortIndex.Text.Trim());
                }
                newWbsSet1.Remark = this.txtRemark.Text.Trim();
                newWbsSet1.IsIn = false;
                //newWbsSet1.IsSelected = true;
                //newWbsSet1.IsApprove = true;
                BLL.WbsSetService.AddWbsSet(newWbsSet1);
                var totalWbsSetMatchCostControlInits = from x in Funs.DB.WBS_WbsSetMatchCostControlInit orderby x.WbsSetCode select x;
                var totalCostControlInits = from x in Funs.DB.WBS_CostControlInit orderby x.CostControlInitCode select x;
                foreach (var noShowWbsSet1 in wbsSets)
                {
                    Model.Wbs_WbsSet newWbsSet2 = new Model.Wbs_WbsSet();
                    newWbsSet2.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                    newWbsSet2.WbsSetCode = GetReplaceCode(noShowWbsSet1.WbsSetCode, wbsSet.WbsSetCode);
                    newWbsSet2.WbsSetName = noShowWbsSet1.WbsSetName;
                    newWbsSet2.CnProfessionId = noShowWbsSet1.CnProfessionId;
                    newWbsSet2.UnitProjectId = noShowWbsSet1.UnitProjectId;
                    newWbsSet2.InstallationId = noShowWbsSet1.InstallationId;
                    newWbsSet2.SuperWbsSetId = newWbsSet1.WbsSetId;
                    newWbsSet2.ProjectId = noShowWbsSet1.ProjectId;
                    newWbsSet2.Weights = noShowWbsSet1.Weights;
                    newWbsSet2.StartDate = noShowWbsSet1.StartDate;
                    newWbsSet2.EndDate = noShowWbsSet1.EndDate;
                    newWbsSet2.SortIndex = noShowWbsSet1.SortIndex;
                    newWbsSet2.Remark = noShowWbsSet1.Remark;
                    newWbsSet2.IsIn = false;
                    //newWbsSet2.IsSelected = true;
                    //newWbsSet2.IsApprove = true;
                    BLL.WbsSetService.AddWbsSet(newWbsSet2);
                    //拷贝费用清单对应关系
                    var wbsSetMatchCostControlInits = from x in totalWbsSetMatchCostControlInits where x.WbsSetCode == noShowWbsSet1.WbsSetCode orderby x.WbsSetCode select x;
                    foreach (var wbsSetMatchCostControlInit in wbsSetMatchCostControlInits)
                    {
                        Model.WBS_WbsSetMatchCostControl wbsSetMatchCostControl = new Model.WBS_WbsSetMatchCostControl();
                        wbsSetMatchCostControl.WbsSetMatchCostControlId = SQLHelper.GetNewID();
                        wbsSetMatchCostControl.WbsSetId = newWbsSet2.WbsSetId;
                        wbsSetMatchCostControl.CostControlCode = wbsSetMatchCostControlInit.CostControlInitCode;
                        BLL.WbsSetMatchCostControlService.AddWbsSetMatchCostControl(wbsSetMatchCostControl);
                        //拷贝费用清单项
                        var costControlInits = from x in totalCostControlInits where x.CostControlInitCode == wbsSetMatchCostControl.CostControlCode select x;
                        foreach (var costControlInit in costControlInits)
                        {
                            Model.WBS_CostControl costControl = new Model.WBS_CostControl();
                            costControl.ProjectId = noShowWbsSet1.ProjectId;
                            costControl.WbsSetId = newWbsSet2.WbsSetId;
                            costControl.CostControlCode = costControlInit.CostControlInitCode;
                            costControl.CostControlName = costControlInit.CostControlInitName;
                            costControl.Unit = costControlInit.Unit;
                            BLL.CostControlService.AddCostControl(costControl);
                        }
                    }
                }
               
               
                //if (noShowWbsSets.Count() == 0)  //首次拷贝
                //{
                //    noShowWbsSets = from x in Funs.DB.Wbs_WbsSet where x.SuperWbsSetId == id orderby x.WbsSetCode select x;
                //    foreach (var noShowWbsSet in noShowWbsSets)
                //    {
                //        noShowWbsSet.NoShow = true;
                //        BLL.WbsSetService.UpdateWbsSet(noShowWbsSet);
                //    }
                //    Model.Wbs_WbsSet newWbsSet1 = new Model.Wbs_WbsSet();
                //    newWbsSet1.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                //    updateId = newWbsSet1.WbsSetId;
                //    newWbsSet1.WbsSetCode = this.txtCode.Text.Trim();
                //    newWbsSet1.WbsSetName = this.txtName.Text.Trim();
                //    newWbsSet1.CnProfessionId = wbsSet.CnProfessionId;
                //    newWbsSet1.UnitProjectId = wbsSet.UnitProjectId;
                //    newWbsSet1.InstallationId = wbsSet.InstallationId;
                //    newWbsSet1.SuperWbsSetId = id;
                //    newWbsSet1.ProjectId = wbsSet.ProjectId;
                //    newWbsSet1.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                //    newWbsSet1.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                //    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                //    {
                //        newWbsSet1.SortIndex = Convert.ToInt32(this.txtSortIndex.Text.Trim());
                //    }
                //    newWbsSet1.Flag = wbsSet.Flag + 1;
                //    newWbsSet1.Remark = this.txtRemark.Text.Trim();
                //    newWbsSet1.IsIn = false;
                //    newWbsSet1.IsSelected = true;
                //    newWbsSet1.IsApprove = true;
                //    BLL.WbsSetService.AddWbsSet(newWbsSet1);
                //    noShowWbsSets = from x in Funs.DB.Wbs_WbsSet where x.SuperWbsSetId == id && x.NoShow == true orderby x.WbsSetCode select x;
                //    foreach (var noShowWbsSet1 in noShowWbsSets)
                //    {
                //        Model.Wbs_WbsSet newWbsSet2 = new Model.Wbs_WbsSet();
                //        newWbsSet2.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                //        newWbsSet2.WbsSetCode = GetReplaceCode(noShowWbsSet1.WbsSetCode, wbsSet.WbsSetCode);
                //        newWbsSet2.WbsSetName = noShowWbsSet1.WbsSetName;
                //        newWbsSet2.CnProfessionId = noShowWbsSet1.CnProfessionId;
                //        newWbsSet2.UnitProjectId = noShowWbsSet1.UnitProjectId;
                //        newWbsSet2.InstallationId = noShowWbsSet1.InstallationId;
                //        newWbsSet2.SuperWbsSetId = newWbsSet1.WbsSetId;
                //        newWbsSet2.ProjectId = noShowWbsSet1.ProjectId;
                //        newWbsSet2.Weights = noShowWbsSet1.Weights;
                //        newWbsSet2.StartDate = noShowWbsSet1.StartDate;
                //        newWbsSet2.EndDate = noShowWbsSet1.EndDate;
                //        newWbsSet2.SortIndex = noShowWbsSet1.SortIndex;
                //        newWbsSet2.Flag = newWbsSet1.Flag + 1;
                //        newWbsSet2.Remark = noShowWbsSet1.Remark;
                //        newWbsSet2.IsIn = false;
                //        newWbsSet2.IsSelected = true;
                //        newWbsSet2.IsApprove = true;
                //        BLL.WbsSetService.AddWbsSet(newWbsSet2);
                //    }
                //}
                //else   //后续拷贝
                //{
                //    Model.Wbs_WbsSet newWbsSet1 = new Model.Wbs_WbsSet();
                //    newWbsSet1.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                //    updateId = newWbsSet1.WbsSetId;
                //    newWbsSet1.WbsSetCode = this.txtCode.Text.Trim();
                //    newWbsSet1.WbsSetName = this.txtName.Text.Trim();
                //    newWbsSet1.CnProfessionId = wbsSet.CnProfessionId;
                //    newWbsSet1.UnitProjectId = wbsSet.UnitProjectId;
                //    newWbsSet1.InstallationId = wbsSet.InstallationId;
                //    newWbsSet1.SuperWbsSetId = id;
                //    newWbsSet1.ProjectId = wbsSet.ProjectId;
                //    newWbsSet1.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                //    newWbsSet1.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                //    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                //    {
                //        newWbsSet1.SortIndex = Convert.ToInt32(this.txtSortIndex.Text.Trim());
                //    }
                //    newWbsSet1.Flag = wbsSet.Flag + 1;
                //    newWbsSet1.Remark = this.txtRemark.Text.Trim();
                //    newWbsSet1.IsIn = false;
                //    newWbsSet1.IsSelected = true;
                //    newWbsSet1.IsApprove = true;
                //    BLL.WbsSetService.AddWbsSet(newWbsSet1);
                //    foreach (var noShowWbsSet1 in noShowWbsSets)
                //    {
                //        Model.Wbs_WbsSet newWbsSet2 = new Model.Wbs_WbsSet();
                //        newWbsSet2.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                //        newWbsSet2.WbsSetCode = GetReplaceCode(noShowWbsSet1.WbsSetCode, wbsSet.WbsSetCode);
                //        newWbsSet2.WbsSetName = noShowWbsSet1.WbsSetName;
                //        newWbsSet2.CnProfessionId = noShowWbsSet1.CnProfessionId;
                //        newWbsSet2.UnitProjectId = noShowWbsSet1.UnitProjectId;
                //        newWbsSet2.InstallationId = noShowWbsSet1.InstallationId;
                //        newWbsSet2.SuperWbsSetId = newWbsSet1.WbsSetId;
                //        newWbsSet2.ProjectId = noShowWbsSet1.ProjectId;
                //        newWbsSet2.Weights = noShowWbsSet1.Weights;
                //        newWbsSet2.StartDate = noShowWbsSet1.StartDate;
                //        newWbsSet2.EndDate = noShowWbsSet1.EndDate;
                //        newWbsSet2.SortIndex = noShowWbsSet1.SortIndex;
                //        newWbsSet2.Flag = newWbsSet1.Flag + 1;
                //        newWbsSet2.Remark = noShowWbsSet1.Remark;
                //        newWbsSet2.IsIn = false;
                //        newWbsSet2.IsSelected = true;
                //        newWbsSet2.IsApprove = true;
                //        BLL.WbsSetService.AddWbsSet(newWbsSet2);
                //    }
                //}
            }
            //BLL.Sys_LogService.AddLog(BLL.Const.System_1, this.CurrUser.LoginProjectId, this.CurrUser.UserId, "拷贝单位、分部、分项工程");
            PageContext.RegisterStartupScript(ActiveWindow.GetWriteBackValueReference(updateId) + ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 循环拷贝分部子级
        /// 
        /// 循环拷贝分部子级
        /// 
        /// 
        /// 
        /// 
        /// 
        private void AddWbsSets(string newSuperWbsSetId, string oldSuperWbsSetId, string code, string unitProjectId)
        {
            var childWbsSets = BLL.WbsSetService.GetWbsSetsBySuperWbsSetId(oldSuperWbsSetId);
            foreach (var wbsSet in childWbsSets)
            {
                Model.Wbs_WbsSet newWbsSet = new Model.Wbs_WbsSet();
                newWbsSet.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                newWbsSet.WbsSetCode = GetReplaceCode(wbsSet.WbsSetCode, code);
                newWbsSet.WbsSetName = wbsSet.WbsSetName;
                newWbsSet.CnProfessionId = wbsSet.CnProfessionId;
                newWbsSet.UnitProjectId = unitProjectId;
                newWbsSet.SuperWbsSetId = newSuperWbsSetId;
                newWbsSet.ProjectId = wbsSet.ProjectId;
                newWbsSet.StartDate = wbsSet.StartDate;
                newWbsSet.EndDate = wbsSet.EndDate;
                newWbsSet.SortIndex = wbsSet.SortIndex;
                newWbsSet.ControlPoint = wbsSet.ControlPoint;
                newWbsSet.Cycle = wbsSet.Cycle;
                newWbsSet.Frequency = wbsSet.Frequency;
                newWbsSet.Flag = wbsSet.Flag;
                newWbsSet.Way = wbsSet.Way;
                newWbsSet.Remark = wbsSet.Remark;
                newWbsSet.IsIn = false;
                if (string.IsNullOrEmpty(Request.Params["HandleType"]))
                {
                    newWbsSet.IsSelected = true;
                }
                else
                {
                    newWbsSet.IsApprove = true;
                }
                BLL.WbsSetService.AddWbsSet(newWbsSet);
                this.AddWbsSets(newWbsSet.WbsSetId, wbsSet.WbsSetId, code, unitProjectId);
            }
        }
        #endregion
        #region 获取编号
        private string GetReplaceCode(string oldStr, string replaceCode)
        {
            if (oldStr.IndexOf(replaceCode) > -1)
            {
                oldStr = oldStr.Remove(oldStr.IndexOf(replaceCode), replaceCode.Length).Insert(oldStr.IndexOf(replaceCode), this.txtCode.Text.Trim());
            }
            return oldStr;
        }
        #endregion
    }
}