using LiveChartsCore.SkiaSharpView.Extensions;
using LiveChartsCore;
using LogoForceTestApp.Modules.MainModule.Models;
using Prism.Events;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Timers;
using LiveChartsCore.SkiaSharpView.Painting;
using SkiaSharp;
using LiveChartsCore.Measure;
using Repository;
using Repository.Entiies;
using LiveChartsCore.SkiaSharpView;
using System.Collections.ObjectModel;
using LiveChartsCore.SkiaSharpView.VisualElements;
using LiveChartsCore.Defaults;
using FontFamily = System.Windows.Media.FontFamily;
using AutoMapper;
using System.Windows;
using ZXing.Maxicode;
using System.Numerics;
using System.Windows.Documents;
using LiveChartsCore.ConditionalDraw;
using LiveChartsCore.Themes;
using System.Threading.Tasks;
using System.Data.SqlTypes;
using System.Threading;
using Timer = System.Timers.Timer;
using System.Windows.Forms;
using Serilog;
using Team.Utility;
using Prism.Ioc;
using System.Xml.Linq;
using LogoForceTestApp.Modules.MainModule.Method;


namespace LogoForceTestApp.Modules.MainModule.ViewModels
{
    public class KBWindowViewModel : BindableBase
    {
        #region
        IRepository repository; 
        private readonly ILogger _logger;
        private readonly IEventAggregator _eventAggregator;
        private readonly IContainerProvider _container;
        public ObservableCollection<HouseDto> MyHouse { get; set; }
        private readonly IMapper _mapper;
        //private readonly List<House> _myHouse;

        private string timeNow = DateTime.Now.ToString();
        public string TimeNow
        {
            get => timeNow;
            set { SetProperty(ref timeNow, value); }
        }
        private System.Timers.Timer _timer;
        public IEnumerable<ISeries> Series { get; set; }
        int[] outwork = new int[3];
        public string TotalPer { get; set; }
        public string ActualPer { get; set; }
        public string FallIllPer { get; set; }
        public string AbsenPer { get; set; }
        #region 产量
        public string BillNo { get; set; }
        public double AuxQty { get; set; }
        public double Finished { get; set; }

        private double _finishRate;

        public double FinishRate
        {
            get { return (Finished / Convert.ToDouble(AuxQty)) * 100; }
            set { SetProperty(ref _finishRate, value); }
        }

        #endregion

        #region 灯
        public string ImgLight1 { get; set; }//1号机
        public string ImgLight2 { get; set; }
        public string ImgLight3 { get; set; }
        public string ImgLight4 { get; set; }
        public string ImgLight5 { get; set; }
        public string ImgLight6 { get; set; }
        public string ImgLight7 { get; set; }
        public string ImgLight8 { get; set; }
        public string ImgLight9 { get; set; }
        public string ImgLight10 { get; set; }
        public string ImgLight11 { get; set; }
        public string ImgLight12 { get; set; }
        public string ImgLight13 { get; set; }
        public string ImgLight14 { get; set; }
        public string ImgLight15 { get; set; }//螺丝机
        public string ImgLight16 { get; set; }
        public string ImgLight17 { get; set; }
        public string ImgLight18 { get; set; }
        public string ImgLight19 { get; set; }
        public string ImgLight20 { get; set; }
        public string ImgLight21 { get; set; }//提升机
        public string ImgLight22 { get; set; }//检测机
        public string ImgLight23 { get; set; }//返修机
        #endregion
        public double TotalNum { get; set; }
        public double PassNum { get; set; }
        public string FirstName { get; set; }
        public string SecondName { get; set; }
        public string ThirdName { get; set; }
        public string Fix { get; set; }

        private MulProductModel _Products;
        public MulProductModel Products
        {
            get { return _Products; }
            set { SetProperty(ref _Products, value); }
        }
        private ObservableCollection<string> _ProductProblem = new ObservableCollection<string>();
        public ObservableCollection<string> ProductProblem
        {
            get { return _ProductProblem; }
            set { SetProperty(ref _ProductProblem, value); }
        }
        private ObservableCollection<string> _ProcessProblem = new ObservableCollection<string>();
        public ObservableCollection<string> ProcessProblem
        {
            get { return _ProcessProblem; }
            set { SetProperty(ref _ProcessProblem, value); }
        }
        private ObservableCollection<string> _MaterialProblem = new ObservableCollection<string>();
        public ObservableCollection<string> MaterialProblem
        {
            get { return _MaterialProblem; }
            set { SetProperty(ref _MaterialProblem, value); }
        }
        #endregion

