PgGetSample.xaml.cs 11.2 KB
using CommunityToolkit.Mvvm.Input;
using FLY.Thick.Base.IService;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Unity;

namespace FLY.Thick.Base.UI
{
    /// <summary>
    /// Page_GetSample.xaml 的交互逻辑
    /// </summary>
    public partial class PgGetSample : Page
    {
        IGetSampleService getSampleService;
        IInitParamService initParamService;

        PgGetSampleVm viewModel;

        public PgGetSample()
        {
            InitializeComponent();
            if (System.ComponentModel.LicenseManager.UsageMode != System.ComponentModel.LicenseUsageMode.Runtime)
                return;

            this.Loaded += UcGridGraph_Loaded;
            this.Unloaded += UcGridGraph_Unloaded;
        }

        private void UcGridGraph_Unloaded(object sender, RoutedEventArgs e)
        {
            viewModel.DisposeBinding();
        }

        private void UcGridGraph_Loaded(object sender, RoutedEventArgs e)
        {
            viewModel.SetBinding();
        }

        [InjectionMethod]
        public void Init(
            IGetSampleService getSampleService,
            IInitParamService initParamService,
            string infoName = null)
        {

            this.getSampleService = getSampleService;
            this.initParamService = initParamService;

            viewModel = new PgGetSampleVm();
            viewModel.Init(getSampleService, infoName);
            this.DataContext = viewModel;
            this.grid_initparam.DataContext = this.initParamService;
        }
    }

    public class PgGetSampleVm : INotifyPropertyChanged
    {
        #region Markno
        const int MARKNO_UPDATE_SAMPLE = 1;
        const int MARKNO_UPDATE_FEATURE = 2;

        #endregion
        #region 参数

        /// <summary>
        /// 参数:使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 参数:样品点范围
        /// </summary>
        public int SampleRange { get; set; }

        /// <summary>
        /// 参数:移动滤波 ,窗口值
        /// </summary>
        public int Window { get; set; }

        /// <summary>
        /// 使用%方式检查异常
        /// </summary>
        public bool IsCheckByPercent { get; set; }
        /// <summary>
        /// 异常%
        /// </summary>
        public double ErrPercent { get; set; }
        /// <summary>
        /// 异常值
        /// </summary>
        public int ErrValue { get; set; }

        /// <summary>
        /// 当前 样品X_AD/样品0_AD 与 上一次比较 差异比例 单位不是%
        /// </summary>
        public double CrossErrPercent { get; set; }

        /// <summary>
        /// 参数:样品点参数
        /// </summary>
        public SampleCellView[] Samples { get; set; }


        #endregion

        #region Command
        public RelayCommand ApplyCmd { get; private set; }
        #endregion

        public event PropertyChangedEventHandler PropertyChanged;


        public string InfoName { get; set; }

        IGetSampleService getSampleService;

        Dictionary<object, List<Misc.BindingOperations.PropertyChangedEventContexts>> bindingConexts = new Dictionary<object, List<Misc.BindingOperations.PropertyChangedEventContexts>>();

        /// <summary>
        /// 数据已经绑定了
        /// </summary>
        bool isBinding;

        public PgGetSampleVm()
        {
            ApplyCmd = new RelayCommand(Apply);
        }



        public void Init(
            IGetSampleService getSampleService,
            string infoName)
        {
            InfoName = infoName;
            this.getSampleService = getSampleService;
            SetBinding();
        }

