using com.hnshituo.core.webapp.vo;
using Common;
using Infragistics.Win;
using Infragistics.Win.UltraWinGrid;
using JC_MeasuringSystem;
using Microsoft.VisualBasic;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace StorageMeterSystem
{
    public partial class frmHotDeliveryScale : Form
    {
        private PreStorageScaleService scaleService = new PreStorageScaleService(); //预报服务
        private ComBaseInfoService comBaseInfoService = new ComBaseInfoService(); //基础数服务
        private MeterBaseScalePointService scalePointService = new MeterBaseScalePointService(); //计量点信息服务
        private MeterWorkStorageSingleWgtService singleWgtService = new MeterWorkStorageSingleWgtService(); //理论重量表
        private MeterWorkStorageActualService actualService = new MeterWorkStorageActualService(); //实绩服务
        private PreStorageScale preTack = new PreStorageScale(); //预报实体类

        private Log l = Log.GetInstance(); //日志
        private string userId = "", userName = ""; //用户ID用户名
        private CoreAppUser appUser = null;//登陆信息
        private frmHotDeliveryBeltWarn fm = null;//预警界面
        public bool bMsgOpen = false; //是否打开了预警界面提醒

        public frmHotDeliveryScale()
        {
            InitializeComponent();
        }

        private void frmHotDeliveryScale_Load(object sender, EventArgs e)
        {
            ClsControlPack.SetNoEnable(ultraGridScale);

            userId = ((ST_MainForm)(this.MdiParent)).UserID;
            userName = ((ST_MainForm)(this.MdiParent)).UserName;
            appUser = ((ST_MainForm)(this.MdiParent)).AppUser;

            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"));

            //用户可以点击抬头进行排序
            this.ultraGridScale.DisplayLayout.ViewStyleBand = ViewStyleBand.OutlookGroupBy;
            this.ultraGridScale.DisplayLayout.GroupByBox.Hidden = true;

            #region 载入基础信息

            /*
            string sql = @"SELECT base_code baseCode,base_name baseName,memo FROM Com_Base_Info where p_base_code='001025' and valid_Flag='1'";
            PbModelDbService<List<ComBaseInfo>> pb = new PbModelDbService<List<ComBaseInfo>>();
            RESTfulResult<List<ComBaseInfo>> rmType = pb.executeSqlDataWf(sql);
            if (rmType.Succeed && rmType.Data != null && rmType.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbScaleState, rmType.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("产品信息载入失败");
                return;
            }

            */

            //计量点信息载入cmbPoint;cmbPointUp
            RESTfulResult<List<MeterBaseScalePoint>> rr = scalePointService.doQueryWf(new MeterBaseScalePoint { validFlag = "1", spotTypeNo = "001002004" });
            if (rr.Succeed && rr.Data != null && rr.Data.Count > 0)
            {
                //ClsControlPack.SetUltraComboDataSource(cmbPoint, rr.Data.ListToDataTable<MeterBaseScalePoint>(), "baseSpotNo", "baseSpotName");
                //ClsControlPack.SetUltraComboDataSource(cmbPointUp, rr.Data.ListToDataTable<MeterBaseScalePoint>(), "baseSpotNo", "baseSpotName");
                ValueList vlist2 = new ValueList();
                foreach (var item in rr.Data)
                {
                    vlist2.ValueListItems.Add(item.baseSpotNo, item.baseSpotName);
                }
                cmbPoint.ValueList = vlist2;
                cmbPoint.SelectedIndex = 0;

                cmbPointUp.ValueList = vlist2;
                cmbPointUp.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("计量点载入失败!");
                return;
            }

            //热送磅规格信息载入cmbSpcNameUp
            RESTfulResult<List<ComBaseInfo>> rrComSpcName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001018" });
            if (rrComSpcName.Succeed && rrComSpcName.Data != null && rrComSpcName.Data.Count > 0)
            {
                rrComSpcName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                ClsControlPack.SetUltraComboDataSource(cmbSpcNameUp, rrComSpcName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅规格信息载入失败!");
                return;
            }

            //热送磅钢种信息载入cmbGradeNameUp
            RESTfulResult<List<ComBaseInfo>> rrComGradeName = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001017" });
            if (rrComGradeName.Succeed && rrComGradeName.Data != null && rrComGradeName.Data.Count > 0)
            {
                rrComGradeName.Data.Sort((a, b) => a.baseName.CompareTo(b.baseName));
                ClsControlPack.SetUltraComboDataSource(cmbGradeNameUp, rrComGradeName.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅钢种信息载入失败!");
                return;
            }

            //热送磅班组信息载入cmbMeterGroupUp
            RESTfulResult<List<ComBaseInfo>> rrComMeterGroop = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001044" });
            if (rrComMeterGroop.Succeed && rrComMeterGroop.Data != null && rrComMeterGroop.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbMeterGroupUp, rrComMeterGroop.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                ClsControlPack.SetUltraComboDataSource(cmbMeterGroup, rrComMeterGroop.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅班组信息载入失败!");
                return;
            }

            //热送磅班次信息载入cmbMeterClassUp
            RESTfulResult<List<ComBaseInfo>> rrComMeterClass = comBaseInfoService.doQueryBaseList(new ComBaseInfo { validFlag = "1", pBaseCode = "001043" });
            if (rrComMeterClass.Succeed && rrComMeterClass.Data != null && rrComMeterClass.Data.Count > 0)
            {
                ClsControlPack.SetUltraComboDataSource(cmbMeterClassUp, rrComMeterClass.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
                ClsControlPack.SetUltraComboDataSource(cmbMeterClass, rrComMeterClass.Data.ListToDataTable<ComBaseInfo>(), "baseCode", "baseName");
            }
            else
            {
                MessageBox.Show("热送磅班次信息载入失败!");
                return;
            }

            //预报状态
            ValueList vlistScale = new ValueList();
            vlistScale.ValueListItems.Add("", "全部");
            vlistScale.ValueListItems.Add("0", "无效");
            vlistScale.ValueListItems.Add("1", "未使用");
            vlistScale.ValueListItems.Add("2", "使用中");
            vlistScale.ValueListItems.Add("3", "已完成");
            cmbScaleState.ValueList = vlistScale;
            cmbScaleState.SelectedIndex = 0;

            ValueList vlistFlag3 = new ValueList();
            vlistFlag3.ValueListItems.Add("", "全部");
            vlistFlag3.ValueListItems.Add("0", "无效");
            vlistFlag3.ValueListItems.Add("1", "未使用");
            vlistFlag3.ValueListItems.Add("2", "使用中");
            vlistFlag3.ValueListItems.Add("3", "已完成");
            this.ultraGridScale.DisplayLayout.Bands[0].Columns["valueFlag"].ValueList = vlistFlag3;

            //ClsControlPack.SetUltraGridRowFilter(ref ultraGridScale, true);

            #endregion 载入基础信息
        }

        #region 点击事件

        /// <summary>
        /// 点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraToolbarsManager1_ToolClick(object sender, Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
        {
            switch (e.Tool.Key)
            {
                case "btnQuery": //btnInsert
                    {
                        btnQuery();
                        break;
                    }
                case "btnInsert": //新增
                    {
                        btnInsert();
                        break;
                    }
                case "btnCancel": //作废
                    {
                        btnUpdate("0");
                    }
                    break;

                case "btnUnCancel": //还原
                    {
                        btnUpdate("1");
                    }
                    break;

                case "btnUpdate": //还原
                    {
                        btnUpdate("");
                    }
                    break;
                case "btnExcelOut": //导出
                    {
                        string str = Interaction.InputBox("请输入要导出的文件名", "提示", "", 100, 100);
                        string pqath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

                        this.ultraGridExcelExporter1.Export(this.ultraGridScale, pqath + "\\" + str + ".xls");
                        //MessageUtil.ShowTips("导出成功，请查看桌面！是否有[" + str + ".xls]表格");
                        MessageBox.Show("导出成功，请查看桌面！是否有[" + str + ".xls]表格");
                    }
                    break;
                default: break;
            }
        }

        /// <summary>
        /// 行点击事件，给下面的区域赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ultraGridScale_ClickCell(object sender, ClickCellEventArgs e)
        {
            try
            {
                if (ultraGridScale.ActiveRow == null)
                {
                    return;
                }

                UltraGridRow ugr = ultraGridScale.ActiveRow;
                if (ugr.Cells["UNCK"].Value.ToString().ToUpper().Equals("TRUE"))
                {
                    ugr.Cells["UNCK"].Value = false;
                }
                else
                {
                    ugr.Cells["UNCK"].Value = true;
                }

                //界面赋值
                if (ugr != null)
                {
                    preTack.predictionNo = ugr.Cells["predictionNo"].Text.Trim(); //预报编号
                    preTack.specName = ugr.Cells["specName"].Text.Trim();         //规格
                    preTack.gradeName = ugr.Cells["gradeName"].Text.Trim();       //钢号
                    preTack.batchNo = ugr.Cells["batchNo"].Text.Trim();           //批次号
                    preTack.heatNo = ugr.Cells["heatNo"].Text.Trim();         //炉号
                    preTack.packageNo = ugr.Cells["packageNo"].Text.Trim();       //顺序号
                    preTack.blockNum = ugr.Cells["blockNum"].Text.Trim();         //总支数
                    preTack.theoreticalWeight = Convert.ToDouble(string.IsNullOrEmpty(ugr.Cells["theoreticalWeight"].Text.Trim()) ? "0" : ugr.Cells["theoreticalWeight"].Text.Trim());    //理论重量
                    preTack.weightDiff = Convert.ToDouble(string.IsNullOrEmpty(ugr.Cells["weightDiff"].Text.Trim()) ? "0" : ugr.Cells["weightDiff"].Text.Trim());    //理论重量差值
                    preTack.meterClass = ugr.Cells["meterClass"].Text.Trim();     //班次
                    preTack.meterGroup = ugr.Cells["meterGroup"].Text.Trim();     //班次
                    preTack.baseSpotNo = ugr.Cells["baseSpotNo"].Text.Trim();     //计量点的编号
                    preTack.baseSpotName = ugr.Cells["baseSpotName"].Text.Trim();     //计量点名称
                    preTack.memo = ugr.Cells["memo"].Text.Trim();     //计量点名称

                    if ("2".Equals(ugr.Cells["valueFlag"].Value + ""))
                    {
                        updateBoxUnEnable();
                    }
                    else
                    {
                        updateBoxEnable();
                    }
                    autoAutoFill(preTack); //点击行进行自动填充界面字段
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("ultraGridScale_ClickCell方法处理异常：" + ex);
            }
        }

        /// <summary>
        /// 全选点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cbAll_CheckedChanged(object sender, EventArgs e)
        {
            foreach (UltraGridRow ugr in ultraGridScale.Rows)
            {
                if (!ugr.HiddenResolved)
                {
                    ugr.Cells["UNCK"].Value = cbAll.Checked;
                }
            }
        }

        /// <summary>
        /// 钢种TextChange事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbGradeNameUp_TextChanged(object sender, EventArgs e)
        {
            queryTheoryWgt();
        }

        /// <summary>
        /// 规格TextChange事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmbSpcNameUp_TextChanged(object sender, EventArgs e)
        {
            queryTheoryWgt();
        }

        private void cmbSpcNameUp_ValueChanged(object sender, EventArgs e)
        {
            queryTheoryWgt();
        }

        private void cmbGradeNameUp_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                queryTheoryWgt();
            }
        }

        #endregion 点击事件

        #region 预报支数报警事件

        /// <summary>
        /// 定时器，用于做预报报警
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            CommonPage<PreStorageScale> cp = new CommonPage<PreStorageScale>();

            cp.param = new PreStorageScale(); //{ valueFlag = "1", baseSpotNo = cmbPoint.Value?.ToString() };
            cp.param.valueFlag = "9";//VALUE_FLAG in ('1','2')
            cp.param.baseSpotNo = cmbPoint.Value + "";
            RESTfulResult<List<PreStorageScale>> rms = scaleService.doQueryWf(cp.param);
            if (rms.Succeed)
            {
                if (rms.Data != null && rms.Data.Count == 1)
                {
                    RESTfulResult<List<MeterWorkStorageActual>> rmsActual = actualService.doQuery(new MeterWorkStorageActual
                    {
                        predictionNo = rms.Data[0].predictionNo,
                        valueFlag = "1"
                    });
                    if (rmsActual.Succeed && rmsActual.Data != null && rmsActual.Data.Count > 0)
                    {
                        if (!string.IsNullOrEmpty(rmsActual.Data[0].packageNo) && !string.IsNullOrEmpty(rmsActual.Data[0].blockNum))
                        {
                            if (Convert.ToInt32(rmsActual.Data[0].blockNum)- Convert.ToInt32(rmsActual.Data[0].packageNo) ==10)
                            {
                                OpenMsg("预报仅剩余 10 支，请及时做预报！");
                            }
                            if (Convert.ToInt32(rmsActual.Data[0].blockNum) - Convert.ToInt32(rmsActual.Data[0].packageNo) == 5)
                            {
                                OpenMsg("预报仅剩余 5 支，请及时做预报！");
                            }
                        }
                    }
                }
                else
                {
                    //直接报警
                }
            }
        }


        /// <summary>
        /// 预报支数报警信息，从用户控件中回调主界面方法
        /// </summary>
        /// <param name="strWarnInfo"></param>
        private void OpenMsg(string strWarnInfo)
        {
            try
            {
                if (!bMsgOpen)
                {
                    if (PbCacheMonitor.frmMsgClosingTime == null)
                    {
                        bMsgOpen = true;
                        fm = frmHotDeliveryBeltWarn.CreateInstrance(this);
                        fm.TopMost = true;
                        fm.setLbTxt(strWarnInfo);
                        fm.Show();
                    }
                    else
                    {
                        TimeSpan secondSpan = new TimeSpan(DateTime.Now.Ticks - PbCacheMonitor.frmMsgClosingTime.Value.Ticks);
                        if (secondSpan.TotalSeconds > 5)
                        {
                            bMsgOpen = true;
                            fm = frmHotDeliveryBeltWarn.CreateInstrance(this);
                            fm.TopMost = true;
                            fm.setLbTxt(strWarnInfo);
                            fm.Show();
                        }
                    }
                }
                /*
                if (!string.IsNullOrEmpty(openPointNo) && fm != null)
                {
                    fm.CloseFrm();
                }
                */
            }
            catch { }
        }
        #endregion 预报支数报警事件

        /// <summary>
        /// 查询理论重量，并给界面赋值
        /// </summary>
        private void queryTheoryWgt()
        {
            if (!string.IsNullOrEmpty(cmbGradeNameUp.Text) && !string.IsNullOrEmpty(cmbSpcNameUp.Text))
            {
                MeterWorkStorageSingleWgt singleWgt = new MeterWorkStorageSingleWgt();
                singleWgt.gradeNo = cmbGradeNameUp.Value + "";
                singleWgt.specNo = cmbSpcNameUp.Value + "";
                singleWgt.valueFlag = "1";//状态(1=有效，0=无效)
                singleWgt.weightType = "1"; //重量类型（0 = 单只实际重量，1 = 预报理论重量，2 = 标准件重量）
                RESTfulResult<List<MeterWorkStorageSingleWgt>> rms = singleWgtService.doQuery(singleWgt);
                if (rms.Succeed && rms.Data != null && rms.Data.Count > 0)
                {
                    tbTheoreticalWeightUp.Text = rms.Data[0].singleWeight + "";
                    tbWeightDiff.Text = rms.Data[0].weightDiff + "";
                }
                else
                {
                    tbTheoreticalWeightUp.Text = "";
                    tbWeightDiff.Text = "";
                    //MessageBox.Show("未查询到钢种【" + cmbGradeNameUp.Text + "】、规格【" + cmbSpcNameUp.Text + "】对应的理论重量，请先在【热送磅理论重量维护】界面维护理论重量！");
                }
            }
        }

        /// <summary>
        /// 除顺序号，界面不可编辑
        /// </summary>
        private void updateBoxUnEnable()
        {
            cmbPointUp.Enabled = false;
            tbHeatNoUp.Enabled = false;
            cmbSpcNameUp.Enabled = false;
            cmbGradeNameUp.Enabled = false;
            tbTheoreticalWeightUp.Enabled = false;
            cmbMeterClassUp.Enabled = false;
            cmbMeterGroupUp.Enabled = false;
            tbPackageNoUp.Enabled = true;
            tbBlockNumUp.Enabled = true;
            tbMemo.Enabled = false;
        }

        /// <summary>
        /// 界面可编辑
        /// </summary>
        private void updateBoxEnable()
        {
            cmbPointUp.Enabled = true;
            tbHeatNoUp.Enabled = true;
            cmbSpcNameUp.Enabled = true;
            cmbGradeNameUp.Enabled = true;
            tbTheoreticalWeightUp.Enabled = true;
            cmbMeterClassUp.Enabled = true;
            cmbMeterGroupUp.Enabled = true;
            tbPackageNoUp.Enabled = true;
            tbBlockNumUp.Enabled = true;
            tbMemo.Enabled = true;
        }

        /// <summary>
        /// 自动填充界面的数据
        /// </summary>
        private void autoAutoFill(PreStorageScale preTack)
        {
            if (preTack == null || string.IsNullOrEmpty(preTack.predictionNo))
            {
                return;
            }
            cmbPointUp.Text = preTack.baseSpotName; //计量点名称1
            tbHeatNoUp.Text = preTack.heatNo; //炉号2
            cmbSpcNameUp.Text = preTack.specName; //规格名称3
            cmbGradeNameUp.Text = preTack.gradeName; //钢号名称4
            tbPackageNoUp.Text = preTack.packageNo;//顺序号5
            tbBlockNumUp.Text = preTack.blockNum;//总支数6
            cmbMeterClassUp.Text = preTack.meterClass;//班次7
            tbTheoreticalWeightUp.Text = preTack.theoreticalWeight + "";//理论重量8
            tbMemo.Text = preTack.memo;//备注
            cmbMeterGroupUp.Text = preTack.meterGroup;//班次7
        }

        /// <summary>
        /// 界面的值给storageActual赋值；用于新增、修改
        /// </summary>
        /// <param name="storageActual"></param>
        private PreStorageScale setTableValue(PreStorageScale tbScale)
        {
            tbScale.baseSpotName = cmbPointUp.Text;
            tbScale.baseSpotNo = cmbPointUp.Value + "";
            tbScale.heatNo = tbHeatNoUp.Text;
            tbScale.specName = cmbSpcNameUp.Text;
            tbScale.specNo = cmbSpcNameUp.Value + "";
            tbScale.gradeName = cmbGradeNameUp.Text;
            tbScale.gradeNo = cmbGradeNameUp.Value + "";
            tbScale.theoreticalWeight = Convert.ToDouble(string.IsNullOrEmpty(tbTheoreticalWeightUp.Text) ? "0" : tbTheoreticalWeightUp.Text);    //理论重量
            tbScale.weightDiff = Convert.ToDouble(string.IsNullOrEmpty(tbWeightDiff.Text) ? "0" : tbWeightDiff.Text);    //理论重量偏差值
            tbScale.meterClass = cmbMeterClassUp.Text;
            tbScale.meterGroup = cmbMeterGroupUp.Text;
            tbScale.packageNo = tbPackageNoUp.Text;
            tbScale.blockNum = tbBlockNumUp.Text;
            tbScale.memo = tbMemo.Text;//备注

            return tbScale;
        }

        /// <summary>
        /// 新增数据
        /// </summary>
        /// <param name="vFlag"></param>
        private void btnInsert()
        {
            queryTheoryWgt();//新增前重复校验，避免停留过久影响
            List<PreStorageScale> scaleList = new List<PreStorageScale>();
            PreStorageScale preStorageScale = new PreStorageScale();
            preStorageScale.createManName = userName;
            preStorageScale.createManNo = userId;
            preStorageScale = setTableValue(preStorageScale);

            if (string.IsNullOrEmpty(tbTheoreticalWeightUp.Text) || string.IsNullOrEmpty(tbWeightDiff.Text))
            {
                MessageBox.Show("未查询到钢种【" + cmbGradeNameUp.Text + "】、规格【" + cmbSpcNameUp.Text + "】对应的理论重量，不允许进行新增，请先到【热送磅理论重量维护】界面维护！");
                return;
            }
            scaleList.Add(preStorageScale);
            RESTfulResult<string> rms = scaleService.doAddWf(scaleList);
            if (rms.Succeed)
            {
                MessageBox.Show("新增成功！");
                btnQuery();
            }
            else
            {
                MessageBox.Show("操作失败：" + rms.Data);
                l.WriteLog(19, "操作失败：" + rms.Data + rms.Message);
            }
        }

        /// <summary>
        /// 修改数据(作废还原)
        /// </summary>
        /// <param name="vFlag"></param>
        private void btnUpdate(string vFlag)
        {
            try
            {
                queryTheoryWgt();//修改前重复校验，避免停留过久影响
                int iCount = 0, updateValueNotUse = 0, updateValueUsed = 0;
                List<PreStorageScale> lt = new List<PreStorageScale>();
                ultraGridScale.UpdateData();
                foreach (UltraGridRow ugr in ultraGridScale.Rows)
                {
                    if (ugr.Cells["UNCK"].Value.ToString().Trim().ToUpper() == "TRUE")
                    {
                        if ("1".Equals(ugr.Cells["valueFlag"].Value + ""))
                        {
                            updateValueNotUse = 1;
                        }
                        if ("2".Equals(ugr.Cells["valueFlag"].Value + ""))
                        {
                            updateValueUsed = 2;
                        }
                        if (updateValueNotUse == 1 && updateValueUsed == 2)
                        {
                            MessageBox.Show("[使用中]的预报与[未使用]的预报不允许同时修改，重新勾选数据！");
                            return;
                        }
                        if (string.IsNullOrEmpty(vFlag) && "0".Equals(ugr.Cells["valueFlag"].Value + ""))
                        {
                            MessageBox.Show("已作废的数据，请先还原再进行修改！");
                            return;
                        }
                        if ("3".Equals(ugr.Cells["valueFlag"].Value + ""))
                        {
                            MessageBox.Show("已使用完成的预报不允许进行修改，请重新做预报！");
                            return;
                        }
                        if (string.IsNullOrEmpty(tbTheoreticalWeightUp.Text) || string.IsNullOrEmpty(tbWeightDiff.Text))
                        {
                            MessageBox.Show("未查询到钢种【" + cmbGradeNameUp.Text + "】、规格【" + cmbSpcNameUp.Text + "】对应的理论重量，不允许进行新增，请先到【热送磅理论重量维护】界面维护！");
                            return;
                        }
                        PreStorageScale preStorageUpdate = new PreStorageScale();
                        preStorageUpdate.predictionNo = ugr.Cells["predictionNo"].Text.Trim();
                        preStorageUpdate.updateManNo = userId;
                        preStorageUpdate.updateManName = userName;
                        if (!string.IsNullOrEmpty(vFlag))
                        {
                            preStorageUpdate.valueFlag = vFlag;
                        }
                        else
                        {
                            preStorageUpdate = setTableValue(preStorageUpdate);
                        }

                        lt.Add(preStorageUpdate);
                        iCount++;
                    }
                }
                if (lt == null || lt.Count == 0)
                {
                    MessageBox.Show("请至少勾选一条数据后再进行更新操作！");
                    return;
                }
                DialogResult dr = MessageBox.Show("共更新[" + iCount + "]条数据，是否继续更新！", "提示", MessageBoxButtons.OKCancel);
                if (dr == DialogResult.Cancel)
                {
                    return;
                }
                RESTfulResult<String> rms = scaleService.doUpdateWf(lt);
                if (rms.Succeed)
                {
                    MessageBox.Show("修改成功！");
                    btnQuery();
                }
                else
                {
                    MessageBox.Show("操作失败：" + rms.Data);
                    l.WriteLog(19, "操作失败：" + rms.Data + rms.Message);
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 查询数据
        /// </summary>

        private void btnQuery()
        {
            DataTable dtScale = dataTable1.Clone();

            if (DateTime.Compare(dtStartTime.Value, dtEndTime.Value) > 0)
            {
                MessageBox.Show("开始时间不能大于结束时间");
                return;
            }
            if ((dtEndTime.Value - dtStartTime.Value).Days > 30)
            {
                MessageBox.Show("时间跨度不能大于30天");
                return;
            }
            if (string.IsNullOrEmpty(cmbPoint.Value?.ToString()))
            {
                MessageBox.Show("请选择计量点");
                return;
            }

            CommonPage<PreStorageScale> cp = new CommonPage<PreStorageScale>();

            cp.param = new PreStorageScale(); //{ valueFlag = "1", baseSpotNo = cmbPoint.Value?.ToString() };
            cp.param.valueFlag = cmbScaleState.Value + "";
            cp.param.baseSpotNo = cmbPoint.Value + "";
            cp.param.startTime = dtStartTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cp.param.endTime = dtEndTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
            cp.param.meterClass = cmbMeterClass.Text + "";
            cp.param.meterGroup = cmbMeterGroup.Text + "";

            RESTfulResult<List<PreStorageScale>> rms = scaleService.doQueryWf(cp.param);
            if (rms.Succeed)
            {
                if (rms.Data != null && rms.Data.Count > 0)
                {
                    dtScale = rms.Data.ListToDataTable<PreStorageScale>();

                    ClsControlPack.CopyDataToDatatable(ref dtScale, ref dataTable1, true);
                    ClsControlPack.RefreshAndAutoSize(ultraGridScale);

                    foreach (UltraGridRow ugr in ultraGridScale.Rows)
                    {
                        if (ugr.Cells["valueFlag"].Value.ToString() == "0")
                        {
                            ugr.Appearance.BackColor = Color.Red;
                        }
                        else
                        {
                            ugr.Appearance.BackColor = Color.White;
                        }
                    }
                }
                else
                {
                    dtScale.Clear();
                    ClsControlPack.CopyDataToDatatable(ref dtScale, ref dataTable1, true);
                    ClsControlPack.RefreshAndAutoSize(ultraGridScale);
                }
            }
        }
        /// <summary>
        /// 是否开启过滤
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ckFilter_CheckedChanged(object sender, EventArgs e)
        {
            if (ckFilter.Checked)
            {
                this.ultraGridScale.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.True;
            }
            else
            {
                this.ultraGridScale.DisplayLayout.Override.AllowRowFiltering = DefaultableBoolean.False;
            }

        }
    }
}