        public KBWindowViewModel(IRepository repository, IEventAggregator eventAggregator, 
            IMapper mapper, ILogger logger, IContainerProvider container)
        {
            this.repository = repository;
            this._eventAggregator = eventAggregator;
            _logger = logger;
            this._container = container;
            Series = new List<ISeries>();
            #region timer
            _timer = new Timer();
            _timer.Interval = 1000;
            _timer.Elapsed += _timer_Elapsed;
            _timer.Start();
            #endregion

            #region chart显示文字
            string name = "微软雅黑";
            SKTypeface skTypeface = SKTypeface.FromFamilyName(name);
            if (skTypeface.FamilyName != name)
            {
                // 字体加载失败了
                skTypeface.Dispose();
            }
            FontFamily fontFamily = new(name);
            foreach (string familyNamesValue in fontFamily.FamilyNames.Values)
            {
                skTypeface = SKTypeface.FromFamilyName(familyNamesValue);
                if (skTypeface.FamilyName == familyNamesValue)
                    break;
                else
                {
                    skTypeface.Dispose();
                }
            }
            LiveChartsSkiaSharp.DefaultSKTypeface = skTypeface;

            #endregion

            Products = _container.Resolve<MulProductModel>();
            if (Products == null)
            {
                Products = new MulProductModel();
            }

            _eventAggregator.GetEvent<AlarmEvent>().Subscribe(GetAlarmMessage);//接收另一个viewmodel的信息
            MyHouse = new ObservableCollection<HouseDto>();
            _mapper = mapper;
            _eventAggregator.GetEvent<OperatorNameEvent>().Subscribe(ShowPeope);
            _eventAggregator.GetEvent<QuestEvent>().Subscribe(ShowOrder);
            Task.Factory.StartNew(Main, TaskCreationOptions.LongRunning);
        }
        private void Main()
        {
            while (true)
            {
                try
                {
                    ShowHouse();
                    ShowQuality();
                    ShowOutWork();
                    AlarmOK();
                    double d = Convert.ToDouble(((PassNum / TotalNum) * 100).ToString("0.00"));
                    PassRate(d);
                    ShowNowProcess();
                    ShowProcess();
                }
                catch (ObjectDisposedException ex)
                {
                    _logger.Error("KB_Main:" + ex);
                }
                finally
                {
                    Thread.Sleep(5000);
                }
            }
        }
        private void _timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            TimeNow = DateTime.Now.ToString();
            DateTime currentDate = DateTime.Now;
            if (currentDate.Day == 25)
            {
                Fix = "保养提醒:整线需要保养";
            }
        }


        #region 合格率
        public void PassRate(double passRate)
        {
            Series = GaugeGenerator.BuildSolidGauge(
            new GaugeItem(passRate, series =>
           {
               series.Fill = new SolidColorPaint(SKColors.YellowGreen);
               series.DataLabelsSize = 23;
               series.DataLabelsPaint = new SolidColorPaint(SKColors.LightPink);
               series.DataLabelsPosition = PolarLabelsPosition.ChartCenter;
               series.InnerRadius = 40;
           }),
           new GaugeItem(GaugeItem.Background, series =>
           {
               series.InnerRadius = 40;
               series.Fill = new SolidColorPaint(new SKColor(100, 181, 246, 90));
           }));
        }
        #endregion

        #region 出勤率

