253 lines
7.0 KiB
C#
253 lines
7.0 KiB
C#
using AutoMapper;
|
||
using CapMachine.Core;
|
||
using CapMachine.Model;
|
||
using CapMachine.Wpf.Dtos;
|
||
using CapMachine.Wpf.Services;
|
||
using Masuit.Tools;
|
||
using Prism.Commands;
|
||
using Prism.Services.Dialogs;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Collections.ObjectModel;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
using System.Windows;
|
||
|
||
namespace CapMachine.Wpf.ViewModels
|
||
{
|
||
/// <summary>
|
||
/// 逻辑规则配置的弹窗
|
||
/// </summary>
|
||
public class DialogLogicRuleViewModel : DialogViewModel
|
||
{
|
||
public DialogLogicRuleViewModel(IFreeSql freeSql, IMapper mapper, LogicRuleService logicRuleService)
|
||
{
|
||
Title = "逻辑规则创建";
|
||
|
||
FreeSql = freeSql;
|
||
Mapper = mapper;
|
||
LogicRuleService = logicRuleService;
|
||
|
||
//var Data = Mapper.Map<List<LogicRuleDto>>(LogicRuleService.LogicRuleDtos);
|
||
|
||
ListLogicRuleDto = LogicRuleService.LogicRuleDtos;
|
||
|
||
}
|
||
public IFreeSql FreeSql { get; }
|
||
public IMapper Mapper { get; }
|
||
public LogicRuleService LogicRuleService { get; }
|
||
|
||
private ObservableCollection<LogicRuleDto> _ListLogicRuleDto;
|
||
/// <summary>
|
||
/// 数据集合
|
||
/// </summary>
|
||
public ObservableCollection<LogicRuleDto> ListLogicRuleDto
|
||
{
|
||
get { return _ListLogicRuleDto; }
|
||
set { _ListLogicRuleDto = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
private LogicRuleDto _selectedRule = new LogicRuleDto();
|
||
/// <summary>
|
||
/// 当前选中的规则
|
||
/// </summary>
|
||
public LogicRuleDto SelectedRule
|
||
{
|
||
get { return _selectedRule; }
|
||
set { _selectedRule = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
/// <summary>
|
||
/// 选中更改之前的LogicRuleDto
|
||
/// </summary>
|
||
public LogicRuleDto SelectedRuleOld { get; set; }
|
||
|
||
private DelegateCommand<object> _GridSelectionChangedCmd;
|
||
/// <summary>
|
||
/// 选中行数据命令
|
||
/// </summary>
|
||
public DelegateCommand<object> GridSelectionChangedCmd
|
||
{
|
||
set
|
||
{
|
||
_GridSelectionChangedCmd = value;
|
||
}
|
||
get
|
||
{
|
||
if (_GridSelectionChangedCmd == null)
|
||
{
|
||
_GridSelectionChangedCmd = new DelegateCommand<object>((par) => GridSelectionChangedCmdMethod(par));
|
||
}
|
||
return _GridSelectionChangedCmd;
|
||
}
|
||
}
|
||
private void GridSelectionChangedCmdMethod(object par)
|
||
{
|
||
var selectedItem = par as LogicRuleDto;
|
||
if (selectedItem != null)
|
||
{
|
||
//防止需要未更改之前的数据
|
||
SelectedRuleOld = selectedItem;
|
||
//先判断是否是正确的集合数据,防止DataGrid的数据源刷新导致的触发事件
|
||
SelectedRule = selectedItem.DeepClone();
|
||
}
|
||
|
||
////选中的行数据
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 是否编辑
|
||
/// </summary>
|
||
private bool IsRuleEdit { get; set; } = false;
|
||
|
||
private DelegateCommand<string> _RuleCmd;
|
||
/// <summary>
|
||
/// 选中行数据命令
|
||
/// </summary>
|
||
public DelegateCommand<string> RuleCmd
|
||
{
|
||
set
|
||
{
|
||
_RuleCmd = value;
|
||
}
|
||
get
|
||
{
|
||
if (_RuleCmd == null)
|
||
{
|
||
_RuleCmd = new DelegateCommand<string>((par) => RuleCmdMethod(par));
|
||
}
|
||
return _RuleCmd;
|
||
}
|
||
}
|
||
private void RuleCmdMethod(string par)
|
||
{
|
||
|
||
IsRuleEdit = true;
|
||
//先判断是否是正确的集合数据,防止DataGrid的数据源刷新导致的触发事件
|
||
switch (par)
|
||
{
|
||
case "Create":
|
||
//SelectedRule
|
||
LogicRuleService.AddRule(Mapper.Map<LogicRule>(SelectedRule));
|
||
break;
|
||
case "Update":
|
||
if (SelectedRule == null)
|
||
{
|
||
MessageBox.Show("选中后再操作数据");
|
||
return;
|
||
}
|
||
LogicRuleService.UpdateRule(Mapper.Map<LogicRule>(SelectedRuleOld), Mapper.Map<LogicRule>(SelectedRule));
|
||
break;
|
||
case "Delete":
|
||
if (SelectedRule == null)
|
||
{
|
||
MessageBox.Show("选中后再操作数据");
|
||
return;
|
||
}
|
||
LogicRuleService.DeleteRule(SelectedRule.Name);
|
||
break;
|
||
default:
|
||
break;
|
||
}
|
||
////选中的行数据
|
||
|
||
}
|
||
|
||
#region 基础弹窗配置
|
||
|
||
|
||
|
||
private string name;
|
||
/// <summary>
|
||
/// 名称
|
||
/// </summary>
|
||
public string Name
|
||
{
|
||
get { return name; }
|
||
set { name = value; RaisePropertyChanged(); }
|
||
}
|
||
|
||
|
||
|
||
private DelegateCommand saveCmd;
|
||
/// <summary>
|
||
/// 保存命令
|
||
/// </summary>
|
||
public DelegateCommand SaveCmd
|
||
{
|
||
set
|
||
{
|
||
saveCmd = value;
|
||
}
|
||
get
|
||
{
|
||
if (saveCmd == null)
|
||
{
|
||
saveCmd = new DelegateCommand(() => SaveCmdMethod());
|
||
}
|
||
return saveCmd;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 保存命令方法
|
||
/// </summary>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
private void SaveCmdMethod()
|
||
{
|
||
DialogParameters pars = new DialogParameters
|
||
{
|
||
{ "IsRuleEdit", IsRuleEdit }
|
||
};
|
||
|
||
RaiseRequestClose(new DialogResult(ButtonResult.OK, pars));
|
||
}
|
||
|
||
private DelegateCommand cancelCmd;
|
||
/// <summary>
|
||
/// 保存命令
|
||
/// </summary>
|
||
public DelegateCommand CancelCmd
|
||
{
|
||
set
|
||
{
|
||
cancelCmd = value;
|
||
}
|
||
get
|
||
{
|
||
if (cancelCmd == null)
|
||
{
|
||
cancelCmd = new DelegateCommand(() => CancelCmdMethod());
|
||
}
|
||
return cancelCmd;
|
||
}
|
||
}
|
||
|
||
|
||
/// <summary>
|
||
/// 取消命令方法
|
||
/// </summary>
|
||
/// <exception cref="NotImplementedException"></exception>
|
||
private void CancelCmdMethod()
|
||
{
|
||
RaiseRequestClose(new DialogResult(ButtonResult.Cancel));
|
||
}
|
||
|
||
/// <summary>
|
||
/// 窗口打开时的传递的参数
|
||
/// </summary>
|
||
/// <param name="parameters"></param>
|
||
public override void OnDialogOpened(IDialogParameters parameters)
|
||
{
|
||
Name = parameters.GetValue<string>("Name");
|
||
IsRuleEdit = false;
|
||
}
|
||
|
||
|
||
|
||
#endregion
|
||
}
|
||
}
|