<%@ WebHandler Language="C#" Class="MESCommonBase02" %>

using System;
using System.Web;
using System.IO;
using System.Web.Script.Serialization;
using BasicData;
using DataLinkMesWork;

public class MESCommonBase02 : IHttpHandler
{

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void ProcessRequest(HttpContext context)
    {
       
        string connType = "01";

        context.Response.ContentType = "application/json";
        var data = context.Request;
        var stream = new StreamReader(data.InputStream).ReadToEnd();
        string InvoiceNum;
        //JsonData jsonData;
        string responseText = "NULL";
        byte[] bytes = null;
        string fileName = "test.xsl";

        try
        {
            JsonData jsonData=null;
            try
            {
                jsonData = JsonMapper.ToObject(stream);

            }
            catch(Exception err)
            { }
            int type=0;
            try
            {
                if(jsonData!=null)
                {
                    if(jsonData.ContainsKey("Type"))
                    {
                        type =  Int32.Parse( jsonData["Type"].ToString());
                    }else if(jsonData.ContainsKey("type"))
                    {
                        type =  Int32.Parse( jsonData["type"].ToString());
                    }
                }
            }
            catch
            {

            }
            jsonobj dataobj=null;
            try
            {
                dataobj = new JavaScriptSerializer().Deserialize<jsonobj>(stream);
                if(dataobj!=null)
                {
                    type =  Int32.Parse(dataobj.Type);
                }

            }
            catch(Exception err)
            {
                dataobj = null;
            }

            if(dataobj==null)
            {
                try
                {
                    InvoiceNum = HttpContext.Current.Request["param"];
                    if(InvoiceNum!=null)
                    {
                        jsonData = JsonMapper.ToObject(InvoiceNum);
                        if(jsonData.ContainsKey("Type"))
                        {
                            type =  Int32.Parse( jsonData["Type"].ToString());
                        }else if(jsonData.ContainsKey("type"))
                        {
                            type =  Int32.Parse( jsonData["type"].ToString());
                        }
                    }
                }
                catch
                {
                    jsonData = null;
                }

            }
            string fileExtension="";
            HttpFileCollection files;
            string suffix;
            string name;
            switch(type)
            {
                case 2001:

                    //DataLink.LogJsonData(jsonData);

                    MESDownloadExcel.ExcelWebCall.ExcelFile(jsonData,out bytes,out fileName,ref fileExtension);
                    fileName = fileName + "."+fileExtension;

                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    //HttpContext.Current.Response.ContentType = getContentType(extension);  
                    //通知浏览器下载文件而不是打开  
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
                    HttpContext.Current.Response.BinaryWrite(bytes);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                    break;
                case 2002:
                    //DataLink.LogJsonData(jsonData);

                    MESDownloadExcel.ExcelWebCall.ExcelFilePdf(jsonData,out bytes,out fileName,out fileExtension);
                    fileName = fileName + "."+fileExtension;

                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    //HttpContext.Current.Response.ContentType = getContentType(extension);  
                    //通知浏览器下载文件而不是打开  
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
                    HttpContext.Current.Response.BinaryWrite(bytes);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();

                    break;

                case 2003:// 合成excel图片，下载
                    string[] dataimg;
                    if(context.Request.Form.Keys.Count>0)
                    {
                        dataimg = data.Form.GetValues(0);
                        MESDownloadExcel.ExcelWebCall.ExcelFile(jsonData,out bytes,out fileName,ref dataimg[0]);
                        if (bytes == null) return;
                        fileName = fileName + "."+dataimg[0];

                        HttpContext.Current.Response.ContentType = "application/octet-stream";
                        //HttpContext.Current.Response.ContentType = getContentType(extension);  
                        //通知浏览器下载文件而不是打开  
                        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                        HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
                        HttpContext.Current.Response.BinaryWrite(bytes);
                        HttpContext.Current.Response.Flush();
                        HttpContext.Current.Response.End();
                        //if(DataLinkMesWork.DataLink.isBakup=="1")
                        //{
                        //    DataLinkMesWork.DataLink.ExePROCEDURE_Type16_Bakup(jsonData, out bytes, out fileName, out suffix);
                        //}
                    }
                    break;
                case 2004:

                    //DataLink.LogJsonData(jsonData);
                    DataLinkMesWork.DataLink.ExePROCEDURE_Type2004(connType,jsonData, out bytes, out fileName, out fileExtension);
                    //MESDownloadExcel.ExcelWebCall.ExcelFile(jsonData,out bytes,out fileName,ref fileExtension);
                    fileName = fileName + "."+fileExtension;

                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    //HttpContext.Current.Response.ContentType = getContentType(extension);  
                    //通知浏览器下载文件而不是打开  
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
                    HttpContext.Current.Response.BinaryWrite(bytes);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                    break;

                case 15://上传文件
                    files = context.Request.Files;
                    if (files.Count > 0)
                    {

                        fileName = files[0].FileName;
                        bytes = new byte[files[0].InputStream.Length];
                        files[0].InputStream.Read(bytes, 0, bytes.Length);

                        suffix = fileName.Substring(fileName.LastIndexOf(".")+1);
                        name = fileName.Substring(0,fileName.LastIndexOf("."));

                        responseText = DataLinkMesWork.DataLink.ExePROCEDURE_Type15(connType,jsonData, name, suffix, bytes);
                        context.Response.Write(responseText);
                        //if(DataLinkMesWork.DataLink.isBakup=="1")
                        //{
                        //    DataLinkMesWork.DataLink.ExePROCEDURE_Type15_Bakup(jsonData, name, suffix, bytes);
                        //}
                    }
                    else
                    {
                        fileName = "";
                        bytes = new byte[1];
                        suffix = "";
                        name = "";
                        responseText = DataLinkMesWork.DataLink.ExePROCEDURE_Type15(connType,jsonData, name, suffix, bytes);
                        context.Response.Write(responseText);
                        //if(DataLinkMesWork.DataLink.isBakup=="1")
                        //{
                        //    DataLinkMesWork.DataLink.ExePROCEDURE_Type15_Bakup(jsonData, name, suffix, bytes);
                        //}
                    }

                    break;
                case 16://上传文件
                    DataLink.LogJsonData(jsonData);
                    DataLinkMesWork.DataLink.ExePROCEDURE_Type16(connType,jsonData, out bytes, out fileName, out suffix);
                    if (bytes == null) return;
                    fileName = fileName + "."+suffix;

                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    //HttpContext.Current.Response.ContentType = getContentType(extension);  
                    //通知浏览器下载文件而不是打开  
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
                    HttpContext.Current.Response.AddHeader("Access-Control-Expose-Headers", "Content-Disposition");
                    HttpContext.Current.Response.BinaryWrite(bytes);
                    HttpContext.Current.Response.Flush();
                    HttpContext.Current.Response.End();
                    //if(DataLinkMesWork.DataLink.isBakup=="1")
                    //{
                    //    DataLinkMesWork.DataLink.ExePROCEDURE_Type16_Bakup(jsonData, out bytes, out fileName, out suffix);
                    //}
                    break;
                default:

                    //DataLink.LogJsonData(jsonData);

                    responseText = DataLinkMesWork.DataLink.SqlWebCall(connType,type,jsonData,dataobj);//.SqlWebCall(stream);

                    context.Response.Write(responseText);

                    //if(DataLinkMesWork.DataLink.isBakup=="1")
                    //{
                    //    DataLinkMesWork.DataLink.SqlWebCall_Bakup(type,jsonData,dataobj);
                    //}
                    break;
            }



        }
        catch(Exception err)
        {
            context.Response.Write(responseText);
        }
        }


}