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 Microsoft.VisualBasic;
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 frmBeltOperationInfo : Form
    {
        public frmBeltOperationInfo()
        {
            InitializeComponent();
        }
        private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
        private MeterWorkBeltTimingService meterWorkBeltTimingService = new MeterWorkBeltTimingService();
        private DataTable dtPoint;//计量点数据源
        private MeterBaseScalePointService monitorService = new MeterBaseScalePointService();
        //菜单栏事件
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "查询":
                    QueryScalePrediction();
                    break;
                
                case "导出":
                    ExportExcel();
                    break;
                
            }
        }



        //查询
        private void  QueryScalePrediction() {
            try
            {
                DataTable dtTmingTable = null;
                CommonPage<MeterWorkBeltTiminginfo> cp = new CommonPage<MeterWorkBeltTiminginfo>();
                MeterWorkBeltTiminginfo timinginfo = new MeterWorkBeltTiminginfo();
                cp.startTime = dtStartTime.Text.ToString();
                cp.endTime = dtEndTime.Text.ToString();
                //if (cmbStateName.Text!="全部"&& cmbStateName.Text != "") {
                //    timinginfo.scaleName = cmbStateName.Text.ToString().Substring(0,1);
                //}
                if (cbPntName_1.Text.ToString() != "") {
                    timinginfo.baseSpotNo = cbPntName_1.Value.ToString();
                }
                timinginfo.valueFlag = "1";
                timinginfo.weightType = "0";
                cp.param = timinginfo;
                RESTfulResult<List<MeterWorkBeltTiminginfo>> rms = meterWorkBeltTimingService.doQueryWf(cp);

                //此处为特殊处理 如需sql查询 请查看后台mapper.xml文件，sql语句已准备。
                if (rms.Succeed)
                {
                    if (rms.Data != null && rms.Data.Count > 0)
                    {
                        List<MeterWorkBeltTiminginfo> list2 = new List<MeterWorkBeltTiminginfo>();
                        MeterWorkBeltTiminginfo ti = new MeterWorkBeltTiminginfo();
                        DataRow[] ARows = rms.Data.ListToDataTable<MeterWorkBeltTiminginfo>().Select("scaleName='A'");
                        DataRow[] BRows = rms.Data.ListToDataTable<MeterWorkBeltTiminginfo>().Select("scaleName='B'");
                        for(int i=0;i<ARows.Length;i++){
                            string packageNo = ARows[i]["packageNo"].ToString();
                            ARows[i]["weightDiff"] = ARows[i]["weightDiff"] + "%";
                            for (int j = 0; j < BRows.Length; j++)
                            { 
                                if (BRows[j]["packageNo"].ToString()== packageNo) {
                                    ARows[i]["weightB"] = BRows[j]["weight"].ToString();
                                }
                            }
                        }
                        if (ARows != null || ARows.Length != 0)
                        {
                            ARows.OrderBy(r => DateTime.Parse(r["createTime"].ToString()));
                            dtTmingTable = ARows.CopyToDataTable();
                        }

                    }

                        ClsControlPack.CopyDataToDatatable(ref dtTmingTable, ref dataTable3, true);

                        ClsControlPack.RefreshAndAutoSize(ultraGrid2);
                    }
                    else
                    {
                        dtTmingTable.Clear();
                        ClsControlPack.CopyDataToDatatable(ref dtTmingTable, ref dataTable3, true);
                        ClsControlPack.RefreshAndAutoSize(ultraGrid2);
                    }
                //}
            }
            catch (Exception ex)
            {
                MessageBox.Show("查询错误"+ex);
            }
        }

        private void ExportExcel() {
            string str = Interaction.InputBox("请输入要导出的文件名", "提示", "", 100, 100);
            string pqath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            this.ultraGridExcelExporter1.Export(this.ultraGrid2, pqath + "\\" + str + ".xls");
            MessageBox.Show("导出成功，请查看桌面！是否有[" + str + ".xls]表格");

        }
        //下拉框控件自定义option
        public static void valueToCombo(DataTable source, UltraComboEditor cbb)
        {
            cbb.DataSource = source;
            cbb.DisplayMember = "VALUE";
            cbb.ValueMember = "TEXT";
        }
        private void frmBeltOperationInfo_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;
        }
    }
}