Prechádzať zdrojové kódy

Добавлен график ценности сотрудников

Ahatov Artur 6 mesiacov pred
rodič
commit
18c415da58

+ 1 - 0
src/DontHarmDesktop/DontHarmDesktop.csproj

@@ -92,6 +92,7 @@
     </ApplicationDefinition>
     <Compile Include="AuthState.cs" />
     <Compile Include="Models\clientsext.cs" />
+    <Compile Include="Models\UsersExt.cs" />
     <Compile Include="Navigation.cs" />
     <Compile Include="Pages\Clients.xaml.cs">
       <DependentUpon>Clients.xaml</DependentUpon>

+ 49 - 0
src/DontHarmDesktop/Models/UsersExt.cs

@@ -0,0 +1,49 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+
+namespace DontHarmDesktop.Models
+{
+    partial class users
+    {
+        /// <summary>
+        /// ФИО в красивом формате
+        /// </summary>
+        public string HumanName
+        {
+            get
+            {
+                string part1 = this.surname;
+                string part2 = this.name[0] + ".";
+                
+                string part3;
+                if (this.patronymic != null)
+                {
+                    part3 = this.patronymic[0] + ".";
+                } else
+                {
+                    part3 = "";
+                }
+                return part1 + " " + part2 + part3;
+            }
+        }
+
+        /// <summary>
+        /// Среднее арифметическое стоимости услуг, оказываемых сотрудником
+        /// </summary>
+        public float Priceness
+        {
+            get
+            {
+                if (employee_services.Count == 0)
+                {
+                    return 0;
+                }
+                decimal sum = employee_services.Sum(e => e.services.price);
+                return (float)(sum / employee_services.Count);
+            }
+        }
+    }
+}

+ 46 - 8
src/DontHarmDesktop/Pages/Reports/EmployeeCost.xaml

@@ -6,7 +6,7 @@
       xmlns:local="clr-namespace:DontHarmDesktop.Pages.Reports"
       xmlns:root="clr-namespace:DontHarmDesktop"
       mc:Ignorable="d" 
-      xmlns:charts="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization"
+      xmlns:charts="clr-namespace:System.Windows.Forms.DataVisualization.Charting;assembly=System.Windows.Forms.DataVisualization" xmlns:reports="clr-namespace:DontHarmDesktop.ViewModels.Reports" d:DataContext="{d:DesignInstance Type=reports:EmployeeCostVM}"
       d:DesignHeight="450" d:DesignWidth="800"
       Title="EmployeeCost">
 
@@ -34,22 +34,60 @@
                 Margin="0,4,0,0"
                 Content="Сформировать"
                 Command="{Binding StartCmd}"/>
+            <Grid
+                Grid.Row="1"
+                Grid.Column="1"
+                Margin="4">
+
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition/>
+                    <ColumnDefinition/>
+                </Grid.ColumnDefinitions>
+
+                <StackPanel 
+                    Grid.Column="0"
+                    Margin="4">
+                    <TextBlock Text="Фильтр по имени"/>
+                    <TextBox
+                        Text="{Binding FilterName}"/>
+                </StackPanel>
 
-            <WindowsFormsHost Grid.Row="2" Grid.ColumnSpan="2">
+                <StackPanel 
+                    Grid.Column="1"
+                    Margin="4">
+                    <TextBlock Text="Фильтр по ценности"/>
+                    <ComboBox SelectedIndex="{Binding PriceIndex}">
+                        <ComboBoxItem Content="От 0 до 100"/>
+                        <ComboBoxItem Content="От 101 до 250"/>
+                        <ComboBoxItem Content="От 251"/>
+                    </ComboBox>
+                </StackPanel>
+
+            </Grid>
+
+            <WindowsFormsHost 
+                Grid.Row="2"
+                Grid.ColumnSpan="2"
+                Height="Auto" 
+                VerticalAlignment="Top">
+                
                 <charts:Chart>
                     <charts:Chart.Legends>
-                        <charts:Legend>
-
-                        </charts:Legend>
+                        <charts:Legend Name="Default"/>
                     </charts:Chart.Legends>
 
                     <charts:Chart.ChartAreas>
-                        <charts:ChartArea/>
+                        <charts:ChartArea Name="Default" />
                     </charts:Chart.ChartAreas>
 
                     <charts:Chart.Series>
