using BLL;
using System;
using System.Linq;

namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WBSSetCopy : PageBase
    {
        #region 加载
        /// <summary>
        /// 加载页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
                string id = Request.Params["Id"];
                string type = Request.Params["Type"];
                if (type == "childUnitProject")
                {
                    Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                    if (unitProject != null)
                    {
                        txtCode.Text = unitProject.UnitProjectCode;
                        if (unitProject.StartDate != null)
                        {
                            txtStartDate.Text = string.Format("{0:yyyy-MM-dd}", unitProject.StartDate);
                            txtEndDate.Text = string.Format("{0:yyyy-MM-dd}", unitProject.EndDate);
                        }
                        txtRemark.Text = unitProject.Remark;
                    }
                }
                else 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 获取新编号
        /// <summary>
        /// 获取新编号
        /// </summary>
        /// <param name="code"></param>
        /// <param name="i"></param>
        /// <returns></returns>
        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 保存
        /// <summary>
        /// 保存按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string updateId = string.Empty;
            string id = Request.Params["Id"];
            string type = Request.Params["Type"];
            if (type == "childUnitProject")
            {
                Model.Wbs_UnitProject childUnitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                childUnitProject.IsSelected = true;
                childUnitProject.IsApprove = true;
                BLL.UnitProjectService.UpdateUnitProject(childUnitProject);
                var noShowWbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == childUnitProject.UnitProjectId && x.NoShow == true orderby x.WbsSetCode select x;
                if (noShowWbsSet1s.Count() == 0)  //首次拷贝
                {
                    noShowWbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == childUnitProject.UnitProjectId orderby x.WbsSetCode select x;
                    foreach (var noShowWbsSet1 in noShowWbsSet1s)
                    {
                        noShowWbsSet1.NoShow = true;
                        BLL.WbsSetService.UpdateWbsSet(noShowWbsSet1);
                    }
                    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 = childUnitProject.CnProfessionId;
                    newWbsSet1.UnitProjectId = childUnitProject.UnitProjectId;
                    newWbsSet1.InstallationId = childUnitProject.InstallationId;
                    newWbsSet1.SuperWbsSetId = null;
                    newWbsSet1.ProjectId = childUnitProject.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 = 1;
                    newWbsSet1.Remark = this.txtRemark.Text.Trim();
                    newWbsSet1.IsIn = false;
                    newWbsSet1.IsSelected = true;
                    newWbsSet1.IsApprove = true;
                    BLL.WbsSetService.AddWbsSet(newWbsSet1);
                    noShowWbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == childUnitProject.UnitProjectId && x.NoShow == true orderby x.WbsSetCode select x;
                    foreach (var noShowWbsSet1 in noShowWbsSet1s)
                    {
                        Model.Wbs_WbsSet newWbsSet2 = new Model.Wbs_WbsSet();
                        newWbsSet2.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                        newWbsSet2.WbsSetCode = GetReplaceCode(noShowWbsSet1.WbsSetCode, childUnitProject.UnitProjectCode);
                        newWbsSet2.WbsSetName = noShowWbsSet1.WbsSetName;
                        newWbsSet2.CnProfessionId = noShowWbsSet1.CnProfessionId;
                        newWbsSet2.UnitProjectId = childUnitProject.UnitProjectId;
                        newWbsSet2.InstallationId = noShowWbsSet1.InstallationId;
                        newWbsSet2.SuperWbsSetId = newWbsSet1.WbsSetId;
                        newWbsSet2.Weights = noShowWbsSet1.Weights;
                        newWbsSet2.ProjectId = noShowWbsSet1.ProjectId;
                        newWbsSet2.StartDate = noShowWbsSet1.StartDate;
                        newWbsSet2.EndDate = noShowWbsSet1.EndDate;
                        newWbsSet1.SortIndex = noShowWbsSet1.SortIndex;
                        newWbsSet2.Flag = 2;
                        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 = childUnitProject.CnProfessionId;
                    newWbsSet1.UnitProjectId = childUnitProject.UnitProjectId;
                    newWbsSet1.InstallationId = childUnitProject.InstallationId;
                    newWbsSet1.SuperWbsSetId = null;
                    newWbsSet1.ProjectId = childUnitProject.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 = 1;
                    newWbsSet1.Remark = this.txtRemark.Text.Trim();
                    newWbsSet1.IsIn = false;
                    newWbsSet1.IsSelected = true;
                    newWbsSet1.IsApprove = true;
                    BLL.WbsSetService.AddWbsSet(newWbsSet1);
                    foreach (var noShowWbsSet1 in noShowWbsSet1s)
                    {
                        Model.Wbs_WbsSet newWbsSet2 = new Model.Wbs_WbsSet();
                        newWbsSet2.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                        newWbsSet2.WbsSetCode = GetReplaceCode(noShowWbsSet1.WbsSetCode, childUnitProject.UnitProjectCode);
                        newWbsSet2.WbsSetName = noShowWbsSet1.WbsSetName;
                        newWbsSet2.CnProfessionId = noShowWbsSet1.CnProfessionId;
                        newWbsSet2.UnitProjectId = childUnitProject.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 = 2;
                        newWbsSet2.Remark = noShowWbsSet1.Remark;
                        newWbsSet2.IsIn = false;
                        newWbsSet2.IsSelected = true;
                        newWbsSet2.IsApprove = true;
                        BLL.WbsSetService.AddWbsSet(newWbsSet2);
                    }
                }
            }
            else if (type == "wbsSet")
            {
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                wbsSet.IsSelected = true;
                wbsSet.IsApprove = true;
                BLL.WbsSetService.UpdateWbsSet(wbsSet);
                var noShowWbsSets = from x in Funs.DB.Wbs_WbsSet where x.SuperWbsSetId == id && x.NoShow == true orderby x.WbsSetCode select x;
                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 循环拷贝分部子级
        /// <summary>
        /// 循环拷贝分部子级
        /// </summary>
        /// <param name="newSuperWbsSetId"></param>
        /// <param name="oldSuperWbsSetId"></param>
        /// <param name="code"></param>
        /// <param name="unitProjectId"></param>
        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
    }
}