using BLL;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web.UI.WebControls;
namespace FineUIPro.Web.WeldingProcess.TrustManage
{
    public partial class TrustBatchOut : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLL.Base_UnitService.InitProjectUnitDropDownList(this.drpUnit, true, this.CurrUser.LoginProjectId, BLL.Const.UnitType_5, Resources.Lan.PleaseSelect);
                BLL.Base_DetectionTypeService.InitDetectionTypeDropDownList(this.drpDetectionType, Resources.Lan.PleaseSelect, true, string.Empty);
                Funs.FineUIPleaseSelect(this.drpInstallation, Resources.Lan.PleaseSelect);
            }
        }
        #region 单位下拉框变化加载对应的委托单信息
        /// 
        /// 单位下拉框变化加载对应的委托单信息
        /// 
        /// 
        /// 
        protected void drpUnit_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpBatchTrust.Items.Clear();
            this.drpInstallation.Items.Clear();
            BLL.Project_InstallationService.InitInstallationDropDownList(this.drpInstallation, true, this.CurrUser.LoginProjectId, drpUnit.SelectedValue, Resources.Lan.PleaseSelect);
            drpInstallation.SelectedValue = Const._Null;
            if (this.drpUnit.SelectedValue != BLL.Const._Null && this.drpInstallation.SelectedValue != BLL.Const._Null && this.drpDetectionType.SelectedValue != BLL.Const._Null)
            {
                BLL.Batch_BatchTrustService.InitTrustBatchDropDownList(this.drpBatchTrust, true, this.drpUnit.SelectedValue, this.drpInstallation.SelectedValue, this.drpDetectionType.SelectedValue, this.txtPipelineCode.Text.Trim(), Resources.Lan.PleaseSelect);
            }
        }
        #endregion
        #region 装置下拉框变化加载对应的委托单信息
        /// 
        /// 装置下拉框变化加载对应的委托单信息
        /// 
        /// 
        /// 
        protected void drpInstallation_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpBatchTrust.Items.Clear();
            if (this.drpUnit.SelectedValue != BLL.Const._Null && this.drpInstallation.SelectedValue != BLL.Const._Null && this.drpDetectionType.SelectedValue != BLL.Const._Null)
            {
                BLL.Batch_BatchTrustService.InitTrustBatchDropDownList(this.drpBatchTrust, true, this.drpUnit.SelectedValue, this.drpInstallation.SelectedValue, this.drpDetectionType.SelectedValue, this.txtPipelineCode.Text.Trim(), Resources.Lan.PleaseSelect);
            }
        }
        #endregion
        #region 探伤类型下拉框变化加载对应的委托单信息
        /// 
        /// 装置下拉框变化加载对应的委托单信息
        /// 
        /// 
        /// 
        protected void drpDetectionType_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.drpBatchTrust.Items.Clear();
            if (this.drpUnit.SelectedValue != BLL.Const._Null && this.drpInstallation.SelectedValue != BLL.Const._Null && this.drpDetectionType.SelectedValue != BLL.Const._Null)
            {
                BLL.Batch_BatchTrustService.InitTrustBatchDropDownList(this.drpBatchTrust, true, this.drpUnit.SelectedValue, this.drpInstallation.SelectedValue, this.drpDetectionType.SelectedValue, this.txtPipelineCode.Text.Trim(), Resources.Lan.PleaseSelect);
            }
        }
        #endregion
        #region 管线号变化加载对应的委托单信息
        /// 
        /// 管线号变化加载对应的委托单信息
        /// 
        /// 
        /// 
        protected void txtPipelineCode_TextChanged(object sender, EventArgs e)
        {
            this.drpBatchTrust.Items.Clear();
            if (this.drpUnit.SelectedValue != BLL.Const._Null && this.drpInstallation.SelectedValue != BLL.Const._Null && this.drpDetectionType.SelectedValue != BLL.Const._Null)
            {
                BLL.Batch_BatchTrustService.InitTrustBatchDropDownList(this.drpBatchTrust, true, this.drpUnit.SelectedValue, this.drpInstallation.SelectedValue, this.drpDetectionType.SelectedValue, this.txtPipelineCode.Text.Trim(), Resources.Lan.PleaseSelect);
            }
        }
        #endregion
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string rootPath = Server.MapPath("~/") + Const.ExcelUrl;
            //模板文件
            string TempletFileName = rootPath + "委托单导出.xlsx";
            //导出文件
            string filePath = rootPath + DateTime.Now.ToString("yyyyMMddhhmmss") + "\\";
            if (!Directory.Exists(filePath))
            {
                Directory.CreateDirectory(filePath);
            }
            string ReportFileName = filePath + "out.xlsx";
            FileStream file = new FileStream(TempletFileName, FileMode.Open, FileAccess.Read);
            XSSFWorkbook hssfworkbook = new XSSFWorkbook(file);
            //HSSFWorkbook hssfworkbook = new HSSFWorkbook(file);
            #region 
            XSSFSheet reportModel = (XSSFSheet)hssfworkbook.GetSheet("Sheet1");
            
            //XSSFFont cs_content_Font = (XSSFFont)hssfworkbook.CreateFont(); //创建字体
            //cs_content_Font.FontName = "sans-serif";//字体
            //cs_content_Font.FontHeightInPoints = 10; //字体大小
            string[] trustList = drpBatchTrust.SelectedValueArray;
            if (trustList.Count() > 0)
            {
                var rowIndex = 1;
                foreach (var trust in trustList)
                {
                    var trustItemList = from x in Funs.DB.Batch_BatchTrustItem
                                        join y in Funs.DB.Batch_BatchTrust on x.TrustBatchId equals y.TrustBatchId
                                        join z in Funs.DB.Pipeline_WeldJoint on x.WeldJointId equals z.WeldJointId
                                        join p in Funs.DB.Pipeline_Pipeline on z.PipelineId equals p.PipelineId
                                        join u in Funs.DB.Base_Unit on y.NDEUuit equals u.UnitId
                                        where x.TrustBatchId == trust && x.IsCancelTrust == null
                                        select new { y.TrustBatchCode, u.UnitName, p.PipelineCode, z.WeldJointCode };
                    foreach (var item in trustItemList)
                    {
                        if (reportModel.GetRow(rowIndex) == null) reportModel.CreateRow(rowIndex);
                        #region 列赋值
                        //TrustBatchCode
                        if (reportModel.GetRow(rowIndex).GetCell(0) == null) reportModel.GetRow(rowIndex).CreateCell(0);
                        reportModel.GetRow(rowIndex).GetCell(0).SetCellValue(item.TrustBatchCode);
                        //UnitName                                                                           //Contractor
                        if (reportModel.GetRow(rowIndex).GetCell(2) == null) reportModel.GetRow(rowIndex).CreateCell(2);
                        reportModel.GetRow(rowIndex).GetCell(2).SetCellValue(item.UnitName);
                        // PipelineCode
                        if (reportModel.GetRow(rowIndex).GetCell(3) == null) reportModel.GetRow(rowIndex).CreateCell(3);
                        reportModel.GetRow(rowIndex).GetCell(3).SetCellValue(item.PipelineCode);
                        // WeldJointCode
                        if (reportModel.GetRow(rowIndex).GetCell(4) == null) reportModel.GetRow(rowIndex).CreateCell(4);
                        reportModel.GetRow(rowIndex).GetCell(4).SetCellValue(item.WeldJointCode);
                        
                        #endregion
                        rowIndex++;
                    }
                   
                }
            }
     
            #endregion
            reportModel.ForceFormulaRecalculation = true;
            using (FileStream filess = System.IO.File.OpenWrite(ReportFileName))
            {
                hssfworkbook.Write(filess);
            }
            FileInfo filet = new FileInfo(ReportFileName);
            Response.Clear();
            Response.Charset = "GB2312";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名
            Response.AddHeader("Content-Disposition", "attachment; filename=trust_" + Server.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度
            Response.AddHeader("Content-Length", filet.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载
            Response.ContentType = "application/ms-excel";
            // 把文件流发送到客户端
            Response.WriteFile(filet.FullName);
            // 停止页面的执行
            Response.End();
        }
    }
}