Browse Source

添加用户和设备关联

ltwork 1 year ago
parent
commit
511ef4d2f7

+ 4 - 0
BlankApp1/BizService/BasicDevice.cs

@@ -9,5 +9,9 @@ namespace BizService
 {
     public class BasicDevice : BaseService<bas_device>, IBasicDeviceService
     {
+        public List<bas_device> FindAllDeviceByPrj(int projectId)
+        {
+            return base.GetList(x => x.project_id == projectId);
+        }
     }
 }

+ 1 - 0
BlankApp1/BizService/IBasicDeviceService.cs

@@ -10,5 +10,6 @@ namespace BizService
   
     public interface IBasicDeviceService : IBaseService<bas_device>
     {
+        public List<bas_device> FindAllDeviceByPrj(int projectId);
     }
 }

+ 14 - 0
BlankApp1/BizService/IUserDeviceService.cs

@@ -0,0 +1,14 @@
+using Model.Entities;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BizService
+{
+    public  interface IUserDeviceService : IBaseService<UserDevice>
+    {
+        public List<UserDevice> FindAllByUserId(int userId);
+    }
+}

+ 1 - 0
BlankApp1/BizService/IUserRoleService.cs

@@ -9,5 +9,6 @@ namespace BizService
 {
     public  interface IUserRoleService : IBaseService<UserRole>
     {
+        public UserRole FindByUserID(int userId);
     }
 }

+ 17 - 0
BlankApp1/BizService/UserDeviceService.cs

@@ -0,0 +1,17 @@
+using Model.Entities;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace BizService
+{
+    public class UserDeviceService : BaseService<UserDevice>, IUserDeviceService
+    {
+        public List<UserDevice> FindAllByUserId(int userId)
+        {
+            return base.GetList(x => x.UserId == userId);
+        }
+    }
+}

+ 4 - 0
BlankApp1/BizService/UserRoleService.cs

@@ -9,5 +9,9 @@ namespace BizService
 {
     public  class UserRoleService : BaseService<UserRole>, IUserRoleService
     {
+        public UserRole FindByUserID(int userId)
+        {
+            return base.GetFirst(x => (x.UserId == userId));
+        }
     }
 }

+ 3 - 0
BlankApp1/BlankApp1/App.xaml.cs

@@ -103,7 +103,10 @@ namespace BlankApp1
             containerRegistry.RegisterDialog<RoleWithMenuView, RoleWithMenuViewModel>();
             containerRegistry.RegisterDialog<RoleWithProjectView, RoleWithProjectViewModel>();
             containerRegistry.RegisterDialog<AddEditMenuView, AddEditMenuViewModel>();
+            containerRegistry.RegisterDialog<UserWithDeviceView, UserWithDeviceViewModel>();
             
+
+
         }
 
         private IMapper GetMapper(IContainerProvider container)

+ 53 - 0
BlankApp1/BlankApp1/Models/SelectUserDeviceModel.cs

@@ -0,0 +1,53 @@
+using Model.Dto;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Models
+{
+    public  class SelectUserDeviceModel : BaseDto
+    {
+        private int userId;
+        public int UserId
+        {
+            get { return userId; }
+            set { userId = value; OnPropertyChanged(); }
+        }
+        private int deviceId;
+        public int DeviceId
+        {
+            get { return deviceId; }
+            set { deviceId = value; OnPropertyChanged(); }
+        }
+
+        private string deviceName;
+        public string DeviceName
+        {
+            get { return deviceName; }
+            set { deviceName = value; OnPropertyChanged(); }
+        }
+
+        private string projectName;
+        public string ProjectName
+        {
+            get { return projectName; }
+            set { projectName = value; OnPropertyChanged(); }
+        }
+        
+
+        private bool isSelected { get; set; }
+        public bool IsSelected
+        {
+            get { return isSelected; }
+            set { isSelected = value; OnPropertyChanged(); }
+        }
+        private string describe { get; set; }
+        public string Describe
+        {
+            get { return describe; }
+            set { describe = value; OnPropertyChanged(); }
+        }
+    }
+}

+ 19 - 0
BlankApp1/BlankApp1/Models/UserWithRoleModel.cs

@@ -0,0 +1,19 @@
+using Model.Dto;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace PLCTool.Models
+{
+    public class UserWithRoleModel:UserDto
+    {
+        private string roleName;
+        public string RoleName
+        {
+            get { return roleName; }
+            set { roleName = value; OnPropertyChanged(); }
+        }
+    }
+}