        /// <summary>
        /// 参数绑定
        /// </summary>
        public void SetBinding()
        {
            if (isBinding)
                return;
            isBinding = true;

            //下面全部event保存在bindingConexts
            Misc.BindingOperations.StartMarkdownEvents(bindingConexts);
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Enable), this, nameof(Enable));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.SampleRange), this, nameof(SampleRange));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Window), this, nameof(Window));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.IsCheckByPercent), this, nameof(IsCheckByPercent));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrPercent), this, nameof(ErrPercent));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.ErrValue), this, nameof(ErrValue));
            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.CrossErrPercent), this, nameof(CrossErrPercent));

            Misc.BindingOperations.SetBinding(this.getSampleService, nameof(this.getSampleService.Samples), this,() =>
            {
                FObjBase.PollModule.Current.Poll_JustOnce(updateSamples, this, MARKNO_UPDATE_SAMPLE);
            });

            //备份event完毕, 必须关闭记录
            Misc.BindingOperations.StopMarkdownEvents();

        }
        /// <summary>
        /// 解除绑定
        /// </summary>
        public void DisposeBinding()
        {
            if (!isBinding)//已经解除绑定了
                return;
            isBinding = false;

            //释放全部 event
            Misc.BindingOperations.DisposeEventTargetObject(bindingConexts);


        }

        void updateSamples()
        {
            var samples = this.getSampleService.Samples;

            if (samples == null)
            {
                Samples = null;
            }
            else
            {
                var sampleViews = new SampleCellView[samples.Count()];
                for (int i = 0; i < sampleViews.Count(); i++)
                {
                    sampleViews[i] = new SampleCellView() { Name = i.ToString() };
                    var sampleView = sampleViews[i];
                    var sample = samples[i];
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.Enable), sampleView, nameof(SampleCellView.Enable));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.JustForCheck), sampleView, nameof(SampleCellView.JustForCheck));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.OrgAD), sampleView, nameof(SampleCellView.OrgAD));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.Position), sampleView, nameof(SampleCellView.Position));

                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.AD), sampleView, nameof(SampleCellView.AD));
                    Misc.BindingOperations.SetBinding(sample, nameof(SampleCell.SampleValue), sampleView, nameof(SampleCellView.SampleValue));
                }
                updateSampleCellViewVisible(sampleViews);

                Samples = sampleViews;

                for (int i = 0; i < Samples.Count(); i++)
                {
                    var sampleView = Samples[i];
                    sampleView.PropertyChanged += (s, e) =>
                    {
                        if (e.PropertyName == nameof(SampleCellView.Enable))
                        {
                            updateSampleCellViewVisible();
                        }
                    };
                }
            }
        }

        protected void updateSampleCellViewVisible(SampleCellView[] sampleViews)
        {
            if (sampleViews == null)
                return;

            for (int i = 0; i < sampleViews.Count(); i++)
            {
                if (i == 0)
                    sampleViews[i].IsVisible = true;
                else
                {
                    sampleViews[i].IsVisible = sampleViews[i - 1].Enable;
                }
            }
        }
        protected void updateSampleCellViewVisible()
        {
            updateSampleCellViewVisible(Samples);
        }
        private void Apply()
        {
            if (!WdPassword.Authorize("GetSample"))
                return;



            this.getSampleService.Enable = this.Enable;
            this.getSampleService.SampleRange = this.SampleRange;
            this.getSampleService.Window = this.Window;
            this.getSampleService.IsCheckByPercent = this.IsCheckByPercent;
            this.getSampleService.ErrPercent = this.ErrPercent;
            this.getSampleService.ErrValue = this.ErrValue;
            this.getSampleService.CrossErrPercent = this.CrossErrPercent;


            for (int i = 0; i < Samples.Count(); i++)
            {
                var sample_src = this.Samples[i];
                var sample_desp = this.getSampleService.Samples[i];

                sample_desp.Enable = sample_src.Enable;
                sample_desp.JustForCheck = sample_src.JustForCheck;
                sample_desp.OrgAD = sample_src.OrgAD;
                sample_desp.Position = sample_src.Position;
            }
            this.getSampleService.SearchRange = 0;

            for (int i = 0; i < this.getSampleService.Features.Count(); i++)
            {
                var feature_desp = this.getSampleService.Features[i];

                feature_desp.Enable = false;
            }

            getSampleService.Apply();
            string tit = (string)Application.Current.TryFindResource("str.PgGetSample.ApplySuccessfully");
            FLY.ControlLibrary.Window_Tip.Show(tit, null, TimeSpan.FromSeconds(2));
        }


    }
    public class PgGetSampleVmUt : PgGetSampleVm
    {
        public PgGetSampleVmUt()
        {
            Samples = new SampleCellView[3];
            for (int i = 0; i < Samples.Count(); i++)
            {
                SampleCellView sampleCell = new SampleCellView() { Name = i.ToString() };
                Samples[i] = sampleCell;
            }

            Enable = true;
            Samples[0].Enable = true;

            updateSampleCellViewVisible();
        }
    }

    /// <summary>
    /// 样品点
    /// </summary>
    public class SampleCellView : INotifyPropertyChanged
    {
        /// <summary>
        /// 序号
        /// </summary>
        public string Name { get; set; }

        /// <summary>
        /// 显示出来
        /// </summary>
        public bool IsVisible { get; set; }

        /// <summary>
        /// 参数:使能
        /// </summary>
        public bool Enable { get; set; }

        /// <summary>
        /// 参数:只检查不标定
        /// </summary>
        public bool JustForCheck { get; set; }

        /// <summary>
        /// 参数:原始AD值
        /// </summary>
        public int OrgAD { get; set; }

        /// <summary>
        /// 参数:位置
        /// </summary>
        public int Position { get; set; }

        public int AD { get; set; }
        public double SampleValue { get; set; }


        #region INotifyPropertyChanged 成员

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

    }

}