547 lines
20 KiB
C#
547 lines
20 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Windows;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Input;
|
|
using PipefyProspUpdate.Services;
|
|
using PipefyProspUpdate.Models;
|
|
using System.Net.Http;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using System.Windows.Media;
|
|
using System.Diagnostics;
|
|
using System.Windows.Navigation;
|
|
using System.Collections.ObjectModel;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
|
|
namespace PipefyProspUpdate
|
|
{
|
|
public partial class Users : Page
|
|
{
|
|
public class ViewModel : INotifyPropertyChanged
|
|
{
|
|
private string _labelContent = "Cards filtrados: 0";
|
|
public string LabelContent
|
|
{
|
|
get { return _labelContent; }
|
|
set
|
|
{
|
|
if (_labelContent != value)
|
|
{
|
|
_labelContent = value;
|
|
OnPropertyChanged(nameof(LabelContent));
|
|
}
|
|
}
|
|
}
|
|
|
|
public event PropertyChangedEventHandler? PropertyChanged;
|
|
|
|
protected void OnPropertyChanged(string propertyName)
|
|
{
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
}
|
|
}
|
|
|
|
private List<Models.Users> _users = AppState.Instance.Users;
|
|
private readonly HttpClient _httpClient = new();
|
|
private readonly PipefyApiService? _apiService;
|
|
private readonly List<Models.Suggestion> _meso = AppState.Instance.MesoChangeBoxItems.ToList();
|
|
private readonly List<Models.Suggestion> _city = AppState.Instance.CityChangeBoxItems.ToList();
|
|
|
|
public Users()
|
|
{
|
|
var progress = new Progress<int>(percent =>
|
|
{
|
|
ProgressBar.Value = percent;
|
|
ProgressBar.IsIndeterminate = percent == 0;
|
|
});
|
|
|
|
InitializeComponent();
|
|
_apiService = new PipefyApiService(_httpClient);
|
|
DataContext = new ViewModel
|
|
{
|
|
LabelContent = "Cards filtrados: 0"
|
|
};
|
|
|
|
LoadUsers();
|
|
FilterCards(progress);
|
|
}
|
|
|
|
public static void startProgress(IProgress<int> progress)
|
|
{
|
|
progress.Report(0);
|
|
}
|
|
public static void endProgress(IProgress<int> progress)
|
|
{
|
|
progress.Report(100);
|
|
}
|
|
private async void FilterCards(Progress<int> progress)
|
|
{
|
|
var viewModel = (ViewModel)DataContext;
|
|
|
|
if (AppState.Instance.filterFlag)
|
|
{
|
|
viewModel.LabelContent = "Cards filtrados: 0";
|
|
CardsChangeBox.ItemsSource = null;
|
|
RunMutations.IsEnabled = false;
|
|
startProgress(progress);
|
|
AppState.Instance.ProspeccaoFilteredCards = new List<Models.Node>();
|
|
AppState.Instance.currentItems = 0;
|
|
|
|
List<Task<List<Node>>> listOfTasks_Prosp = new();
|
|
|
|
foreach (var meso in _meso)
|
|
{
|
|
_city.AddRange(AppState.Instance.Suggestions!.Where(x => x.Meso == meso.Meso).Select(x => new Models.Suggestion { ID = x.ID, UF = x.UF, Meso = x.Meso, City = x.City }));
|
|
}
|
|
foreach (var city in _city)
|
|
{
|
|
listOfTasks_Prosp.Add((_apiService ?? new PipefyApiService(_httpClient)).GetCardsAsync("303017662", "cidade_importa_o", city, progress, _city.Count * 2));
|
|
listOfTasks_Prosp.Add((_apiService ?? new PipefyApiService(_httpClient)).GetCardsAsync("303017678", "cidade", city, progress, _city.Count * 2));
|
|
}
|
|
|
|
var allResults_Prosp = await Task.WhenAll(listOfTasks_Prosp);
|
|
|
|
List<Node> allResults = new();
|
|
foreach (var result in allResults_Prosp)
|
|
{
|
|
allResults.AddRange(result);
|
|
}
|
|
|
|
AppState.Instance.ProspeccaoFilteredCards.AddRange(allResults.OrderByDescending(x => (x.pipe ?? new Models.Pipe { name = "_" }).name));
|
|
AppState.Instance.filterFlag = false;
|
|
RunMutations.IsEnabled = true;
|
|
}
|
|
else
|
|
{
|
|
endProgress(progress);
|
|
}
|
|
|
|
AppState.Instance.ProspeccaoFilteredCards = AppState.Instance.ProspeccaoFilteredCards.Distinct().ToList();
|
|
|
|
AppState.Instance.Save();
|
|
|
|
CardsChangeBox.ItemsSource = AppState.Instance.ProspeccaoFilteredCards;
|
|
|
|
viewModel.LabelContent = "Cards filtrados: " + (AppState.Instance.ProspeccaoFilteredCards.Count);
|
|
|
|
}
|
|
private void LoadUsers()
|
|
{
|
|
_users ??= AppState.Instance.Users;
|
|
|
|
UsersSuggestionsList.ItemsSource = null;
|
|
UsersSuggestionsList.ItemsSource = _users;
|
|
|
|
UsersSearchBox.TextChanged += SearchBox_TextChanged;
|
|
|
|
UsersSuggestionsList.PreviewMouseLeftButtonDown += ListBox_MouseLeftButtonDown;
|
|
|
|
KeepProsp.IsChecked = AppState.Instance.KeepProsp;
|
|
KeepResp.IsChecked = AppState.Instance.KeepResp;
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
|
|
}
|
|
private void ListBox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
|
|
{
|
|
if (sender is ListBox listBox)
|
|
{
|
|
var clickedItem = e.OriginalSource as FrameworkElement;
|
|
|
|
while (clickedItem != null && clickedItem is not ListBoxItem)
|
|
{
|
|
clickedItem = VisualTreeHelper.GetParent(clickedItem) as FrameworkElement;
|
|
}
|
|
|
|
if (clickedItem is ListBoxItem)
|
|
{
|
|
var selectedItems = listBox.SelectedItems.OfType<Models.Users>().ToList();
|
|
|
|
if (selectedItems.Count > 0)
|
|
{
|
|
DragDrop.DoDragDrop(listBox, selectedItems, DragDropEffects.Move);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void ListBox_DragOver(object sender, DragEventArgs e)
|
|
{
|
|
e.Effects = DragDropEffects.Move;
|
|
e.Handled = true;
|
|
}
|
|
|
|
private void HandleDrop(object sender, DragEventArgs e)
|
|
{
|
|
if (e.Data.GetData(typeof(List<Models.Users>)) is List<Models.Users> droppedData && sender is ListBox origin)
|
|
{
|
|
switch (origin.Name)
|
|
{
|
|
case "ProspectanteChangeBox":
|
|
AppState.Instance.Prospectante.Clear();
|
|
AppState.Instance.Prospectante.Add(droppedData[0]);
|
|
|
|
if (AppState.Instance.Others.Any(x => x.id == droppedData[0].id))
|
|
{
|
|
var existingItem = AppState.Instance.Others.First(x => x.id == droppedData[0].id);
|
|
AppState.Instance.Others.Remove(existingItem);
|
|
}
|
|
break;
|
|
|
|
case "ResponsavelChangeBox":
|
|
foreach (var item in droppedData)
|
|
{
|
|
if (!AppState.Instance.Others.Contains(item))
|
|
{
|
|
AppState.Instance.Others.Add(item);
|
|
}
|
|
}
|
|
|
|
AppState.Instance.Prospectante.RemoveAll(x => droppedData.Any(d => d.id == x.id));
|
|
break;
|
|
}
|
|
|
|
AppState.Instance.Save();
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
}
|
|
}
|
|
|
|
private void ChangeBox_PreviewKeyDown(object sender, KeyEventArgs e)
|
|
{
|
|
ListBox? origin = sender as ListBox;
|
|
Models.Users? selectedItem = origin!.SelectedItem as Models.Users;
|
|
|
|
if (sender != null && (e.Key == Key.Delete || e.Key == Key.Back))
|
|
{
|
|
switch (origin!.Name)
|
|
{
|
|
case ("ProspectanteChangeBox"):
|
|
|
|
AppState.Instance.Prospectante.Remove(selectedItem!);
|
|
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ProspectanteChangeBox.SelectedIndex = 0;
|
|
|
|
break;
|
|
case ("ResponsavelChangeBox"):
|
|
|
|
AppState.Instance.Others.Remove(selectedItem!);
|
|
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
ResponsavelChangeBox.SelectedIndex = 0;
|
|
|
|
break;
|
|
}
|
|
|
|
AppState.Instance.Save();
|
|
|
|
}
|
|
}
|
|
static string RemoveDiacritics(string text)
|
|
{
|
|
var normalizedString = text.Normalize(NormalizationForm.FormD);
|
|
var stringBuilder = new StringBuilder(capacity: normalizedString.Length);
|
|
|
|
for (int i = 0; i < normalizedString.Length; i++)
|
|
{
|
|
char c = normalizedString[i];
|
|
var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c);
|
|
if (unicodeCategory != UnicodeCategory.NonSpacingMark)
|
|
{
|
|
stringBuilder.Append(c);
|
|
}
|
|
}
|
|
|
|
return stringBuilder
|
|
.ToString()
|
|
.Normalize(NormalizationForm.FormC);
|
|
}
|
|
private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
|
|
{
|
|
var filteredSuggestions = _users?.AsEnumerable() ?? Enumerable.Empty<Models.Users>();
|
|
|
|
List<Models.Users> list = filteredSuggestions.Where(
|
|
x => RemoveDiacritics(x.email!.ToLower())?.Replace("@energiasmart.com.br", "").Replace("@smartenergia.com.br", "").Contains(RemoveDiacritics(UsersSearchBox.Text.ToLower())) ?? false).ToList();
|
|
list.AddRange(filteredSuggestions.Where(
|
|
x => RemoveDiacritics(x.name!.ToLower())?.Contains(RemoveDiacritics(UsersSearchBox.Text.ToLower())) ?? false));
|
|
|
|
|
|
|
|
UsersSuggestionsList.ItemsSource = null;
|
|
UsersSuggestionsList.ItemsSource = list.Distinct();
|
|
}
|
|
|
|
private void BackButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
AppState.Instance.Save();
|
|
NavigationService?.Navigate(new Home());
|
|
}
|
|
private void ReloadButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var progress = new Progress<int>(percent =>
|
|
{
|
|
ProgressBar.Value = percent;
|
|
ProgressBar.IsIndeterminate = percent == 0;
|
|
});
|
|
AppState.Instance.filterFlag = true;
|
|
AppState.Instance.Save();
|
|
FilterCards(progress);
|
|
}
|
|
|
|
private void CheckBox_Changed(object sender, RoutedEventArgs e)
|
|
{
|
|
CheckBox? checkBox = sender as CheckBox;
|
|
|
|
Type type = AppState.Instance.GetType();
|
|
|
|
PropertyInfo propertyInfo = type.GetProperty(checkBox!.Name)!;
|
|
|
|
if (propertyInfo != null)
|
|
{
|
|
propertyInfo.SetValue(AppState.Instance, (bool)checkBox.IsChecked!);
|
|
|
|
if (checkBox.Name == "KeepProsp")
|
|
{
|
|
|
|
labelProsp.IsEnabled = !((bool)checkBox.IsChecked);
|
|
ProspectanteChangeBox.IsEnabled = !((bool)checkBox.IsChecked!);
|
|
ProspAdd.IsEnabled = !((bool)checkBox.IsChecked!);
|
|
ProspRemove.IsEnabled = !((bool)checkBox.IsChecked!);
|
|
if ((bool)checkBox.IsChecked) { AppState.Instance.Prospectante.Clear(); }
|
|
|
|
}
|
|
}
|
|
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
|
|
AppState.Instance.Save();
|
|
}
|
|
|
|
private void ProspAdd_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var selectedItems = UsersSuggestionsList.SelectedItems.OfType<Models.Users>().ToList();
|
|
|
|
if (selectedItems.Count > 0 && selectedItems is not null && !AppState.Instance.KeepProsp)
|
|
{
|
|
AppState.Instance.Prospectante.Clear();
|
|
AppState.Instance.Prospectante.Add(selectedItems[0]);
|
|
|
|
if (AppState.Instance.Others.Any(x => x.id == selectedItems[0].id))
|
|
{
|
|
var existingItem = AppState.Instance.Others.First(x => x.id == selectedItems[0].id);
|
|
AppState.Instance.Others.Remove(existingItem);
|
|
}
|
|
|
|
UsersSuggestionsList.SelectedIndex = -1;
|
|
|
|
AppState.Instance.Save();
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
}
|
|
}
|
|
|
|
private void ProspRemove_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
|
|
AppState.Instance.Prospectante.Clear();
|
|
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ProspectanteChangeBox.SelectedIndex = -1;
|
|
|
|
AppState.Instance.Save();
|
|
|
|
}
|
|
|
|
private void OtherAdd_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var selectedItems = UsersSuggestionsList.SelectedItems.OfType<Models.Users>().ToList();
|
|
|
|
if (selectedItems.Count > 0 && selectedItems is not null)
|
|
{
|
|
foreach (var item in selectedItems)
|
|
{
|
|
if (!AppState.Instance.Others.Contains(item))
|
|
{
|
|
AppState.Instance.Others.Add(item);
|
|
}
|
|
}
|
|
|
|
AppState.Instance.Prospectante.RemoveAll(x => selectedItems.Any(d => d.id == x.id));
|
|
}
|
|
|
|
UsersSuggestionsList.SelectedIndex = -1;
|
|
|
|
AppState.Instance.Save();
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
|
|
}
|
|
|
|
private void OtherRemove_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
var selectedItems = ResponsavelChangeBox.SelectedItems.OfType<Models.Users>().ToList();
|
|
|
|
if (selectedItems.Count > 0 && selectedItems != null)
|
|
{
|
|
foreach (var item in selectedItems)
|
|
{
|
|
if (item != null)
|
|
{
|
|
var select = AppState.Instance.Others.Where(x => x.id == item.id).First();
|
|
AppState.Instance.Others.Remove(select);
|
|
}
|
|
}
|
|
|
|
AppState.Instance.Save();
|
|
ProspectanteChangeBox.ItemsSource = null;
|
|
ProspectanteChangeBox.ItemsSource = AppState.Instance.Prospectante;
|
|
ResponsavelChangeBox.ItemsSource = null;
|
|
ResponsavelChangeBox.ItemsSource = AppState.Instance.Others;
|
|
|
|
ResponsavelChangeBox.SelectedIndex = 0;
|
|
}
|
|
}
|
|
|
|
private async void RunMutations_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
bool val = (
|
|
!AppState.Instance.KeepProsp && AppState.Instance.Prospectante.Any()
|
|
)
|
|
||
|
|
(
|
|
AppState.Instance.KeepProsp && !AppState.Instance.KeepResp && !AppState.Instance.Prospectante.Any()
|
|
)
|
|
||
|
|
(
|
|
AppState.Instance.KeepProsp && !AppState.Instance.Prospectante.Any() && AppState.Instance.Others.Any()
|
|
);
|
|
|
|
if (val && AppState.Instance.ProspeccaoFilteredCards.Any())
|
|
{
|
|
string Prosp = "Manter";
|
|
string Resp = "Manter";
|
|
|
|
int first = 3;
|
|
int count = AppState.Instance.Others.Count;
|
|
|
|
if (!AppState.Instance.KeepProsp && AppState.Instance.Prospectante.Any())
|
|
{
|
|
Prosp = AppState.Instance.Prospectante.Select(x => x.name).First() ?? "Erro";
|
|
}
|
|
|
|
switch (AppState.Instance.KeepResp, AppState.Instance.Others.Any())
|
|
{
|
|
case (false, false):
|
|
|
|
Resp = "Limpar Demais Envolvidos";
|
|
break;
|
|
|
|
case (false, true):
|
|
|
|
Resp = "Substituir todos por ";
|
|
break;
|
|
|
|
case (true, true):
|
|
|
|
Resp = "Adicionar ";
|
|
break;
|
|
}
|
|
|
|
Resp += string.Join(", ", AppState.Instance.Others.Select(x => x.name).ToList().Take(first));
|
|
|
|
string plural = (count - first) > 1 ? "s" : "";
|
|
|
|
if (count > first) { Resp += $" e {count - first} outro{plural}."; }
|
|
|
|
string message = @$"Você está prestes a realizar as seguintes modificações em {AppState.Instance.ProspeccaoFilteredCards.Count} cards:
|
|
|
|
Prospectante:
|
|
{Prosp}
|
|
|
|
Demais envolvidos:
|
|
{Resp}
|
|
|
|
Deseja continuar?
|
|
";
|
|
|
|
MessageBoxResult answer = MessageBox.Show(message, "Pipefy", MessageBoxButton.YesNo, MessageBoxImage.Warning);
|
|
if (answer.ToString() != "Yes") { return; }
|
|
AppState.Instance.currentItems = 0;
|
|
|
|
var progress = new Progress<int>(percent =>
|
|
{
|
|
ProgressBar.Value = percent;
|
|
ProgressBar.IsIndeterminate = percent == 0;
|
|
});
|
|
startProgress(progress);
|
|
|
|
List<Task<List<Node>>> listOfTasks = new();
|
|
|
|
List<Models.Node> currentCards = new();
|
|
|
|
List<Models.Node> allCards = new();
|
|
|
|
allCards.AddRange(AppState.Instance.ProspeccaoFilteredCards);
|
|
|
|
for (int i = 0; i < allCards.Count; i += 250)
|
|
{
|
|
currentCards.AddRange(allCards.Skip(i).Take(250));
|
|
|
|
listOfTasks.Add(
|
|
(_apiService ?? new PipefyApiService(_httpClient)).
|
|
MutateCardsAsync(
|
|
currentCards,
|
|
AppState.Instance.Prospectante,
|
|
AppState.Instance.Others,
|
|
AppState.Instance.KeepProsp,
|
|
AppState.Instance.KeepResp,
|
|
progress,
|
|
AppState.Instance.ProspeccaoFilteredCards.Count
|
|
)
|
|
);
|
|
|
|
currentCards.Clear();
|
|
|
|
}
|
|
|
|
var allResults = await Task.WhenAll(listOfTasks);
|
|
|
|
AppState.Instance.recentMutations.Clear();
|
|
|
|
foreach (var result in allResults)
|
|
{
|
|
AppState.Instance.recentMutations.AddRange(result);
|
|
}
|
|
AppState.Instance.Save();
|
|
|
|
MessageBox.Show("Atualização concluída!", "Informação", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
}
|
|
}
|
|
private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e)
|
|
{
|
|
Process.Start(new ProcessStartInfo(e.Uri.AbsoluteUri) { UseShellExecute = true });
|
|
e.Handled = true;
|
|
}
|
|
}
|
|
}
|