+ 210 - 0
BlankApp1/BlankApp1/ViewModels/SystemManageViewModel/Dialogs/UserWithDeviceViewModel.cs

@@ -0,0 +1,210 @@
+using AutoMapper;
+using BizService;
+using Microsoft.Extensions.Logging;
+using Microsoft.VisualBasic.ApplicationServices;
+using Model;
+using Model.Entities;
+using PLCTool.Models;
+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 UserWithDeviceViewModel : BindableBase, IDialogAware
+    {
+        private readonly IMapper _mapper;
+        private readonly ILogger _logger;
+        private readonly IUserService _iUserService;
+        private readonly IUserRoleService _iUserRoleService;
+        private readonly IUserDeviceService _iUserDeviceService;
+        private readonly IBasicProjectService _iBasicProjectService;
+        private readonly IRoleProjectService _iRoleProjectService;
+        private readonly IBasicDeviceService _iBasicDeviceService;
+        private string tyepName = String.Empty;
+        private int userId;
+
+        public UserWithDeviceViewModel(IEventAggregator aggregator, IUserService iUserService, IUserDeviceService iUserDeviceService, IUserRoleService iUserRoleService, IBasicProjectService iBasicProjectService, IRoleProjectService iRoleProjectService, IBasicDeviceService iBasicDeviceService, IMapper mapper, ILogger logger)
+        {
+
+
+            _mapper = mapper;
+            _logger = logger;
+            _iUserDeviceService = iUserDeviceService;
+            _iBasicProjectService = iBasicProjectService;
+            _iUserService = iUserService;
+            _iUserRoleService = iUserRoleService;
+            _iRoleProjectService = iRoleProjectService;
+            _iBasicDeviceService = iBasicDeviceService;
+            SureCommand = new DelegateCommand<string>(Sure);
+
+            SelectAllCommand = new DelegateCommand<object>(SelectAll);
+            UnSelectAllCommand = new DelegateCommand<object>(UnSelecttAll);
+
+        }
+
+        private void GetAllDevicesByRole()
+        {
+            var userRo = _iUserRoleService.FindByUserID(userId);
+            if (userRo != null)
+            {
+                int roleId = (int)userRo.RoleId;
+                var roleProjects= _iRoleProjectService.FindAllByRoleId(roleId);
+                //找到项目
+                foreach(var pro in roleProjects)
+                {
+                    string projectName = _iBasicProjectService.Find((int)pro.ProjectId).project_name;
+                    //项目对应的设备
+                    var devices=_iBasicDeviceService.FindAllDeviceByPrj((int)pro.ProjectId);
+                    foreach(var device in devices)
+                    {
+                        //查找用户是否带有这个设备
+                        bool isSelect = false;
+
+                        var findDevice = _iUserDeviceService.FindAllByUserId(userId).FirstOrDefault(x => x.DeviceId == device.device_id);
+                        if (findDevice != null)
+                        {
+                            isSelect = true;
+                        }
+                        UserWithDeviceList.Add(new SelectUserDeviceModel()
+                        {
+                            DeviceName = device.device_name,
+                            ProjectName= projectName,
+                            Describe = device.remark,
+                            IsSelected = isSelect,
+                            UserId=userId,
+                            DeviceId= (int)device.device_id
+
+                        });
+                    }
+                }
+
+            }
+        
+        }
+
+        private void UnSelecttAll(object obj)
+        {
+            UserWithDeviceList.ToList().FindAll(p => p.IsSelected = false);
+        }
+
+        private void SelectAll(object obj)
+        {
+            UserWithDeviceList.ToList().FindAll(p => p.IsSelected = true);
+        }
+
+
+
+
+        private void Sure(string obj)
+        {
+            //查找是否有这个测试方案
+
+            var findSelect = UserWithDeviceList.FirstOrDefault(x => x.IsSelected == true);
+            if (findSelect == null)
+            {
+                MessageBoxResult boxResult = MessageBox.Show("请勾选测试项!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
+                return;
+            }
+            //查找的信息
+            //删除这个用户以前的设备
+            var findDevices =_iUserDeviceService.FindAllByUserId(userId);
+            foreach (var item in findDevices)
+            {
+                _iUserDeviceService.Delete(item.Id);
+            }
+            //添加用户关联的设备到数据库
+            var roleProjectss = UserWithDeviceList.ToList().FindAll(x => x.IsSelected).OrderBy(y => y.DeviceId);
+            foreach (var item in roleProjectss)
+            {
+                _iUserDeviceService.Add(new UserDevice
+                {
+                    UserId=item.UserId,
+                    DeviceId=item.DeviceId,
+
+                });
+            }
+            RequestClose?.Invoke(new DialogResult(ButtonResult.OK));
+        }
+
+        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)
+                {
+
+                    userId = item;
+                    GetAllDevicesByRole();
+                    UserName=_iUserService.Find(userId)?.UserName;
+
+
+                }
+                
+
+            }
+
+
+        }
+        #region 命令绑定
+        public DelegateCommand<string> SureCommand { set; get; }
+
+
+        public DelegateCommand<object> SelectAllCommand { set; get; }
+        public DelegateCommand<object> UnSelectAllCommand { set; get; }
+
+        #endregion
+        #region 数据绑定
+        /// <summary>
+        /// 用户名
+        /// </summary>
+        private string userName;
+        public string UserName
+        {
+            get { return userName; }
+            set { userName = value; RaisePropertyChanged(); }
+        }
+
+
+
+        private ObservableCollection<SelectUserDeviceModel> userWithDeviceList = new ObservableCollection<SelectUserDeviceModel>();
+        public ObservableCollection<SelectUserDeviceModel> UserWithDeviceList
+        {
+            get { return userWithDeviceList; }
+            set { SetProperty(ref userWithDeviceList, value); }
+        }
+
+
+        #endregion
+
+    }
+}

