using Aspose.Words;
using BLL;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.CQMS.QualityPlanning
{
    public partial class Organizational : PageBase
    {
        #region 加载
        /// 
        /// 加载页面
        /// 
        /// 
        /// 
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string projectId = this.CurrUser.LoginProjectId;
                this.ContentPanel3.Title = "项目名称:" + BLL.ProjectService.GetProjectNameByProjectId(projectId);
                if (TabStrip1.ActiveTabIndex == 0)
                {
                    this.drpUnitId.Items.Clear();
                    BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnitId, projectId, BLL.Const.ProjectUnitType_1, false);
                }
                else if (TabStrip1.ActiveTabIndex == 1)
                {
                    this.drpUnitId.Items.Clear();
                    BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnitId, projectId, BLL.Const.ProjectUnitType_2, false);
                }
                DataBind(this.drpUnitId.SelectedValue);
            }
        }
        /// 
        /// 加载数据
        /// 
        /// 
        /// 
        private void DataBind(string unitId)
        {
            string projectId = this.CurrUser.LoginProjectId;
            if (unitId == BLL.Const.UnitId_CWCEC)
            {
                string projectManagerName = string.Empty;
                var projectManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "项目经理");
                if (projectManager != null)
                {
                    foreach (var item in projectManager)
                    {
                        projectManagerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(projectManagerName))
                    {
                        this.Label1.Text = "项目经理:" + projectManagerName.Substring(0, projectManagerName.LastIndexOf(','));
                    }
                }
                string sjName = string.Empty;
                var sj = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "设计经理");
                if (sj != null)
                {
                    foreach (var item in sj)
                    {
                        sjName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(sjName))
                    {
                        this.Label2.Text = "设计经理:" + sjName.Substring(0, sjName.LastIndexOf(','));
                    }
                }
                string cgName = string.Empty;
                var cg = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "采购经理");
                if (cg != null)
                {
                    foreach (var item in cg)
                    {
                        cgName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(cgName))
                    {
                        this.Label3.Text = "采购经理:" + cgName.Substring(0, cgName.LastIndexOf(','));
                    }
                }
                string name4 = string.Empty;
                var con = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "施工经理");
                if (con != null)
                {
                    foreach (var item in con)
                    {
                        name4 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name4))
                    {
                        this.Label4.Text = "施工经理:" + name4.Substring(0, name4.LastIndexOf(','));
                    }
                }
                string name5 = string.Empty;
                var qa = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "质量经理");
                if (qa != null)
                {
                    foreach (var item in qa)
                    {
                        name5 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name5))
                    {
                        this.Label5.Text = "质量经理:" + name5.Substring(0, name5.LastIndexOf(','));
                    }
                }
                string name6 = string.Empty;
                var hseManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE经理");
                if (hseManager != null)
                {
                    foreach (var item in hseManager)
                    {
                        name6 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name6))
                    {
                        this.Label6.Text = "HSE经理:" + name6.Substring(0, name6.LastIndexOf(','));
                    }
                }
                string name7 = string.Empty;
                var carManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "开车经理");
                if (carManager != null)
                {
                    foreach (var item in carManager)
                    {
                        name7 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name7))
                    {
                        this.Label7.Text = "开车经理:" + name7.Substring(0, name7.LastIndexOf(','));
                    }
                }
                string warehousemanName = string.Empty;
                var warehousemanLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "仓库管理员");
                if (warehousemanLists != null)
                {
                    foreach (var item in warehousemanLists)
                    {
                        warehousemanName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(warehousemanName))
                    {
                        warehousemanName = warehousemanName.Substring(0, warehousemanName.LastIndexOf(','));
                    }
                }
                this.Label8.Text = "仓库管理员:" + warehousemanName;
                string docName = string.Empty;
                var doc = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "文控工程师");
                if (doc != null)
                {
                    foreach (var item in doc)
                    {
                        docName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(docName))
                    {
                        docName = docName.Substring(0, docName.LastIndexOf(','));
                    }
                }
                this.Label9.Text = "文控工程师:" + docName;
                string hseEngineerName = string.Empty;
                var hseEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE工程师");
                if (hseEngineerLists != null)
                {
                    foreach (var item in hseEngineerLists)
                    {
                        hseEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(hseEngineerName))
                    {
                        hseEngineerName = hseEngineerName.Substring(0, hseEngineerName.LastIndexOf(','));
                    }
                }
                this.Label10.Text = "HSE工程师:" + hseEngineerName;
                string carEngineerName = string.Empty;
                var carEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "开车工程师");
                if (carEngineerLists != null)
                {
                    foreach (var item in carEngineerLists)
                    {
                        carEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(carEngineerName))
                    {
                        carEngineerName = carEngineerName.Substring(0, carEngineerName.LastIndexOf(','));
                    }
                }
                this.Label11.Text = "开车工程师:" + carEngineerName;
                string CVEngineerName = string.Empty;
                var CVEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "土建工程师");
                if (CVEngineerLists != null)
                {
                    foreach (var item in CVEngineerLists)
                    {
                        CVEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(CVEngineerName))
                    {
                        CVEngineerName = CVEngineerName.Substring(0, CVEngineerName.LastIndexOf(','));
                    }
                }
                this.Label12.Text = "土建工程师:" + CVEngineerName;
                string FEEngineerName = string.Empty;
                var FEEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "设备工程师");
                if (FEEngineerLists != null)
                {
                    foreach (var item in FEEngineerLists)
                    {
                        FEEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(FEEngineerName))
                    {
                        FEEngineerName = FEEngineerName.Substring(0, FEEngineerName.LastIndexOf(','));
                    }
                }
                this.Label13.Text = "设备工程师:" + FEEngineerName;
                string PDEngineerName = string.Empty;
                var PDEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "管道工程师");
                if (PDEngineerLists != null)
                {
                    foreach (var item in PDEngineerLists)
                    {
                        PDEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(PDEngineerName))
                    {
                        PDEngineerName = PDEngineerName.Substring(0, PDEngineerName.LastIndexOf(','));
                    }
                }
                this.Label14.Text = "管道工程师:" + PDEngineerName;
                string EHEngineerName = string.Empty;
                var EHEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "电气工程师");
                if (EHEngineerLists != null)
                {
                    foreach (var item in EHEngineerLists)
                    {
                        EHEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EHEngineerName))
                    {
                        EHEngineerName = EHEngineerName.Substring(0, EHEngineerName.LastIndexOf(','));
                    }
                }
                this.Label15.Text = "电气工程师:" + EHEngineerName;
                string EAEngineerName = string.Empty;
                var EAEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "仪表工程师");
                if (EAEngineerLists != null)
                {
                    foreach (var item in EAEngineerLists)
                    {
                        EAEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EAEngineerName))
                    {
                        EAEngineerName = EAEngineerName.Substring(0, EAEngineerName.LastIndexOf(','));
                    }
                }
                this.Label16.Text = "仪表工程师:" + EAEngineerName;
            }
            else
            {
                this.divSubOrganizational.Visible = true;
                this.divOrganizational.Visible = false;
                string projectManagerName = string.Empty;
                var projectManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "项目经理");
                if (projectManager != null)
                {
                    foreach (var item in projectManager)
                    {
                        projectManagerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(projectManagerName))
                    {
                        projectManagerName = projectManagerName.Substring(0, projectManagerName.LastIndexOf(','));
                    }
                }
                this.Label17.Text = "项目经理:" + projectManagerName;
                string name4 = string.Empty;
                var con = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "施工经理");
                if (con != null)
                {
                    foreach (var item in con)
                    {
                        name4 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name4))
                    {
                        name4 = name4.Substring(0, name4.LastIndexOf(','));
                    }
                }
                this.Label18.Text = "施工经理:" + name4;
                string name5 = string.Empty;
                var qa = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "质量经理");
                if (qa != null)
                {
                    foreach (var item in qa)
                    {
                        name5 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name5))
                    {
                        name5 = name5.Substring(0, name5.LastIndexOf(','));
                    }
                }
                this.Label19.Text = "质量经理:" + name5;
                string name6 = string.Empty;
                var hseManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE经理");
                if (hseManager != null)
                {
                    foreach (var item in hseManager)
                    {
                        name6 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name6))
                    {
                        name6 = name6.Substring(0, name6.LastIndexOf(','));
                    }
                }
                this.Label20.Text = "HSE经理:" + name6;
                string docName = string.Empty;
                var doc = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "文控工程师");
                if (doc != null)
                {
                    foreach (var item in doc)
                    {
                        docName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(docName))
                    {
                        docName = docName.Substring(0, docName.LastIndexOf(','));
                    }
                }
                this.Label21.Text = "文控工程师:" + docName;
                string hseEngineerName = string.Empty;
                var hseEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE工程师");
                if (hseEngineerLists != null)
                {
                    foreach (var item in hseEngineerLists)
                    {
                        hseEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(hseEngineerName))
                    {
                        hseEngineerName = hseEngineerName.Substring(0, hseEngineerName.LastIndexOf(','));
                    }
                }
                this.Label22.Text = "HSE工程师:" + hseEngineerName;
                string CVEngineerName = string.Empty;
                var CVEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "土建工程师");
                if (CVEngineerLists != null)
                {
                    foreach (var item in CVEngineerLists)
                    {
                        CVEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(CVEngineerName))
                    {
                        CVEngineerName = CVEngineerName.Substring(0, CVEngineerName.LastIndexOf(','));
                    }
                }
                this.Label23.Text = "土建工程师:" + CVEngineerName;
                string FEEngineerName = string.Empty;
                var FEEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "设备工程师");
                if (FEEngineerLists != null)
                {
                    foreach (var item in FEEngineerLists)
                    {
                        FEEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(FEEngineerName))
                    {
                        FEEngineerName = FEEngineerName.Substring(0, FEEngineerName.LastIndexOf(','));
                    }
                }
                this.Label24.Text = "设备工程师:" + FEEngineerName;
                string PDEngineerName = string.Empty;
                var PDEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "管道工程师");
                if (PDEngineerLists != null)
                {
                    foreach (var item in PDEngineerLists)
                    {
                        PDEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(PDEngineerName))
                    {
                        PDEngineerName = PDEngineerName.Substring(0, PDEngineerName.LastIndexOf(','));
                    }
                }
                this.Label25.Text = "管道工程师:" + PDEngineerName;
                string EHEngineerName = string.Empty;
                var EHEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "电气工程师");
                if (EHEngineerLists != null)
                {
                    foreach (var item in EHEngineerLists)
                    {
                        EHEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EHEngineerName))
                    {
                        EHEngineerName = EHEngineerName.Substring(0, EHEngineerName.LastIndexOf(','));
                    }
                }
                this.Label26.Text = "电气工程师:" + EHEngineerName;
                string EAEngineerName = string.Empty;
                var EAEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "仪表工程师");
                if (EAEngineerLists != null)
                {
                    foreach (var item in EAEngineerLists)
                    {
                        EAEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EAEngineerName))
                    {
                        EAEngineerName = EAEngineerName.Substring(0, EAEngineerName.LastIndexOf(','));
                    }
                }
                this.Label27.Text = "仪表工程师:" + EAEngineerName;
            }
        }
        #endregion
        protected void TabStrip1_TabIndexChanged(object sender, EventArgs e)
        {
            if (TabStrip1.ActiveTabIndex == 0)
            {
                this.drpUnitId.Items.Clear();
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnitId, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_1, false);
                this.drpUnitId.SelectedIndex = 0;
            }
            else if (TabStrip1.ActiveTabIndex == 1)
            {
                this.drpUnitId.Items.Clear();
                BLL.UnitService.InitUnitByProjectIdUnitTypeDropDownList(this.drpUnitId, this.CurrUser.LoginProjectId, BLL.Const.ProjectUnitType_2, false);
                this.drpUnitId.SelectedIndex = 0;
            }
            DataBind(this.drpUnitId.SelectedValue);
        }
        #region 导出
        /// 
        /// 导出
        /// 
        /// 
        /// 
        protected void btnOut_Click(object sender, EventArgs e)
        {
            string rootPath = Server.MapPath("~/");
            string initTemplatePath = string.Empty;
            string uploadfilepath = string.Empty;
            string newUrl = string.Empty;
            string filePath = string.Empty;
            string projectId = this.CurrUser.LoginProjectId;
            string unitId = this.drpUnitId.SelectedValue;
            var pModel = Funs.DB.Base_Project.FirstOrDefault(x => x.ProjectId == projectId);
            if (unitId == BLL.Const.UnitId_CWCEC)
            {
                initTemplatePath = Const.CQMS_OrganizationalUrl;
                uploadfilepath = rootPath + initTemplatePath;
                newUrl = uploadfilepath.Replace("总包质量管理组织机构图", "总包质量管理组织机构图(" + pModel.ShortName + ")");
                //filePath = initTemplatePath.Replace(".doc", trainRecords.TrainRecordsId.Replace("/", "-") + ".pdf");
                if (File.Exists(newUrl))
                {
                    File.Delete(newUrl);
                }
                File.Copy(uploadfilepath, newUrl);
                Document doc = new Aspose.Words.Document(uploadfilepath);
                Bookmark projectName = doc.Range.Bookmarks["ProjectName"];
                if (projectName != null)
                {
                    projectName.Text = "项目名称:" + BLL.ProjectService.GetProjectNameByProjectId(projectId);
                }
                Bookmark projectManager = doc.Range.Bookmarks["ProjectManager"];
                string pm = string.Empty;
                var lists1 = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "项目经理");
                if (lists1 != null)
                {
                    foreach (var item in lists1)
                    {
                        pm += item + ",";
                    }
                    if (!string.IsNullOrEmpty(pm))
                    {
                        pm = pm.Substring(0, pm.LastIndexOf(','));
                    }
                }
                projectManager.Text = "项目经理:" + pm;
                Bookmark sj = doc.Range.Bookmarks["sjManager"];
                string sjName = string.Empty;
                var sjLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "设计经理");
                if (sjLists != null)
                {
                    foreach (var item in sjLists)
                    {
                        sjName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(sjName))
                    {
                        sjName = sjName.Substring(0, sjName.LastIndexOf(','));
                    }
                }
                sj.Text = "设计经理:" + sjName;
                Bookmark cgManager = doc.Range.Bookmarks["cgManager"];
                string cgName = string.Empty;
                var cg = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "采购经理");
                if (cg != null)
                {
                    foreach (var item in cg)
                    {
                        cgName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(cgName))
                    {
                        cgName = cgName.Substring(0, cgName.LastIndexOf(','));
                    }
                }
                cgManager.Text = "采购经理:" + cgName;
                Bookmark sgManager = doc.Range.Bookmarks["sgManager"];
                string name4 = string.Empty;
                var con = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "施工经理");
                if (con != null)
                {
                    foreach (var item in con)
                    {
                        name4 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name4))
                    {
                        name4 = name4.Substring(0, name4.LastIndexOf(','));
                    }
                }
                sgManager.Text = "施工经理:" + name4;
                Bookmark qaManager = doc.Range.Bookmarks["qaManager"];
                string name5 = string.Empty;
                var qa = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "质量经理");
                if (qa != null)
                {
                    foreach (var item in qa)
                    {
                        name5 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name5))
                    {
                        name5 = name5.Substring(0, name5.LastIndexOf(','));
                    }
                }
                qaManager.Text = "质量经理:" + name5;
                Bookmark hse = doc.Range.Bookmarks["hseManager"];
                string name6 = string.Empty;
                var hseManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE经理");
                if (hseManager != null)
                {
                    foreach (var item in hseManager)
                    {
                        name6 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name6))
                    {
                        name6 = name6.Substring(0, name6.LastIndexOf(','));
                    }
                }
                hse.Text = "HSE经理:" + name6;
                Bookmark car = doc.Range.Bookmarks["carManager"];
                string name7 = string.Empty;
                var carManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "开车经理");
                if (carManager != null)
                {
                    foreach (var item in carManager)
                    {
                        name7 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name7))
                    {
                        name7 = name7.Substring(0, name7.LastIndexOf(','));
                    }
                }
                car.Text = "开车经理:" + name7;
                Bookmark warehouse = doc.Range.Bookmarks["warehouseManager"];
                string warehousemanName = string.Empty;
                var warehousemanLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "仓库管理员");
                if (warehousemanLists != null)
                {
                    foreach (var item in warehousemanLists)
                    {
                        warehousemanName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(warehousemanName))
                    {
                        warehousemanName = warehousemanName.Substring(0, warehousemanName.LastIndexOf(','));
                    }
                }
                warehouse.Text = "仓库管理员:" + warehousemanName;
                Bookmark docManager = doc.Range.Bookmarks["docManager"];
                string docName = string.Empty;
                var docm = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "文控工程师");
                if (docm != null)
                {
                    foreach (var item in docm)
                    {
                        docName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(docName))
                    {
                        docName = docName.Substring(0, docName.LastIndexOf(','));
                    }
                }
                docManager.Text = "文控工程师:" + docName;
                Bookmark hseEngineer = doc.Range.Bookmarks["hseEngineer"];
                string hseEngineerName = string.Empty;
                var hseEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE工程师");
                if (hseEngineerLists != null)
                {
                    foreach (var item in hseEngineerLists)
                    {
                        hseEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(hseEngineerName))
                    {
                        hseEngineerName = hseEngineerName.Substring(0, hseEngineerName.LastIndexOf(','));
                    }
                }
                hseEngineer.Text = "HSE工程师:" + hseEngineerName;
                Bookmark carEngineer = doc.Range.Bookmarks["carEngineer"];
                string carEngineerName = string.Empty;
                var carEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "开车工程师");
                if (carEngineerLists != null)
                {
                    foreach (var item in carEngineerLists)
                    {
                        carEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(carEngineerName))
                    {
                        carEngineerName = carEngineerName.Substring(0, carEngineerName.LastIndexOf(','));
                    }
                }
                carEngineer.Text = "开车工程师:" + carEngineerName;
                Bookmark CVEngineer = doc.Range.Bookmarks["CVEngineer"];
                string CVEngineerName = string.Empty;
                var CVEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "土建工程师");
                if (CVEngineerLists != null)
                {
                    foreach (var item in CVEngineerLists)
                    {
                        CVEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(CVEngineerName))
                    {
                        CVEngineerName = CVEngineerName.Substring(0, CVEngineerName.LastIndexOf(','));
                    }
                }
                CVEngineer.Text = "土建工程师:" + CVEngineerName;
                Bookmark FEEngineer = doc.Range.Bookmarks["FEEngineer"];
                string FEEngineerName = string.Empty;
                var FEEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "设备工程师");
                if (FEEngineerLists != null)
                {
                    foreach (var item in FEEngineerLists)
                    {
                        FEEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(FEEngineerName))
                    {
                        FEEngineerName = FEEngineerName.Substring(0, FEEngineerName.LastIndexOf(','));
                    }
                }
                FEEngineer.Text = "设备工程师:" + FEEngineerName;
                Bookmark PDEngineer = doc.Range.Bookmarks["PDEngineer"];
                string PDEngineerName = string.Empty;
                var PDEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "管道工程师");
                if (PDEngineerLists != null)
                {
                    foreach (var item in PDEngineerLists)
                    {
                        PDEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(PDEngineerName))
                    {
                        PDEngineerName = PDEngineerName.Substring(0, PDEngineerName.LastIndexOf(','));
                    }
                }
                PDEngineer.Text = "管道工程师:" + PDEngineerName;
                Bookmark EHEngineer = doc.Range.Bookmarks["EHEngineer"];
                string EHEngineerName = string.Empty;
                var EHEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "电气工程师");
                if (EHEngineerLists != null)
                {
                    foreach (var item in EHEngineerLists)
                    {
                        EHEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EHEngineerName))
                    {
                        EHEngineerName = EHEngineerName.Substring(0, EHEngineerName.LastIndexOf(','));
                    }
                }
                EHEngineer.Text = "电气工程师:" + EHEngineerName;
                Bookmark EAEngineer = doc.Range.Bookmarks["EAEngineer"];
                string EAEngineerName = string.Empty;
                var EAEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "仪表工程师");
                if (EAEngineerLists != null)
                {
                    foreach (var item in EAEngineerLists)
                    {
                        EAEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EAEngineerName))
                    {
                        EAEngineerName = EAEngineerName.Substring(0, EAEngineerName.LastIndexOf(','));
                    }
                }
                EAEngineer.Text = "仪表工程师:" + EAEngineerName;
                doc.Save(newUrl);
            }
            else
            {
                initTemplatePath = Const.CQMS_SubOrganizationalUrl;
                uploadfilepath = rootPath + initTemplatePath;
                newUrl = uploadfilepath.Replace("分包质量管理组织机构图", "分包质量管理组织机构图(" + pModel.ShortName + ")");
                //filePath = initTemplatePath.Replace(".doc", trainRecords.TrainRecordsId.Replace("/", "-") + ".pdf");
                if (File.Exists(newUrl))
                {
                    File.Delete(newUrl);
                }
                File.Copy(uploadfilepath, newUrl);
                Document doc = new Aspose.Words.Document(uploadfilepath);
                Bookmark projectName = doc.Range.Bookmarks["ProjectName"];
                if (projectName != null)
                {
                    projectName.Text = "项目名称:" + BLL.ProjectService.GetProjectNameByProjectId(projectId);
                }
                Bookmark projectManager = doc.Range.Bookmarks["ProjectManager"];
                string pm = string.Empty;
                var lists1 = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "项目经理");
                if (lists1 != null)
                {
                    foreach (var item in lists1)
                    {
                        pm += item + ",";
                    }
                    if (!string.IsNullOrEmpty(pm))
                    {
                        pm = pm.Substring(0, pm.LastIndexOf(','));
                    }
                }
                projectManager.Text = "项目经理:" + pm;
                Bookmark sgManager = doc.Range.Bookmarks["sgManager"];
                string name4 = string.Empty;
                var con = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "施工经理");
                if (con != null)
                {
                    foreach (var item in con)
                    {
                        name4 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name4))
                    {
                        name4 = name4.Substring(0, name4.LastIndexOf(','));
                    }
                }
                sgManager.Text = "施工经理:" + name4;
                Bookmark qaManager = doc.Range.Bookmarks["qaManager"];
                string name5 = string.Empty;
                var qa = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "质量经理");
                if (qa != null)
                {
                    foreach (var item in qa)
                    {
                        name5 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name5))
                    {
                        name5 = name5.Substring(0, name5.LastIndexOf(','));
                    }
                }
                qaManager.Text = "质量经理:" + name5;
                Bookmark hse = doc.Range.Bookmarks["hseManager"];
                string name6 = string.Empty;
                var hseManager = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE经理");
                if (hseManager != null)
                {
                    foreach (var item in hseManager)
                    {
                        name6 += item + ",";
                    }
                    if (!string.IsNullOrEmpty(name6))
                    {
                        name6 = name6.Substring(0, name6.LastIndexOf(','));
                    }
                }
                hse.Text = "HSE经理:" + name6;
                Bookmark docManager = doc.Range.Bookmarks["docManager"];
                string docName = string.Empty;
                var docm = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "文控工程师");
                if (docm != null)
                {
                    foreach (var item in docm)
                    {
                        docName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(docName))
                    {
                        docName = docName.Substring(0, docName.LastIndexOf(','));
                    }
                }
                docManager.Text = "文控工程师:" + docName;
                Bookmark hseEngineer = doc.Range.Bookmarks["hseEngineer"];
                string hseEngineerName = string.Empty;
                var hseEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "HSE工程师");
                if (hseEngineerLists != null)
                {
                    foreach (var item in hseEngineerLists)
                    {
                        hseEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(hseEngineerName))
                    {
                        hseEngineerName = hseEngineerName.Substring(0, hseEngineerName.LastIndexOf(','));
                    }
                }
                hseEngineer.Text = "HSE工程师:" + hseEngineerName;
                Bookmark CVEngineer = doc.Range.Bookmarks["CVEngineer"];
                string CVEngineerName = string.Empty;
                var CVEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "土建工程师");
                if (CVEngineerLists != null)
                {
                    foreach (var item in CVEngineerLists)
                    {
                        CVEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(CVEngineerName))
                    {
                        CVEngineerName = CVEngineerName.Substring(0, CVEngineerName.LastIndexOf(','));
                    }
                }
                CVEngineer.Text = "土建工程师:" + CVEngineerName;
                Bookmark FEEngineer = doc.Range.Bookmarks["FEEngineer"];
                string FEEngineerName = string.Empty;
                var FEEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "设备工程师");
                if (FEEngineerLists != null)
                {
                    foreach (var item in FEEngineerLists)
                    {
                        FEEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(FEEngineerName))
                    {
                        FEEngineerName = FEEngineerName.Substring(0, FEEngineerName.LastIndexOf(','));
                    }
                }
                FEEngineer.Text = "设备工程师:" + FEEngineerName;
                Bookmark PDEngineer = doc.Range.Bookmarks["PDEngineer"];
                string PDEngineerName = string.Empty;
                var PDEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "管道工程师");
                if (PDEngineerLists != null)
                {
                    foreach (var item in PDEngineerLists)
                    {
                        PDEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(PDEngineerName))
                    {
                        PDEngineerName = PDEngineerName.Substring(0, PDEngineerName.LastIndexOf(','));
                    }
                }
                PDEngineer.Text = "管道工程师:" + PDEngineerName;
                Bookmark EHEngineer = doc.Range.Bookmarks["EHEngineer"];
                string EHEngineerName = string.Empty;
                var EHEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "电气工程师");
                if (EHEngineerLists != null)
                {
                    foreach (var item in EHEngineerLists)
                    {
                        EHEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EHEngineerName))
                    {
                        EHEngineerName = EHEngineerName.Substring(0, EHEngineerName.LastIndexOf(','));
                    }
                }
                EHEngineer.Text = "电气工程师:" + EHEngineerName;
                Bookmark EAEngineer = doc.Range.Bookmarks["EAEngineer"];
                string EAEngineerName = string.Empty;
                var EAEngineerLists = BLL.UserService.GetUserNameListsByUnitIdWorkPostName(projectId, unitId, "仪表工程师");
                if (EAEngineerLists != null)
                {
                    foreach (var item in EAEngineerLists)
                    {
                        EAEngineerName += item + ",";
                    }
                    if (!string.IsNullOrEmpty(EAEngineerName))
                    {
                        EAEngineerName = EAEngineerName.Substring(0, EAEngineerName.LastIndexOf(','));
                    }
                }
                EAEngineer.Text = "仪表工程师:" + EAEngineerName;
                doc.Save(newUrl);
            }
            string fileName = Path.GetFileName(newUrl);
            FileInfo info = new FileInfo(newUrl);
            long fileSize = info.Length;
            Response.Clear();
            Response.ContentType = "application/x-zip-compressed";
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
            Response.AddHeader("Content-Length", fileSize.ToString());
            Response.TransmitFile(newUrl, 0, fileSize);
            Response.Flush();
            Response.Close();
            File.Delete(newUrl);
        }
        #endregion
        #region DropDownList下拉选择事件
        /// 
        /// 单位下拉选择
        /// 
        /// 
        /// 
        protected void drpUnitId_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataBind(this.drpUnitId.SelectedValue);
        }
        #endregion
    }
}