using com.hnshituo.core.webapp.vo;
using Common;
using Common.vo.pb;
using Infragistics.Win;
using Infragistics.Win.UltraWinEditors;
using Infragistics.Win.UltraWinGrid;
using JC_MeasuringSystem;
using MeterModuleLibrary;
using MeterPlugInLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows.Forms;

namespace BeltScaleSystem

{
    public partial class frmGraphInfo : Form
    {
        private MeterWorkBeltTimingService meterWorkBeltTimingService = new MeterWorkBeltTimingService();
        private StrapSeries _series1 = new StrapSeries();
        private StrapSeries _series2 = new StrapSeries();
        private const int _maxPointCount = 300;
        private DataTable dtTmingTable;//实时量数据源
        private DataTable dtTmingTable2;//实时量数据源
        private DataTable dtPoint;//计量点数据源
        private Log l = Log.GetInstance(); //日志
        public frmGraphInfo()
        {
            InitializeComponent();
        }
        //菜单栏事件
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "查询":
                    QueryScalePrediction();
                    break;
                
                
            }
        }


        private void frmGraphInfo_Load(object sender, EventArgs e)
        {

            dtStartTime.Value = Convert.ToDateTime((DateTime.Now.ToString("yyyy-MM-dd") + " 00:00:00"));
            dtEndTime.Value = Convert.ToDateTime((DateTime.Now.ToString("yyyy-MM-dd") + " 23:59:59"));
            MeterBaseSpotInfoService meterBaseSpotInfoService = new MeterBaseSpotInfoService();//计量点信息
            RESTfulResult<List<MeterBaseSpotInfo>> spo = meterBaseSpotInfoService.doQueryWf(new MeterBaseSpotInfo { validFlag = "1", spotTypeNo = "001002005" });
            if (spo.Succeed && spo.Data != null && spo.Data.Count > 0)
            {
                dtPoint = spo.Data.ListToDataTable<MeterBaseSpotInfo>();
            }
            if (dtPoint != null)
            {
                cbPntName_1.DataSource = dtPoint;
                cbPntName_1.DisplayMember = "baseSpotName";
                cbPntName_1.ValueMember = "baseSpotNo";
            }
            cbPntName_1.SelectedIndex = -1;
            DataTable dt3 = new DataTable();
            dt3.Columns.Add("TEXT");
            dt3.Columns.Add("VALUE");
            dt3.Rows.Add("0", "全部");
            dt3.Rows.Add("1", "A秤");
            dt3.Rows.Add("2", "B秤");
            valueToCombo(dt3, cmbStateName);
            this.cmbStateName.SelectedIndex = 0;

            ultraChart1.Series.Clear();
            ultraChart2.Series.Clear();
            ultraChart1.Series.Add(_series1);
            ultraChart2.Series.Add(_series2);
            StrapDataPoint point = new StrapDataPoint();
            point.NumericValue = 0;
            point.TimeValue = DateTime.Now.AddSeconds(-_maxPointCount);
            _series1.Points.Add(point);
            _series2.Points.Add(point);
            ultraChart1.Tooltips.Format = Infragistics.UltraChart.Shared.Styles.TooltipStyle.LabelPlusDataValue;
            ultraChart2.Tooltips.Format = Infragistics.UltraChart.Shared.Styles.TooltipStyle.LabelPlusDataValue;
        }
        //下拉框控件自定义option
        public static void valueToCombo(DataTable source, UltraComboEditor cbb)
        {
            cbb.DataSource = source;
            cbb.DisplayMember = "VALUE";
            cbb.ValueMember = "TEXT";
        }

        //实绩查询
        private void  QueryScalePrediction() {
            try
            {
                if (string.IsNullOrEmpty(this.cbPntName_1.Text.Trim().ToString()))
                {
                    MessageBox.Show("请选择计量点!");
                    this.cbPntName_1.Focus();
                    return;
                }
                if (string.IsNullOrEmpty(this.cmbStateName.Text.Trim().ToString()))
                {
                    MessageBox.Show("请选择AB秤!");
                    this.cmbStateName.Focus();
                    return;
                }
                string substract = (dtStartTime.Value.Date.Day - dtEndTime.Value.Date.Day).ToString();
                if (Convert.ToInt32(substract) > 1)
                {
                    MessageBox.Show("查询天数不能超过1天！");
                    return;
                }
                string scaleName = "";
                if (cmbStateName.Text != "全部" && cmbStateName.Text != "")
                {
                    scaleName = cmbStateName.Text.ToString().Substring(0, 1);
                    RESTfulResult<List<MeterWorkBeltTiminginfo>> rms = findRealTiming(scaleName);
                    if (rms.Succeed)
                    {
                        if (rms.Data != null && rms.Data.Count > 0)
                        {
                            dtTmingTable = rms.Data.ListToDataTable<MeterWorkBeltTiminginfo>();
                            if (dtTmingTable == null || dtTmingTable.Rows.Count <= 0)
                            {
                                MessageBox.Show("没有数据！");
                                return;
                            }
                            if (scaleName == "A")
                            {
                                ultraChart1.Series.Clear();
                                _series1.Points.Clear();
                            }
                            else
                            {
                                ultraChart2.Series.Clear();
                                _series2.Points.Clear();
                            }
                            for (int i = 0; i < dtTmingTable.Rows.Count; i++)
                            {
                                StrapDataPoint point = new StrapDataPoint();
                                point.TimeValue = Convert.ToDateTime(dtTmingTable.Rows[i]["createTime"]);
                                point.NumericValue = Convert.ToDouble(dtTmingTable.Rows[i]["realtimeAmount"]);
                                point.Label = "时间:" + point.TimeValue.ToString("MM-dd HH:mm:ss") + "\r\n流量";
                                if (scaleName == "A")
                                {
                                    _series1.Points.Add(point);
                                }
                                else if (scaleName == "B")
                                {
                                    _series2.Points.Add(point);
                                }

                            }
                            if (scaleName == "A")
                            {
                                ultraChart1.Series.Add(_series1);
                                if (_series1.Points.Count > _maxPointCount)
                                {
                                    _series1.Points.RemoveAt(0);
                                }
                            }
                            else if (scaleName == "B")
                            {
                                ultraChart2.Series.Add(_series2);
                                if (_series2.Points.Count > _maxPointCount)
                                {
                                    _series2.Points.RemoveAt(0);
                                }
                            }

                        }
                        else
                        {
                            MessageBox.Show(rms.Message);
                            if (dtTmingTable != null)
                                dtTmingTable.Clear();
                            return;
                        }
                    }
                }
                else if(cmbStateName.Text == "全部")
                {
                    RESTfulResult<List<MeterWorkBeltTiminginfo>> rms = findRealTiming("A");
                    RESTfulResult<List<MeterWorkBeltTiminginfo>> rms2 = findRealTiming("B");
                    if (rms.Succeed&&rms2.Succeed)
                    {
                        if (rms.Data != null && rms.Data.Count > 0&& rms2.Data != null && rms2.Data.Count > 0)
                        {
                            dtTmingTable = rms.Data.ListToDataTable<MeterWorkBeltTiminginfo>();
                            dtTmingTable2 = rms.Data.ListToDataTable<MeterWorkBeltTiminginfo>();
                            if (dtTmingTable == null || dtTmingTable.Rows.Count <= 0&& dtTmingTable2 == null || dtTmingTable2.Rows.Count <= 0)
                            {
                                MessageBox.Show("没有数据！");
                                return;
                            }
                            ultraChart1.Series.Clear();
                            _series1.Points.Clear();
                            ultraChart2.Series.Clear();
                            _series2.Points.Clear();
                            for (int i = 0; i < dtTmingTable.Rows.Count; i++)
                            {
                                StrapDataPoint point = new StrapDataPoint();
                                point.TimeValue = Convert.ToDateTime(dtTmingTable.Rows[i]["createTime"]);
                                point.NumericValue = Convert.ToDouble(dtTmingTable.Rows[i]["realtimeAmount"]);
                                point.Label = "时间:" + point.TimeValue.ToString("MM-dd HH:mm:ss") + "\r\n流量";
                                _series1.Points.Add(point);

                            }
                            for (int i = 0; i < dtTmingTable2.Rows.Count; i++)
                            {
                                StrapDataPoint point = new StrapDataPoint();
                                point.TimeValue = Convert.ToDateTime(dtTmingTable.Rows[i]["createTime"]);
                                point.NumericValue = Convert.ToDouble(dtTmingTable.Rows[i]["realtimeAmount"]);
                                point.Label = "时间:" + point.TimeValue.ToString("MM-dd HH:mm:ss") + "\r\n流量";
                                _series2.Points.Add(point);

                            }
                            ultraChart1.Series.Add(_series1);
                            if (_series1.Points.Count > _maxPointCount)
                            {
                                _series1.Points.RemoveAt(0);
                            }

                            ultraChart2.Series.Add(_series2);
                            if (_series2.Points.Count > _maxPointCount)
                            {
                                _series2.Points.RemoveAt(0);
                            }

                        }
                        else
                        {
                            MessageBox.Show("没有数据");
                            if(dtTmingTable!=null)dtTmingTable.Clear();
                            if(dtTmingTable2!=null)dtTmingTable2.Clear();
                            return;
                        }
                    }
                }


               
            }
            catch (Exception ex )
            {
                l.WriteLog("皮带秤计量异常_", "查询错误" + ex.Message);
                MessageBox.Show("查询错误"+ex.Message);
                return;
            }
        }

        private RESTfulResult<List<MeterWorkBeltTiminginfo>> findRealTiming(string ScaleName) {
            CommonPage<MeterWorkBeltTiminginfo> cp = new CommonPage<MeterWorkBeltTiminginfo>();
            MeterWorkBeltTiminginfo timinginfo = new MeterWorkBeltTiminginfo();
            cp.startTime = dtStartTime.Text.ToString();
            cp.endTime = dtEndTime.Text.ToString();
            timinginfo.scaleName = ScaleName;
            if (cbPntName_1.Text.ToString() != "")
            {
                timinginfo.baseSpotNo = cbPntName_1.Value.ToString();
            }
            timinginfo.valueFlag = "1";
            timinginfo.weightType = "1";
            cp.param = timinginfo;
            return meterWorkBeltTimingService.doQueryWf(cp);
        }

    }
}