+ 57 - 9
BlankApp1/BlankApp1/ViewModels/SystemManageViewModel/UserManageViewModel.cs

@@ -25,15 +25,19 @@ namespace PLCTool.ViewModels.SystemManageViewModel
     public  class UserManageViewModel:BindableBase
     {
         private readonly IUserService _iUserService;
+        private readonly IUserRoleService _iUserRoleService;
+        private readonly IRoleService _iRoleService;
         private readonly ILogger _logger;
         private readonly IMapper _mapper;
         private readonly IDialogService _dialog;
         private readonly IEventAggregator _aggregator;
-        private List<UserDto> allUserInfoList = new List<UserDto>();
-        public UserManageViewModel(IUserService iUserService, IMapper mapper, ILogger logger, IDialogService dialog,IEventAggregator aggregator)
+        private List<UserWithRoleModel> allUserInfoList = new List<UserWithRoleModel>();
+        public UserManageViewModel(IUserService iUserService, IUserRoleService iUserRoleService, IRoleService iRoleService,IMapper mapper, ILogger logger, IDialogService dialog,IEventAggregator aggregator)
         {
 
             _iUserService = iUserService;
+            _iUserRoleService = iUserRoleService;
+            _iRoleService = iRoleService;
             _mapper = mapper;
             _logger = logger;
             _dialog = dialog;
@@ -43,10 +47,11 @@ namespace PLCTool.ViewModels.SystemManageViewModel
             DeleteCommand = new DelegateCommand<object>(Delete);
             OnLoadCommand = new DelegateCommand(OnLoad);
             QueryCommand = new DelegateCommand<object>(Query);
-  
+            AuthoDeviceCommand = new DelegateCommand<object>(AuthDevice);
         }
 
-     
+      
+
 
 
 
@@ -71,6 +76,24 @@ namespace PLCTool.ViewModels.SystemManageViewModel
 
             });
         }
+        private void AuthDevice(object obj)
+        {
+            int id = Convert.ToInt32(obj);
+
+            DialogParameters parm = new DialogParameters();
+            parm.Add("Key", id);
+            //蒙层显示
+            _aggregator.GetEvent<MaskEvent>().Publish(true);
+            _dialog.ShowDialog("UserWithDeviceView", parm, async callback =>
+            {
+                if (callback.Result == ButtonResult.OK)
+                {
+               
+                }
+
+            });
+            _aggregator.GetEvent<MaskEvent>().Publish(false);
+        }
 
         private void EditUser(object obj)
         {
@@ -88,6 +111,7 @@ namespace PLCTool.ViewModels.SystemManageViewModel
                 }
 
             });
+            _aggregator.GetEvent<MaskEvent>().Publish(false);
         }
 
         private void Delete(object obj)
