using BLL;
using BLL.Common;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
namespace FineUIPro.Web.common
{
    public class ExportExcel : PageBase
    {
        NPOIExcel excel;
        //构造 采用模板方式
        public ExportExcel(string tempPath)
        {
            excel = new NPOIExcel(tempPath);
        }
        /// 
        /// 流的形式
        /// 
        /// 
        private MemoryStream WriteToStream()
        {
            return excel.Save();
        }
        /// 
        /// 导出
        /// 
        /// 主键
        /// 导出文件名称
        public void PPKToExcel(string eprojectId, string fileName)
        {
            HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
           // HttpContext.Current.Response.ContentType = "application/excel";
            HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", HttpUtility.UrlEncode(fileName)));
            //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.Clear();
            ISheet sheet;
            sheet = excel.ActiveSheet;
            sheet.ForceFormulaRecalculation = true;   //允许excel里的公式生效
            ICellStyle style = excel.CreateCellStyle();
            style.BorderBottom = BorderStyle.Thin;
            style.BorderLeft = BorderStyle.Thin;
            style.BorderRight = BorderStyle.Thin;
            style.BorderTop = BorderStyle.Thin;
            style.WrapText = true;
            style.VerticalAlignment = VerticalAlignment.Center;
            ///////////////
            ICellStyle styleGray = excel.CreateCellStyle();
            styleGray.FillForegroundColor = NPOI.HSSF.Util.HSSFColor.Grey40Percent.Index;
            styleGray.FillPattern = FillPattern.SolidForeground;//设置背景是否填充           
            styleGray.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.Grey40Percent.Index;
            styleGray.VerticalAlignment = VerticalAlignment.Center;
            var eProject = BLL.EProjectService.GeteProjectById(eprojectId);
            var ss = BLL.CostReportService.GetCostReportByEProjectIdAndMonth(eprojectId, DateTime.Now.ToString("yyyy-MM"));
            if (eProject != null)
            {
                //第1行
                excel.SetValue(0, 9, DateTime.Now.ToString("yyyy-MM-dd"));
                //第2行
                excel.SetValue(1, 9, this.CurrUser.UserName);
                //第3行
                excel.SetValue(2, 9, eProject.ProjectControl_JobNo);
                //第4行
                excel.SetValue(3, 1, eProject.ProjectControl_JobTitle);
                excel.SetValue(3, 9, eProject.ProjectControl_BUCode);
                //第5行
                excel.SetValue(4, 9, eProject.ProjectControl_OrginalBudget);
                //第6行
                //第7行
                //eProject.PM_SC_ApprovedQty=M  eProject.PM_SC_PendingQty=N
                string aps = "";
                if (Funs.GetNewDecimalOrZero(eProject.PM_SC_PendingQty.ToString()) > 0)
                {
                    aps = "C";
                }
                else if (Funs.GetNewDecimalOrZero(eProject.PM_SC_PendingQty.ToString()) == 0 && Funs.GetNewDecimalOrZero(eProject.PM_SC_ApprovedQty.ToString()) > 0)
                {
                    aps = "B";
                }
                else if (Funs.GetNewDecimalOrZero(eProject.PM_SC_ApprovedQty.ToString()) == 0 && Funs.GetNewDecimalOrZero(eProject.PM_SC_PendingQty.ToString()) == 0 )
                {
                    aps = "A";
                }
                else
                {
                    aps = "";
                }
                excel.SetValue(6, 1, aps);
                string cbs = "";
                if (ss != null)
                {
                    double cbchjs = Convert.ToDouble(((Funs.GetNewDecimalOrZero(ss.ActualCost.ToString()) + Funs.GetNewDecimalOrZero(ss.CommittedPRPO.ToString()) + Funs.GetNewDecimalOrZero(ss.CommittedSSRs.ToString()) + Funs.GetNewDecimalOrZero(ss.CostToComplete.ToString())) - Funs.GetNewDecimalOrZero(ss.OrginalBudget.ToString()) - Funs.GetNewDecimalOrZero(ss.ChangedBudget.ToString())) / (Funs.GetNewDecimalOrZero(ss.OrginalBudget.ToString()) + Funs.GetNewDecimalOrZero(ss.ChangedBudget.ToString())));
                    if (cbchjs > 0.1)
                    {
                        cbs = "C";
                    }
                    else if (cbchjs > 0.05 && cbchjs <= 0.1)
                    {
                        cbs = "B";
                    }
                    else if (cbchjs <= 0.05)
                    {
                        cbs = "A";
                    }
                    else
                    {
                        cbs = "";
                    }
                }
                excel.SetValue(6, 3, cbs);
                // PM_MA_ProjectApproval=m ProjectControl_CM_SchEnd=x CM_CC_RevisedEnd=y
                TimeSpan ts1 = Convert.ToDateTime(eProject.CM_CC_RevisedEnd) - Convert.ToDateTime(eProject.ProjectControl_CM_SchEnd);
                double xy = ts1.Days;
                TimeSpan ts2 = Convert.ToDateTime(eProject.ProjectControl_CM_SchEnd) - Convert.ToDateTime(eProject.PM_MA_ProjectApproval);
                double xm = ts2.Days;
                double xym = xy / xm;
                string xyms = "";
                if (xym > 0.1)
                {
                    xyms = "C";
                }
                else if (xym > 0.05 && xym <= 0.1)
                {
                    xyms = "B";
                }
                else if (xym <= 0.05)
                {
                    xyms = "A";
                }
                else
                {
                    xyms = "";
                }
                excel.SetValue(6, 5, xyms);
                excel.SetValue(6, 8, eProject.PM_SC_ApprovedQty);
                excel.SetValue(6, 9, eProject.PM_SC_ApprovedCost);
                //第8行
                excel.SetValue(7, 8, eProject.PM_SC_PendingQty);
                excel.SetValue(7, 9, eProject.PM_SC_PendingCost);
                //第11行
                excel.SetValue(10, 3, eProject.ProjectControl_CC_SchStart);
                excel.SetValue(10, 4, eProject.ProjectControl_CC_SchEnd);
                excel.SetValue(10, 6, eProject.ProjectControl_CC_Progress);
                //excel.SetValue(10, 7, eProject.pro);
                //excel.SetValue(10, 8, eProject.);
                //excel.SetValue(10, 9, eProject.);
                //第12行
                excel.SetValue(11, 3, eProject.ProjectControl_CM_SchStart);
                excel.SetValue(11, 4, eProject.ProjectControl_CM_SchEnd);
                excel.SetValue(11, 6, eProject.ProjectControl_CM_Progress);
                //excel.SetValue(11, 7, eProject.);
                //excel.SetValue(11, 8, eProject.);
                //excel.SetValue(11, 9, eProject.);
                //第13行
                excel.SetValue(12, 3, eProject.ProjectControl_LP_SchStart);
                excel.SetValue(12, 4, eProject.ProjectControl_LP_SchEnd);
                excel.SetValue(12, 6, eProject.ProjectControl_LP_Progress);
                excel.SetValue(12, 7, eProject.PM_LLEP_RevisedStart);
                excel.SetValue(12, 8, eProject.PM_LLEP_RevisedEnd);
                excel.SetValue(12, 9, eProject.PM_LLEP_ActualProgress);
                //第14行
                // excel.SetValue(13, 3, eProject.);
                //excel.SetValue(13, 4, eProject.);
                excel.SetValue(13, 6, eProject.CM_CC_RevisedStart);
                excel.SetValue(13, 7, eProject.CM_CC_RevisedEnd);
                // excel.SetValue(13, 8, eProject.);
                excel.SetValue(13, 9, eProject.CM_CC_AcutalProgress);
                //第15行
                //excel.SetValue(14, 3, eProject.CM_CM_RevisedStart);
                //excel.SetValue(14, 4, eProject.);
                excel.SetValue(14, 6, eProject.CM_CM_RevisedStart);
                excel.SetValue(14, 7, eProject.CM_CM_RevisedEnd);
                // excel.SetValue(14, 8, "");
                excel.SetValue(14, 9, eProject.CM_CM_AcutalProgress);
                //第16行
                //excel.SetValue(15, 3, eProject.);
                excel.SetValue(15, 4, eProject.ProjectControl_MS_MC);
                //excel.SetValue(15, 6, eProject.);
                excel.SetValue(15, 7, eProject.CM_MA_MC);
                //excel.SetValue(15, 8, eProject.);
                // excel.SetValue(15, 9, eProject.);
                if (ss != null)
                {
                    string cbchj = "";
                    if (ss!=null)
                    {
                        //第19行
                        excel.SetValue(18, 0, ss.OrginalBudget);
                        excel.SetValue(18, 1, ss.ActualCost);
                        decimal? acs = ss.CommittedPRPO + ss.CommittedSSRs;
                        excel.SetValue(18, 2, acs);
                        excel.SetValue(19, 3, ss.CostToComplete);
                        decimal? cb = ss.ActualCost + ss.CommittedPRPO + ss.CommittedSSRs + ss.CostToComplete;
                        excel.SetValue(18, 4, cb);
                       cbchj = Convert.ToString(((cb - ss.OrginalBudget - ss.ChangedBudget) / (ss.OrginalBudget + ss.ChangedBudget) * 100)) + "%";
                    }
                   
                    excel.SetValue(18, 5, cbchj);
                }
                //第22行
                excel.SetValue(21, 0, eProject.PM_Remarks_Engineering);
                //第24行
                excel.SetValue(23, 0, eProject.PM_Remarks_Procurement);
                //第27行
                excel.SetValue(26, 0, eProject.CM_Remarks_Construction);
                //第29行
                excel.SetValue(28, 0, eProject.CM_Remarks_QualityHSE);
                //第31行
                //excel.SetValue(30, 0, eProject.ar);
                //第34行
                excel.SetValue(33, 0, eProject.ProjectControl_ProjectManager);
                //第36行
                excel.SetValue(35, 0, eProject.ProjectControl_ConstManager);
            }
            byte[] fs;
            fs = WriteToStream().ToArray();
            HttpContext.Current.Response.BinaryWrite(WriteToStream().GetBuffer());
            HttpContext.Current.Response.End();
        }
    }
}