/// <summary>
 /// 格式化单元格颜色
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void superGridControl_FormatCellColor_GetCellStyle(object sender, GridGetCellStyleEventArgs e)
 {
     if (e.GridCell.GridColumn.DataPropertyName == "TrainNumber")
     {
         View_TrainTipperQueue view_TrainTipperQueue = e.GridCell.GridRow.DataItem as View_TrainTipperQueue;
         if (view_TrainTipperQueue != null)
         {
             e.Style.Background.Color1 = this.dicCellColors[view_TrainTipperQueue.InFactoryBatchId + "-" + view_TrainTipperQueue.TrainSampleSchemeId];
         }
     }
 }
        /// <summary>
        /// 发送采样计划,若存在则更新
        /// </summary>
        /// <param name="view_TrainTipperQueue"></param>
        /// <returns></returns>
        public bool SendSamplePlan(View_TrainTipperQueue view_TrainTipperQueue)
        {
            CmcsInFactoryBatch inFactoryBatch = Dbers.GetInstance().SelfDber.Get <CmcsInFactoryBatch>(view_TrainTipperQueue.InFactoryBatchId);

            if (inFactoryBatch == null)
            {
                throw new ArgumentNullException("inFactoryBatchId", "未找到Id=" + view_TrainTipperQueue.InFactoryBatchId + "的批次");
            }

            CmcsFuelKind fuelKind             = Dbers.GetInstance().SelfDber.Get <CmcsFuelKind>(inFactoryBatch.FuelKindId);
            List <View_TrainTipperQueue> list = trainTipperDAO.GetView_TrainTipperQueueBy(view_TrainTipperQueue.TrainSampleSchemeId);

            InfBeltSamplePlan oldBeltSamplePlan = Dbers.GetInstance().SelfDber.Entity <InfBeltSamplePlan>("where InFactoryBatchId=:InFactoryBatchId and SampleCode=:SampleCode", new { InFactoryBatchId = view_TrainTipperQueue.InFactoryBatchId, SampleCode = view_TrainTipperQueue.YuSampleCode });

            if (oldBeltSamplePlan == null)
            {
                Log4Neter.Info(this.trainTipper.EquipmentName + " - 向[" + this.trainBeltSampler.EquipmentCode + "]发送采样计划,采样码:" + view_TrainTipperQueue.YuSampleCode);

                return(Dbers.GetInstance().SelfDber.Insert <InfBeltSamplePlan>(new InfBeltSamplePlan
                {
                    DataFlag = 0,
                    InterfaceType = this.trainBeltSampler.InterfaceType,
                    InFactoryBatchId = view_TrainTipperQueue.InFactoryBatchId,
                    SampleCode = view_TrainTipperQueue.YuSampleCode,
                    FuelKindName = fuelKind != null ? fuelKind.Name : string.Empty,
                    CarCount = list.Count,
                    Mt = 0,
                    TicketWeight = list.Sum(a => a.TicketQty),
                    SampleType = eEquInfSampleType.到集样罐.ToString(),
                    GatherType = commonDAO.GetCommonAppletConfigString(this.trainBeltSampler.EquipmentCode + "集样方式")
                }) > 0);
            }
            else
            {
                Log4Neter.Info(this.trainTipper.EquipmentName + " - 向[" + this.trainBeltSampler.EquipmentCode + "]更新采样计划,采样码:" + view_TrainTipperQueue.YuSampleCode);

                oldBeltSamplePlan.DataFlag     = 0;
                oldBeltSamplePlan.FuelKindName = fuelKind != null ? fuelKind.Name : string.Empty;
                oldBeltSamplePlan.CarCount     = list.Count;
                oldBeltSamplePlan.Mt           = 0;
                oldBeltSamplePlan.TicketWeight = list.Sum(a => a.TicketQty);
                oldBeltSamplePlan.SampleType   = eEquInfSampleType.到集样罐.ToString();
                oldBeltSamplePlan.GatherType   = commonDAO.GetCommonAppletConfigString(this.trainBeltSampler.EquipmentCode + "集样方式");

                return(Dbers.GetInstance().SelfDber.Update(oldBeltSamplePlan) > 0);
            }
        }
        /// <summary>
        /// 发送结束采样命令
        /// </summary>
        /// <param name="view_TrainTipperQueue"></param>
        private bool SendSamplerStopCmd(View_TrainTipperQueue view_TrainTipperQueue)
        {
            this.lastView_TrainTipperQueue = null;

            bool res = false;

            // 检测采样机系统的状态
            string samplerSystemStatue = commonDAO.GetSignalDataValue(this.trainBeltSampler.EquipmentCode, eSignalDataName.系统.ToString());

            if (samplerSystemStatue == eEquInfSamplerSystemStatus.正在运行.ToString())
            {
                string sampleCode = view_TrainTipperQueue != null ? view_TrainTipperQueue.YuSampleCode : string.Empty;

                Log4Neter.Info(this.trainTipper.EquipmentName + " - 向[" + this.trainBeltSampler.EquipmentCode + "]发送[结束采样]命令,采样码:" + sampleCode);

                string cmdId;
                bool   sendSuccess = beltSamplerDAO.SendSampleCmd(this.trainBeltSampler.EquipmentCode, eEquInfSamplerCmd.结束采样, sampleCode, out cmdId);

                ChangeUIEnabled(false);
                OutputRunInfo("等待皮带采样机返回命令(结束采样)执行结果");

                eEquInfCmdResultCode equInfCmdResultCode;
                do
                {
                    Thread.Sleep(10000);

                    equInfCmdResultCode = beltSamplerDAO.GetSampleCmdResult(cmdId);
                }while (sendSuccess && equInfCmdResultCode == eEquInfCmdResultCode.默认);

                res = equInfCmdResultCode == eEquInfCmdResultCode.成功;

                ChangeUIEnabled(true);
                OutputRunInfo("皮带采样机执行命令(结束采样)" + (res ? "成功" : "失败"));

                return(res);
            }
            else
            {
                OutputRunInfo(this.trainBeltSampler.EquipmentCode + "未处于正在运行状态,无须发送结束采样命令");
                Log4Neter.Info(this.trainTipper.EquipmentName + " - " + this.trainBeltSampler.EquipmentCode + "未处于正在运行状态,无须发送结束采样命令");

                return(true);
            }
        }
        /// <summary>
        /// 发送开始采样命令
        /// </summary>
        /// <param name="view_TrainTipperQueue"></param>
        /// <returns></returns>
        private bool SendSamplerStartCmd(View_TrainTipperQueue view_TrainTipperQueue)
        {
            this.lastView_TrainTipperQueue = view_TrainTipperQueue;

            bool res = false;

            // 检测采样机系统的状态
            string samplerSystemStatue = commonDAO.GetSignalDataValue(this.trainBeltSampler.EquipmentCode, eSignalDataName.系统.ToString());

            if (samplerSystemStatue == eEquInfSamplerSystemStatus.就绪待机.ToString() || samplerSystemStatue == eEquInfSamplerSystemStatus.正在运行.ToString())
            {
                Log4Neter.Info(this.trainTipper.EquipmentName + " - 向[" + this.trainBeltSampler.EquipmentCode + "]发送[开始采样]命令,采样码:" + view_TrainTipperQueue.YuSampleCode);

                // 发送采样计划
                if (SendSamplePlan(view_TrainTipperQueue))
                {
                    string cmdId;
                    bool   sendSuccess = beltSamplerDAO.SendSampleCmd(this.trainBeltSampler.EquipmentCode, eEquInfSamplerCmd.开始采样, view_TrainTipperQueue.YuSampleCode, out cmdId);

                    ChangeUIEnabled(false);
                    OutputRunInfo("等待皮带采样机返回命令(开始采样)执行结果");

                    eEquInfCmdResultCode equInfCmdResultCode;
                    do
                    {
                        Thread.Sleep(10000);

                        equInfCmdResultCode = beltSamplerDAO.GetSampleCmdResult(cmdId);
                    }while (sendSuccess && equInfCmdResultCode == eEquInfCmdResultCode.默认);

                    res = equInfCmdResultCode == eEquInfCmdResultCode.成功;

                    ChangeUIEnabled(true);
                    OutputRunInfo("皮带采样机执行命令(开始采样)" + (res ? "成功" : "失败"));
                }
            }
            else
            {
                Log4Neter.Info(this.trainTipper.EquipmentName + " - " + this.trainBeltSampler.EquipmentCode + "未处于" + samplerSystemStatue + "状态,禁止发送开始采样命令");
            }

            return(res);
        }
        /// <summary>
        /// 设置为当前翻车
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAddTrainCarriagePass_Click(object sender, EventArgs e)
        {
            GridRow gridRow = superGridControl_DF.PrimaryGrid.SelectedRows.OfType <GridRow>().FirstOrDefault();

            if (gridRow == null)
            {
                return;
            }

            View_TrainTipperQueue view_TrainTipperQueue = gridRow.DataItem as View_TrainTipperQueue;

            if (view_TrainTipperQueue == null)
            {
                return;
            }

            if (this.CurrentTrainCarriagePass == null ||
                (this.CurrentTrainCarriagePass != null && (view_TrainTipperQueue.TrainNumber != this.CurrentTrainCarriagePass.TrainNumber ||
                                                           (view_TrainTipperQueue.TrainNumber == this.CurrentTrainCarriagePass.TrainNumber &&
                                                            MessageBoxEx.Show("此车已经处于翻车中,确定要重新设置?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes))))
            {
                // 跳过车厢设置时进行提示
                if (view_TrainTipperQueue != this.view_TrainTipperQueue_DF.FirstOrDefault() && MessageBoxEx.Show("车厢[" + view_TrainTipperQueue.TrainNumber + "]不是当前队列中第一节车厢,确定要设置?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                {
                    return;
                }

                // 插入车号识别通过记录
                if (carriageRecognitionerDAO.SaveTrainCarriagePass(this.carriageRecognitionerMachineCode, view_TrainTipperQueue.TrainNumber, DateTime.Now, eTrainPassDirection.进厂))
                {
                    Log4Neter.Info(this.trainTipper.EquipmentName + " - 设置车厢[" + view_TrainTipperQueue.TrainNumber + "]为当前翻车");
                    OutputRunInfo("设置车厢[" + view_TrainTipperQueue.TrainNumber + "]为当前翻车");
                }
                else
                {
                    MessageBoxEx.Show("设置失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
        /// <summary>
        /// 接收车号识别数据,开始翻车逻辑控制
        /// </summary>
        /// <param name="inTrainCarriagePass"></param>
        private void StartTippingTask(CmcsTrainCarriagePass trainCarriagePass)
        {
            Task task = new Task((state) =>
            {
                CmcsTrainCarriagePass inTrainCarriagePass = state as CmcsTrainCarriagePass;
                if (inTrainCarriagePass != null)
                {
                    #region 车号为空时,执行车号补录

                    if (string.IsNullOrEmpty(inTrainCarriagePass.TrainNumber))
                    {
                        Log4Neter.Info(this.trainTipper.EquipmentName + " - 车号识别失败,要求输入车号");

                        this.InvokeEx(() => { Form1.superTabControlManager.ChangeToTab(this.trainTipper.EquipmentCode); });

                        // 弹出输入框,要求输入车厢号
                        FrmInput frmInput = new FrmInput("请输入所翻车厢号", (input) =>
                        {
                            if (string.IsNullOrEmpty(input))
                            {
                                MessageBoxEx.Show("请输入车厢号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return(false);
                            }

                            if (!this.view_TrainTipperQueue_DF.Any(a => a.TrainNumber == input))
                            {
                                MessageBoxEx.Show("在队列中未找到此车,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return(false);
                            }

                            return(true);
                        });

                        if (frmInput.ShowDialog() == DialogResult.OK)
                        {
                            Log4Neter.Info(this.trainTipper.EquipmentName + " - 用户输入:" + frmInput.Input);
                            inTrainCarriagePass.TrainNumber = frmInput.Input;
                        }
                        else
                        {
                            Log4Neter.Info(this.trainTipper.EquipmentName + " - 用户关闭输入窗口");

                            inTrainCarriagePass.DataFlag = 1;
                            Dbers.GetInstance().SelfDber.Update(inTrainCarriagePass);

                            autoResetEvent.Set();
                        };
                    }

                    #endregion

                    Log4Neter.Info(this.trainTipper.EquipmentName + " - 当前车号:" + inTrainCarriagePass.TrainNumber);
                    commonDAO.SetSignalDataValue(this.trainTipper.EquipmentCode, eSignalDataName.当前车号.ToString(), inTrainCarriagePass.TrainNumber);

                    View_TrainTipperQueue selfView_TrainTipperQueue = this.view_TrainTipperQueue_All.FirstOrDefault(a => a.TrainNumber == inTrainCarriagePass.TrainNumber);
                    if (selfView_TrainTipperQueue != null)
                    {
                        commonDAO.SetSignalDataValue(this.trainTipper.EquipmentCode, eSignalDataName.当前车Id.ToString(), selfView_TrainTipperQueue.TransportId);

                        if (selfView_TrainTipperQueue.SampleType != eSamplingType.皮带采样.ToString())
                        {
                            // 采样方案中设置为非火车皮采

                            Log4Neter.Info(this.trainTipper.EquipmentName + " - 采样方案中设置为非皮带采样,SampleType=" + selfView_TrainTipperQueue.SampleType);

                            DialogResult dialogResult = MessageBoxEx2Show("<font size='+2'>车号: <font color='red'>" + selfView_TrainTipperQueue.TrainNumber + "</font><br/><br/>该车为" + selfView_TrainTipperQueue.SampleType + "<br/><br/>点击<font color='red'>[是]</font>立即通知皮带采样机停止采样<br/>然后在采样机成功停止后开始翻车<br/><br/>点击<font color='red'>[否]</font>直接开始翻车<br/><br/>点击<font color='red'>[取消]</font>不做任何处理</font>", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning);
                            if (dialogResult == DialogResult.Yes)
                            {
                                if (SendSamplerStopCmd(selfView_TrainTipperQueue))
                                {
                                    // 标记为已处理
                                    ToHandled(inTrainCarriagePass.Id, selfView_TrainTipperQueue.Id);
                                }
                            }
                            else if (dialogResult == DialogResult.No)
                            {
                                // 标记为已处理
                                ToHandled(inTrainCarriagePass.Id, selfView_TrainTipperQueue.Id);
                            }
                        }
                        else
                        {
                            if (MessageBoxEx2Show("<font size='+2'>车号: <font color='red'>" + selfView_TrainTipperQueue.TrainNumber + "</font><br/><br/>点击<font color='red'>[确定]</font>立即通知皮带采样机开始采样<br/>确认采样机启动成功后再开始翻车<br/><br/>点击<font color='red'>[取消]</font>不做任何处理</font>", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
                            {
                                if (SendSamplerStartCmd(selfView_TrainTipperQueue))
                                {
                                    // 标记为已处理
                                    ToHandled(inTrainCarriagePass.Id, selfView_TrainTipperQueue.Id);
                                }
                            }
                        }
                    }
                    else
                    {
                        // 未找到此车

                        commonDAO.SetSignalDataValue(this.trainTipper.EquipmentCode, eSignalDataName.当前车Id.ToString(), string.Empty);

                        if (MessageBoxEx2Show("未找到车厢[" + inTrainCarriagePass.TrainNumber + "],是否忽略?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            // 标记车号识别记录为已处理
                            carriageRecognitionerDAO.ChangeTrainCarriagePassToHandled(inTrainCarriagePass.Id);
                        }
                    }
                }

                autoResetEvent.Set();
            }, trainCarriagePass);

            task.Start();
        }