@@ -107,9 +131,31 @@ namespace PLCTool.ViewModels.SystemManageViewModel
         }
         private  void GetAllUsers()
         {
+            allUserInfoList.Clear();
+            UserInfoList.Clear();
             var users = _iUserService.QueryList();
-            allUserInfoList = _mapper.Map<List<User>, List<UserDto>>(users);
-            UserInfoList = new ObservableCollection<UserDto>(allUserInfoList);
+            foreach(var user in users)
+            {
+                var userRo= _iUserRoleService.FindByUserID(user.Id);
+                if(userRo!=null)
+                {
+                    int roleId= (int)userRo.RoleId;
+                    string roleName = _iRoleService.Find(roleId)?.Name;
+                    UserWithRoleModel userRole = new UserWithRoleModel()
+                    {
+                        Id=user.Id,
+                        UserName=user.UserName,
+                        RoleName=roleName,
+                        Description=user.Description,
+
+                    };
+                    allUserInfoList.Add(userRole);
+                    UserInfoList.Add(userRole);
+                }
+             
+
+            }
+          
         }
 
         /// <summary>
@@ -133,13 +179,13 @@ namespace PLCTool.ViewModels.SystemManageViewModel
                                select a).ToList();
             //默认显示的第一页
 
-            UserInfoList = new ObservableCollection<UserDto>(conUsers);
+            UserInfoList = new ObservableCollection<UserWithRoleModel>(conUsers);
         }
 
         #endregion
         #region 数据绑定
-        private ObservableCollection<UserDto> userInfoList = new ObservableCollection<UserDto>();
-        public ObservableCollection<UserDto> UserInfoList
+        private ObservableCollection<UserWithRoleModel> userInfoList = new ObservableCollection<UserWithRoleModel>();
+        public ObservableCollection<UserWithRoleModel> UserInfoList
         {
             get { return userInfoList; }
             set { userInfoList = value; RaisePropertyChanged(); }
@@ -159,7 +205,9 @@ namespace PLCTool.ViewModels.SystemManageViewModel
         public DelegateCommand<object> EditCommand { set; get; }
         public DelegateCommand<object> DeleteCommand { set; get; }
         public DelegateCommand<object> QueryCommand { set; get; }
+        public DelegateCommand<object> AuthoDeviceCommand { set; get; }
         
+
         #endregion
     }
 }

+ 56 - 0
BlankApp1/BlankApp1/Views/SystemManageView/Dialogs/UserWithDeviceView.xaml

@@ -0,0 +1,56 @@
+<UserControl x:Class="PLCTool.Views.SystemManageView.Dialogs.UserWithDeviceView"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+             xmlns:local="clr-namespace:PLCTool.Views.SystemManageView.Dialogs"
+                          xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
+             Height="600" Width="800">
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="60" />
+            <RowDefinition />
+        </Grid.RowDefinitions>
+        <DockPanel Grid.Row="0" >
+            <TextBlock Text="用户名:"  Style="{StaticResource NormalTextBlockStyle}" Margin="10,0,5,0"/>
+            <TextBox  Height="28" Width="160" Text="{Binding UserName}" />
+
+            <Button  Content="确认授权" Width="80"  Margin="5,0"  Command="{Binding SureCommand}" Style="{StaticResource NormalButtonStyle}" />
+        </DockPanel>
+        <Grid Grid.Row="1">
+            <DataGrid  Grid.Row="2"  Style="{StaticResource MyDataGridSyle}"
+             ColumnHeaderStyle="{StaticResource ColumnHeaderStyle}"  RowHeaderStyle="{StaticResource RowHeaderStyle}" RowStyle="{StaticResource DataGridRowtyle}"  AlternationCount="2"
+            ItemsSource="{Binding UserWithDeviceList}"  >
+
+                <DataGrid.Columns >
+                    <DataGridTemplateColumn CanUserResize="False" Width="50">
+                        <DataGridTemplateColumn.HeaderTemplate>
+                            <DataTemplate>
+                                <CheckBox Content="全选" IsChecked="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}, Path=AllSelected}" Foreground="Black" >
+                                    <i:Interaction.Triggers>
+                                        <i:EventTrigger EventName="Checked">
+                                            <i:InvokeCommandAction  Command="{Binding DataContext.SelectAllCommand, RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding IsSelectAll, ElementName=qx}"/>
+                                        </i:EventTrigger>
+                                        <i:EventTrigger EventName="Unchecked">
+                                            <i:InvokeCommandAction   Command="{Binding DataContext.UnSelectAllCommand,  RelativeSource={RelativeSource AncestorType={x:Type DataGrid}}}" CommandParameter="{Binding IsSelectAll, ElementName=qx}"/>
+                                        </i:EventTrigger>
+                                    </i:Interaction.Triggers>
+                                </CheckBox>
+                            </DataTemplate>
+                        </DataGridTemplateColumn.HeaderTemplate>
+                        <DataGridTemplateColumn.CellTemplate>
+                            <DataTemplate>
+                                <CheckBox IsChecked="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  HorizontalAlignment="Center" />
+                            </DataTemplate>
+                        </DataGridTemplateColumn.CellTemplate>
+                    </DataGridTemplateColumn>
+                    <DataGridTextColumn Header="设备名称" Binding="{Binding DeviceName}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
+                    <DataGridTextColumn Header="项目名称" Binding="{Binding ProjectName}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
+                    <DataGridTextColumn Header="描述" Binding="{Binding Describe}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
+
+                </DataGrid.Columns>
+
+            </DataGrid>
+        </Grid>
+    </Grid>
+</UserControl>