-                        <charts:Series 
-                        IsValueShownAsLabel="True"/>
+                        <charts:Series
+                            x:Name="DefaultSeries"
+                            ChartArea="Default"
+                            Name="Среднее арифметическое стоимости услуг"
+                            IsValueShownAsLabel="True"
+                            ChartType="Bar" 
+                            Legend="Default"/>
                     </charts:Chart.Series>
 
                 </charts:Chart>

+ 3 - 1
src/DontHarmDesktop/Pages/Reports/EmployeeCost.xaml.cs

@@ -23,7 +23,9 @@ namespace DontHarmDesktop.Pages.Reports
         public EmployeeCost()
         {
             InitializeComponent();
-            DataContext = new ViewModels.Reports.EmployeeCostVM();
+            DataContext = new ViewModels.Reports.EmployeeCostVM(
+                this.DefaultSeries
+            );
         }
     }
 }

+ 93 - 1
src/DontHarmDesktop/ViewModels/Reports/EmployeeCostVM.cs

@@ -2,24 +2,116 @@
 using Prism.Mvvm;
 using System;
 using System.Collections.Generic;
+using System.Data.Entity;
 using System.Linq;
+using System.Runtime.InteropServices;
 using System.Text;
 using System.Threading.Tasks;
+using System.Windows.Controls;
+using System.Windows.Forms.DataVisualization.Charting;
+using System.Windows.Media;
+
+// https://www.c-sharpcorner.com/article/static-and-dynamic-line-chart-in-wpf-with-mvvm-pattern-using-prism-library/
 
 namespace DontHarmDesktop.ViewModels.Reports
 {
     public class EmployeeCostVM : BindableBase
     {
+        /// <summary>
+        /// Команда "Сформировать"
+        /// </summary>
         public DelegateCommand StartCmd { get; set; }
 
-        public EmployeeCostVM() 
+        /// <summary>
+        /// Даные для графика
+        /// </summary>
+        private List<KeyValuePair<string, int>> Costs { get; set; }
+
+        /// <summary>
+        /// Интерфейс для отображения точек
+        /// </summary>
+        private readonly Series SeriesInterface;
+
+        /// <summary>
+        /// Фильтр по имени
+        /// </summary>
+        public string FilterName { get;  set; }
+
+        /// <summary>
+        /// Фильтр по ценности.
+        /// 0: От 0 до 100
+        /// 1: От 101 до 250
+        /// 2: От 251
+        /// </summary>
+        public int PriceIndex { get; set; }
+
+        public EmployeeCostVM(Series series) 
         {
             StartCmd = new DelegateCommand(StartExecuted);
+            Costs = new List<KeyValuePair<string, int>>();
+            SeriesInterface = series;
+            FilterName = "";
+            PriceIndex = -1;
         }
 
         public void StartExecuted()
         {
+            Refresh();
+        }
+
+        private async void Refresh()
+        {
+            var db = new Models.Entities();
+            Costs.Clear();
+
+            await db.users.ForEachAsync(u =>
+            {
+                // Фильтр имени пользователя
+                if (!string.IsNullOrEmpty(FilterName) && !u.HumanName.ToLower().Contains(FilterName.ToLower()))
+                {
+                    return;
+                }
+
+                float priceness = u.Priceness;
+                
+                // Фильтр ценности
+                int lowerPriceness;
+                int higherPriceness;
+                switch(PriceIndex)
+                {
+                    case 0:
+                        lowerPriceness = 0;
+                        higherPriceness = 100;
+                        break;
+                    case 1:
+                        lowerPriceness = 101;
+                        higherPriceness = 250;
+                        break;
+                    case 2:
+                        lowerPriceness = 251;
+                        higherPriceness = int.MaxValue;
+                        break;
+                    default:
+                        lowerPriceness = 0;
+                        higherPriceness = int.MaxValue;
+                        break;
+                }
+                if (!(lowerPriceness < priceness && priceness < higherPriceness))
+                {
+                    return;
+                }
+                
+                Costs.Add(new KeyValuePair<string, int>
+                (
+                    u.HumanName,
+                    (int)priceness
+                ));
+            });
 
+            SeriesInterface.Points.Clear();
+            Costs.ForEach(
+                item => SeriesInterface.Points.AddXY(item.Key, item.Value)
+            );
         }
     }
 }