using CarMeterSystem.OptionCls;
using com.hnshituo.core.webapp.vo;
using Common;
using MeterConditionLibrary;
using MeterPlugInLibrary;
using MeterSceneLibrary;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading;
using System.Windows.Forms;

namespace CarMeterSystem
{
    public partial class frmCarSalePurchase : Form
    {
        public frmCarSalePurchase()
        {
            InitializeComponent();
        }

        private Print printClass = new Print();//打印
        private MeterWorkCarRecoverService carRecoverService = new MeterWorkCarRecoverService(); //复磅操作
        private MeterBaseTermTareDataService termTareDate = new MeterBaseTermTareDataService();//留期限皮操作
        private MeterWorkMonitorService monitor = new MeterWorkMonitorService(); //设备及远程呼叫监控表
        private MeterWorkCarActualFirstService workCarActualFirstService = new MeterWorkCarActualFirstService(); //一次计量数据
        private PreTrackScaleService trackService = new PreTrackScaleService();//查询预报信息
        private validTermTareCar termTareCar = new validTermTareCar();//验证是否可留期限皮
        private validRecoverInfo recoverInfo = new validRecoverInfo();//验证是否需要复磅以及是否满足复磅条件

        private MeterWorkCarActualFirst firstDb = null;//一次计量数据
        private SceneControl inner = new SceneControl(); //非内倒业务处理
        private validPrediction validPred = new validPrediction(); //验证预报信息
        private validUnloadAutomatic unloadAutomatic = new validUnloadAutomatic(); //自动卸货
        private VoicePlay vicPlayClass = new VoicePlay();//语音播放
        private Log lg = Log.GetInstance(); //写日志

        private double firstWgt = 0;
        private bool bRailwayActual = true;
        private bool isVoiceDownCar = false;//车上人员请下车播放一次
        private string firstCode = "";//上次扫码编号
        private string TrackCarNo = "";//预报车号
        private string sPreCarNo = "";//摄像头识别车号
        private string limsMsg = "";

        public bool isTest = false;

        private void frmCarSalePurchase_Load(object sender, EventArgs e)
        {
            try
            {
                timer1.Start();
                lbPointName.Text = PbCache.sportInfo.baseSpotName;
                ucCarMeterInfo21.setMeterType(PbCache.bussinessTypeName);
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, PbCache.lockCarNo + "frmCarSalePurchase_Load事件异常：" + ex.Message);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (PbCache.collect == null) return;
                PbCache.isLockFrm = true;
                PbCache.lockCarNo = TrackCarNo;
                PbCache.lockWgt = PbCache.collect.weight;
                btnSave.Enabled = false;
                lg.WriteLog(15, PbCache.lockCarNo + "点击保存按钮");

                //2021年6月22日 存在需要复磅的数据，不允许进行保存操作
                recoverInfo.ValidAllowSave(PbCache.lockCarNo);
                if (PbCache.isAllowRecover)
                {
                    PbCache.isLockFrm = false;
                    btnSave.Enabled = true;
                    setMsg(PbCache.ResultMessage);
                    return;
                }

                txtMsgInfo.Text = "称重保存中......";
                FrmMessage frmMessage = FrmMess("是否确认保存当前计量数据！", "继续保存");
                if (frmMessage.ShowDialog(this) == DialogResult.Cancel)
                {
                    PbCache.isLockFrm = false;
                    btnSave.Enabled = true;
                    txtMsgInfo.Text = "";
                    lg.WriteLog(15, PbCache.lockCarNo + "保存按钮选择否");
                    return;
                }

                try
                {
                    //是否在黑名单内
                    MeterBaseCarBlacelistService bService = new MeterBaseCarBlacelistService();
                    RESTfulResult<List<MeterBaseCarBlacelist>> rmB = bService.doQueryWf(new MeterBaseCarBlacelist { carNo = PbCache.lockCarNo, valueFlag = "0" });
                    if (rmB.Succeed)
                    {
                        if (rmB.Data != null && rmB.Data.Count > 0)
                        {
                            PbCache.ResultMessage = string.Format("车辆[{0}在黑名单内，不能计量", PbCache.lockCarNo);
                            lg.WriteLog(3, string.Format("车辆[{0}在黑名单内，不能计量", PbCache.lockCarNo));

                            PbCache.isLockFrm = false;
                            btnSave.Enabled = true;
                            return;
                        }
                    }
                    else
                    {
                        PbCache.ResultMessage = "保存失败，获取黑名单数据异常，请稍后再试";
                        lg.WriteLog(3, "获取黑名单数据异常，请稍后再试");

                        PbCache.isLockFrm = false;
                        btnSave.Enabled = true;
                        return;
                    }

                    PreTrackScale preTrack = ucCarMeterInfo21._preTrack;
                    string strMsg = "";

                    // 2021年6月24日 智能计量做退货流程的配置
                    ComBaseInfoService cis = new ComBaseInfoService();
                    RESTfulResult<List<PbModelDb>> rm = cis.doQueryWf(new ComBaseInfo { baseCode = "001036001", validFlag = "1" });
                    if (rm.Succeed && rm.Data != null && rm.Data.Count == 1)
                    {
                        if (PbCache.monitor != null && !"2".Equals(PbCache.monitor.isTakeOver) && preTrack != null && preTrack.meterTypeName.Contains("退货"))
                        {
                            PbCache.isLockFrm = false;
                            btnSave.Enabled = true;
                            setMsg("[" + preTrack.meterTypeName + "]流程，不允许进行在自助计量，请点击[语音求助]由司秤工远程过磅！");
                            return;
                        }
                    }

                    PbCache.addWgt = preTrack.addWeightSum == null ? 0 : preTrack.addWeightSum.Value;
                    bool flag = false;

                    //说明：不管是成功还是失败，都在180行有提示信息
                    if (inner.WeightMeter(preTrack, null, out strMsg))  //meterTypeNo  "001006001","001006002"
                    {
                        flag = true;
                        //票据打印
                        Print1(PbCache.actualFirstNo);
                        try
                        {
                            #region 截取图片信息

                            //截取屏幕信息

                            Point screenPoint = this.PointToScreen(new Point());
                            Rectangle rect = new Rectangle(screenPoint, this.Size);
                            Image img = new Bitmap(rect.Width, rect.Height);
                            Graphics g = Graphics.FromImage(img);
                            g.CopyFromScreen(rect.X - 1, rect.Y - 1, 0, 0, rect.Size);//"D://file/1.jpg"
                            img.Save(string.Format("{0}imgShort\\formalImg\\{1}_{2}_{3}.jpg",
                                AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                PbCache.sportInfo.baseSpotNo, "C" + PbCache.actualFirstNo, 7), System.Drawing.Imaging.ImageFormat.Jpeg);
                            //(PbCache.videoChild.Count + 1)), System.Drawing.Imaging.ImageFormat.Jpeg);

                            //*
                            //最后进行截图操作
                            CameraShotCls cameraShot = new CameraShotCls();
                            cameraShot.CapMethod(PbCache.actualFirstNo);
                            //*/

                            #endregion 截取图片信息
                        }
                        catch (Exception ex)
                        {
                            lg.WriteLog(3, "计量完成，但图片截取失败，编号：" + PbCache.actualFirstNo);
                        }

                        //自动卸货
                        unloadAutomatic.ValidMethod(preTrack);

                        //若不是测试，保存完成后则不能返回，若是测试界面进入的则可点击返回按钮
                        if (!isTest)
                        {
                            btnFH.Enabled = false;
                        }
                    }
                    else
                    {
                        PbCache.isLockFrm = false;
                        btnSave.Enabled = true;
                    }

                    #region 2021-02-22 add 增加留期限皮提醒

                    string sql = @"select base_spot_no   baseSpotNo,
                                       base_spot_name baseSpotName,
                                       warn_time      warnTime,
                                       end_time    endTime,
                                       car_no         carNo
                                  from Meter_Base_Term_Tare_Data
                                  where car_no='" + PbCache.lockCarNo + "' and value_flag='0'";
                    PbModelDbService<List<MeterBaseTermTareData>> pbModelDbService = new PbModelDbService<List<MeterBaseTermTareData>>();
                    RESTfulResult<List<MeterBaseTermTareData>> rES = pbModelDbService.executeSqlDataWf(sql);

                    if (rES.Succeed)
                    {
                        if (rES.Data != null && rES.Data.Count > 0)
                        {
                            if (DateTime.Now.CompareTo(rES.Data[0].endTime) >= 0)
                            {
                                PbCache.ResultMessage += "\n\r提醒：期限皮重已过期，请及时留期限皮";
                            }
                            else if (DateTime.Now.CompareTo(rES.Data[0].warnTime) >= 0)
                            {
                                PbCache.ResultMessage += "\n\r提醒：期限皮重将要过期，请及时留期限皮";
                            }
                        }
                    }

                    #endregion 2021-02-22 add 增加留期限皮提醒

                    //语音提醒部分
                    //flag =true表示称重成功
                    if (flag && PbCache.ResultMessage.Contains("请及时留期限皮"))
                    {
                        vicPlayClass.GetVoicePlay("称重完成空车留期限皮", PbCache.lockCarNo);
                    }
                    else
                    {
                        vicPlayClass.GetVoicePlay(strMsg, PbCache.lockCarNo);
                    }

                    //提示信息，
                    //txtMsgInfo.Text = PbCache.ResultMessage;
                    lblog = string.IsNullOrEmpty(PbCache.ResultMessage) ? strMsg : PbCache.ResultMessage;//偶尔出现ResultMessage为空的情况
                    DoShowlbLog(lblog);

                    #region 2021-02-23 add 期限皮按钮显示

                    if (flag && !btnTare.Visible)
                    {
                        try
                        {
                            //2021年3月15日 杨秀东 期限皮重显示按钮的显示方法修改 isShowBtnTare   ValidMethod
                            if (!string.IsNullOrEmpty(TrackCarNo) && termTareCar.isShowBtnTare(TrackCarNo))
                            {
                                btnTare.Visible = true;
                            }
                        }
                        catch (Exception ex)
                        {
                            lg.WriteLog(13, PbCache.lockCarNo + "期限皮重按钮显示事件异常：" + ex.Message);
                        }
                    }

                    #endregion 2021-02-23 add 期限皮按钮显示

                    #region LED写入

                    PbCache.ledInfo = string.Format("计量完成，车号：{0}，车重：{1}，物资：{2}", PbCache.lockCarNo, PbCache.lockWgt, preTrack != null ? preTrack.matterName : "");

                    #endregion LED写入
                }
                catch (Exception ex)
                {
                    //语音播报
                    vicPlayClass.GetVoicePlay("称重数据保存失败", PbCache.lockCarNo);
                    //提示信息，
                    //txtMsgInfo.Text = "称重数据保存失败";
                    lblog = "称重数据保存失败";
                    DoShowlbLog(lblog);
                    lg.WriteLog(3, PbCache.lockCarNo + "称重保存失败：" + ex.Message);
                    PbCache.isLockFrm = false;
                    btnSave.Enabled = true;
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, PbCache.lockCarNo + "保存事件异常：" + ex.Message);
            }
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            if (PbCache.monitor.isHelp == "0")
            {
                RESTfulResult<string> rms = monitor.doUpdateWf(new MeterWorkMonitor { pointNo = PbCache.sportInfo.baseSpotNo, isHelp = "1" });
                if (!rms.Succeed)
                {
                    lg.WriteLog(3, PbCache.lockCarNo + "求助写入失败");
                }
            }
            lg.WriteLog(15, PbCache.lockCarNo + "点击了语音求助按钮");
        }