        public void ShowOutWork()
        {
            try
            {
                var list = repository.GetFirstDefault<OutofWork>(c => c.DateOutWork == DateTime.Today);
                if (list != null)
                {
                    outwork[0] = list.Actual;
                    outwork[1] = list.FallIll;
                    outwork[2] = list.Absenteeism;
                    int total = outwork[2] + outwork[1] + outwork[0];
                    OutWork(outwork[2], outwork[1], outwork[0], total);

                    string a = ((double)outwork[0] / (double)total).ToString("P");
                    string b = ((double)outwork[1] / (double)total).ToString("P");
                    string c = ((double)outwork[2] / (double)total).ToString("P");
                    TotalPer = $"应到人数:{total}";
                    ActualPer = $"实到人数:{outwork[0]}【{a}】";
                    FallIllPer = $"请假人数:{outwork[1]}【{b}】";
                    AbsenPer = $"旷工人数:{outwork[2]}【{c}】";
                }
            }
            catch (Exception ex)
            {
                _logger.Error("KB_ShowOutWork:" + ex);
            }
        }
        public IEnumerable<ISeries> Series12 { get; set; }
        public double GaugeTotal12 { get; set; }
        public double InitialRotation12 { get; set; }
        public double MaxAngle12 { get; set; }
        /// <summary>
        /// "旷工人数","请假人数" ,"实到人数","应到人数"
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <param name="c"></param>
        /// <param name="d"></param>
        public void OutWork(int a, int b, int c, int d)
        {
            GaugeTotal12 = d;//最大数
            InitialRotation12 = 90;
            MaxAngle12 = 270;

            void SetStyle12(string name, PieSeries<ObservableValue> series)
            {
                series.Name = name;
                series.DataLabelsPosition = PolarLabelsPosition.Start;
                series.DataLabelsFormatter =
                    point => $"{point.Context.Series.Name} {point.Coordinate.PrimaryValue}";
                series.DataLabelsSize = 16;
                series.InnerRadius = 20;
                series.RelativeInnerRadius = 4;
                series.RelativeOuterRadius = 4;
                series.DataLabelsPaint = new SolidColorPaint(new SKColor(150, 150, 150));
            }

            Series12 = GaugeGenerator.BuildSolidGauge(
                new GaugeItem(a, series => SetStyle12("旷工人数", series)),
                new GaugeItem(b, series => SetStyle12("请假人数", series)),
                new GaugeItem(c, series => SetStyle12("实到人数", series)),
                new GaugeItem(d, series => SetStyle12("应到人数", series)),
                new GaugeItem(GaugeItem.Background, series =>
                {
                    series.InnerRadius = 20;
                    series.RelativeInnerRadius = 10;
                    series.RelativeOuterRadius = 10;
                }));
        }
        //public IEnumerable<ISeries> SeriesOutWork { get; set; }
        //private static int _index = 0;
        //private static string[] _names = new[] { "实到", "请假", "旷工" };

        //public void OutWork(int[] val)
        //{
        //    SeriesOutWork = val.AsPieSeries((value, series) =>
        //    {
        //        series.Name = _names[_index++ % _names.Length];
        //        series.DataLabelsPosition = LiveChartsCore.Measure.PolarLabelsPosition.Outer; // mark
        //        series.DataLabelsSize = 10;
        //        series.DataLabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180));

        //        //series.DataLabelsFormatter =
        //        //   point =>
        //        //       $"This slide takes {point.Coordinate.PrimaryValue} " +
        //        //       $"out of {point.StackedValue!.Total} parts";
        //        //series.ToolTipLabelFormatter = point => $"{point.StackedValue!.Share:P2}";
        //        series.ToolTipLabelFormatter =
        //           point =>
        //           {
        //               var pv = point.Coordinate.PrimaryValue;
        //               var sv = point.StackedValue!;

        //               var a = $"{pv}/{sv.Total}{Environment.NewLine}{sv.Share:P2}";
        //               return a;
        //           };

        //        series.DataLabelsFormatter =
        //            point =>
        //            {
        //                var pv = point.Coordinate.PrimaryValue;
        //                var sv = point.StackedValue!;

        //                var a = $"{series.Name}{Environment.NewLine}{sv.Share:P2}";
        //                return a;
        //            };
        //    });

        //}

        #endregion

        #region 质量
        int[] qua1 = new int[10]; int[] qua2 = new int[10]; int[] qua3 = new int[10];
        
        public void ShowQuality()
        {
            try
            {
                //string str = DateTime.Now.ToString("yyyy/MM/dd");
                DateTime dt = DateTime.Today;
                var list = repository.GetFirstDefault<Quality>(c => c.QuPCode == BillNo);
                if (list != null)
                {
                    qua1[0] = list.Q1; qua1[1] = list.Q2; qua1[2] = list.Q3; qua1[3] = list.Q4; qua1[4] = list.Q5;
                    qua1[5] = list.Q6; qua1[6] = list.Q7; qua1[7] = list.Q8; qua1[8] = list.Q9; qua1[9] = list.Q10;
                    qua2[0] = list.Q11; qua2[1] = list.Q12; qua2[2] = list.Q13; qua2[3] = list.Q14; qua2[4] = list.Q15;
                    qua2[5] = list.Q16; qua2[6] = list.Q17; qua2[7] = list.Q18; qua2[8] = list.Q19; qua2[9] = list.Q20;
                    qua3[0] = list.Q21; qua3[1] = list.Q22; qua3[2] = list.Q23; qua3[3] = list.Q24; qua3[4] = list.Q25;
                    qua3[5] = list.Q26; qua3[6] = list.Q27; qua3[7] = list.Q28; qua3[8] = list.Q29; qua3[9] = list.Q30;
                    
                    Thread.Sleep(200);
                    Products.LoadParame();
                    var res1=Products.ProductCollection.FirstOrDefault(p => p.Name == GVariable.ProductTypeName);
                    if (res1 != null)
                    {
                        ProductProblem = res1.QuestionType1;
                        ProcessProblem = res1.QuestionType2;
                        MaterialProblem = res1.QuestionType3;
                    }
                    
                    SerQuality1(qua1, ProductProblem);
                    SerQuality2(qua2, ProcessProblem);
                    SerQuality3(qua3, MaterialProblem);
                }
            }
            catch (Exception ex)
            {
                _logger.Error("KB_ShowQuality:" + ex);
            }
        }
        #region 生产问题
        public void SerQuality1(int[] val,ObservableCollection<string> xlabel)
        {
            SeriesQuality1[0].Values = val;
            XAxes1[0].Labels = xlabel;
        }

