using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;

namespace FineUIPro.Web.JDGL.WBS
{
    public partial class WBSSetCopy2 : 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 == "unitProject" || 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;
                        if (BLL.WbsSetInitService.GetWbsSetInitsBySuperWbsSetCode(id).Count == 0)  //末级节点
                        {
                            this.trWbsSet2.Hidden = false;
                        }
                    }
                }
            }
        }
        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"];
            string handleType = Request.Params["HandleType"];
            if (type == "unitProject")
            {
                Model.Wbs_UnitProject unitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                if (unitProject != null)
                {
                    //拷贝单位工程
                    Model.Wbs_UnitProject newUnitProject = new Wbs_UnitProject();
                    newUnitProject.UnitProjectId = SQLHelper.GetNewID(typeof(Model.Wbs_UnitProject));
                    updateId = newUnitProject.UnitProjectId;
                    newUnitProject.UnitProjectCode = txtCode.Text.Trim();
                    newUnitProject.UnitProjectName = txtName.Text.Trim();
                    newUnitProject.ProjectId = unitProject.ProjectId;
                    newUnitProject.CnProfessionId = unitProject.CnProfessionId;
                    newUnitProject.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    newUnitProject.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        newUnitProject.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    newUnitProject.Remark = txtRemark.Text.Trim();
                    newUnitProject.SuperUnitProjectId = unitProject.SuperUnitProjectId;
                    newUnitProject.IsIn = false;
                    if (string.IsNullOrEmpty(handleType))
                    {
                        newUnitProject.IsSelected = true;
                    }
                    else
                    {
                        newUnitProject.IsApprove = true;
                    }
                    BLL.UnitProjectService.AddUnitProject(newUnitProject);
                    //拷贝子单位工程
                    List<Model.Wbs_UnitProject> childUnitProjects = (from x in Funs.DB.Wbs_UnitProject where x.SuperUnitProjectId == unitProject.UnitProjectId orderby x.UnitProjectCode select x).ToList();
                    if (childUnitProjects.Count > 0)   //存在子单位工程
                    {
                        foreach (Wbs_UnitProject childUnitProject in childUnitProjects)
                        {
                            Model.Wbs_UnitProject newChildUnitProject = new Wbs_UnitProject();
                            newChildUnitProject.UnitProjectId = SQLHelper.GetNewID(typeof(Model.Wbs_UnitProject));
                            newChildUnitProject.UnitProjectCode = GetReplaceCode(childUnitProject.UnitProjectCode, unitProject.UnitProjectCode);
                            newChildUnitProject.UnitProjectName = childUnitProject.UnitProjectName;
                            newChildUnitProject.ProjectId = childUnitProject.ProjectId;
                            newChildUnitProject.CnProfessionId = childUnitProject.CnProfessionId;
                            newChildUnitProject.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                            newChildUnitProject.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                            newChildUnitProject.SortIndex = childUnitProject.SortIndex;
                            newChildUnitProject.Remark = txtRemark.Text.Trim();
                            newChildUnitProject.SuperUnitProjectId = newUnitProject.UnitProjectId;
                            newChildUnitProject.IsIn = false;
                            if (string.IsNullOrEmpty(handleType))
                            {
                                newChildUnitProject.IsSelected = true;
                            }
                            else
                            {
                                newChildUnitProject.IsApprove = true;
                            }
                            BLL.UnitProjectService.AddUnitProject(newChildUnitProject);
                            //拷贝分部工程
                            var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == childUnitProject.UnitProjectId orderby x.WbsSetCode select x;
                            foreach (var wbsSet1 in wbsSet1s)
                            {
                                Model.Wbs_WbsSet newWbsSet1 = new Wbs_WbsSet();
                                newWbsSet1.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                                //newWbsSet1.WbsSetCode = wbsSet1.WbsSetCode.Replace(unitProject.UnitProjectCode, this.txtCode.Text.Trim());
                                newWbsSet1.WbsSetCode = GetReplaceCode(wbsSet1.WbsSetCode, unitProject.UnitProjectCode);
                                newWbsSet1.WbsSetName = wbsSet1.WbsSetName;
                                newWbsSet1.CnProfessionId = wbsSet1.CnProfessionId;
                                newWbsSet1.UnitProjectId = newChildUnitProject.UnitProjectId;
                                newWbsSet1.SuperWbsSetId = null;
                                newWbsSet1.ProjectId = wbsSet1.ProjectId;
                                newWbsSet1.StartDate = wbsSet1.StartDate;
                                newWbsSet1.EndDate = wbsSet1.EndDate;
                                newWbsSet1.Flag = wbsSet1.Flag;
                                newWbsSet1.Way = wbsSet1.Way;
                                newWbsSet1.Remark = wbsSet1.Remark;
                                newWbsSet1.IsIn = false;
                                newWbsSet1.SortIndex = wbsSet1.SortIndex;
                                if (string.IsNullOrEmpty(handleType))
                                {
                                    newWbsSet1.IsSelected = true;
                                }
                                else
                                {
                                    newWbsSet1.IsApprove = true;
                                }
                                BLL.WbsSetService.AddWbsSet(newWbsSet1);
                                this.AddWbsSets(newWbsSet1.WbsSetId, wbsSet1.WbsSetId, unitProject.UnitProjectCode, newChildUnitProject.UnitProjectId);
                            }
                        }
                    }
                    else     //不存在子单位工程
                    {
                        //拷贝分部工程
                        var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == unitProject.UnitProjectId orderby x.WbsSetCode select x;
                        if (wbsSet1s.Count() > 0)
                        {
                            foreach (var wbsSet1 in wbsSet1s)
                            {
                                Model.Wbs_WbsSet newWbsSet1 = new Wbs_WbsSet();
                                newWbsSet1.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                                //newWbsSet1.WbsSetCode = wbsSet1.WbsSetCode.Replace(unitProject.UnitProjectCode, this.txtCode.Text.Trim());
                                newWbsSet1.WbsSetCode = GetReplaceCode(wbsSet1.WbsSetCode, unitProject.UnitProjectCode);
                                newWbsSet1.WbsSetName = wbsSet1.WbsSetName;
                                newWbsSet1.CnProfessionId = wbsSet1.CnProfessionId;
                                newWbsSet1.UnitProjectId = newUnitProject.UnitProjectId;
                                newWbsSet1.SuperWbsSetId = null;
                                newWbsSet1.ProjectId = wbsSet1.ProjectId;
                                newWbsSet1.StartDate = wbsSet1.StartDate;
                                newWbsSet1.EndDate = wbsSet1.EndDate;
                                newWbsSet1.SortIndex = wbsSet1.SortIndex;
                                newWbsSet1.Flag = wbsSet1.Flag;
                                newWbsSet1.Way = wbsSet1.Way;
                                newWbsSet1.Remark = wbsSet1.Remark;
                                newWbsSet1.IsIn = false;
                                if (string.IsNullOrEmpty(handleType))
                                {
                                    newWbsSet1.IsSelected = true;
                                }
                                else
                                {
                                    newWbsSet1.IsApprove = true;
                                }
                                BLL.WbsSetService.AddWbsSet(newWbsSet1);
                                this.AddWbsSets(newWbsSet1.WbsSetId, wbsSet1.WbsSetId, unitProject.UnitProjectCode, unitProject.UnitProjectId);
                            }
                        }
                        else    //单位工程下直接是分项内容,如质量行为
                        {
                            var wbsSet3s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 3 && x.UnitProjectId == unitProject.UnitProjectId orderby x.WbsSetCode select x;
                            if (wbsSet3s.Count() > 0)
                            {
                                foreach (var wbsSet3 in wbsSet3s)
                                {
                                    Model.Wbs_WbsSet newWbsSet3 = new Wbs_WbsSet();
                                    newWbsSet3.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                                    //newWbsSet1.WbsSetCode = wbsSet1.WbsSetCode.Replace(unitProject.UnitProjectCode, this.txtCode.Text.Trim());
                                    newWbsSet3.WbsSetCode = GetReplaceCode(wbsSet3.WbsSetCode, unitProject.UnitProjectCode);
                                    newWbsSet3.WbsSetName = wbsSet3.WbsSetName;
                                    newWbsSet3.CnProfessionId = wbsSet3.CnProfessionId;
                                    newWbsSet3.UnitProjectId = newUnitProject.UnitProjectId;
                                    newWbsSet3.SuperWbsSetId = null;
                                    newWbsSet3.ProjectId = wbsSet3.ProjectId;
                                    newWbsSet3.StartDate = wbsSet3.StartDate;
                                    newWbsSet3.EndDate = wbsSet3.EndDate;
                                    newWbsSet3.SortIndex = wbsSet3.SortIndex;
                                    newWbsSet3.Flag = wbsSet3.Flag;
                                    newWbsSet3.Way = wbsSet3.Way;
                                    newWbsSet3.Remark = wbsSet3.Remark;
                                    newWbsSet3.IsIn = false;
                                    if (string.IsNullOrEmpty(handleType))
                                    {
                                        newWbsSet3.IsSelected = true;
                                    }
                                    else
                                    {
                                        newWbsSet3.IsApprove = true;
                                    }
                                    BLL.WbsSetService.AddWbsSet(newWbsSet3);
                                    this.AddWbsSets(newWbsSet3.WbsSetId, wbsSet3.WbsSetId, unitProject.UnitProjectCode, unitProject.UnitProjectId);
                                }
                            }
                        }
                    }
                }
            }
            else if (type == "childUnitProject")
            {
                Model.Wbs_UnitProject childUnitProject = BLL.UnitProjectService.GetUnitProjectByUnitProjectId(id);
                if (childUnitProject != null)
                {
                    //拷贝子单位工程
                    Model.Wbs_UnitProject newChildUnitProject = new Wbs_UnitProject();
                    newChildUnitProject.UnitProjectId = SQLHelper.GetNewID(typeof(Model.Wbs_UnitProject));
                    updateId = newChildUnitProject.UnitProjectId;
                    newChildUnitProject.UnitProjectCode = txtCode.Text.Trim();
                    newChildUnitProject.UnitProjectName = txtName.Text.Trim();
                    newChildUnitProject.ProjectId = childUnitProject.ProjectId;
                    newChildUnitProject.CnProfessionId = childUnitProject.CnProfessionId;
                    newChildUnitProject.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    newChildUnitProject.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        newChildUnitProject.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    newChildUnitProject.Remark = txtRemark.Text.Trim();
                    newChildUnitProject.SuperUnitProjectId = childUnitProject.SuperUnitProjectId;
                    newChildUnitProject.IsIn = false;
                    if (string.IsNullOrEmpty(handleType))
                    {
                        newChildUnitProject.IsSelected = true;
                    }
                    else
                    {
                        newChildUnitProject.IsApprove = true;
                    }
                    BLL.UnitProjectService.AddUnitProject(newChildUnitProject);
                    //拷贝分部工程
                    var wbsSet1s = from x in Funs.DB.Wbs_WbsSet where x.Flag == 1 && x.UnitProjectId == childUnitProject.UnitProjectId orderby x.WbsSetCode select x;
                    foreach (var wbsSet1 in wbsSet1s)
                    {
                        Model.Wbs_WbsSet newWbsSet1 = new Wbs_WbsSet();
                        newWbsSet1.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                        //newWbsSet1.WbsSetCode = wbsSet1.WbsSetCode.Replace(childUnitProject.UnitProjectCode, this.txtCode.Text.Trim());
                        newWbsSet1.WbsSetCode = GetReplaceCode(wbsSet1.WbsSetCode, childUnitProject.UnitProjectCode);
                        newWbsSet1.WbsSetName = wbsSet1.WbsSetName;
                        newWbsSet1.CnProfessionId = wbsSet1.CnProfessionId;
                        newWbsSet1.UnitProjectId = newChildUnitProject.UnitProjectId;
                        newWbsSet1.SuperWbsSetId = null;
                        newWbsSet1.ProjectId = wbsSet1.ProjectId;
                        newWbsSet1.StartDate = wbsSet1.StartDate;
                        newWbsSet1.EndDate = wbsSet1.EndDate;
                        newWbsSet1.SortIndex = wbsSet1.SortIndex;
                        newWbsSet1.Flag = wbsSet1.Flag;
                        newWbsSet1.Way = wbsSet1.Way;
                        newWbsSet1.Remark = wbsSet1.Remark;
                        newWbsSet1.IsIn = false;
                        if (string.IsNullOrEmpty(handleType))
                        {
                            newWbsSet1.IsSelected = true;
                        }
                        else
                        {
                            newWbsSet1.IsApprove = true;
                        }
                        BLL.WbsSetService.AddWbsSet(newWbsSet1);
                        this.AddWbsSets(newWbsSet1.WbsSetId, wbsSet1.WbsSetId, childUnitProject.UnitProjectCode, newChildUnitProject.UnitProjectId);
                    }
                }
            }
            else if (type == "wbsSet")
            {
                Model.Wbs_WbsSet wbsSet = BLL.WbsSetService.GetWbsSetByWbsSetId(id);
                if (wbsSet != null)
                {
                    Model.Wbs_WbsSet newWbsSet = new Wbs_WbsSet();
                    newWbsSet.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                    updateId = newWbsSet.WbsSetId;
                    newWbsSet.WbsSetCode = this.txtCode.Text.Trim();
                    newWbsSet.WbsSetName = txtName.Text.Trim();
                    newWbsSet.CnProfessionId = wbsSet.CnProfessionId;
                    newWbsSet.UnitProjectId = wbsSet.UnitProjectId;
                    newWbsSet.IsChild = true;
                    newWbsSet.SuperWbsSetId = wbsSet.WbsSetId;
                    newWbsSet.ProjectId = wbsSet.ProjectId;
                    newWbsSet.StartDate = Convert.ToDateTime(txtStartDate.Text.Trim());
                    newWbsSet.EndDate = Convert.ToDateTime(txtEndDate.Text.Trim());
                    if (!string.IsNullOrEmpty(txtSortIndex.Text.Trim()))
                    {
                        newWbsSet.SortIndex = Convert.ToInt32(txtSortIndex.Text.Trim());
                    }
                    newWbsSet.Flag = wbsSet.Flag + 1;
                    newWbsSet.Way = wbsSet.Way;
                    newWbsSet.Remark = txtRemark.Text.Trim();
                    newWbsSet.IsIn = false;
                    newWbsSet.IsApprove = true;
                    BLL.WbsSetService.AddWbsSet(newWbsSet);
                    this.AddWbsSets(newWbsSet.WbsSetId, wbsSet.WbsSetId, wbsSet.WbsSetCode, wbsSet.UnitProjectId);
                }
            }
            //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.GetIsChildWbsSetsBySuperWbsSetId(oldSuperWbsSetId);
            foreach (var wbsSet in childWbsSets)
            {
                Model.Wbs_WbsSet newWbsSet = new Wbs_WbsSet();
                newWbsSet.WbsSetId = SQLHelper.GetNewID(typeof(Model.Wbs_WbsSet));
                //newWbsSet.WbsSetCode = wbsSet.WbsSetCode.Replace(code, this.txtCode.Text.Trim());
                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;
                newWbsSet.IsApprove = true;
                BLL.WbsSetService.AddWbsSet(newWbsSet);
                this.AddWbsSets(newWbsSet.WbsSetId, wbsSet.WbsSetId, code, unitProjectId);
            }
        }
        #endregion

        #region 获取编号
        /// <summary>
        /// 获取编号
        /// </summary>
        /// <param name="oldStr"></param>
        /// <param name="replaceCode"></param>
        /// <returns></returns>
        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
    }
}