+ 28 - 0
BlankApp1/BlankApp1/Views/SystemManageView/Dialogs/UserWithDeviceView.xaml.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+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.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace PLCTool.Views.SystemManageView.Dialogs
+{
+    /// <summary>
+    /// UserWithDeviceView.xaml 的交互逻辑
+    /// </summary>
+    public partial class UserWithDeviceView : UserControl
+    {
+        public UserWithDeviceView()
+        {
+            InitializeComponent();
+        }
+    }
+}

+ 8 - 2
BlankApp1/BlankApp1/Views/SystemManageView/UserManageView.xaml

@@ -40,15 +40,21 @@
         ItemsSource="{Binding UserInfoList}" >
             <DataGrid.Columns >
                 <DataGridTextColumn Header="用户名" Binding="{Binding UserName}" CellStyle="{StaticResource MyDataGridCellStyle}" />
+                <DataGridTextColumn Header="角色" Binding="{Binding RoleName}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
                 <DataGridTextColumn Header="备注" Binding="{Binding Description}" CellStyle="{StaticResource MyDataGridCellStyle}"/>
                
-                <DataGridTemplateColumn Header="操作" Width="180" CellStyle="{StaticResource MyDataGridCellStyle}">
+                <DataGridTemplateColumn Header="操作"  CellStyle="{StaticResource MyDataGridCellStyle}">
                     <DataGridTemplateColumn.CellTemplate>
                         <DataTemplate>
                             <StackPanel Orientation="Horizontal">
                                 <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
+   Command="{Binding DataContext.AuthoDeviceCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding Id}" Cursor="Hand" Margin="0,0,10,0" >
+                                    <StackPanel Orientation="Horizontal">
+                                        <TextBlock  Text="分配设备" VerticalAlignment="Center" Foreground="Blue"/>
+                                    </StackPanel>
+                                </Button>
+                                <Button  Width="auto" Background="Transparent" HorizontalContentAlignment ="Left" Foreground="Black" Height="25" BorderThickness="0"
                                  Command="{Binding DataContext.EditCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=DataGrid}}" CommandParameter="{Binding Id}" Cursor="Hand" Margin="0,0,10,0" >
-
                                     <StackPanel Orientation="Horizontal">
                                         <TextBlock  Text="编辑" VerticalAlignment="Center" Foreground="Blue"/>
                                     </StackPanel>

+ 43 - 0
BlankApp1/Model/Entities/UserDevice.cs

@@ -0,0 +1,43 @@
+using SqlSugar;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace Model.Entities
+{
+   
+
+    [SugarTable("userdevices")]
+    public partial class UserDevice
+    {
+        public UserDevice()
+        {
+
+
+        }
+        /// <summary>
+        /// Desc:
+        /// Default:
+        /// Nullable:False
+        /// </summary>           
+        [SugarColumn(IsPrimaryKey = true, IsIdentity = true)]
+        public int Id { get; set; }
+
+        /// <summary>
+        /// Desc:
+        /// Default:
+        /// Nullable:True
+        /// </summary>           
+        public int? UserId { get; set; }
+
+        /// <summary>
+        /// Desc:
+        /// Default:
+        /// Nullable:True
+        /// </summary>           
+        public int? DeviceId { get; set; }
+
+    }
+}