You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
4.2 KiB
C#

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using Microsoft.Extensions.DependencyInjection;
using SlnMesnac.Model.domain;
using SlnMesnac.Repository.service;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace SlnMesnac.WPF.ViewModel
{
public class SetKindWindowViewModel : ObservableObject
{
private ILogoFormulaService? formulaService;
private ILogoConfigService? logoConfigService;
private string CachMaterialType;
// 缓存第一次查到的海康配方
public Dictionary<string, int> TypeCache = new Dictionary<string, int>();
public LogoConfig record = null;
public delegate void SetKindWindowDelegate();
public SetKindWindowViewModel(string materialType)
{
formulaService = App.ServiceProvider.GetService<ILogoFormulaService>();
logoConfigService = App.ServiceProvider.GetService<ILogoConfigService>();
SaveCommand = new RelayCommand(Save);
// 初始加载内容
Init(materialType);
}
public void Init(string materialType)
{
record = logoConfigService.GetByMaterialType(materialType);
if (record == null) return;
MaterialType = record.MaterialName;
IsDetectionChecked = record.IsChecked == 0 ? false : true;
List<LogoFormula> list = formulaService.GetListAsync().Result;
if(list!=null && list.Count > 0)
{
foreach (var item in list)
{
MaterialTypes.Add(item.Value);
TypeCache.Add(item.Value, item.Key);
}
}
}
public void Save()
{
if (string.IsNullOrEmpty(_selectedMaterialType) && IsDetectionChecked)
{
MessageBox.Show("请选择型号");
return;
}
if (!string.IsNullOrEmpty(_selectedMaterialType)){
//需要检测
string select = _selectedMaterialType;
TypeCache.TryGetValue(select, out int key);
record.CheckKind = key;
}
#region 修改配方
if (record != null)
{
record.IsChecked = IsDetectionChecked ? 1 : 0;
if(record.IsChecked == 0)
{
// 不检测复位0
record.CheckKind = 0;
}
logoConfigService.updateByMaterialType(record);
}
#endregion
MessageBox.Show("修改完成");
}
#region 属性
/// <summary>
/// 型号
/// </summary>
private string _MaterialType;
public string MaterialType
{
get { return _MaterialType; }
set { _MaterialType = value; RaisePropertyChanged(); }
}
/// <summary>
/// 单选框
/// </summary>
private bool _IsDetectionChecked;
public bool IsDetectionChecked
{
get { return _IsDetectionChecked; }
set { _IsDetectionChecked = value; RaisePropertyChanged(); }
}
private string _selectedMaterialType;
public string SelectedMaterialType
{
get { return _selectedMaterialType; }
set
{
_selectedMaterialType = value;
RaisePropertyChanged();
// 可以在这里处理选中项变化后的逻辑
}
}
/// <summary>
/// 下拉框集合
/// </summary>
private ObservableCollection<string> _materialTypes = new ObservableCollection<string>();
public ObservableCollection<string> MaterialTypes
{
get { return _materialTypes; }
set
{
_materialTypes = value;
RaisePropertyChanged();
}
}
#endregion
public RelayCommand SaveCommand { get; set; }
}
}