|
@@ -0,0 +1,281 @@
|
|
|
+using AutoMapper;
|
|
|
+using BizService;
|
|
|
+using Model.Dto;
|
|
|
+using Model.Entities;
|
|
|
+using Prism.Commands;
|
|
|
+using Prism.Events;
|
|
|
+using Prism.Mvvm;
|
|
|
+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 PLCTool.ViewModels.SystemManageViewModel.Dialogs
|
|
|
+{
|
|
|
+
|
|
|
+ public class AddEditMenuViewModel : BindableBase, IDialogAware
|
|
|
+ {
|
|
|
+ private readonly IEventAggregator _aggregator;
|
|
|
+
|
|
|
+ private readonly IMenuService _iMenuService;
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+ private List<MenuDto> allMenuInfoList = new List<MenuDto>();
|
|
|
+
|
|
|
+
|
|
|
+ private int id = 0;
|
|
|
+ public AddEditMenuViewModel(IEventAggregator aggregator, IMenuService iMenuService, IMapper mapper)
|
|
|
+ {
|
|
|
+ _aggregator = aggregator;
|
|
|
+
|
|
|
+ _iMenuService = iMenuService;
|
|
|
+ _mapper = mapper;
|
|
|
+ CloseCommand = new DelegateCommand(Close);
|
|
|
+ SureCommand = new DelegateCommand<string>(Sure);
|
|
|
+ CancelCommand = new DelegateCommand(Close);
|
|
|
+
|
|
|
+ GetMenus();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #region idialog接口实现
|
|
|
+ public string Title { set; get; } = "新增用户";
|
|
|
+
|
|
|
+ public event Action<IDialogResult> RequestClose;
|
|
|
+
|
|
|
+ public bool CanCloseDialog()
|
|
|
+ {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnDialogClosed()
|
|
|
+ {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public void OnDialogOpened(IDialogParameters parameters)
|
|
|
+ {
|
|
|
+ var getMsg = parameters.GetValues<int>("Key");
|
|
|
+ ///值不为空,表示修改
|
|
|
+ if (getMsg != null)
|
|
|
+ {
|
|
|
+ foreach (var item in getMsg)
|
|
|
+ {
|
|
|
+ if (item != null)
|
|
|
+ {
|
|
|
+ Title = "修改菜单";
|
|
|
+ id = item;
|
|
|
+ var findMenu = allMenuInfoList.FirstOrDefault(x => x.MenuId == id);
|
|
|
+ if (findMenu != null)
|
|
|
+ {
|
|
|
+ MenuHeader = findMenu.MenuHeader;
|
|
|
+ TargetView = findMenu.TargetView;
|
|
|
+ ParentId = findMenu.ParentId;
|
|
|
+ MenuIcon = findMenu.MenuIcon;
|
|
|
+ Index = findMenu.Index;
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ #region 私有方法
|
|
|
+
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 判断输入的数据
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="input"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+
|
|
|
+
|
|
|
+ private void Close()
|
|
|
+ {
|
|
|
+ RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel));
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 确认
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj"></param>
|
|
|
+ private void Sure(string obj)
|
|
|
+ {
|
|
|
+ if (string.IsNullOrEmpty(MenuHeader))
|
|
|
+ {
|
|
|
+ MessageBox.Show("请填写菜单名", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(TargetView))
|
|
|
+ {
|
|
|
+ MessageBox.Show("请填写菜单界面!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (string.IsNullOrEmpty(MenuIcon))
|
|
|
+ {
|
|
|
+ MessageBox.Show("请选择图标!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ //判断是否存在用户名
|
|
|
+
|
|
|
+ var findResult = allMenuInfoList.FirstOrDefault(x => x.MenuHeader == MenuHeader);
|
|
|
+ if (findResult != null)
|
|
|
+ {
|
|
|
+ if (Title == "新增用户")
|
|
|
+ {
|
|
|
+ MessageBox.Show("已存在此菜单名!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //修改时,若查到其他的用户名和这个重复
|
|
|
+ if (findResult.MenuId != id)
|
|
|
+ {
|
|
|
+ MessageBox.Show("已存在此菜单名!", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (Title == "新增用户")
|
|
|
+ {
|
|
|
+ AddUser();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ EditUser();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
|
|
|
+ }
|
|
|
+
|
|
|
+ private void AddUser()
|
|
|
+ {
|
|
|
+
|
|
|
+ ////添加到数据库user表中
|
|
|
+ //iMenuService.Add(new User()
|
|
|
+ //{
|
|
|
+ // UserName = UserName,
|
|
|
+ // Password = PassWord,
|
|
|
+ // Description = Describe
|
|
|
+ //});
|
|
|
+ ////查找添加的userid
|
|
|
+ //int newUserId = 0;
|
|
|
+ //var findId = _iUserService.QueryList().FirstOrDefault(x => x.UserName == UserName);
|
|
|
+ //if (findId != null)
|
|
|
+ //{
|
|
|
+ // newUserId = findId.Id;
|
|
|
+ //}
|
|
|
+ ////添加到数据库useRole表中
|
|
|
+ //bool isAdd = _iMenuService.Add(new UserRole()
|
|
|
+ //{
|
|
|
+ // UserId = newUserId,
|
|
|
+ // RoleId = allRoles.FirstOrDefault(x => x.Name == (SelectRole)).Id
|
|
|
+ //});
|
|
|
+ //if (isAdd)
|
|
|
+ //{
|
|
|
+ // MessageBox.Show("添加用户成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+
|
|
|
+ //}
|
|
|
+
|
|
|
+ }
|
|
|
+ private void EditUser()
|
|
|
+ {
|
|
|
+ Menu menu = new Menu()
|
|
|
+ {
|
|
|
+
|
|
|
+ MenuHeader = MenuHeader,
|
|
|
+ TargetView = TargetView,
|
|
|
+ ParentId = ParentId,
|
|
|
+ MenuIcon= MenuIcon,
|
|
|
+ Index=Index
|
|
|
+
|
|
|
+ };
|
|
|
+ //修改到数据库user表中
|
|
|
+ bool isedit = _iMenuService.Edit(menu);
|
|
|
+ if (isedit)
|
|
|
+ {
|
|
|
+ MessageBox.Show("修改用户成功", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void GetMenus()
|
|
|
+ {
|
|
|
+ var menus = _iMenuService.QueryList();
|
|
|
+ allMenuInfoList = _mapper.Map<List<Menu>, List<MenuDto>>(menus);
|
|
|
+ }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #region 命令绑定
|
|
|
+ public DelegateCommand CloseCommand { set; get; }
|
|
|
+ public DelegateCommand<string> SureCommand { set; get; }
|
|
|
+ public DelegateCommand CancelCommand { set; get; }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ #region 变量绑定
|
|
|
+ private string menuHeader;
|
|
|
+ public string MenuHeader
|
|
|
+ {
|
|
|
+ get { return menuHeader; }
|
|
|
+ set { menuHeader = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+ private string targetView;
|
|
|
+ public string TargetView
|
|
|
+ {
|
|
|
+ get { return targetView; }
|
|
|
+ set { targetView = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ private int parentId;
|
|
|
+ public int ParentId
|
|
|
+ {
|
|
|
+ get { return parentId; }
|
|
|
+ set { parentId = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ private string menuIcon;
|
|
|
+ public string MenuIcon
|
|
|
+ {
|
|
|
+ get { return menuIcon; }
|
|
|
+ set { menuIcon = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+ private int index;
|
|
|
+ public int Index
|
|
|
+ {
|
|
|
+ get { return index; }
|
|
|
+ set { index = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ private string describe;
|
|
|
+ public string Describe
|
|
|
+ {
|
|
|
+ get { return describe; }
|
|
|
+ set { describe = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ }
|
|
|
+}
|