using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.CQMS.ProcessControl
{
    public partial class InspectionNoticeEdit : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //测试阶段,仅hfnbd账号使用
                if (this.CurrUser.UserId == BLL.Const.hfnbdId)
                {
                    this.drpUnitWorkId.Hidden = false;
                    this.fr1.Hidden = false;
                    this.fr2.Hidden = false;
                }
                GetButtonPower();
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(drpUnit, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, true);//施工分包商
               
                UserService.InitUserProjectIdUnitTypeDropDownList(this.drpAcceptanceCheckMan, this.CurrUser.LoginProjectId, string.Empty, false);
                //UserService.InitUserNameProjectIdUnitTypeDropDownList(this.drpAcceptanceCheckMan, this.CurrUser.LoginProjectId, string.Empty, false);
                if (this.CurrUser.UserId == BLL.Const.hfnbdId)//测试阶段,仅hfnbd账号使用
                {
                    //专业
                    this.drpCNProfessionalId.DataTextField = "DetectionItems";
                    this.drpCNProfessionalId.DataValueField = "ControlId";
                    this.drpCNProfessionalId.DataSource = BLL.CNProfessionalService.GetCnProList();
                    this.drpCNProfessionalId.DataBind();
                    Funs.FineUIPleaseSelect(this.drpCNProfessionalId);
                }
                else
                {
                    BLL.CNProfessionalService.InitCNProfessionalDownList(this.drpCNProfessionalId, true);//专业
                }
                //单位工程
                var q = (from x in Funs.DB.WBS_DivisionDivide
                         join y in Funs.DB.WBS_UnitWork on x.UnitWorkId equals y.UnitWorkId
                         where x.ProjectId == this.CurrUser.LoginProjectId
                         && (y.SuperUnitWork == null || y.SuperUnitWork == "0")
                         select new
                         {
                             y.UnitWorkId,
                             UnitWorkName = y.UnitWorkCode + "-" + y.UnitWorkName
                         }).Distinct().ToList();
                this.drpUnitWorkId.DataTextField = "UnitWorkName";
                this.drpUnitWorkId.DataValueField = "UnitWorkId";
                this.drpUnitWorkId.DataSource = q;
                this.drpUnitWorkId.DataBind();
                Funs.FineUIPleaseSelect(this.drpUnitWorkId);//单位工程
                Funs.FineUIPleaseSelect(this.drpChirldUnitWorkId);//子单位工程
                Funs.FineUIPleaseSelect(this.drpBranch);//分部工程
                Funs.FineUIPleaseSelect(this.drpChildBranch);//子分部工程
                Funs.FineUIPleaseSelect(this.drpProEngineering);//分项工程
                this.hdInspectionNoticeId.Text = Request.Params["inspectionId"];
                if (!string.IsNullOrEmpty(Request.Params["View"]))
                {
                    this.btnSave.Hidden = true;
                }
                if (!string.IsNullOrEmpty(this.hdInspectionNoticeId.Text))
                {
                    Model.ProcessControl_InspectionManagement inspectionManagement = BLL.InspectionManagementService.GetInspectionManagementById(this.hdInspectionNoticeId.Text.Trim());
                    if (inspectionManagement != null)
                    {
                        if (!string.IsNullOrEmpty(inspectionManagement.UnitId))
                        {
                            this.drpUnit.SelectedValue = inspectionManagement.UnitId;
                        }
                        if (!string.IsNullOrEmpty(inspectionManagement.CNProfessionalId))
                        {
                            this.drpCNProfessionalId.SelectedValue = inspectionManagement.CNProfessionalId;
                        }
                        this.txtAcceptanceSite.Text = inspectionManagement.AcceptanceSite;
                        if (!string.IsNullOrEmpty(inspectionManagement.AcceptanceCheckMan))
                        {
                            this.drpAcceptanceCheckMan.SelectedValueArray = inspectionManagement.AcceptanceCheckMan.Split(',');
                        }
                        //单位工程、分部工程、分项工程
                        if (!string.IsNullOrEmpty(inspectionManagement.UnitWorkId))
                        {
                            this.drpUnitWorkId.SelectedValue = inspectionManagement.UnitWorkId;
                            if (!string.IsNullOrEmpty(inspectionManagement.ChildUnitWorkId))
                            {
                                //子单位工程
                                var childUnitWorks = (from x in Funs.DB.WBS_DivisionDivide
                                                      join y in Funs.DB.WBS_UnitWork on x.UnitWorkId equals y.UnitWorkId
                                                      where x.ProjectId == this.CurrUser.LoginProjectId
                                                      && y.SuperUnitWork == inspectionManagement.UnitWorkId
                                                      && (y.SuperUnitWork != "0")
                                                      select new
                                                      {
                                                          y.UnitWorkId,
                                                          UnitWorkName = y.UnitWorkCode + "-" + y.UnitWorkName
                                                      }).Distinct().ToList();
                                this.drpChirldUnitWorkId.DataTextField = "UnitWorkName";
                                this.drpChirldUnitWorkId.DataValueField = "UnitWorkId";
                                this.drpChirldUnitWorkId.DataSource = childUnitWorks;
                                this.drpChirldUnitWorkId.DataBind();
                                Funs.FineUIPleaseSelect(this.drpChirldUnitWorkId);
                                this.drpChirldUnitWorkId.SelectedIndex = 0;
                                this.drpChirldUnitWorkId.SelectedValue = inspectionManagement.ChildUnitWorkId;
                            }
                            if (!string.IsNullOrEmpty(inspectionManagement.Branch))
                            {
                                if (string.IsNullOrEmpty(inspectionManagement.ChildUnitWorkId))//无子单位工程
                                {
                                    //分部工程
                                    var a = (from x in Funs.DB.WBS_DivisionDivide
                                             join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                             where x.ProjectId == this.CurrUser.LoginProjectId
                                             && x.UnitWorkId == inspectionManagement.UnitWorkId
                                             && y.ParentId == null
                                             select new
                                             {
                                                 y.DivisionId,
                                                 y.BranchEngineeringName
                                             }).Distinct().ToList();
                                    this.drpBranch.DataTextField = "BranchEngineeringName";
                                    this.drpBranch.DataValueField = "DivisionId";
                                    this.drpBranch.DataSource = a;
                                    this.drpBranch.DataBind();
                                    Funs.FineUIPleaseSelect(this.drpBranch);
                                    this.drpBranch.SelectedIndex = 0;
                                    this.drpBranch.SelectedValue = inspectionManagement.Branch;
                                }
                                else
                                {
                                    //分部工程(有子单位工程)
                                    var a = (from x in Funs.DB.WBS_DivisionDivide
                                             join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                             where x.ProjectId == this.CurrUser.LoginProjectId
                                             && x.UnitWorkId == inspectionManagement.ChildUnitWorkId
                                             && y.ParentId == null
                                             select new
                                             {
                                                 y.DivisionId,
                                                 y.BranchEngineeringName
                                             }).Distinct().ToList();
                                    this.drpBranch.DataTextField = "BranchEngineeringName";
                                    this.drpBranch.DataValueField = "DivisionId";
                                    this.drpBranch.DataSource = a;
                                    this.drpBranch.DataBind();
                                    Funs.FineUIPleaseSelect(this.drpBranch);
                                    this.drpBranch.SelectedIndex = 0;
                                    this.drpBranch.SelectedValue = inspectionManagement.Branch;
                                }
                                if (!string.IsNullOrEmpty(inspectionManagement.ChildBranch))
                                {
                                    #region 子分部工程
                                    if (!string.IsNullOrEmpty(inspectionManagement.ChildUnitWorkId))
                                    {
                                        //子分部工程
                                        var childBranchs = (from x in Funs.DB.WBS_DivisionDivide
                                                            join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                                            where x.ProjectId == this.CurrUser.LoginProjectId
                                                            && y.ParentId == inspectionManagement.Branch
                                                            && x.UnitWorkId == inspectionManagement.ChildUnitWorkId
                                                            && y.SubBranchEngineeringName != null
                                                            select new
                                                            {
                                                                y.DivisionId,
                                                                y.SubBranchEngineeringName
                                                            }).Distinct().ToList();
                                        this.drpChildBranch.DataTextField = "SubBranchEngineeringName";
                                        this.drpChildBranch.DataValueField = "DivisionId";
                                        this.drpChildBranch.DataSource = childBranchs;
                                        this.drpChildBranch.DataBind();
                                        Funs.FineUIPleaseSelect(this.drpChildBranch);
                                        this.drpChildBranch.SelectedIndex = 0;
                                    }
                                    else
                                    {
                                        var childBranchs = (from x in Funs.DB.WBS_DivisionDivide
                                                            join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                                            where x.ProjectId == this.CurrUser.LoginProjectId
                                                            && y.ParentId == inspectionManagement.Branch
                                                            && x.UnitWorkId == inspectionManagement.UnitWorkId
                                                            && y.SubBranchEngineeringName != null
                                                            select new
                                                            {
                                                                y.DivisionId,
                                                                y.SubBranchEngineeringName
                                                            }).Distinct().ToList();
                                        this.drpChildBranch.DataTextField = "SubBranchEngineeringName";
                                        this.drpChildBranch.DataValueField = "DivisionId";
                                        this.drpChildBranch.DataSource = childBranchs;
                                        this.drpChildBranch.DataBind();
                                        Funs.FineUIPleaseSelect(this.drpChildBranch);
                                        this.drpChildBranch.SelectedIndex = 0;
                                    }
                                    this.drpChildBranch.SelectedValue = inspectionManagement.ChildBranch;
                                    #endregion
                                }
                                if (!string.IsNullOrEmpty(inspectionManagement.ControlPointType))
                                {
                                    //分项工程
                                    var b = (from x in Funs.DB.WBS_DivisionDivide
                                             join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                             where x.ProjectId == this.CurrUser.LoginProjectId
                                             && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                                             && y.ParentId == this.drpBranch.SelectedValue
                                             && y.ParentId != null
                                             select new
                                             {
                                                 y.DivisionId,
                                                 y.ProEngineeringName,
                                                 y.SubBranchEngineeringName
                                             }).Distinct().ToList();
                                    if (b.Where(w => w.SubBranchEngineeringName == null).Count() > 0)
                                    {
                                        this.drpProEngineering.DataTextField = "ProEngineeringName";
                                        this.drpProEngineering.DataValueField = "DivisionId";
                                        this.drpProEngineering.DataSource = b;
                                    }
                                    else
                                    {
                                        var c = (from x in Funs.DB.WBS_DivisionDivide
                                                 join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                                 join z in Funs.DB.Division_SubProjects on y.ParentId equals z.DivisionId
                                                 join w in Funs.DB.Division_SubProjects on z.ParentId equals w.DivisionId
                                                 where x.ProjectId == this.CurrUser.LoginProjectId
                                                 && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                                                 && z.ParentId == this.drpBranch.SelectedValue
                                                 select new
                                                 {
                                                     y.DivisionId,
                                                     y.ProEngineeringName
                                                 }).Distinct().ToList();
                                        this.drpProEngineering.DataTextField = "ProEngineeringName";
                                        this.drpProEngineering.DataValueField = "DivisionId";
                                        this.drpProEngineering.DataSource = c;
                                    }
                                    this.drpProEngineering.DataBind();
                                    Funs.FineUIPleaseSelect(this.drpProEngineering);
                                    this.drpProEngineering.SelectedIndex = 0;
                                    
                                    this.drpProEngineering.SelectedValue = inspectionManagement.ControlPointType;
                                }
                            }
                        }
                        this.txtNoticeCode.Text = inspectionManagement.NoticeCode;
                        this.Grid1.DataSource = BLL.InspectionManagementDetailService.GetInspectionDetails(this.hdInspectionNoticeId.Text.Trim());
                        this.Grid1.DataBind();
                    }
                }
            }
            else
            {
                string requestArg = GetRequestEventArgument(); // 此函数所在文件:PageBase.cs
                if (requestArg.StartsWith("ReloadGrid$"))
                {
                    this.hdItemsString.Text = requestArg.Substring("ReloadGrid$".Length);
                    Window1_Close(null, null);
                }
            }
        }
        #endregion
        #region 保存
        /// 
        /// 保存按钮
        /// 
        /// 
        /// 
        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.drpUnit.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请先选择施工分包商!", MessageBoxIcon.Warning);
                return;
            }
            if (this.drpCNProfessionalId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请先选择专业!", MessageBoxIcon.Warning);
                return;
            }
            if (this.Grid1.Rows.Count == 0)
            {
                Alert.ShowInTop("请选择共检内容!", MessageBoxIcon.Warning);
                return;
            }
            Model.ProcessControl_InspectionManagement inspectionManagement = new Model.ProcessControl_InspectionManagement();
            inspectionManagement.ProjectId = this.CurrUser.LoginProjectId;
            if (this.drpUnit.SelectedValue != BLL.Const._Null)
            {
                inspectionManagement.UnitId = this.drpUnit.SelectedValue;
            }
            if (this.drpCNProfessionalId.SelectedValue != BLL.Const._Null)
            {
                inspectionManagement.CNProfessionalId = this.drpCNProfessionalId.SelectedValue;
            }
            inspectionManagement.NoticeCode = this.txtNoticeCode.Text.Trim();
            inspectionManagement.AcceptanceSite = this.txtAcceptanceSite.Text.Trim();
            //检查人
            string CheckManIds = string.Empty;
            foreach (var item in this.drpAcceptanceCheckMan.SelectedValueArray)
            {
                CheckManIds += item + ",";
            }
            if (!string.IsNullOrEmpty(CheckManIds))
            {
                CheckManIds = CheckManIds.Substring(0, CheckManIds.LastIndexOf(","));
            }
            inspectionManagement.AcceptanceCheckMan = CheckManIds;
            if (this.drpUnitWorkId.SelectedValue != BLL.Const._Null)
            {
                inspectionManagement.UnitWorkId = this.drpUnitWorkId.SelectedValue;
            }
            if (this.drpChirldUnitWorkId.SelectedValue!=BLL.Const._Null)
            {
                inspectionManagement.ChildUnitWorkId = this.drpChirldUnitWorkId.SelectedValue;
            }
            if (this.drpBranch.SelectedValue != BLL.Const._Null)
            {
                inspectionManagement.Branch = this.drpBranch.SelectedValue;
            }
            if (this.drpChildBranch.SelectedValue!=BLL.Const._Null)
            {
                inspectionManagement.ChildBranch = this.drpChildBranch.SelectedValue;
            }
            if (this.drpProEngineering.SelectedValue != BLL.Const._Null)
            {
                inspectionManagement.ControlPointType = this.drpProEngineering.SelectedValue;
            }
            //inspectionManagement.AcceptanceCheckMan = this.txtAcceptanceCheckMan.Text.Trim();
            if (string.IsNullOrEmpty(Request.Params["inspectionId"]))
            {
                //编号不能重复
                var InspectionManagementModel = Funs.DB.ProcessControl_InspectionManagement.FirstOrDefault(x => x.NoticeCode == this.txtNoticeCode.Text.Trim());
                if (InspectionManagementModel != null)
                {
                    ShowNotify("共检通知单编号已存在,不能重复!", MessageBoxIcon.Warning);
                    return;
                }
                inspectionManagement.CompileMan = this.CurrUser.UserId;
                inspectionManagement.CompileDate = DateTime.Now;
                if (!string.IsNullOrEmpty(this.hdInspectionNoticeId.Text.Trim()))
                {
                    inspectionManagement.InspectionId = this.hdInspectionNoticeId.Text.Trim();
                }
                else
                {
                    inspectionManagement.InspectionId = SQLHelper.GetNewID(typeof(Model.ProcessControl_InspectionManagement));
                    this.hdInspectionNoticeId.Text = inspectionManagement.InspectionId;
                }
                /*var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == inspectionManagement.InspectionId);
                if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }*/
                BLL.InspectionManagementService.AddInspectionManagement(inspectionManagement);
            }
            else
            {
                //编号不能重复
                var InspectionManagementModel = Funs.DB.ProcessControl_InspectionManagement.FirstOrDefault(x => x.NoticeCode == this.txtNoticeCode.Text.Trim()
                && x.InspectionId != Request.Params["inspectionId"]);
                if (InspectionManagementModel != null)
                {
                    ShowNotify("共检通知单编号已存在,不能重复!", MessageBoxIcon.Warning);
                    return;
                }
                Model.ProcessControl_InspectionManagement oldInspectionManagement = BLL.InspectionManagementService.GetInspectionManagementById(this.hdInspectionNoticeId.Text.Trim());
                inspectionManagement.AttachUrl2 = oldInspectionManagement.AttachUrl2;
                inspectionManagement.InspectionId = this.hdInspectionNoticeId.Text.Trim();
                var sour = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == inspectionManagement.InspectionId);
                /*if (sour == null || string.IsNullOrEmpty(sour.AttachUrl))
                {
                    Alert.ShowInTop("请上传附件!", MessageBoxIcon.Warning);
                    return;
                }*/
                BLL.InspectionManagementService.UpdateInspectionManagement(inspectionManagement);
                BLL.InspectionManagementDetailService.DeleteAllInspectionDetail(inspectionManagement.InspectionId);
            }
            var details = jerqueSaveList();
            foreach (var detail in details)
            {
                detail.InspectionId = inspectionManagement.InspectionId;
                BLL.InspectionManagementDetailService.AddInspectionDetail(detail);
            }
            ShowNotify("保存成功!", MessageBoxIcon.Success);
            PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
        }
        #endregion
        #region 附件上传
        /// 
        /// 附件上传
        /// 
        /// 
        /// 
        protected void btnAttach_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.hdInspectionNoticeId.Text))   //新增记录
            {
                this.hdInspectionNoticeId.Text = SQLHelper.GetNewID(typeof(Model.ProcessControl_InspectionManagement));
            }
            PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format("../../AttachFile/webuploader.aspx?type=0&toKeyId={0}&path=FileUpload/CQMS/InspectionManagement&menuId={1}", this.hdInspectionNoticeId.Text, BLL.Const.InspectionNoticeMenuId)));
        }
        #endregion
        #region Grid1行内事件
        /// 
        /// Grid1行内事件
        /// 
        /// 
        /// 
        protected void Grid1_RowCommand(object sender, GridCommandEventArgs e)
        {
            string itemId = Grid1.DataKeys[e.RowIndex][0].ToString();
            List details = jerqueSaveList();
            if (e.CommandName == "delete")
            {
                foreach (Model.ProcessControl_InspectionManagementDetail detail in details)
                {
                    if (detail.InspectionDetailId == itemId)
                    {
                        details.Remove(detail);
                        break;
                    }
                }
                Grid1.DataSource = details;
                Grid1.DataBind();
            }
        }
        #endregion
        #region 搜索
        /// 
        /// 搜索
        /// 
        /// 
        /// 
        protected void btnNew_Click(object sender, EventArgs e)
        {
            if (this.drpCNProfessionalId.SelectedValue == BLL.Const._Null)
            {
                Alert.ShowInTop("请先选择专业!", MessageBoxIcon.Warning);
                return;
            }
            string window = String.Format("ShowUnitWork.aspx?CNPrefessionalId={0}", this.drpCNProfessionalId.SelectedValue, "查找 - ");
            PageContext.RegisterStartupScript(Window1.GetSaveStateReference(hdItemsString.ClientID) + Window1.GetShowReference(window));
        }
        #endregion
        #region  保存Grid集合
        /// 
        /// 检查并保存集合
        /// 
        private List jerqueSaveList()
        {
            List details = new List();
            foreach (JObject mergedRow in Grid1.GetMergedData())
            {
                JObject values = mergedRow.Value("values");
                int i = mergedRow.Value("index");
                Model.ProcessControl_InspectionManagementDetail detail = new Model.ProcessControl_InspectionManagementDetail();
                detail.InspectionDetailId = this.Grid1.Rows[i].RowID;
                detail.UnitWorkId = values.Value("UnitWorkId");
                detail.Branch = values.Value("Branch");
                detail.ControlPointType = this.Grid1.Rows[i].DataKeys[1].ToString();
                string createDate = values.Value("CreateDate");
                if (!string.IsNullOrEmpty(createDate))
                {
                    detail.CreateDate = Convert.ToDateTime(createDate);
                }
                details.Add(detail);
            }
            return details;
        }
        #endregion
        #region 关闭弹出窗口
        /// 
        /// 关闭弹出窗口
        /// 
        /// 
        /// 
        protected void Window1_Close(object sender, WindowCloseEventArgs e)
        {
            if (!string.IsNullOrEmpty(hdItemsString.Text))
            {
                string[] ids = hdItemsString.Text.Split(',');
                int i = 1;
                var details = jerqueSaveList();
                foreach (var id in ids)
                {
                    var oldDetail = details.FirstOrDefault(x => x.ControlPointType == id);
                    if (oldDetail == null)   //添加集合没有的新纪录
                    {
                        Model.ProcessControl_InspectionManagementDetail detail = new Model.ProcessControl_InspectionManagementDetail();
                        detail.InspectionDetailId = SQLHelper.GetNewID();
                        var breakdownProject = BLL.BreakdownProjectService.GetBreakdownProjectById(id);
                        if (breakdownProject != null)
                        {
                            var divisionProject = BLL.DivisionProjectService.GetDivisionProjectById(breakdownProject.DivisionProjectId);
                            if (divisionProject != null)
                            {
                                detail.UnitWorkId = divisionProject.UnitWorkId;
                                detail.Branch = divisionProject.DivisionProjectId;
                            }
                        }
                        detail.ControlPointType = id;
                        detail.CreateDate = DateTime.Now.AddMinutes(i);
                        details.Add(detail);
                    }
                    i++;
                }
                this.Grid1.DataSource = details;
                this.Grid1.DataBind();
            }
        }
        #endregion
        #region 获取按钮权限
        /// 
        /// 获取按钮权限
        /// 
        /// 
        /// 
        private void GetButtonPower()
        {
            if (Request.Params["value"] == BLL.Const._Null)
            {
                return;
            }
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.InspectionNoticeMenuId);
            if (buttonList.Count > 0)
            {
                if (buttonList.Contains(BLL.Const.BtnSave))
                {
                    this.btnSave.Hidden = false;
                }
            }
        }
        #endregion
        #region 转换字符串
        /// 
        /// 获取单位工程
        /// 
        /// 
        /// 
        protected string ConvertUnitWork(object UnitWorkId)
        {
            string name = string.Empty;
            if (UnitWorkId != null)
            {
                Model.WBS_UnitWork unitWork = BLL.UnitWorkService.GetUnitWorkByUnitWorkId(UnitWorkId.ToString());
                if (unitWork != null)
                {
                    name = unitWork.UnitWorkName;
                }
            }
            return name;
        }
        /// 
        /// 获取分部
        /// 
        /// 
        /// 
        protected string ConvertBranch(object Branch)
        {
            string name = string.Empty;
            if (Branch != null)
            {
                var branch = BLL.DivisionProjectService.GetDivisionProjectById(Branch.ToString());
                if (branch != null)
                {
                    name = branch.DivisionName;
                }
            }
            return name;
        }
        /// 
        /// 获取控制点内容
        /// 
        /// 
        /// 
        protected string ConvertControlPointType(object ControlPointType)
        {
            string name = string.Empty;
            if (ControlPointType != null)
            {
                var controlPointType = BLL.BreakdownProjectService.GetBreakdownProjectById(ControlPointType.ToString());
                if (controlPointType != null)
                {
                    name = controlPointType.BreakdownName;
                }
            }
            return name;
        }
        /// 
        /// 获取控制点等级
        /// 
        /// 
        /// 
        protected string ConvertClass(object ControlPointType)
        {
            string name = string.Empty;
            if (ControlPointType != null)
            {
                var controlPointType = BLL.BreakdownProjectService.GetBreakdownProjectById(ControlPointType.ToString());
                if (controlPointType != null)
                {
                    name = controlPointType.Class;
                }
            }
            return name;
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 单位工程下拉选择事件
        /// 
        /// 
        /// 
        protected void drpUnitWorkId_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpChirldUnitWorkId.Items.Clear();
            if (this.drpUnitWorkId.SelectedValue != BLL.Const._Null)
            {
                //子单位工程
                var childUnitWorks = (from x in Funs.DB.WBS_DivisionDivide
                                      join y in Funs.DB.WBS_UnitWork on x.UnitWorkId equals y.UnitWorkId
                                      where x.ProjectId == this.CurrUser.LoginProjectId
                                      && y.SuperUnitWork == this.drpUnitWorkId.SelectedValue
                                      && (y.SuperUnitWork != "0")
                                      select new
                                      {
                                          y.UnitWorkId,
                                          UnitWorkName = y.UnitWorkCode + "-" + y.UnitWorkName
                                      }).Distinct().ToList();
                this.drpChirldUnitWorkId.DataTextField = "UnitWorkName";
                this.drpChirldUnitWorkId.DataValueField = "UnitWorkId";
                this.drpChirldUnitWorkId.DataSource = childUnitWorks;
                this.drpChirldUnitWorkId.DataBind();
                Funs.FineUIPleaseSelect(this.drpChirldUnitWorkId);
                this.drpChirldUnitWorkId.SelectedIndex = 0;
                //分部工程
                this.drpBranch.Items.Clear();
                var q = (from x in Funs.DB.WBS_DivisionDivide
                         join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                         where x.ProjectId == this.CurrUser.LoginProjectId
                         && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                         && (y.ParentId == null || y.ParentId == "")
                         select new
                         {
                             y.DivisionId,
                             y.BranchEngineeringName
                         }).Distinct().ToList();
                this.drpBranch.DataTextField = "BranchEngineeringName";
                this.drpBranch.DataValueField = "DivisionId";
                this.drpBranch.DataSource = q;
                this.drpBranch.DataBind();
                Funs.FineUIPleaseSelect(this.drpBranch);
                this.drpBranch.SelectedIndex = 0;
            }
        }
        /// 
        /// 子单位工程下拉选择事件
        /// 
        /// 
        /// 
        protected void drpChirldUnitWorkId_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (this.drpChirldUnitWorkId.SelectedValue != BLL.Const._Null)
            {
                this.drpBranch.Items.Clear();
                var q = (from x in Funs.DB.WBS_DivisionDivide
                         join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                         where x.ProjectId == this.CurrUser.LoginProjectId
                         && x.UnitWorkId == this.drpChirldUnitWorkId.SelectedValue
                         && (y.ParentId == null || y.ParentId == "")
                         select new
                         {
                             y.DivisionId,
                             y.BranchEngineeringName
                         }).Distinct().ToList();
                this.drpBranch.DataTextField = "BranchEngineeringName";
                this.drpBranch.DataValueField = "DivisionId";
                this.drpBranch.DataSource = q;
                this.drpBranch.DataBind();
                Funs.FineUIPleaseSelect(this.drpBranch);
                this.drpBranch.SelectedIndex = 0;
            }
        }
        /// 
        /// 分部工程下拉选择事件
        /// 
        /// 
        /// 
        protected void drpBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpChildBranch.Items.Clear();
            if (this.drpBranch.SelectedValue != BLL.Const._Null)
            {
                #region 子分部工程
                if (this.drpChirldUnitWorkId.SelectedValue != BLL.Const._Null)
                {
                    //子分部工程
                    var childBranchs = (from x in Funs.DB.WBS_DivisionDivide
                                        join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                        where x.ProjectId == this.CurrUser.LoginProjectId
                                        && y.ParentId == this.drpBranch.SelectedValue
                                        && x.UnitWorkId == this.drpChirldUnitWorkId.SelectedValue
                                        && y.SubBranchEngineeringName != null
                                        select new
                                        {
                                            y.DivisionId,
                                            y.SubBranchEngineeringName
                                        }).Distinct().ToList();
                    this.drpChildBranch.DataTextField = "SubBranchEngineeringName";
                    this.drpChildBranch.DataValueField = "DivisionId";
                    this.drpChildBranch.DataSource = childBranchs;
                    this.drpChildBranch.DataBind();
                    Funs.FineUIPleaseSelect(this.drpChildBranch);
                    this.drpChildBranch.SelectedIndex = 0;
                }
                else
                {
                    var childBranchs = (from x in Funs.DB.WBS_DivisionDivide
                                        join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                                        where x.ProjectId == this.CurrUser.LoginProjectId
                                        && y.ParentId == this.drpBranch.SelectedValue
                                        && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                                        && y.SubBranchEngineeringName != null
                                        select new
                                        {
                                            y.DivisionId,
                                            y.SubBranchEngineeringName
                                        }).Distinct().ToList();
                    this.drpChildBranch.DataTextField = "SubBranchEngineeringName";
                    this.drpChildBranch.DataValueField = "DivisionId";
                    this.drpChildBranch.DataSource = childBranchs;
                    this.drpChildBranch.DataBind();
                    Funs.FineUIPleaseSelect(this.drpChildBranch);
                    this.drpChildBranch.SelectedIndex = 0;
                }
                #endregion
               
                //分项工程
                this.drpProEngineering.Items.Clear();
                var q = (from x in Funs.DB.WBS_DivisionDivide
                         join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                         where x.ProjectId == this.CurrUser.LoginProjectId
                         && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                         && y.ParentId == this.drpBranch.SelectedValue
                         && (y.ParentId != null && y.ParentId != "")
                         select new
                         {
                             y.DivisionId,
                             y.ProEngineeringName,
                             y.SubBranchEngineeringName
                         }).Distinct().ToList();
                if (q.Where(w => w.SubBranchEngineeringName == null).Count() > 0)
                {
                    this.drpProEngineering.DataTextField = "ProEngineeringName";
                    this.drpProEngineering.DataValueField = "DivisionId";
                    this.drpProEngineering.DataSource = q;
                }
                else
                {
                    var a = (from x in Funs.DB.WBS_DivisionDivide
                             join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                             join z in Funs.DB.Division_SubProjects on y.ParentId equals z.DivisionId
                             join w in Funs.DB.Division_SubProjects on z.ParentId equals w.DivisionId
                             where x.ProjectId == this.CurrUser.LoginProjectId
                             && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                             && z.ParentId == this.drpBranch.SelectedValue
                             select new
                             {
                                 y.DivisionId,
                                 y.ProEngineeringName
                             }).Distinct().ToList();
                    this.drpProEngineering.DataTextField = "ProEngineeringName";
                    this.drpProEngineering.DataValueField = "DivisionId";
                    this.drpProEngineering.DataSource = a;
                }                
                this.drpProEngineering.DataBind();
                Funs.FineUIPleaseSelect(this.drpProEngineering);
            }
            this.drpProEngineering.SelectedIndex = 0;
        }
        /// 
        /// 子分部工程下拉选择事件
        /// 
        /// 
        /// 
        protected void drpChildBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpProEngineering.Items.Clear();
            if (this.drpChildBranch.SelectedValue != BLL.Const._Null)
            {
                //分项工程
                var a = (from x in Funs.DB.WBS_DivisionDivide
                         join y in Funs.DB.Division_SubProjects on x.DivisionId equals y.DivisionId
                         where x.ProjectId == this.CurrUser.LoginProjectId
                         && x.UnitWorkId == this.drpUnitWorkId.SelectedValue
                         && y.ParentId == this.drpChildBranch.SelectedValue
                         select new
                         {
                             y.DivisionId,
                             y.ProEngineeringName
                         }).Distinct().ToList();
                this.drpProEngineering.DataTextField = "ProEngineeringName";
                this.drpProEngineering.DataValueField = "DivisionId";
                this.drpProEngineering.DataSource = a;
                this.drpProEngineering.DataBind();
                Funs.FineUIPleaseSelect(this.drpProEngineering);
                this.drpProEngineering.SelectedIndex = 0;
            }
        }
        #endregion
    }
}