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

using System;
using System.Web;
using System.Data;
using MES_SPC;

public class QualityData_TrendPicture : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string type = HttpContext.Current.Request["type"];
        string responseText = "";
        switch (type)
        {
            case "SaveImg":
                responseText = SaveImg();
                break;
            case "BindMinutes":
                responseText = BindMinutes();
                break;
            case "BindHour":
                responseText = BindHour();
                break;
            case "BindMachineType":
                responseText = BindMachineType();
                break;
            case "BindOPName":
                responseText = BindOPName();
                break;
            //绑定测量内容
            case "BindMeasurePartContent":
                responseText = BindMeasurePartContent();
                break;
            //绑定测量内容
            case "BindMeasurePart":
                responseText = BindMeasurePart();
                break;
            case "GetBaseTrendData":
                responseText = GetBaseTrendData();
                break;
            case "GetDataTableByConditent":
                responseText = GetDataTableByConditent();
                break;
            case "OutPutData":
                responseText = OutPutData();
                break;
        }
        context.Response.Write(responseText);

    }
    public string OutPutData()
    {
        DataTable dt = null;
        bool EnginIDCheck = false;
        string EnginID = "";
        bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        string OPName = HttpContext.Current.Request["OPName"];
        bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        string shaftName = HttpContext.Current.Request["shaftName"];
        bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
        string itemName = HttpContext.Current.Request["itemName"];
        string startTime = HttpContext.Current.Request["startTime"];
        string endTime = null;
        string hour = HttpContext.Current.Request["Hour"];
        string minutes = HttpContext.Current.Request["Minutes"];
        startTime = startTime + " " + hour + ":" + minutes + ":" + "00";
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"]; ;
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
          OPNameCheck, OPName,
           EngineTypeID, EngineTypeIDCheck,
           shaftNameCheck, shaftName,
          itemNameCheck, itemName,
           startTime, endTime, n, k, out  dt);
        //string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_TrendPicture.png");
        string path = "QualityData_TrendPicture.png";
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                // dt.TableName = "SPC分析排列图";

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    dt.Rows[i]["序号"] = (i + 1).ToString("000000");
                }

                CommonFunction.OutPutDataAndPic(path, dt);
                if (System.IO.File.Exists(path))
                {
                    System.IO.File.Delete(path);
                }
            }
        }
        return "";
    }

    public string GetDataTableByConditent()
    {
        DataTable dt = null;
        DataTable dt1 = null;

        int pageSize = Convert.ToInt32(HttpContext.Current.Request["page"]);//请求的当前页码
        int pageList = Convert.ToInt32(HttpContext.Current.Request["rows"]);//请求一页显示的数据条数。

        bool EnginIDCheck = false;
        string EnginID = "";
        //bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        bool OPNameCheck = true;
        string OPName = HttpContext.Current.Request["OPName"];
        //bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        bool shaftNameCheck = true;
        string shaftName = HttpContext.Current.Request["shaftName"];
        //bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
        bool itemNameCheck = true;
        string itemName = HttpContext.Current.Request["itemName"];
        string startTime = HttpContext.Current.Request["startTime"];
        string endTime = HttpContext.Current.Request["endTime"];
        string hour = HttpContext.Current.Request["Hour"];
        string minutes = HttpContext.Current.Request["Minutes"];
        //startTime = startTime + " " + hour + ":" + minutes + ":" + "00";
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"]; ;
        //bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        bool EngineTypeIDCheck = true;
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EnginID,
         OPNameCheck, OPName,
            EngineTypeID,
          EngineTypeIDCheck,
          shaftNameCheck, shaftName,
         itemNameCheck, itemName,
          startTime, endTime, n, k, out  dt);

        dt1 = dt.Copy(); //复制dt
        dt1.Rows.Clear();//清空内容，只保留dt结构。
        for (int i = (pageSize - 1) * pageList; i < pageSize * pageList; i++)//循环需要显示的行
        {
            if (i < dt.Rows.Count)
            {
                dt1.ImportRow(dt.Rows[i]);//这是加入的是第i行
            }
        }

        string text = "";
        if (dt1 != null && dt1.Rows.Count > 0)
        {
            text = JsonHelper.CreateJsonParameters(dt1, true, dt.Rows.Count);
        }
        return text;
    }


        /// <summary>
        /// ///////////////////////////////////////////////
        /// </summary>









    double[] X;
    public string GetBaseTrendData()
    {
        DataTable dt = null;
        string result = "";
        bool EnginIDCheck = false;
        string EnginID = "";
        //bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        bool OPNameCheck = true;
        string OPName = HttpContext.Current.Request["OPName"];
        bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        string shaftName = HttpContext.Current.Request["shaftName"];//测量位置
        bool itemNameCheck = Convert.ToBoolean(HttpContext.Current.Request["itemNameCheck"]);
        string itemName = HttpContext.Current.Request["itemName"];
        string startTime = HttpContext.Current.Request["startTime"];
        string endTime = HttpContext.Current.Request["endTime"];
        //string endTime = null;
        string hour = HttpContext.Current.Request["Hour"];
        string minutes = HttpContext.Current.Request["Minutes"];
        //startTime = startTime + " " + hour + ":" + minutes + ":" + "00";
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"]; ;
        //bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        bool EngineTypeIDCheck = true;
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        QualityData.SelectGoodDataReport_DataGroup_TrendPictureBasic(EnginIDCheck, EnginID,
                                  OPNameCheck, OPName,
                                   EngineTypeID,
                                    EngineTypeIDCheck,
                                   shaftNameCheck, shaftName,
                                  itemNameCheck, itemName,
                                   startTime, endTime, n, k, out  dt);

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                result += dt.Rows[i]["测量值"];
                if (i != dt.Rows.Count - 1)
                {
                    result += ",";
                }
            }
        }
        return result;
    }

    /// <summary>
    /// 绑定测量名称
    /// </summary>
    /// <returns></returns>
    public string BindMeasurePart()
    {
        string text = "";
        DataTable dt;
        string opName;
        opName = HttpContext.Current.Request["opName"];
        QualityData.GetShaftName(opName, out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    /// <summary>
    /// 绑定测量内容
    /// </summary>
    /// <returns></returns>
    public string BindMeasurePartContent()
    {
        string text = "";
        string opName;
        string shaftName;
        opName = HttpContext.Current.Request["opName"];
        shaftName = HttpContext.Current.Request["shaftName"];
        DataTable dt;
        QualityData.GetItemName(opName, shaftName, out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    //绑定工位名称
    public string BindOPName()
    {
        string text = "";
        DataTable dt;
        QualityData.GetOpName(out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    //绑定机型
    public string BindMachineType()
    {
        string text = "";
        DataTable dt;
        CraftMana_step.enginetypeSelect(out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    //控件绑定分钟
    public string BindMinutes()
    {
        string text = "";
        DataTable dt = new DataTable();
        dt.Columns.Add("分钟", typeof(string));
        for (int i = 0; i < 60; i++)
        {
            DataRow dr = dt.NewRow();
            dr["分钟"] = i.ToString("00");
            dt.Rows.Add(dr);
        }
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }

    //控件绑定小时
    public string BindHour()
    {
        string text = "";
        DataTable dt = new DataTable();
        dt.Columns.Add("小时", typeof(string));
        for (int i = 0; i < 24; i++)
        {
            DataRow dr = dt.NewRow();
            dr["小时"] = i;
            dt.Rows.Add(dr);
        }
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    public string SaveImg()
    {
        //string path = "c:\\QualityData_TrendPictureBasic_01.png";
        string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_TrendPicture.png");
        string byteStr = HttpContext.Current.Request["ImageDate"].Replace("data:image/png;base64,", "");
        System.Drawing.Image returnImage = Base64StringToImage(byteStr);
        if (System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
        }
        returnImage.Save(path, System.Drawing.Imaging.ImageFormat.Png);
        return "{\"result\":\"1\"}"; ;
    }
    private System.Drawing.Image Base64StringToImage(string txt)
    {
        byte[] arr = Convert.FromBase64String(txt);
        System.IO.MemoryStream ms = new System.IO.MemoryStream(arr);
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
        return bmp;
    }
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}