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

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



public class QualityData_Histogram : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string type = HttpContext.Current.Request["type"];
        string responseText = "";
        switch (type)
        {
            case "OutExcel":
                responseText = OutExcel();
                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 "data":
                responseText = data();
                break;
            case "data1":
                responseText = data1();
                break;
            case "SaveImg":
                responseText = SaveImg();
                break;
        }



        context.Response.Write(responseText);
    }

    public string SaveImg()
    {
        //string path = "c:\\QualityData_Histogram_01.png";
        string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_Histogram.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\"}";
    }




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

        int pageSize = Convert.ToInt32(HttpContext.Current.Request["page"]);//请求的当前页码
        int pageList = Convert.ToInt32(HttpContext.Current.Request["rows"]);//请求一页显示的数据条数。
        bool EngineTypeIDCheck =  Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        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 Hour = HttpContext.Current.Request["Hour"];
        //string Minutes = HttpContext.Current.Request["Minutes"];
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        //startTime = startTime + " " + Hour + ":" + Minutes + ":" + "00";
        string endTime = HttpContext.Current.Request["endTime"];;
        bool EnginIDCheck = false;
        string EngineID = null;
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EngineID,
        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;

    }

    public string OutExcel()
    {
        System.Data.DataTable dt = null;

        bool EngineTypeIDCheck = true;
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        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 Hour = HttpContext.Current.Request["Hour"];
        string Minutes = HttpContext.Current.Request["Minutes"];
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        startTime = startTime + " " + Hour + ":" + Minutes + ":" + "00";
        string endTime = null;
        bool EnginIDCheck = false;
        string EngineID = null;
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EngineID,
        OPNameCheck, OPName,
         EngineTypeID, EngineTypeIDCheck,
         shaftNameCheck, shaftName,
        itemNameCheck, itemName,
         startTime, endTime, n, k, out  dt);
        //string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_Histogram.png");
        string path ="QualityData_Histogram.png";
        if (dt != null)
        {
            if (dt.Rows.Count > 0)
            {
                dt.TableName = "测量值";

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

                }

                CommonFunction.OutPutDataAndPic(path, dt);
            }
        }
        return "";

    }

    public string data()
    {
        System.Data.DataTable dt = null;
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        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 Hour = HttpContext.Current.Request["Hour"];
        //string Minutes = HttpContext.Current.Request["Minutes"];
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        //startTime = startTime + " " + Hour + ":" + Minutes + ":" + "00";
        string endTime =HttpContext.Current.Request["endTime"];
        bool EnginIDCheck = false;
        string EngineID = null;
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EngineID,
        OPNameCheck, OPName,
         EngineTypeID, EngineTypeIDCheck,
         shaftNameCheck, shaftName,
        itemNameCheck, itemName,
         startTime, endTime, n, k, out  dt);
        double max = 0;
        double min = 0;
        double c = Convert.ToDouble(dt.Rows[0]["测量值"]);
        double e = Convert.ToDouble(dt.Rows[0]["测量值"]);
        for (int i = 1; i < dt.Rows.Count; i++)
        {
            double a = Convert.ToDouble(dt.Rows[i]["测量值"]);
            if (c > a)
            {
                max = c;
            }
            else
            {
                c = a;
                max = a;
            }
        }
        for (int r = 1; r < dt.Rows.Count; r++)
        {
            double d = Convert.ToDouble(dt.Rows[r]["测量值"]);
            if (e < d)
            {
                min = e;
            }
            else
            {
                e = d;
                min = d;
            }
        }
        string max1 = max.ToString("f2");

        int t = (int)(min * 100);
        float min2 = (float)(t * 1.0) / 100;
        string min1 = min2.ToString();

       double mid = Math.Round((max - min) / n, 3);
        string mid2 = string.Format("{0:N3}", mid);
        string mid3 = string.Format("{0:N2}", mid);
        double mid4 = Convert.ToDouble(mid2) * 1000 - Convert.ToDouble(mid3) * 1000;
        float o2;
        float n1 = (float)n;

        int o = (int)(mid * 1000);
        o++;
        float o3 = (float)(o * 1.0) / 1000;
        string last = min2.ToString() + "~" + (min2 + o3).ToString();

        //int mid1 = Convert.ToInt32((Convert.ToDouble(max1) - Convert.ToDouble(min1)) / 0.2);

        if (mid != 0)
        {
            o2 = o3;
            last = min2.ToString() + "~" + (min2 + o3).ToString();
            for (float i = (min2 + o2);i+0.1  < (min2 + o2 * n1);i = i + o2)
            {

                string i1 = i.ToString("f2");
                float l = i + o2;
                string i2 = l.ToString("f2");
                //i1 = i1.Substring(0, i1.IndexOf(".", 0) + 2);
                //i1 = i2.Substring(0, i2.IndexOf(".", 0) + 2);
                last = last + "," + i1 + "~" + i2;
            }
        }
        return last;




    }
    public string data1()
    {
        System.Data.DataTable dt = null;
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        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 Hour = HttpContext.Current.Request["Hour"];
        //string Minutes = HttpContext.Current.Request["Minutes"];
        int n = Convert.ToInt32(HttpContext.Current.Request["Yangben"]);
        int k = Convert.ToInt32(HttpContext.Current.Request["Num"]);
        //startTime = startTime + " " + Hour + ":" + Minutes + ":" + "00";
        string endTime =  HttpContext.Current.Request["endTime"];;
        bool EnginIDCheck = false;
        string EngineID = null;
        QualityData.SelectGoodDataReport_DataGroup(EnginIDCheck, EngineID,
        OPNameCheck, OPName,
         EngineTypeID, EngineTypeIDCheck,
         shaftNameCheck, shaftName,
        itemNameCheck, itemName,
         startTime, endTime, n, k, out  dt);
        double max = 0;
        double min = 0;
        double c = Convert.ToDouble(dt.Rows[0]["测量值"]);
        double e = Convert.ToDouble(dt.Rows[0]["测量值"]);
        for (int i = 1; i < dt.Rows.Count; i++)
        {
            double a = Convert.ToDouble(dt.Rows[i]["测量值"]);
            if (c > a)
            {
                max = c;
            }
            else
            {
                c = a;
                max = a;
            }
        }
        for (int r = 1; r < dt.Rows.Count; r++)
        {
            double d = Convert.ToDouble(dt.Rows[r]["测量值"]);
            if (e < d)
            {
                min = e;
            }
            else
            {
                e = d;
                min = d;
            }
        }


        string max1 = max.ToString("f2");

        int t = (int)(min * 100);
        float min2 = (float)(t * 1.0) / 100;
        string min1 = min2.ToString();

        double mid = Math.Round((max - min) / n, 3);
        string mid2 = string.Format("{0:N3}", mid);
        string mid3 = string.Format("{0:N2}", mid);
        double mid4 = Convert.ToDouble(mid2) * 1000 - Convert.ToDouble(mid3) * 1000;

        float n1 = (float)n;

        int[] arr1 = new int[n];
        int o = (int)(mid * 1000);
        o++;
        float o3 = (float)(o * 1.0) / 1000;


        for(int i = 0; i < n; i++){
            for (int r = 0; r < dt.Rows.Count; r++)
            {
                double dd = Convert.ToDouble(dt.Rows[r]["测量值"]);
                if (dd >= (min2 + o3 * (i)) && dd < (min2 + o3 * (i + 1)))
                {
                    arr1[i]++;
                }
            }

        }
        string str = arr1[0].ToString();
        for (int d = 1; d < n; d++) {
            string str1 = arr1[d].ToString();
            str = str + "," + str1;
        }
        return str;
    }



    /// <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;
    }

    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;
        }
    }

}