        private bool isOverSound = false;

        private bool isCalibration = true;
        private bool bRelation = true;
        private bool isShowRecover = true; //是否需要判断显示复磅按钮

        //验证校秤数据
        private validCalibration vCalib = new validCalibration();

        private validCarno vCarNo = new validCarno();

        private bool bXCStart = false;

        private void timer1_Tick(object sender, EventArgs e)
        {
            #region 初始设置值

            try
            {
                if (PbCache.collect.weight < 500)
                {
                    isCalibration = true;
                    PbCache.addWgt = 0;
                    this.Close();
                }

                #region 求助按钮控制

                if (PbCache.monitor != null && !string.IsNullOrEmpty(PbCache.monitor.isHelp))
                {
                    switch (PbCache.monitor.isHelp)
                    {
                        case "0": //未呼叫
                            {
                                if (btnHelp.Text != "语音求助")
                                {
                                    btnHelp.BackColor = Color.DodgerBlue;
                                    btnHelp.Text = "语音求助";
                                }
                            }; break;
                        case "1": //呼叫待接听
                            {
                                if (btnHelp.Text != "呼叫中")
                                {
                                    btnHelp.BackColor = Color.Red;
                                    btnHelp.Text = "呼叫中";
                                }
                            }; break;
                        case "2": //呼叫已接听
                            {
                                if (btnHelp.Text != "已接听")
                                {
                                    btnHelp.BackColor = Color.Green;
                                    btnHelp.Text = "已接听";
                                }
                            }; break;
                    }
                }

                #endregion 求助按钮控制

                if (!PbCache.isLockFrm)
                {
                    //ucWeightT1.setWgt(PbCache.collect.weight);
                    ucWeightT1.setWgt(Math.Round((double)PbCache.collect.weight / 1000, 2)); //2021年5月10日界面显示单位，调整为T
                    ucWeightT1.setStable(PbCache.collect.weightStatus != 1 ? true : false);
                    ucWeightT1.setExceed(PbCache.collect.weight > PbCache.range.upperRange ? false : true);

                    #region 远程计量

                    //如果处于远程计量状态，且远程保存完成了，则提示保存完成，让司机知道
                    if (PbCache.monitor != null)// && PbCache.collect.weight > 500)
                    {
                        if (!string.IsNullOrEmpty(PbCache.monitor.isOverWgt) && PbCache.monitor.isTakeOver == "2" && !isOverSound) //&& !PbCache.isLockFrm)
                        {
                            txtMsgInfo.Text = "称重完成";
                            vicPlayClass.GetVoicePlay("称重完成", PbCache.collect.carno);
                            isOverSound = true;
                            Print1(PbCache.monitor.isOverWgt);
                            return;
                        }

                        if (PbCache.monitor.isTakeOver == "2" || !string.IsNullOrEmpty(PbCache.monitor.isOverWgt))
                        {
                            btnSave.Visible = false;
                            bRelation = true;
                            lg.WriteLog(14, "车号：" + PbCache.collect.carno + "；远程计量完成后，保存按钮隐藏！");
                            if (PbCache.monitor.isTakeOver == "2" && !txtMsgInfo.Text.Contains("称重完成"))
                            {
                                txtMsgInfo.Text = "司秤工正在远程计量，请等待处理！！";
                            }
                            return;
                        }
                    }

                    #endregion 远程计量

                    if (string.IsNullOrEmpty(TrackCarNo))
                    {
                        txtCarNo.Text = PbCache.collect.carno;
                    }
                    else
                    {
                        txtCarNo.Text = TrackCarNo;

                        if (PbCache.collect != null && !string.IsNullOrEmpty(PbCache.collect.carno))
                        {
                            if (TrackCarNo != PbCache.collect.carno)
                            {
                                if (PbCache.monitor.carErr != "1") //写入一次车号异常
                                {
                                    RESTfulResult<string> rms = monitor.doUpdateWf(new MeterWorkMonitor { pointNo = PbCache.sportInfo.baseSpotNo, carErr = "1" });
                                }
                            }
                            else if (PbCache.monitor.carErr == "1") //若车号相等，但是之前写了一次车号异常，此时要改为车号正常
                            {
                                RESTfulResult<string> rms = monitor.doUpdateWf(new MeterWorkMonitor { pointNo = PbCache.sportInfo.baseSpotNo, carErr = "0" });
                            }
                        }

                        vCarNo.ValidMethod(new PreTrackScale { carNo = TrackCarNo });
                        if (TrackCarNo != PbCache.collect.carno && !PbCache.monitorResult.valid_carno)
                        {
                            // 2021年5月11日 未识别到车牌号，进行提示，避免出现重新扫码点保存的情况
                            if (string.IsNullOrEmpty(PbCache.collect.carno))
                            {
                                txtMsgInfo.Text = string.Format("摄像头未识别到车牌号，不允许进行【保存】操作，请点击语音求助！");
                                lg.WriteLog(14, "摄像头未识别到车牌号，不允许进行【保存】操作，请点击语音求助！");
                            }
                            else
                            {
                                txtMsgInfo.Text = string.Format("预报车号【{0}】与摄像头识别车号【{1}】不一致，请点击语音求助", TrackCarNo, PbCache.collect.carno);
                                lg.WriteLog(14, "车号：" + PbCache.collect.carno + "；预报车号" + TrackCarNo + "与摄像头识别车号" + PbCache.collect.carno + "不一致！");
                            }
                            btnSave.Visible = false;
                            return;
                        }
                        else
                        {
                            if (txtMsgInfo.Text.Contains("摄像头"))
                            {
                                txtMsgInfo.Text = ""; //将原来的不允许计量的提示给去掉
                            }
                            PbCache.collect.carno = TrackCarNo;
                        }
                    }
                }

                #endregion 初始设置值

                #region 查一次是否校秤，若未校秤则不能计量

                //*
                if (bRelation)
                {
                    string msgInfo = "";
                    isCalibration = vCalib.ValidMethod(new MeterWorkCalibrationMain { baseSpotNo = PbCache.sportInfo.baseSpotNo, valueFlag = "2" }, out msgInfo);
                    bRelation = false;
                }

                if (!isCalibration)
                {
                    MeterBaseCalibraNumRel calibraNumRel = PbCache.ltBaseCalibraNumRel.Where(s => s.baseSpotNo == PbCache.sportInfo.baseSpotNo).FirstOrDefault();
                    if (calibraNumRel == null || calibraNumRel.isMeter == "否" || PbCache.collect.weight > (calibraNumRel.upWeight == null ? 0 : calibraNumRel.upWeight.Value))
                    {
                        if (!bXCStart)
                        {
                            vicPlayClass.GetVoicePlay("未校秤", "");
                            bXCStart = true;
                        }
                        txtMsgInfo.Text = "未校秤无法计量";
                        return;
                    }
                }
                //*/

                #endregion 查一次是否校秤，若未校秤则不能计量

                #region 判断是否需要复磅

                if (isShowRecover && !string.IsNullOrEmpty(txtCarNo.Text))
                {
                    recoverInfo.ValidMethod(txtCarNo.Text);
                    if (PbCache.isAllowRecover)
                    {
                        btnRecover.Visible = true;
                    }
                    else
                    {
                        btnRecover.Visible = false;
                    }
                    isShowRecover = false;
                }

                #endregion 判断是否需要复磅

                #region 第一次查询，有车号，重量稳定，红外验证通过的情况只查一次

                //如果没有查过一次，且扫描到车号信息，且重量稳定
                if ((bRailwayActual || sPreCarNo != TrackCarNo || firstCode != PbCache.strCode) && PbCache.collect.weightStatus == 0 && !string.IsNullOrEmpty(TrackCarNo) && PbCache.monitorResult.valid_park_status)
                {
                    firstWgt = 0;
                    sPreCarNo = TrackCarNo;

                    #region 查询一次计量表中是否存在一条未结净的数据，非一车联运情况扫码两次，一车联运的话

                    RESTfulResult<List<MeterWorkCarActualFirst>> RESTfulResult = workCarActualFirstService.doQueryWf(new MeterWorkCarActualFirst { carNo = TrackCarNo, valueFlag = "1" });
                    if (RESTfulResult.Succeed)
                    {
                        if (RESTfulResult.Data != null && RESTfulResult.Data.Count > 0)
                        {
                            firstWgt = RESTfulResult.Data[0].meterWeight.Value;
                            firstDb = RESTfulResult.Data[0];
                            //给用户控件设置值
                            ucCarMeterInfo21.setFormControlValue(firstDb);
                        }
                        bRailwayActual = false;
                    }

                    #endregion 查询一次计量表中是否存在一条未结净的数据，非一车联运情况扫码两次，一车联运的话

                    #region 查一次是否可留期限皮，可以则显示按钮

                    PbCache.lockWgt = PbCache.collect.weight;
                    //*
                    btnTare.Visible = false;
                    //2021年3月15日 期限皮重显示按钮的显示方法修改 isShowBtnTare   ValidMethod
                    if (termTareCar.isShowBtnTare(TrackCarNo))
                    {
                        lg.WriteLog(3, string.Format("9车辆[{0}进入期限皮重显示按钮的显示判断", TrackCarNo));
                        btnTare.Visible = true;
                    }
                    //*/

                    #endregion 查一次是否可留期限皮，可以则显示按钮
                }

                #endregion 第一次查询，有车号，重量稳定，红外验证通过的情况只查一次

                #region 查询扫码信息

                if (string.IsNullOrEmpty(PbCache.strCode))
                {
                    firstCode = "";
                }
                else if (!PbCache.isLockFrm)
                {
                    if (firstCode != PbCache.strCode)
                    {
                        //将预报信息查出来，然后写入到界面
                        RESTfulResult<List<PreTrackScale>> rm = trackService.doQueryWf(new PreTrackScale { predictionNo = PbCache.strCode });
                        if (rm.Succeed)
                        {
                            firstCode = PbCache.strCode;
                            if (rm.Data == null || rm.Data.Count == 0)
                            {
                                lblog = "未找到编号为[" + PbCache.strCode + "]的有效预报";
                                DoShowlbLog(lblog);

                                //未找到预报信息，预报已删除
                                vicPlayClass.GetVoicePlay("未找到有效预报", PbCache.collect.carno);
                                lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；未找到有效预报，保存按钮隐藏！");
                                btnSave.Visible = false;
                                return;
                            }
                            else
                            {
                                lblog = "请仔细核对扫码信息，无误且重量稳定后请点击保存";
                                DoShowlbLog(lblog);

                                string ResultMessage = "";
                                //验证预报信息
                                if (validPred.ValidMethod(rm.Data[0], out ResultMessage))
                                {
                                    //预报验证无误，则将预报信息写入到用户控件
                                    TrackCarNo = rm.Data[0].carNo;
                                    ucCarMeterInfo21.setFormControlValue(rm.Data[0]);//预报信息写入界面

                                    //将扫码车号信息录入
                                    RESTfulResult<string> rms = monitor.doUpdateWf(new MeterWorkMonitor { pointNo = PbCache.sportInfo.baseSpotNo, scanCar = TrackCarNo });

                                    #region 这里查下这个车有没有期限皮，当前物料是否能使用期限皮 termTareDate
                                    // and t2.meter_nature_name like  '%外%'  //此为车辆计量属性； 例如：若不是中控，则不允许使用期限皮重（九江取消了）
                                    if (firstWgt < 500)
                                    {
                                        firstWgt = 0;
                                        string sql = @"select t1.meter_weight meterWeight,
                                          t1.data_no dataNo,
                                          t1.create_time createTime,
                                          t1.base_spot_no baseSpotNo,
                                          t1.base_spot_name baseSpotName
                                      from Meter_Base_Term_Tare_Data t1, Meter_Base_Term_Tare_Car t2
                                     where t1.car_no = t2.car_no
                                       and t2.value_flag = '0'
                                       and t1.value_flag = '0'
                                       and t1.car_no = '" + rm.Data[0].carNo + @"'
                                       and t1.end_time>sysdate";
                                        PbModelDbService<List<MeterBaseTermTareData>> pb = new PbModelDbService<List<MeterBaseTermTareData>>();
                                        RESTfulResult<List<MeterBaseTermTareData>> rmType = pb.executeSqlDataWf(sql);
                                        if (rmType.Succeed)
                                        {
                                            // //and t.meter_nature_name like '%外%' //此为物料计量属性，例如：若不是外购物料，则不允许使用期限皮重；（九江取消了）
                                            if (rmType.Data != null && rmType.Data.Count > 0)
                                            {
                                                //查下当前拖的物料是否可用期限皮;验证业务类型是否允许使用期限皮重
                                                string sqls = @"select t.matter_no id, t.matter_name text
                                              from meter_base_matter_info t,meter_base_metertype_conf t2
                                             where t.allowed_tare_weight = '1'
                                               and t.valid_flag = '1'
                                               and t.matter_no='" + rm.Data[0].matterNo + @"'
                                               and t2.is_allow_term_tare = '1' and  t2.meter_type_no = '" + rm.Data[0].meterTypeNo + @"'";

                                                PbModelDbService<List<PbModelDb>> pbp = new PbModelDbService<List<PbModelDb>>();
                                                RESTfulResult<List<PbModelDb>> rmT = pbp.executeSqlDataWf(sqls);
                                                if (rmT.Succeed)
                                                {
                                                    if (rmT.Data != null && rmT.Data.Count > 0)
                                                    {
                                                        //查找期限皮重信息
                                                        firstWgt = rmType.Data[0].meterWeight.Value;
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    #endregion 这里查下这个车有没有期限皮，当前物料是否能使用期限皮 termTareDate
                                }
                                else
                                {
                                    //播报语音，提示预报异常
                                    vicPlayClass.GetVoicePlay(ResultMessage, PbCache.collect.carno);
                                    lblog = PbCache.ResultMessage;
                                    DoShowlbLog(lblog);
                                    btnSave.Visible = false;
                                    lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；预报查询异常，保存按钮隐藏！");
                                    return;
                                }
                            }
                        }
                        else
                        {
                            lg.WriteLog(1, "查询预报编号异常");
                            vicPlayClass.GetVoicePlay("未找到有效预报", PbCache.collect.carno);
                            lblog = "获取预报失败!";
                            DoShowlbLog(lblog);
                            btnSave.Visible = false;
                            lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；未找到有效预报，保存按钮隐藏！");
                            return;
                        }
                    }

                    #region 如果存在一次信息，且当前预报编号与一次的不一样

                    if (firstDb != null && !string.IsNullOrEmpty(firstDb.predictionNo) && !string.IsNullOrEmpty(PbCache.strCode))
                    {
                        if (PbCache.strCode != firstDb.predictionNo)
                        {
                            if (firstDb.predictionType != "2")//一次计量的计量预报如果不是联运预报，那么当前扫的码必须与前面那个一致
                            {
                                string str = "当前计量预报【" + PbCache.strCode + "】与上次未结净数据预报【" + firstDb.predictionNo + "】不一致，请重新扫码或语音求助!";
                                if (lblog != str)
                                {
                                    vicPlayClass.GetVoicePlay("预报不一致", PbCache.collect.carno);
                                    lblog = str;
                                    DoShowlbLog(lblog);
                                }
                                btnSave.Visible = false;
                                lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；当前计量预报【" + PbCache.strCode + "】与上次未结净数据预报【" + firstDb.predictionNo + "】不一致，保存按钮隐藏！");
                                return;
                            }
                            else //如果一次计量是联运预报，且预报的使用状态为4(正使用)则认为走了互锁式计量，因为非互锁的情况计量一次预报状态会变为2
                            {
                                RESTfulResult<List<PreTrackScale>> rm = trackService.doQueryWf(new PreTrackScale { predictionNo = firstDb.predictionNo, valueFlag = "4" });
                                if (rm.Succeed)
                                {
                                    if (rm.Data != null && rm.Data.Count > 0)
                                    {
                                        string str = "当前计量预报【" + PbCache.strCode + "】与上次未结净数据预报【" + firstDb.predictionNo + "】不一致，请重新扫码或语音求助!";
                                        if (lblog != str)
                                        {
                                            vicPlayClass.GetVoicePlay("预报不一致", PbCache.collect.carno);
                                            lblog = str;
                                            DoShowlbLog(lblog);
                                        }
                                        btnSave.Visible = false;
                                        lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；计量预报【" + PbCache.strCode + "】与上次未结净数据预报【" + firstDb.predictionNo + "】不一致，保存按钮隐藏！");
                                        return;
                                    }
                                    else //非互锁的情况计量，此时编号应该存在在一次计量中的编号中
                                    {
                                        string sCode = firstDb.predictionCombination.Split(',').ToList().Where(s => s.CompareTo(PbCache.strCode) == 0).FirstOrDefault();
                                        if (string.IsNullOrEmpty(sCode))
                                        {
                                            string str = "当前计量预报【" + PbCache.strCode + "】不在上次计量联运预报【" + firstDb.predictionCombination + "】中，请重新扫码或语音求助!";
                                            if (lblog != str)
                                            {
                                                vicPlayClass.GetVoicePlay("预报不一致", PbCache.collect.carno);
                                                lblog = str;
                                                DoShowlbLog(lblog);
                                            }
                                            btnSave.Visible = false;
                                            lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；计量预报【" + PbCache.strCode + "】不在上次计量联运预报【" + firstDb.predictionCombination + "】中，保存按钮隐藏！");
                                            return;
                                        }
                                    }
                                }
                                else
                                {
                                    lg.WriteLog(1, "查询预报编号异常");
                                    vicPlayClass.GetVoicePlay("未找到有效预报", PbCache.collect.carno);
                                    lblog = "获取一次计量时的预报失败，预报编号【" + firstDb.predictionNo + "】!";
                                    DoShowlbLog(lblog);
                                    btnSave.Visible = false;
                                    lg.WriteLog(14, "车号：" + PbCache.lockCarNo + "；获取一次计量时的预报失败，预报编号【" + firstDb.predictionNo + "】，保存按钮隐藏！");
                                    return;
                                }
                            }
                        }
                    }

                    #endregion 如果存在一次信息，且当前预报编号与一次的不一样
                }

                #endregion 查询扫码信息

                #region 毛皮净赋值

                if (PbCache.collect.weightStatus == 0 && firstWgt > 500)
                {
                    if (Math.Abs(firstWgt - PbCache.collect.weight) > AppConfigCache.differenceWgt)
                    {
                        //2021年5月10日界面显示单位，调整为T
                        if (PbCache.collect.weight > firstWgt)
                        {
                            txtGroess.Text = Math.Round((double)PbCache.collect.weight / 1000, 2) + "";
                            txtTare.Text = Math.Round(firstWgt / 1000, 2) + "";
                        }
                        else
                        {
                            txtTare.Text = Math.Round((double)PbCache.collect.weight / 1000, 2) + "";
                            txtGroess.Text = Math.Round(firstWgt / 1000, 2) + "";
                        }
                        txtNet.Text = Math.Round(Math.Abs(PbCache.collect.weight - firstWgt) / 1000, 2) + "";
                    }
                }

                #endregion 毛皮净赋值

                #region 界面未锁定的情况下，如果，有车号，重量稳定了,停车线验证通过，完成了扫码验证

                //没有远程接管，且已查过一次数据，且界面未锁定的情况下，如果，有车号，重量稳定了,停车线验证通过，完成了扫码验证的情况下，则按钮可用
                if (PbCache.monitor != null && PbCache.monitor.isTakeOver == "0" && !bRailwayActual
                    && !PbCache.isLockFrm && PbCache.collect.weightStatus == 0 && !string.IsNullOrEmpty(firstCode)
                    && PbCache.monitorResult.valid_park_status && ucWeightT1.isGreenWgt)
                {
                    if (!btnSave.Visible)
                    {
                        btnSave.Visible = true;
                    }
                    if (!isVoiceDownCar)
                    {
                        isVoiceDownCar = true;
                        vicPlayClass.GetVoicePlay("车上人员请下车", PbCache.collect.carno);
                        //txtMsgInfo.Text = "请核对车号后，扫描您手机中的二维码";
                    }
                }
                else
                {
                    if (btnSave.Visible && !PbCache.isLockFrm)
                    {
                        btnSave.Visible = false;
                        lg.WriteLog(14, "车号：" + PbCache.collect.carno + "；对射没对准【" + PbCache.monitorResult.valid_park_status + "】" +
                            "||重量不是绿色【" + ucWeightT1.isGreenWgt + "】" +
                            "||重量不稳定 0:重量稳定; 1:重量不稳定;  2:空磅【" + PbCache.collect.weightStatus + "】" +
                            "||不是智能计量状态【" + PbCache.monitor.isTakeOver + "】" +
                            "||扫码编号为空隐藏【" + firstCode + "】" +
                            "||一次计量数据已经查询完成【" + bRailwayActual + "】" +
                            "||界面已锁定【" + PbCache.isLockFrm + "】，保存按钮隐藏！");
                    }
                }

                #endregion 界面未锁定的情况下，如果，有车号，重量稳定了,停车线验证通过，完成了扫码验证
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, "采购销售页面timer1异常！异常信息：" + ex);
            }
        }

        #region 信息提示框

        private string lblog;//信息显示

        public string Lblog//信息显示
        {
            get { return lblog; }
            set
            {
                lblog = value;
                DoShowlbLog(value);
            }
        }

        private delegate void ShowlbLog(string lblog);

        private void DoShowlbLog(string _lbLog)
        {
            if (this.txtMsgInfo.InvokeRequired)
            {//在线程里以安全方式调用控件
                ShowlbLog _MyInvokeOne = new ShowlbLog(DoShowlbLog);
                txtMsgInfo.Invoke(_MyInvokeOne, new object[] { _lbLog });
            }
            else
            {
                txtMsgInfo.Text = _lbLog;
            }
            //2021年3月13日；不管是哪一种方式调用界面控件的值，均记录现场日志
            if (!string.IsNullOrEmpty(lblog) && PbCache.collect != null)
            {
                lg.WriteLog(9, "[" + PbCache.collect.carno + "]" + lblog);
            }
        }

        #endregion 信息提示框

        private void frmCarSalePurchase_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                //初始化一些线程及数据，并关闭该界面
                if (PbCache.collect != null && !string.IsNullOrEmpty(PbCache.collect.carno))
                {
                    PbCache.collect.carno = "";
                }
                isShowRecover = true;//是否去查询“复磅”按钮的显示与否
                PbCache.strCode = "";
                bRailwayActual = true; //为true时会重新查询一次计量数据
                isShowRecover = true;//是否去查询“复磅”按钮的显示与否
                PbCache.Load();
                //关闭一些线程及定时任务
                timer1.Stop();
                timer1.Dispose();

                #region 清理一次监控表的几个数值，防止上车速度过快导致提示称重完成

                //RESTfulResult<string> rms = monitor.doUpdateWf(new MeterWorkMonitor { pointNo = PbCache.sportInfo.baseSpotNo, isOverWgt = "" });

                RESTfulResult<string> rms = monitor.doUpdateWf(new MeterWorkMonitor
                {
                    pointNo = PbCache.sportInfo.baseSpotNo,
                    validWgt = "1",
                    validCarno = "1",
                    validTareTime = "1",
                    validMatTime = "1",
                    validTimeInterval = "1",
                    validParkStatus = "1",
                    validExceedWgt = "1",
                    validPredictionDiff = "1",
                    setCar = "",
                    rfidCar = "",
                    photoCar = "",
                    scanCar = "",
                    editCar = "",
                    carErr = "0",
                    wgtErr = "0",
                    stopOverTime = "0",
                    msgInfo = "",
                    isOverWgt = "",
                    isHelp = "0",
                    wgt = 0
                });
            }
            catch (Exception ex)
            {
                lg.WriteLog(0, "frmCarSalePurchase_FormClosing异常：下秤关闭界面异常！异常信息：" + ex);
            }

            #endregion 清理一次监控表的几个数值，防止上车速度过快导致提示称重完成
        }

        #region 打印

        private string strFirstNo = "";

        private class PrintCls
        {
            public int printNum { get; set; }
            public MeterWorkCarActual printDb { get; set; }
            public int iType { get; set; } //0毛 1 皮 2净
            public string qrCode { get; set; }
        }

        private PrintCls printData = new PrintCls();

        private void Print1(string firstNo)
        {
            try
            {
                //lg.WriteLog(1, "打印开始");

                #region 获取打印信息，若需要打印则调用打印操作

                List<PrintCls> prrintC = new List<PrintCls>();

                string sql = @"select actual_no actualNo,
                                   prediction_no predictionNo,
                                   car_no carNo,
                                   matter_no matterNo,
                                   matter_name matterName,
                                   gross_weight grossWeight,
                                   gross_time grossTime,
                                   tare_weight tareWeight,
                                   tare_time tareTime,
                                   net_weight netWeight,
                                   net_Time netTime,
                                   'x' meterTypeNo,
                                   meter_type_name meterTypeName,
                                   spec_name specName,
                                   forwarding_unit_name forwardingUnitName,
                                   receiving_uint_name receivingUintName,
                                   load_point_name loadPointName,
                                   add_weight addWeight,
                                   '1' uploadFlag --1结净数据 0一次表数据
                              from meter_work_car_actual where actual_first1_no = '" + firstNo + @"' or actual_first2_no='" + firstNo + @"'
                            union all
                            select t.actual_first_no      actualNo,
                                   t.prediction_no        predictionNo,
                                   t.car_no               carNo,
                                   t.matter_no            matterNo,
                                   t.matter_name          matterName,
                                   t.meter_weight         grossWeight,
                                   t.create_time          grossTime,
                                   0                      tareWeight,
                                   t.create_time          tareTime,
                                   0                      netWeight,
                                   null                   netTime,
                                   t.weight_type          meterTypeNo, --0毛重 否则皮重
                                   t.meter_type_name      meterTypeName,
                                   t.spec_name            specName,
                                   t.forwarding_unit_name forwardingUnitName,
                                   t.receiving_uint_name  receivingUintName,
                                   t.load_point_name      loadPointName,
                                   t.add_weight addWeight,
                                   '0' uploadFlag
                              from METER_WORK_CAR_ACTUAL_FIRST t
                                where t.actual_first_no = '" + firstNo + "' or  t.actual_first_no = (select max(actual_first2_no) from meter_work_car_actual where actual_first1_no='" + firstNo + "')";
                //查下如果有毛重或者结净数据，则可进入打印判断
                PbModelDbService<List<MeterWorkCarActual>> pbModelDbService = new PbModelDbService<List<MeterWorkCarActual>>();
                RESTfulResult<List<MeterWorkCarActual>> rm = pbModelDbService.executeSqlDataWf(sql);

                //lg.WriteLog(1, "打印第一个sql：" + sql);

                if (rm.Succeed)
                {                  
                    if (rm.Data != null && rm.Data.Count > 0)
                    {
                        //2021年6月26日 对一车联运的进行处理；用一次计量编号查二次计量数据，
                        sql = @"select t3.actual_no actualNo, t3.prediction_no predictionNo, t3.car_no carNo, t3.matter_no matterNo, t3.matter_name matterName,
                                t3.gross_weight grossWeight, t3.gross_time grossTime, t3.tare_weight tareWeight, t3.tare_time tareTime, t3.net_weight netWeight,
                                t3.net_Time netTime, 'x' meterTypeNo, t3.meter_type_name meterTypeName, t3.spec_name specName,
                                t3.forwarding_unit_name forwardingUnitName, t3.receiving_uint_name receivingUintName, t3.load_point_name loadPointName,
                                t3.add_weight addWeight, '1' uploadFlag from meter_work_car_actual_first t2
                                  left join meter_work_car_actual t3 on t2.actual_first_no = t3.actual_first1_no
                                 where t2.prediction_combination in (select t.prediction_combination
                                           from meter_work_car_actual_first t
                                      where t3.value_flag = '0' and t.actual_first_no = '" + firstNo + @"' and
                                            instr(t.prediction_combination, '20') > 0) order by t3.net_time desc";
                        RESTfulResult<List<MeterWorkCarActual>> rmLy = pbModelDbService.executeSqlDataWf(sql);
                        if (rmLy.Succeed && rmLy.Data != null && rmLy.Data.Count == 1)
                        {
                            rm.Data = rmLy.Data; //一车联运，将毛重替换为净重数据
                        }

                        //若有毛重或者净重，找到对应数据的物料信息，若有，那么去找打印配置信息数据
                        List<MeterWorkCarActual> mwa = rm.Data.Where(s => !string.IsNullOrEmpty(s.matterNo) && (s.meterTypeNo == "x" || s.meterTypeNo == "0")).ToList();
                        if (mwa != null && mwa.Count > 0 && !string.IsNullOrEmpty(mwa[0].matterNo))
                        {
                            sql = @"select BASE_SPOT_NO    baseSpotNo,
                                           load_point_no   loadPointNo,
                                           matter_no       matterNo,
                                           METER_TYPE_NO   meterTypeNo,
                                           weight_type_name weightTypeName,
                                           PRINT_NUM       printNum
                                      from METER_BASE_BILL_PRINT
                                     where matter_no = '" + mwa[0].matterNo + @"'
                                       and nvl(METER_TYPE_NO, '" + PbCache.bussinessTypeCode + @"') = '" + PbCache.bussinessTypeCode + @"'
                                       and nvl(BASE_SPOT_NO, '" + PbCache.sportInfo.baseSpotNo + @"') = '" + PbCache.sportInfo.baseSpotNo + @"'
                                       and nvl(load_point_no, '" + (mwa[0].loadPointNo == null ? "1" : mwa[0].loadPointNo) + @"') = '" + (mwa[0].loadPointNo == null ? "1" : mwa[0].loadPointNo) + @"'";

                            //lg.WriteLog(1, "打印第二个sql：" + sql);

                            //and nvl(METER_TYPE_NO, '') = ''
                            PbModelDbService<List<MeterBaseBillPrint>> pbPrintService = new PbModelDbService<List<MeterBaseBillPrint>>();
                            RESTfulResult<List<MeterBaseBillPrint>> rmPint = pbPrintService.executeSqlDataWf(sql);
                            if (rmPint.Succeed)
                            {
                                if (rmPint.Data != null && rmPint.Data.Count > 0)
                                {
                                    //若有净重信息：1毛重时间晚于皮重时间，则打印毛、皮、净重，2毛重早于皮重，打印皮、净
                                    //若只有毛重信息则打印毛重
                                    List<MeterBaseBillPrint> lGrossprint = rmPint.Data.Where(s => s.weightTypeName.Contains("毛")).ToList();
                                    List<MeterBaseBillPrint> lTarePrint = rmPint.Data.Where(s => s.weightTypeName.Contains("皮")).ToList();
                                    List<MeterBaseBillPrint> lNetprint = rmPint.Data.Where(s => s.weightTypeName.Contains("净")).ToList();

                                    List<MeterWorkCarActual> mwaChild = mwa.Where(s => s.meterTypeNo == "x").ToList();
                                    if (mwaChild != null && mwaChild.Count > 0) //若有净重信息
                                    {
                                        if (lTarePrint != null && lTarePrint.Count > 0)
                                        {
                                            //获取非毛，非净重信息如果没有则用的期限皮，此时不打，否则则打印
                                            MeterWorkCarActual mwca = rm.Data.Where(s => s.meterTypeNo != "x" && s.meterTypeNo != "0").FirstOrDefault();
                                            if (mwca != null)
                                            {
                                                PrintCls cls = new PrintCls();
                                                cls.printNum = (int)(lTarePrint[0].printNum == null ? 0 : lTarePrint[0].printNum.Value);
                                                cls.iType = 1;//皮重
                                                cls.printDb = mwca;
                                                prrintC.Add(cls);
                                            }
                                        }
                                        if (lGrossprint != null && lGrossprint.Count > 0)
                                        {
                                            //获取毛重信息
                                            MeterWorkCarActual mwca = rm.Data.Where(s => s.meterTypeNo == "0").FirstOrDefault();
                                            if (mwca != null)
                                            {
                                                PrintCls cls = new PrintCls();
                                                cls.printNum = (int)(lGrossprint[0].printNum == null ? 0 : lGrossprint[0].printNum.Value);
                                                cls.iType = 0;//毛重
                                                cls.printDb = mwca;
                                                prrintC.Add(cls);
                                            }
                                        }
                                        if (lNetprint != null && lNetprint.Count > 0)
                                        {
                                            //获取净重信息
                                            MeterWorkCarActual mwca = rm.Data.Where(s => s.meterTypeNo == "x").FirstOrDefault();
                                            if (mwca != null)
                                            {
                                                PrintCls cls = new PrintCls();
                                                cls.printNum = (int)(lNetprint[0].printNum == null ? 0 : lNetprint[0].printNum.Value);
                                                cls.iType = 2;//净重
                                                cls.printDb = mwca;
                                                prrintC.Add(cls);
                                            }
                                        }
                                    }
                                    else //只有毛重信息则打印毛重
                                    {
                                        if (lGrossprint != null && lGrossprint.Count > 0)
                                        {
                                            //获取毛重信息
                                            MeterWorkCarActual mwca = rm.Data.Where(s => s.meterTypeNo == "0").FirstOrDefault();
                                            if (mwca != null)
                                            {
                                                PrintCls cls = new PrintCls();
                                                cls.printNum = (int)(lGrossprint[0].printNum == null ? 0 : lGrossprint[0].printNum.Value);
                                                cls.iType = 0;//毛重
                                                cls.printDb = mwca;
                                                prrintC.Add(cls);
                                            }
                                        }
                                    }

                                    if (prrintC != null && prrintC.Count > 0)
                                    {
                                        FrmMessage frmMessage = FrmMess("是否打印第【1】张票据！", "打印");
                                        if (frmMessage.ShowDialog(this) == DialogResult.OK)
                                        {
                                            lg.WriteLog(10, "车号【" + PbCache.lockCarNo + "】是否打印选择了【是】");
                                            lg.WriteLog(15, PbCache.lockCarNo + "是否打印选择了【是】");
                                            strFirstNo = firstNo;
                                            //printDocument1.PrinterSettings.PrinterName = PbCache.sportInfo.printerName;//strPntSvrName;
                                            System.Drawing.Printing.PaperSize customSize1 = new System.Drawing.Printing.PaperSize("TestPaper", 398, 433); //101mm*110mm,换算成英寸
                                            printDocument1.DefaultPageSettings.PaperSize = customSize1;//将自定义的Papersize赋给PrintDocument
                                            System.Drawing.Printing.Margins marginss = new System.Drawing.Printing.Margins(0, 0, 0, 0);
                                            printDocument1.DefaultPageSettings.Margins = marginss;
                                            printDocument1.PrintController = new System.Drawing.Printing.StandardPrintController();

                                            foreach (PrintCls db in prrintC)
                                            {
                                                switch (db.printDb.meterTypeNo)
                                                {
                                                    case "0": //毛
                                                        db.qrCode = db.printDb.actualNo + "," + db.printDb.grossWeight + "," + "1";
                                                        break;

                                                    case "x": //净
                                                        db.qrCode = db.printDb.actualNo + "," + db.printDb.netWeight + "," + "2";
                                                        break;

                                                    default: //皮
                                                        db.qrCode = db.printDb.actualNo + "," + db.printDb.grossWeight + "," + "1";
                                                        break;
                                                }
                                                RESTfulResult<string> rQRCode = workCarActualFirstService.doQRCodeWf(db.qrCode);
                                                db.qrCode = rQRCode.Data;

                                                for (int i = 0; i < db.printNum; i++)
                                                {
                                                    if (i == 0)
                                                    {
                                                        printData = db;
                                                        printDocument1.Print();
                                                        lg.WriteLog(10, "当前第" + (i + 1) + "次打印：" + printData.iType + "  车号：" + db.printDb.carNo + ",二维码:" + db.qrCode);
                                                    }
                                                    else
                                                    {
                                                        FrmMessage frmMessageContinue = FrmMess("已打印【" + i + "】张，是否继续打印第【" + (i + 1) + "】张票据！", "打印");
                                                        if (frmMessageContinue.ShowDialog(this) == DialogResult.OK)
                                                        {
                                                            printData = db;
                                                            printDocument1.Print();
                                                            lg.WriteLog(10, "当前第" + (i + 1) + "次打印：" + printData.iType + "  车号：" + db.printDb.carNo + ",二维码:" + db.qrCode);
                                                        }
                                                        else
                                                        {
                                                            lg.WriteLog(10, "车号【" + PbCache.lockCarNo + "】第" + i + "次是否打印，选择了【否】");
                                                        }
                                                    }
                                                    //printData = db;
                                                    //printDocument1.Print();
                                                    //lg.WriteLog(11, "当前打印：" + printData.iType + "  车号：" + db.printDb.carNo);
                                                }
                                            }
                                        }
                                        else
                                        {
                                            lg.WriteLog(10, "车号【" + PbCache.lockCarNo + "】是否打印选择了【否】");
                                            lg.WriteLog(15, PbCache.lockCarNo + "是否打印选择了【否】");
                                        }
                                    }
                                }
                            }
                            else
                            {
                                lg.WriteLog(10, "车号【" + PbCache.lockCarNo + "】获取打印配置数据信息异常sql:【" + sql + "】");
                            }
                        }
                    }
                }
                else
                {
                    lg.WriteLog(10, "车号【" + PbCache.lockCarNo + "】获取计量数据信息异常sql:【" + sql + "】");
                }

                #endregion 获取打印信息，若需要打印则调用打印操作
            }
            catch (Exception ex)
            {
                lg.WriteLog(10, "车号【" + PbCache.lockCarNo + "】打印异常" + ex);
            }
        }

        private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            try
            {
                if (printData != null && printData.printDb != null)
                {
                    printClass.PrintAllPage(e, printData.printDb, printData.qrCode);

                    /*
                    //结净打印
                    if (printData.iType == 2)
                    {
                        printClass.PrintPage(e, printData.printDb, PbCache.strQRCode);
                    }
                    else //非结净打印
                    {
                        printClass.PrintWgtPage(e, printData.printDb, PbCache.strQRCode);
                    }
                    //*/
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(11, "打印异常" + ex);
            }
        }

        #endregion 打印

        private void btnFH_Click(object sender, EventArgs e)
        {
            lg.WriteLog(15, PbCache.lockCarNo + "点击了返回主界面");
            this.Close();
        }

        private static FrmMessage FrmMess(string strMess, string strBtnSaveInfo)
        {
            FrmMessage frmMessage = new FrmMessage();
            frmMessage.StartPosition = FormStartPosition.Manual;
            frmMessage.Location = new Point(350, 450);
            frmMessage.ShowMessage = strMess;
            frmMessage.BtnSaveText = strBtnSaveInfo;
            frmMessage.BtnCancelText = "取消";
            return frmMessage;
        }

        private void btnTare_Click(object sender, EventArgs e)
        {
            try
            {
                if (PbCache.collect == null) return;
                //界面锁定
                PbCache.lockWgt = PbCache.collect.weight;
                PbCache.lockCarNo = PbCache.collect.carno;
                //PbCache.lockCarNo = TrackCarNo;
                btnTare.Enabled = false;
                lg.WriteLog(15, PbCache.lockCarNo + "点击期限皮重按钮");

                //2021年3月13日 杨秀东新增逻辑，车号为空则不允许进行期限皮重保存操作
                if (string.IsNullOrEmpty(PbCache.lockCarNo))
                {
                    vicPlayClass.GetVoicePlay("不允许进行期限皮重保存", PbCache.lockCarNo);
                    PbCache.ResultMessage = "未抓拍到车牌号，进行期限皮重保存，请点[语音求助]！";
                    setMsg(PbCache.ResultMessage);
                    btnTare.Enabled = true;
                    return;
                }
                //*
                //查一次是否可留期限皮
                if (!termTareCar.ValidMethod(PbCache.lockCarNo))
                {
                    vicPlayClass.GetVoicePlay("不允许进行期限皮重保存", PbCache.lockCarNo);
                    setMsg(PbCache.ResultMessage);
                    btnTare.Enabled = true;
                    return;
                }
                //*/

                //暂停取实时数据
                setMsg("称重保存中......");
                FrmMessage frmMessage = FrmMess("是否确认保存期限皮重！", "继续保存");
                if (frmMessage.ShowDialog(this) == DialogResult.Cancel)
                {
                    setMsg("");
                    btnTare.Enabled = true;
                    lg.WriteLog(15, PbCache.lockCarNo + "期限皮重继续保存选择了【否】");
                    return;
                }

                MeterBaseTermTareData mbttd = new MeterBaseTermTareData();
                mbttd.baseSpotNo = PbCache.sportInfo.baseSpotNo;
                mbttd.baseSpotName = PbCache.sportInfo.baseSpotName;
                mbttd.carNo = PbCache.collect.carno;
                mbttd.meterWeight = PbCache.lockWgt;
                mbttd.valueFlag = "0";
                mbttd.createTime = DateTime.Now;
                mbttd.createManNo = PbCache.sportInfo.baseSpotNo;
                mbttd.createManName = PbCache.sportInfo.baseSpotName;
                mbttd.meterTime = mbttd.createTime.Value;
                mbttd.startTime = mbttd.createTime.Value;

                //操作日志
                MeterMonitorNote monitorNote = new MeterMonitorNote();
                monitorNote.baseSpotNo = mbttd.baseSpotNo;
                monitorNote.baseSpotName = mbttd.baseSpotName;
                monitorNote.operationContent = string.Format("保存期限皮重");
                monitorNote.operationTime = mbttd.createTime.Value.ToString("yyyy-MM-dd HH:mm:ss");
                monitorNote.carNo = mbttd.carNo;
                monitorNote.meterNoteSource = "0";

                //期限皮重数据保存
                RESTfulResult<string> RESTfulResult = termTareDate.doSaveWf(mbttd, monitorNote);
                if (RESTfulResult.Succeed)
                {
                    //保存成功，语音提示保存成功
                    vicPlayClass.GetVoicePlay("期限皮重保存成功", PbCache.lockCarNo);//播放语音
                    btnTare.Visible = false;
                    lg.WriteLog(3, string.Format("车号[{0}]期限皮重保存成功", PbCache.lockCarNo));
                    setMsg("称重保存成功，请离开秤台！");
                }
                else
                {
                    btnTare.Enabled = true;
                    setMsg(RESTfulResult.ResultMessage);
                    vicPlayClass.GetVoicePlay("期限皮重保存失败", PbCache.lockCarNo);//播放语音
                    lg.WriteLog(3, string.Format("车号[{0}]期限皮重保存失败", PbCache.lockCarNo));
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, PbCache.lockCarNo + "期限皮重事件异常：" + ex.Message);
            }
        }

        /// <summary>
        /// 复磅保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRecover_Click(object sender, EventArgs e)
        {
            try
            {
                if (PbCache.collect == null) return;
                PbCache.isLockFrm = true;
                PbCache.lockCarNo = PbCache.collect.carno;
                PbCache.lockWgt = PbCache.collect.weight;
                btnRecover.Enabled = false;
                lg.WriteLog(15, PbCache.lockCarNo + "点击【复磅】按钮");

                //验证是否满足复磅的条件
                recoverInfo.ValidMethod(PbCache.lockCarNo);
                if (!PbCache.isAllowRecover)
                {
                    PbCache.isLockFrm = false;
                    btnRecover.Enabled = true;
                    setMsg(PbCache.ResultMessage);
                    lg.WriteLog(15, PbCache.lockCarNo + "不满足复磅条件！" + PbCache.ResultMessage);
                    return;
                }

                txtMsgInfo.Text = "复磅保存中......";
                FrmMessage frmMessage = FrmMess("是否确认保存当前复磅重量数据！", "继续保存");
                if (frmMessage.ShowDialog(this) == DialogResult.Cancel)
                {
                    PbCache.isLockFrm = false;
                    btnRecover.Enabled = true;
                    txtMsgInfo.Text = "";
                    lg.WriteLog(15, PbCache.lockCarNo + "【复磅】按钮选择否");
                    return;
                }

                //复磅数据保存
                RESTfulResult<string> RESTfulResult = carRecoverService.doRecover(new MeterWorkCarRecover { carNo = PbCache.lockCarNo, recMeterWeight = PbCache.lockWgt });
                if (RESTfulResult.Succeed)
                {
                    //保存成功，语音提示保存成功
                    vicPlayClass.GetVoicePlay("称重完成", PbCache.lockCarNo);//播放语音
                    btnTare.Visible = false;
                    lg.WriteLog(3, string.Format("车号[{0}]复磅保存成功", PbCache.lockCarNo));
                    setMsg("复磅保存成功，请离开秤台！");
                }
                else
                {
                    PbCache.isLockFrm = false;
                    btnRecover.Enabled = true;
                    setMsg(RESTfulResult.ResultMessage);
                    vicPlayClass.GetVoicePlay("称重数据保存失败", PbCache.lockCarNo);//播放语音
                    lg.WriteLog(3, string.Format("车号[{0}]复磅保存失败" + RESTfulResult.Message, PbCache.lockCarNo));
                }
            }
            catch (Exception ex)
            {
                lg.WriteLog(13, PbCache.lockCarNo + "复磅保存事件异常：" + ex.Message);
            }
        }

        #region 信息提示框

        public void setMsgInfo(string ResultMessage)
        {
            setMsg(ResultMessage);
        }

        private void setMsg(string ResultMessage)
        {
            if (txtMsgInfo.InvokeRequired)
            {
                Action<string> action = new Action<string>(setMsgInfo);
                Invoke(action, new object[] { ResultMessage });
            }
            else
            {
                txtMsgInfo.Text = ResultMessage;
            }
            if (!string.IsNullOrEmpty(ResultMessage) && PbCache.collect != null)
            {
                lg.WriteLog(9, "[" + PbCache.collect.carno + "]" + ResultMessage);
            }
        }

        #endregion 信息提示框

        /// <summary>
        /// 车号改变时，判断是否需要显示复磅按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtCarNo_TextChanged(object sender, EventArgs e)
        {
            //第一次判断由timerk控件执行
            if (!string.IsNullOrEmpty(txtCarNo.Text) && !isShowRecover)
            {
                recoverInfo.ValidMethod(txtCarNo.Text);
                if (PbCache.isAllowRecover)
                {
                    btnRecover.Visible = true;
                }
                else
                {
                    btnRecover.Visible = false;
                }
                isShowRecover = false;
            }
        }
    }
}