///// <summary>
        /////  获取某类型的设备
        ///// </summary>
        ///// <param name="deviceCategoryId">设备类型</param>
        ///// <returns></returns>
        //public static string GenDevice(int deviceCategoryId)
        //{
        //    _devices = RedisHelper.Get<List<SmartDeviceProcess>>(DeviceKey);
        //    var 闲置设备 = _devices.Where(x=>x.State == SmartDeviceState.未加工)
        //    return results.FirstOrDefault() ?? "";
        //}

        private static void Simulate()
        {
            var createUserId   = "System";
            var markedDateTime = DateTime.Now;
            var faults         = new List <SmartProcessFault>();
            var processDevices = ServerConfig.ApiDb.Query <SmartFlowCardProcessDevice>("SELECT a.*, b.CategoryId, b.ProcessNumber, b.ProcessData FROM " +
                                                                                       "(SELECT * FROM `t_flow_card_process` WHERE MarkedDelete = 0 AND `State` != @state GROUP BY FlowCardId) a " +
                                                                                       "JOIN (SELECT a.Id, b.CategoryId, b.Process, a.ProcessNumber, a.ProcessData FROM `t_product_process` a " +
                                                                                       "JOIN (SELECT a.Id, b.CategoryId, b.Process FROM `t_process_code_category_process` a " +
                                                                                       "JOIN `t_process` b ON a.ProcessId = b.Id) b ON a.ProcessId = b.Id) b ON a.ProcessId = b.Id WHERE a.MarkedDelete = 0 ORDER BY a.Id;", new { state = SmartFlowCardProcessState.已完成 });
            var categroy0   = new List <int>();
            var categroy_0  = new List <int>();
            var dict        = new Dictionary <int, bool>();
            var processTime = 10;
            var checkTime   = 10;
            var rate        = 85;

            foreach (var processDevice in processDevices)
            {
                if (processDevice.Fault)
                {
                    continue;
                }
                if (processDevice.Before <= 0)
                {
                    faults.Add(new SmartProcessFault
                    {
                        CreateUserId   = createUserId,
                        MarkedDateTime = markedDateTime,
                        FaultTime      = markedDateTime,
                        Type           = ProcessFault.缺少原材料,
                        DeviceId       = processDevice.DeviceId,
                        FlowCardId     = processDevice.FlowCardId,
                        ProcessId      = processDevice.Id,
                    });
                    processDevice.State = SmartFlowCardProcessState.暂停中;
                    processDevice.Fault = true;
                    categroy0.Add(processDevice.Id);
                    continue;
                }

                if (processDevice.CategoryId == 0)
                {
                    if (!dict.ContainsKey(processDevice.CategoryId))
                    {
                        dict.Add(processDevice.CategoryId, true);
                    }
                    if (processDevice.State == SmartFlowCardProcessState.未加工 || processDevice.State == SmartFlowCardProcessState.等待中)
                    {
                        if (dict[processDevice.CategoryId])
                        {
                            if (ArrangeProcess("检验", out var processorId) == ScheduleState.成功)
                            {
                                processDevice.State       = SmartFlowCardProcessState.加工中;
                                processDevice.ProcessorId = processorId;
                                processDevice.StartTime   = markedDateTime;
                                processDevice.Count++;
                                var productProcess = SmartProductProcessHelper.Instance.Get <SmartProductProcess>(processDevice.ProcessId);
                                processDevice.Doing = processDevice.Left < productProcess.ProcessNumber ? processDevice.Left : productProcess.ProcessNumber;
                                categroy0.Add(processDevice.Id);
                            }
                        }
                        else
                        {
                            dict[processDevice.CategoryId] = false;
                        }
                    }
                    else if (processDevice.State == SmartFlowCardProcessState.加工中)
                    {
                        var productProcess = SmartProductProcessHelper.Instance.Get <SmartProductProcess>(processDevice.ProcessId);
                        var endTime        = processDevice.StartTime.AddSeconds((double)(productProcess.TotalSecond < checkTime ? checkTime : productProcess.TotalSecond));
                        if (endTime <= markedDateTime)
                        {
                            processDevice.EndTime = markedDateTime;
                            processDevice.State   = SmartFlowCardProcessState.等待中;
                            var 合格率         = (RateWeight)RandomSeed.GetWeightRandom(SimulateHelper.合格率);
                            var qualified   = processDevice.Doing * 合格率.Rate / 100;
                            var unqualified = processDevice.Doing - qualified;
                            processDevice.Qualified   += qualified;
                            processDevice.Unqualified += unqualified;
                            processDevice.Doing        = 0;
                            var last = processDevice.Left == 0;
                            if (last)
                            {
                                processDevice.State = SmartFlowCardProcessState.已完成;
                                ReleaseProcess(processDevice.ProcessorId);
                                SmartFlowCardProcessHelper.UpdateSmartFlowCardProcessNextBefore(processDevice.FlowCardId, processDevice.Id, processDevice.Qualified);
                                if (processDevice.Rate < rate)
                                {
                                    processDevice.Fault = true;
                                    faults.Add(new SmartProcessFault
                                    {
                                        CreateUserId   = createUserId,
                                        MarkedDateTime = markedDateTime,
                                        FaultTime      = markedDateTime,
                                        Type           = ProcessFault.合格率低,
                                        Remark         = $"合格率{processDevice.Rate.ToRound()}%,低于{rate}%",
                                        DeviceId       = processDevice.DeviceId,
                                        FlowCardId     = processDevice.FlowCardId,
                                        ProcessId      = processDevice.Id,
                                    });
                                }
                            }
                            var processor = AccountInfoHelper.GetAccountInfo(processDevice.ProcessorId)?.Account ?? "";
                            //todo
                            var log = new SmartFlowCardProcessLog(0, processor, markedDateTime, processDevice, qualified, unqualified);
                            SmartFlowCardProcessLogHelper.Instance.Add(log);
                            categroy0.Add(processDevice.Id);
                        }
                    }
                }
                else
                {
                    if (processDevice.State == SmartFlowCardProcessState.加工中)
                    {
                        var weight = (DeviceStateWeight)RandomSeed.GetWeightRandom(故障_暂停概率);
                        if (weight != null)
                        {
                            switch (weight.State)
                            {
                            case SmartDeviceOperateState.故障中:
                                faults.Add(new SmartProcessFault
                                {
                                    CreateUserId   = createUserId,
                                    MarkedDateTime = markedDateTime,
                                    FaultTime      = markedDateTime,
                                    Type           = ProcessFault.设备故障,
                                    DeviceId       = processDevice.DeviceId,
                                    FlowCardId     = processDevice.FlowCardId,
                                    ProcessId      = processDevice.Id,
                                });
                                processDevice.State = SmartFlowCardProcessState.暂停中;
                                processDevice.Fault = true;
                                UpdateDeviceState(processDevice.DeviceId, processDevice.CategoryId, weight.State);
                                categroy_0.Add(processDevice.Id);
                                break;

                            case SmartDeviceOperateState.暂停中:
                                processDevice.State = SmartFlowCardProcessState.暂停中;
                                UpdateDeviceState(processDevice.DeviceId, processDevice.CategoryId, weight.State);
                                categroy_0.Add(processDevice.Id);
                                break;
                            }
                        }
                    }
                    else if (processDevice.State == SmartFlowCardProcessState.未加工)
                    {
                        if (!dict.ContainsKey(processDevice.CategoryId))
                        {
                            dict.Add(processDevice.CategoryId, true);
                        }

                        if (dict[processDevice.CategoryId])
                        {
                            var totalSecond  = processDevice.TotalSecond < processTime ? processTime : processDevice.TotalSecond;
                            var processorId  = processDevice.ProcessorId;
                            var processCount = (int)Math.Ceiling((decimal)processDevice.Before / processDevice.ProcessNumber);
                            if (Arrange(processDevice.CategoryId, processDevice.Id, processDevice.ProcessNumber, processCount, (int)totalSecond,
                                        out var deviceId, ref processorId) == ScheduleState.成功)
                            {
                                processDevice.State       = SmartFlowCardProcessState.等待中;
                                processDevice.DeviceId    = deviceId;
                                processDevice.ProcessorId = processorId;
                                categroy_0.Add(processDevice.Id);
                            }
                            else
                            {
                                dict[processDevice.CategoryId] = false;
                            }
                        }
                    }
                }
            }
        /// <summary>
        /// 完成本次加工
        /// </summary>
        public bool CompleteThisProcess(out int processorId)
        {
            processorId = 0;
            var now = DateTime.Now;
            var f   = false;

            if (State == SmartDeviceOperateState.加工中)
            {
                if (EndTime <= now)
                {
                    f = true;
                    if (ProcessId != 0)
                    {
                        var process = SmartFlowCardProcessHelper.Instance.Get <SmartFlowCardProcess>(ProcessId);
                        if (process != null)
                        {
                            var 合格率         = (RateWeight)RandomSeed.GetWeightRandom(SimulateHelper.合格率);
                            var qualified   = process.Doing * 合格率.Rate / 100;
                            var unqualified = process.Doing - qualified;
                            process.Qualified   += qualified;
                            process.Unqualified += unqualified;
                            process.Doing        = 0;
                            process.EndTime      = now;
                            var second = (int)(process.EndTime - process.StartTime).TotalSeconds;
                            ThisTotalSecond += second;
                            TotalSecond     += second;
                            var last = process.Left == 0;
                            process.State = last ? SmartFlowCardProcessState.已完成 : SmartFlowCardProcessState.等待中;
                            var createUserId = AccountInfoHelper.GetAccountInfo(process.ProcessorId)?.Account ?? "";
                            if (last)
                            {
                                processorId = ProcessorId;
                                NextProcesses.Remove(NextProcesses.First());
                                SmartFlowCardProcessHelper.UpdateSmartFlowCardProcessNextBefore(process.FlowCardId, process.Id, process.Qualified);
                                if (process.Rate < Rate)
                                {
                                    process.Fault = true;
                                    SmartProcessFaultHelper.Instance.Add(new SmartProcessFault
                                    {
                                        CreateUserId   = createUserId,
                                        MarkedDateTime = now,
                                        FaultTime      = now,
                                        Type           = ProcessFault.合格率低,
                                        Remark         = $"合格率{process.Rate.ToRound()}%,低于{Rate}%",
                                        DeviceId       = process.DeviceId,
                                        FlowCardId     = process.FlowCardId,
                                        ProcessId      = process.Id,
                                    });
                                }

                                Init();
                                LastDone = true;
                            }
                            SmartFlowCardProcessHelper.Instance.Update(process);

                            process.StartTime = StartTime;
                            //todo
                            var log = new SmartFlowCardProcessLog(0, createUserId, now, process, qualified, unqualified);
                            SmartFlowCardProcessLogHelper.Instance.Add(log);
                        }
                    }
                    State     = SmartDeviceOperateState.准备中;
                    StartTime = now;
                    EndTime   = now.AddSeconds(30);
                }
            }
            return(f);
        }