using HxModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using XHandler.Class;
using HxUserManagement.Classes;
using HxUserManagement.HxBLL;
using HxUserManagement.HxDAL;
namespace HxUserManagement.Views
{
///
/// EditAuthority.xaml 的交互逻辑
///
public partial class EditAuthority : UserControl
{
public event EventHandler closeEvent;
public event EventHandler saveOkEvent;
string ID;
RolesModel roleModel;
RolesBLL rolesBLL = new RolesBLL();
MenuTree menuTrees = new MenuTree();
public EditAuthority(string id)
{
InitializeComponent();
ID = id;
roleModel = rolesBLL.GetInfodById(ID);
this.DataContext = roleModel;
List statusList = DataModel.GetStatusList();
comboxStatus.ItemsSource= statusList;
GetAuthorityTrees(roleModel);
tv.ItemsSource = menuTrees.Children;
}
private List GetAuthorityTrees(RolesModel role)
{
List treeList = new List();
List menuList = new MenuBLL().GetAllMenus();
var parentList = menuList.Where(s => s.Level==0).OrderBy(x=>x.Sequence).ToList();
var childList = menuList.Where(s => s.Level==1).ToList();
foreach (var menu in parentList)
{
MenuTree m = new MenuTree(menu);
menuTrees.Children.Add(m);
//if (!string.IsNullOrEmpty(menu.ChildrenId))
//{
// string[] strAry = menu.ChildrenId.Split(',');
// foreach (string str in strAry)
// {
// var sub = childList.Where(s => s.Id == str).FirstOrDefault();
// MenuTree menuTree = new MenuTree(sub);
// bool isCheck = false;
// if (role.MenuId.Contains(sub.Id))
// {
// isCheck = true;
// }
// m.CreateTreeWithChildren(menuTree, isCheck);
// }
//}
int a = 0;
if (m.Name == "设备管理")
a = 0;
AddSubMenu(m, childList);
}
return treeList;
}
private void AddSubMenu(MenuTree parentMenu, List list)
{
if (!string.IsNullOrEmpty(parentMenu.ChildrenId))
{
string[] strAry = parentMenu.ChildrenId.Split(',');
foreach (string str in strAry)
{
var sub = list.Where(s => s.Id == str).FirstOrDefault();
MenuTree menuTree = new MenuTree(sub);
bool isCheck = false;
if (roleModel.MenuId.Contains(sub.Id))
{
isCheck = true;
}
parentMenu.CreateTreeWithChildren(menuTree, isCheck);
AddSubMenu(menuTree, list);
}
}
else
{
if (roleModel.MenuId.Contains(parentMenu.Id))
parentMenu.IsChecked = true;
else
parentMenu.IsChecked = false;
}
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(0, 600, new Duration(TimeSpan.FromSeconds(0.25)));
Storyboard.SetTarget(doubleAnimation, border);//Target对象
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
storyboard.Children.Add(doubleAnimation);
ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
showAnimation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(showAnimation, border);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromSeconds(0.01));
showAnimation.KeyFrames.Add(closeKeyFrame);
storyboard.Children.Add(showAnimation);
storyboard.Begin();
if (roleModel.DataAuthority == "0")
radioBtnSelf.IsChecked = true;
else
radioBtnAll.IsChecked = true;
comboxStatus.SelectedIndex = Convert.ToInt16(roleModel.Type);
}
private void root_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
if (e.OriginalSource is Grid)
{
Grid grid = e.OriginalSource as Grid;
if (grid.Name == "root")
Button_Click(this, null);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(600, 0, new Duration(TimeSpan.FromSeconds(0.25)));
Storyboard.SetTarget(doubleAnimation, border);//Target对象
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
storyboard.Children.Add(doubleAnimation);
ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
showAnimation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(showAnimation, border);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26));
showAnimation.KeyFrames.Add(closeKeyFrame);
storyboard.Children.Add(showAnimation);
storyboard.Completed += delegate { closeEvent?.Invoke(this, EventArgs.Empty); };
storyboard.Begin();
}
private void ButtonOK_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
Storyboard storyboard = new Storyboard();
DoubleAnimation doubleAnimation = new DoubleAnimation(600, 0, new Duration(TimeSpan.FromSeconds(0.25)));
Storyboard.SetTarget(doubleAnimation, border);//Target对象
Storyboard.SetTargetProperty(doubleAnimation, new PropertyPath("Width"));//Target属性
storyboard.Children.Add(doubleAnimation);
ObjectAnimationUsingKeyFrames showAnimation = new ObjectAnimationUsingKeyFrames();
showAnimation.BeginTime = TimeSpan.FromSeconds(0);
Storyboard.SetTarget(showAnimation, border);
Storyboard.SetTargetProperty(showAnimation, new PropertyPath("(UIElement.Visibility)"));
DiscreteObjectKeyFrame closeKeyFrame = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(0.26));
showAnimation.KeyFrames.Add(closeKeyFrame);
storyboard.Children.Add(showAnimation);
storyboard.Completed += delegate { saveOkEvent?.Invoke(this, EventArgs.Empty); };
storyboard.Begin();
}
private void radioBtnSelf_Checked(object sender, RoutedEventArgs e)
{
if (radioBtnSelf.IsChecked == true)
roleModel.DataAuthority = "0";
}
private void radioBtnAll_Checked(object sender, RoutedEventArgs e)
{
if (radioBtnAll.IsChecked == true)
roleModel.DataAuthority = "1";
}
private void btnSave_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
string ids = "";
foreach (var item in menuTrees.Children)
{
string id = GetSelectedAuthorityIDs(item);
if (!string.IsNullOrEmpty(id))
{
if (string.IsNullOrEmpty(ids))
ids = id;
else
ids += "," + id;
}
}
roleModel.MenuId = ids;
roleModel.Remarks = textboxRemark.Text.Trim();
roleModel.ModifyName = UserManagement.currentUser.UserName;
roleModel.ModifyTime = DateTime.Now;
RolesModel model = rolesBLL.Update(roleModel);
if(model!=null)
MessageBox.Show(Properties.Resources.strModifyRoleSuccess, "Tips");
else
MessageBox.Show(Properties.Resources.strModifyRoleFailed, "Tips");
ButtonOK_Click(this, null);
}
private void btnCancel_Click(object sender, RoutedEventArgs e)
{
if (EventResponseController.Instance.CanExecute() == false)
return;
Button_Click(this, null);
}
private string GetSelectedAuthorityIDs(MenuTree tree)
{
string ret = "";
if (tree.IsChecked == false)
return ret;
else
{
if(tree.Children.Count==0)
{
if(tree.IsChecked == true)
{
ret = tree.Id;
return ret;
}
}
else
{
foreach(var item in tree.Children)
{
string id = GetSelectedAuthorityIDs(item);
if (!string.IsNullOrEmpty(id))
{
if (string.IsNullOrEmpty(ret))
ret = id;
else
ret += "," + id;
}
}
return ret;
}
}
return ret;
}
private void comboxStatus_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (comboxStatus.SelectedIndex < 0)
return;
roleModel.Type = comboxStatus.SelectedIndex.ToString();
}
}
}