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

using System;
using System.Web;
using System.Data;
using System.Drawing;
using MES_SPC;
using MES_SPC2;
public class QualityData_TrendPictureBasic : 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 "BindMachineType":
                responseText = BindMachineType();
                break;
            //绑定测量内容
            case "BindMeasurePartContent":
                responseText = BindMeasurePartContent();
                break;
            //绑定工位名称 
            case "BindOpName":
                responseText = BindOpName();
                break;
            //绑定测量内容
            case "BindMeasurePart":
                responseText = BindMeasurePart();
                break;
            case "GetBaseTrendData":
                responseText = GetBaseTrendData();
                break;
            case "GetDataTableByConditent":
                responseText = GetDataTableByConditent();
                break;
            case "GetMarkLineByConditent":
                responseText = GetMarkLineByConditent();
                break;
            case "outputData":
                responseText = OutData();
                break;
        }

        context.Response.Write(responseText);
    }
    public string OutData()
    {
        System.Data.DataTable dt_trend_baobiao = null;
        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 = HttpContext.Current.Request["endTime"];
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        bool EnginIDCheck = false;
        string EnginID = "";
        QualityData.SelectGoodDataReport_TrendPictureBasic_baobiao(EnginIDCheck, EnginID,
       OPNameCheck, OPName,
          EngineTypeIDCheck,
         EngineTypeID,
        shaftNameCheck, shaftName,
       itemNameCheck, itemName,
        startTime, endTime, out  dt_trend_baobiao);
        //string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_TrendPictureBasic_01.png");
        string path = "QualityData_TrendPictureBasic_01.png";
        if (dt_trend_baobiao != null)
        {
            if (dt_trend_baobiao.Rows.Count > 0)
            {
                CommonFunction.OutPutDataAndPic(path, dt_trend_baobiao);
            }
        }
        return "";
    }
    public string GetBaseTrendData()
    {
        string result = "";
        //bool OPNameCheck = Convert.ToBoolean(HttpContext.Current.Request["OPNameCheck"]);
        bool OPNameCheck = true;
        string OPName = HttpContext.Current.Request["OPName"];
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        //bool shaftNameCheck = Convert.ToBoolean(HttpContext.Current.Request["shaftNameCheck"]);
        bool shaftNameCheck = false;
        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 uuidCheck = HttpContext.Current.Request["uuidCheck"];
        string uuid = HttpContext.Current.Request["uuid"];
        DataTable dt = GetReport(OPNameCheck, OPName, shaftNameCheck, shaftName, itemNameCheck, itemName, startTime, endTime,EngineTypeIDCheck,EngineTypeID,uuidCheck,uuid);
        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;
    }
    public string GetDataTableByConditent()
    {
        DataTable dt = null;
        //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 = false;
        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"];
        //bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        bool EngineTypeIDCheck = false;
        string  EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        bool EnginIDCheck = false;
        //bool EnginIDCheck = true;
        string EnginID = "";
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["rows"]);
        int PageCurrent = Convert.ToInt32(HttpContext.Current.Request["page"]);
        int PageCount = 0;
        int ItemCount = 0;
        QualityData.GetQualityDataEveryOpNameDataListTable_Trend(startTime, endTime,
        EngineTypeIDCheck, EngineTypeID, EnginIDCheck, EnginID,
        OPNameCheck, OPName,
       shaftNameCheck, shaftName,
        itemNameCheck, itemName, 0,
        PageCurrent, PageSize, out  PageCount, out  ItemCount,
        out  dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            return JsonHelper.CreateJsonParameters(dt, true, ItemCount);
        }
        else
        {
            return "{\"result\":\"0\"}";
        }
    }


    public string GetMarkLineByConditent()
    {
        DataTable dt = null;

        //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 = false;
        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"];
        //bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        //bool EngineTypeIDCheck = false;
        //string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        bool EnginIDCheck = false;
        bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        string EngineTypeID = HttpContext.Current.Request["EngineTypeID"];
        string EnginID = "";
        string uuidCheck = HttpContext.Current.Request["uuidCheck"];
        string uuid = HttpContext.Current.Request["uuid"];
        int PageSize = Convert.ToInt32(HttpContext.Current.Request["rows"]);
        int PageCurrent = Convert.ToInt32(HttpContext.Current.Request["page"]);
        int PageCount = 0;
        int ItemCount = 0;
        QualityData2.GetQualityDataEveryOpNameDataListTable_Trend(startTime, endTime,
        EngineTypeIDCheck, EngineTypeID, EnginIDCheck, EnginID,
        OPNameCheck, OPName,
       shaftNameCheck, shaftName,
        itemNameCheck, itemName, 0,uuidCheck,uuid,
PageCurrent, PageSize, out  PageCount, out  ItemCount,
out  dt);

        // bool EngineTypeIDCheck = Convert.ToBoolean(HttpContext.Current.Request["EngineTypeIDCheck"]);
        DataTable table = GetReport(OPNameCheck, OPName, shaftNameCheck, shaftName, itemNameCheck, itemName, startTime, endTime,EngineTypeIDCheck,EngineTypeID,uuidCheck,uuid);
        double Usl = 0;
        double Lsl = 0;
        try
        {
            Usl = Convert.ToDouble(HttpContext.Current.Request["Usl"]); // 上限值 
            Lsl = Convert.ToDouble(HttpContext.Current.Request["Lsl"]); // 下限值 
        }
        catch
        {
            Usl = 0; // 上限值 
            Lsl = 0; // 下限值 
        }
        //if (dt != null && dt.Rows.Count > 0)
        //{
        //    try
        //    {
        //        Usl = Convert.ToDouble(dt.Rows[0]["上限值"]);
        //        Lsl = Convert.ToDouble(dt.Rows[0]["下限值"]);
        //    }
        //    catch
        //    {
        //        Usl = 0;
        //        Lsl = 0;
        //    }
        //}
        double x;
        double s;
        double QU;
        double QL;
        double cp;
        double cpk;
        double[] sx;
        double[] sy;

        double[] X;
        if (table.Rows.Count < 3)
        {
            return "";
        }
        X = new double[table.Rows.Count];
        for (int i = 0; i < table.Rows.Count; i++)
        {
            X[i] = Convert.ToDouble(table.Rows[i][0]);
        }

        double ValueUp = Max(X);
        double ValueDown = Min(X);
        //计算SPC参数
        SpcCaculator.SpcValue(X, ref Usl, ref Lsl, out  x, out  s, out  QU, out  QL, out  cp, out  cpk, out  sx, out  sy);

        //坐标轴
        double ValueUpX;
        double ValueDownX;

        ValueDownX = Math.Min(ValueDown, Lsl);
        ValueDownX = Math.Min(ValueDownX, QL);

        ValueUpX = Math.Max(ValueUp, Usl);
        ValueUpX = Math.Max(ValueUpX, QU);

        double X1 = (ValueUpX - ValueDownX) / 10;
        double minValue = Math.Round(ValueDownX - X1, 4);
        double maxValue = Math.Round(ValueUpX + X1, 4);
        string Yvalue =minValue.ToString()+",";
        for (int i = 1; i < 5; i++)
        {
            Yvalue += (i * (double)(maxValue - minValue) / 5+minValue).ToString()+",";
        }
        Yvalue += maxValue.ToString();
        string Text = Usl + "," + QU + "," + sx[5].ToString("F4") + "," + QL + "," + Lsl + "," + cpk + "," + cp + "|" + Yvalue;
        return Text;
    }
    /// <summary>
    /// 观测值最大值
    /// </summary>
    /// <param name="Xn">子组观测值</param>
    /// <returns></returns>
    static private double Max(double[] Xn)
    {
        double temp;
        temp = Xn[0];
        for (int i = 0; i < Xn.Length; i++)
        {
            temp = (Xn[i] > temp) ? Xn[i] : temp;
        }
        return temp;
    }

    /// <summary>
    /// 观测值最小值
    /// </summary>
    /// <param name="Xn">子组观测值</param>
    /// <returns></returns>
    static private double Min(double[] Xn)
    {
        double temp;
        temp = Xn[0];
        for (int i = 0; i < Xn.Length; i++)
        {
            temp = (Xn[i] < temp) ? Xn[i] : temp;
        }
        return temp;
    }

    /// <summary>
    /// 符合条件的全部数据
    /// </summary>
    /// <returns></returns>
    private DataTable GetReport(bool OPNameCheck, string OPName, bool shaftNameCheck, string shaftName, bool itemNameCheck,string itemName, string startTime, string endTime,bool  EngineTypeIDCheck,string EngineTypeID,string uuidCheck,string uuid)
    {
        DataTable dt;
        bool EnginIDCheck;
        string EnginID;
        EnginIDCheck = EngineTypeIDCheck;
        EnginID = EngineTypeID;
        QualityData2.SelectGoodDataReport_TrendPictureBasic(EnginIDCheck, EnginID,
        OPNameCheck, OPName,
         shaftNameCheck, shaftName,
        itemNameCheck, itemName,
         startTime, endTime,uuidCheck,uuid, out  dt);
        return dt;
    }
    public string BindMachineType()
    {
        string text = "";
        DataTable dt;
        MES_SPC.CraftMana_step.enginetypeSelect(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;
    }

    /// <summary>
    /// 绑定工位名称
    /// </summary>
    /// <returns></returns>
    public string BindOpName()
    {
        string text = "";
        System.Data.DataTable dt;
        MES_SPC.QualityData.GetOpName(out dt);
        if (dt != null && dt.Rows.Count > 0)
        {
            text = JsonHelper.DataTableToJson(dt);
        }
        return text;
    }
    /// <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;
    }

    public string SaveImg()
    {

        // string path = "c:\\QualityData_TrendPictureBasic_01.png";
        string path = HttpContext.Current.Server.MapPath("~/Chart_excel/QualityData_TrendPictureBasic_01.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);

        // CommonFunction.ExcelTable(dt_trend_baobiao);
        //
        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,true);
        return bmp;
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}