        public ISeries[] SeriesQuality1 { get; set; } = new ISeries[]
        {
            new ColumnSeries<int>
            {
                Name = "数量",
                //Values = new[] {23, 27, 22, 29, 24 ,21,11,45,32,19},
                Stroke = null,
                DataLabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180)),
                DataLabelsPosition = DataLabelsPosition.Top,
                Padding = 3,//每个条之间的距离

                //Fill = new LinearGradientPaint(
                //    new [] { new SKColor(255, 140, 148), new SKColor(220, 237, 194) },
                //    new SKPoint(0.5f, 0),
                //    new SKPoint(0.5f, 1))
                Fill = new LinearGradientPaint(new[]{new SKColor(255, 140, 148), new SKColor(220, 237, 194) })
            }
        };

        public Axis[] XAxes1 { get; set; } =
        {
            new Axis
            {
            //    Labels = new string[] { "螺丝未上紧","3D人脸无效" ,"螺丝漏打" ,"显示屏幕漏光" ,"胶体未打好",
            //        "对讲喇叭无声","执手过于松紧" ,"螺丝打变形" ,"理线错乱" ,"弹簧未装好" },
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                LabelsRotation = 90,
                TextSize = 12,
                ForceStepToMin = true,
                MinStep = 1
            }
        };
        public Axis[] YAxes1 { get; set; } =
        {
            new Axis
            {
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                MinLimit=0,
                //最小步长
                //ForceStepToMin = true,
                //MinStep = 5
            }
        };
        #endregion
        #region 工艺问题
        public void SerQuality2(int[] val, ObservableCollection<string> xlabel)
        {
            SeriesQuality2[0].Values = val;
            XAxes2[0].Labels= xlabel;
        }

        public ISeries[] SeriesQuality2 { get; set; } = new ISeries[]
        {
            new ColumnSeries<int>
            {
                Name = "数量",
                //Values = new[] {23, 27, 22, 29, 24 ,21,11,45,32,19},
                Stroke = null,
                DataLabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180)),
                DataLabelsPosition = DataLabelsPosition.Top,
                Padding = 3,//每个条之间的距离

                //Fill = new LinearGradientPaint(
                //    new [] { new SKColor(255, 140, 148), new SKColor(220, 237, 194) },
                //    new SKPoint(0.5f, 0),
                //    new SKPoint(0.5f, 1))
                Fill = new LinearGradientPaint(new[]{new SKColor(255, 140, 148), new SKColor(220, 237, 194) })
            }
        };

        public Axis[] XAxes2 { get; set; } =
        {
            new Axis
            {
                //Labels = new string[] {"对讲无声","执手问题" ,"壳体脚位不平衡" ,"前板锁芯两孔位不一致" ,"后板显示屏水印",
                //    "电池异常耗电","喷漆过厚导致组装困难" ,"设计线路太长" ,"执手靠垫片改善" ,"锁芯顶杆没固定位置"},
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                LabelsRotation = 90,
                TextSize = 12,
                ForceStepToMin = true,
                MinStep = 1
            }
        };
        public Axis[] YAxes2 { get; set; } =
        {
            new Axis
            {
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                MinLimit=0,
                //最小步长
                //ForceStepToMin = true,
                //MinStep = 5
            }
        };
        #endregion
        #region 物料问题
        public void SerQuality3(int[] val, ObservableCollection<string> xlabel)
        {
            SeriesQuality3[0].Values = val;
            XAxes3[0].Labels= xlabel;
        }

        public ISeries[] SeriesQuality3 { get; set; } = new ISeries[]
        {
            new ColumnSeries<int>
            {
                Name = "数量",
                //Values = new[] {23, 27, 22, 29, 24 ,21,11,45,32,19},
                Stroke = null,
                DataLabelsPaint = new SolidColorPaint(new SKColor(180, 180, 180)),
                DataLabelsPosition = DataLabelsPosition.Top,
                Padding = 3,//每个条之间的距离

                //Fill = new LinearGradientPaint(
                //    new [] { new SKColor(255, 140, 148), new SKColor(220, 237, 194) },
                //    new SKPoint(0.5f, 0),
                //    new SKPoint(0.5f, 1))
                Fill = new LinearGradientPaint(new[]{new SKColor(255, 140, 148), new SKColor(220, 237, 194) })
            }
        };

        public Axis[] XAxes3 { get; set; } =
        {
            new Axis
            {
                //Labels = new string[] {"面板变形","电机异常" ,"屏幕异常" ,"物料毛刺" ,"大屏上支架变形",
                //    "壳料划痕","显示屏水印" , "壳体脚位不平横" ,"装饰圈气泡脱皮" ,"电池异常"},
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                LabelsRotation = 90,
                TextSize = 12,
                ForceStepToMin = true,
                MinStep = 1
            }
        };
        public Axis[] YAxes3 { get; set; } =
        {
            new Axis
            {
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                MinLimit=0,
                //最小步长
                //ForceStepToMin = true,
                //MinStep = 5
            }
        };
        #endregion

        #endregion

        #region 人员管理
        //int nums;
        //private void ShowPeope(string opername)
        //{
        //    var value = repository.GetAllQuery<Information>(c => c.OperatorCode == opername && c.CreateTime == DateTime.Today);
        //    if (value.Count != 0)
        //    {
        //        nums = value.Count();
        //        PerManagement(opername, nums);
        //    }
        //}
        int[] nums = new int[30];
        
        private void ShowPeope(string[] opername)
        {
            PerManage = new List<Person>();
            PerManage1 = new List<Person>();
            PerManage2 = new List<Person>();
            for (int i = 0; i < opername.Length; i++)
            {
                if (opername[i] == null || string.IsNullOrEmpty(opername[i]))
                {
                    continue;
                }
                var value = repository.GetAllQuery<Information>(c => c.OperatorCode == opername[i] && c.CreateTime == DateTime.Today);
                if (value.Count != 0)
                {
                    nums[i] = value.Count();
                    PerManagement(opername[i], nums[i]);
                }
                var num= repository.GetAllQuery<Information>(c => c.OperatorCode == opername[i] && c.CreateTime <= DateTime.Today&& c.CreateTime >= DateTime.Today.AddDays(-9));
                if (num.Count != 0)
                {
                    nums[i] = value.Count();
                    PerManagement2(opername[i], nums[i]);
                }
            }
        }
        public List<Person> PerManage { get; set; }
        private List<Person> PerManage1 { get; set; }
        private List<Person> PerManage2 { get; set; }
        public void PerManagement(string ename, int yield)
        {
            //PerManage = new List<Person>();//放在外面就累加,放这里就不累加一直刷
            PerManage1.Add(new Person { Id = new Guid(), EmployeeName = ename, Yield = yield });
            var m = PerManage1.OrderByDescending(x => x.Yield).ToList();//降序
            PerManage = m;
            //if (m.Count >= 3)
            //{
            //    FirstName = PerManage[0].EmployeeName;
            //    SecondName = PerManage[1].EmployeeName;
            //    ThirdName = PerManage[2].EmployeeName;
            //}
        }
        public void PerManagement2(string ename, int yield)
        {
            PerManage2.Add(new Person { Id = new Guid(), EmployeeName = ename, Yield = yield });
            var m = PerManage2.OrderByDescending(x => x.Yield).ToList();//降序
            PerManage = m;
            if (m.Count >= 3)
            {
                FirstName = PerManage[0].EmployeeName;
                SecondName = PerManage[1].EmployeeName;
                ThirdName = PerManage[2].EmployeeName;
            }
        }
        #endregion

        #region 仓库

        public void ShowHouse()
        {
            try
            {
                //var list = repository.GetAllQuery<House>(c => c.DateH == str);//查一个集合
                var list = repository.GetFirstDefault<House>(c => c.DateH == DateTime.Today);
                if (list != null)
                {
                    var dto = _mapper.Map<HouseDto>(list);
                    System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
                    {
                        MyHouse.Clear();
                        MyHouse.Add(dto);
                        if (list.LackH==null)
                        {
                            list.LackH = "0";
                        }
                        if (list.UsedH == null)
                        {
                            list.UsedH = "0";
                        }
                        if (list.TotalH == null)
                        {
                            list.TotalH = "0";
                        }
                        SerHouse(double.Parse(list.LackH), double.Parse(list.UsedH), double.Parse(list.TotalH));
                    }));


                }
            }
            catch (Exception ex)
            {
                _logger.Error("KB_ShowHouse:" + ex);
            }
        }
        public IEnumerable<ISeries> Series13 { get; set; }
        public double GaugeTotal13 { get; set; }
        public double InitialRotation13 { get; set; }
        public double MaxAngle13 { get; set; }
        /// <summary>
        /// "缺料订单数","已领料订单数" ,"当日订单数"
        /// </summary>
        /// <param name="a">小</param>
        /// <param name="b">中</param>
        /// <param name="c">最大</param>
        public void SerHouse(double a, double b, double c)
        {
            GaugeTotal13 = c;//最大数
            InitialRotation13 = 90;
            MaxAngle13 = 270;

            void SetStyle13(string name, PieSeries<ObservableValue> series)
            {
                series.Name = name;
                series.DataLabelsPosition = PolarLabelsPosition.Start;
                series.DataLabelsFormatter =
                    point => $"{point.Context.Series.Name} {point.Coordinate.PrimaryValue}";
                series.DataLabelsSize = 16;
                series.InnerRadius = 20;
                series.RelativeInnerRadius = 4;
                series.RelativeOuterRadius = 4;
                series.DataLabelsPaint = new SolidColorPaint(new SKColor(150, 150, 150));
            }

            Series13 = GaugeGenerator.BuildSolidGauge(
                new GaugeItem(a, series => SetStyle13("缺料订单数", series)),
                new GaugeItem(b, series => SetStyle13("已领料订单数", series)),
                new GaugeItem(c, series => SetStyle13("当日订单数", series)),
                new GaugeItem(GaugeItem.Background, series =>
                {
                    series.InnerRadius = 20;
                    series.RelativeInnerRadius = 10;
                    series.RelativeOuterRadius = 10;
                }));
        }

        #endregion

        #region 设备状态
        public void AlarmOK()
        {
            ImgLight1 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight2 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight3 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight4 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight5 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight6 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight7 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight8 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight9 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight10 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight11 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight12 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight13 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight14 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight21 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight22 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
            ImgLight23 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
        }
        private void GetAlarmMessage(Dictionary<string, string> dictionary)//dictionary 就是接收到的信息
        {
            var keys = new List<string>(dictionary.Keys);
            for (int i = 0; i < keys.Count; i++)
            {
                var key = keys[i];
                var value = dictionary[key];
                #region 号机
                //1号机
                if ((key == "1101" && value == "报警"))
                {
                    ImgLight1 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1141.15" && value == "缺料"))
                {
                    ImgLight1 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //2号机
                if ((key == "1102" && value == "报警"))
                {
                    ImgLight2 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1142.15" && value == "缺料"))
                {
                    ImgLight2 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //3号机
                if ((key == "1103" && value == "报警"))
                {
                    ImgLight3 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1143.15" && value == "缺料"))
                {
                    ImgLight3 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //4号机
                if ((key == "1104" && value == "报警"))
                {
                    ImgLight4 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1144.15" && value == "缺料"))
                {
                    ImgLight4 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //5号机
                if ((key == "1105" && value == "报警"))
                {
                    ImgLight5 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1145.15" && value == "缺料"))
                {
                    ImgLight5 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //6号机
                if ((key == "1106" && value == "报警"))
                {
                    ImgLight6 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1146.15" && value == "缺料"))
                {
                    ImgLight6 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //7号机
                if ((key == "1110" && value == "报警"))
                {
                    ImgLight7 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1150.15" && value == "缺料"))
                {
                    ImgLight7 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //8号机
                if ((key == "1111" && value == "报警"))
                {
                    ImgLight8 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1151.15" && value == "缺料"))
                {
                    ImgLight8 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //9号机
                if ((key == "1112" && value == "报警"))
                {
                    ImgLight9 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1152.15" && value == "缺料"))
                {
                    ImgLight9 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //10号机
                if ((key == "1113" && value == "报警"))
                {
                    ImgLight10 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1153.15" && value == "缺料"))
                {
                    ImgLight10 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //11号机
                if ((key == "1117" && value == "报警"))
                {
                    ImgLight11 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1157.15" && value == "缺料"))
                {
                    ImgLight11 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //12号机
                if ((key == "1118" && value == "报警"))
                {
                    ImgLight12 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1158.15" && value == "缺料"))
                {
                    ImgLight12 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //13号机
                if ((key == "1119" && value == "报警"))
                {
                    ImgLight13 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1159.15" && value == "缺料"))
                {
                    ImgLight13 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                //14号机
                if ((key == "1120" && value == "报警"))
                {
                    ImgLight14 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1160.15" && value == "缺料"))
                {
                    ImgLight14 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }

                #endregion
                #region 螺丝机
                //螺丝机1
                if ((key == "1107" && value == "报警"))
                {
                    ImgLight15 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1107" && value == "正常"))
                {
                    ImgLight15 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
                }
                //螺丝机2
                if ((key == "1108" && value == "报警"))
                {
                    ImgLight16 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1108" && value == "正常"))
                {
                    ImgLight16 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
                }
                //螺丝机3
                if ((key == "1115" && value == "报警"))
                {
                    ImgLight17 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1115" && value == "正常"))
                {
                    ImgLight17 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
                }
                //螺丝机4
                if ((key == "1116" && value == "报警"))
                {
                    ImgLight18 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1116" && value == "正常"))
                {
                    ImgLight18 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
                }
                //螺丝机5
                if ((key == "1121" && value == "报警"))
                {
                    ImgLight19 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1121" && value == "正常"))
                {
                    ImgLight19 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
                }
                //螺丝机6
                if ((key == "1122" && value == "报警"))
                {
                    ImgLight20 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                if ((key == "1122" && value == "正常"))
                {
                    ImgLight20 = System.Windows.Forms.Application.StartupPath + @"\image\绿灯.png";
                }
                #endregion
                //提升机
                if ((key == "1100" && value == "报警") || (key == "1124" && value == "报警") || (key == "D1125" && value == "报警"))
                {
                    ImgLight21 = System.Windows.Forms.Application.StartupPath + @"\image\红灯.png";
                }
                //检测
                if ((key == "1166.0" && value == "缺料") || (key == "1166.1" && value == "缺料") || (key == "D1166.2" && value == "缺料") || (key == "D1166.3" && value == "缺料"))
                {
                    ImgLight22 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }
                //返修
                if ((key == "1166.4" && value == "缺料") || (key == "1166.5" && value == "缺料") || (key == "D1166.6" && value == "缺料"))
                {
                    ImgLight23 = System.Windows.Forms.Application.StartupPath + @"\image\黄灯.png";
                }
            }

        }
        #endregion

        #region 生产进度
        double changeDay; double okrate;
        List<string> xlabel = new List<string>();
        List<double> listval1 = new List<double>();
        List<double> listval2 = new List<double>();
        public void ShowProcess()
        {
            try
            {
                listval1.Clear();
                listval2.Clear();
                xlabel.Clear();
                //for (int i = 1; i < 20; i++)
                //{
                //    changeDay = -i;
                //    var list = repository.GetFirstDefault<AllNumber>(c => c.DateA == DateTime.Today.AddDays(changeDay));
                //    if (list != null)
                //    {
                //        if (list.RealityA!=0)
                //        {
                //            listval1.Add(list.RealityA);
                //            okrate = Math.Round((list.OkA / list.RealityA) * 100, 2);
                //            listval2.Add(okrate);
                //            xlabel.Add(list.DateA.ToString("MM-dd"));
                //            if (listval1.Count >= 7)
                //            {
                //                break;
                //            }
                //        }
                //    }
                //}
                DateTime start = DateTime.Now.AddDays(-((int)DateTime.Now.DayOfWeek - 1));
                DateTime end = start.AddDays(7);
                start = new DateTime(start.Year, start.Month, start.Day, 0, 0, 0);
                end = new DateTime(end.Year, end.Month, end.Day, 0, 0, 0);
                var list = repository.GetAllQuery<AllNumber>(c => c.DateA >=start&&c.DateA<end);
                if (list != null)
                {
                    for (int i = 0; i < list.Count; i++)
                    {
                        listval1.Add(list[i].RealityA);
                        okrate = Math.Round((list[i].OkA / list[i].RealityA) * 100, 2);
                        listval2.Add(okrate);
                        xlabel.Add(list[i].DateA.ToString("MM-dd"));
                    }
                }

                SerLine(listval1, listval2, xlabel);
            }
            catch (Exception ex)
            {
                _logger.Error("KB_ShowProcess:" + ex);
            }
        }

        public void SerLine(List<double> val1, List<double> val2, List<string> list)
        {
            SeriesLine[0].Values = val1;
            SeriesLine[1].Values = val2;
            XAxesLine[0].Labels = list;
        }

        public ISeries[] SeriesLine { get; set; } =
        {
            new LineSeries<double>
            {
            Name = "产量",
            //Values = new []{ 4, 2, 8, 5, 3 },
            GeometrySize = 12,
            Stroke = new LinearGradientPaint(new[]{ new SKColor(100, 135, 15), new SKColor(255, 212, 96)}) { StrokeThickness = 5 },
            GeometryStroke = new LinearGradientPaint(new[]{ new SKColor(100, 135, 15), new SKColor(255, 212, 96)}) { StrokeThickness = 5 },
            Fill = null,
            ScalesYAt = 0
            //Fill = new SolidColorPaint(new SKColor(255, 212, 96, 90))
            },
             new LineSeries<double>
            {
            Name = "合格率(%)",
            GeometrySize = 12,
            Stroke = new LinearGradientPaint(new[]{ new SKColor(100, 62, 99), new SKColor(255, 137, 219) }) { StrokeThickness = 5 },
            GeometryStroke = new LinearGradientPaint(new[]{ new SKColor(100, 62, 99), new SKColor(255, 137, 219)}) { StrokeThickness = 5 },
            Fill = null,
            ScalesYAt = 1
            }
        };
        public LabelVisual PcTitle { get; set; } =
        new LabelVisual
        {
            Text = "近一周产量趋势",
            TextSize = 16,
            Padding = new LiveChartsCore.Drawing.Padding(15),
            Paint = new SolidColorPaint(new SKColor(47, 152, 255))
        };

        public DrawMarginFrame DrawMarginFrame => new()
        {
            Stroke = new SolidColorPaint(new SKColor(45, 64, 89), 2)
        };


        public Axis[] XAxesLine { get; set; } =
        {
            new Axis
            {
                //Labels = new string[] { "1","2" ,"3" ,"4" ,"5","6","7"},
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                LabelsRotation = 0,
                ForceStepToMin = true,
                MinLimit=0,
                MinStep = 1
            }
        };
        public Axis[] YAxesLine { get; set; } =
        {
            new Axis
            {
                Name = "产量",
                NameTextSize = 14,
                NamePaint = new SolidColorPaint(new SKColor(153, 194, 40)),
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                MinLimit=0,
                //最小步长
                //ForceStepToMin = true,
                //MinStep = 5
            },
            new Axis
            {
                Name = "合格率(%)",
                NameTextSize = 14,
                NamePaint = new SolidColorPaint(new SKColor(255, 137, 219)),
                LabelsPaint = new SolidColorPaint(new SKColor(100, 100, 100)),
                MinLimit=0,
                MaxLimit=150,
                Position = LiveChartsCore.Measure.AxisPosition.End
            }
        };
        #endregion

        #region 当前订单情况
        public void ShowOrder(string str)
        {
            var customRepo = new CustomRepository();
            try
            {
                //DateTime max = DateTime.Today.AddDays(1);
                //DateTime min = DateTime.Today.AddDays(-1);
                DateTime d = DateTime.Today;
                var list = customRepo.GetFirstDefault<ICMO>(c => c.FBillNo == str);
                if (list != null)
                {
                    BillNo = str;
                    AuxQty = (double)list.FAuxQty;
                }
                var ps = repository.GetFirstDefault<ProStatus>(c => c.OrdNoS == str);
                if (ps != null)
                {
                    Finished = ps.RealityS;
                }
            }
            catch (Exception ex)
            {
                _logger.Error("KB_ShowOrder:"+ex);
            }
        }
        #endregion

        #region 实时产量
        public void ShowNowProcess()
        {
            try
            {
                var allnumbers = repository.GetFirstDefault<AllNumber>(c => c.DateA == DateTime.Today);
                if (allnumbers != null)
                {
                    TotalNum = allnumbers.RealityA;
                    if (allnumbers.OkA <= 0)
                    {
                        allnumbers.OkA= 0;
                        repository.Update(allnumbers);
                    }
                    PassNum = allnumbers.OkA;
                }
            }
            catch (Exception ex)
            {
                _logger.Error("KB_ShowNowProcess:" + ex);
            }
        }
        #endregion

    }

}