AssignSettingView.xaml.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. using AIPractice.LabellerServer.Managers;
  2. using AIPractice.LabellerServer.Managers.Entities;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Windows.Controls;
  10. using AIPractice.LabellerServer.ViewModels;
  11. namespace AIPractice.LabellerServer.WPF
  12. {
  13. /// <summary>
  14. /// Interaction logic for AssignSettingView.xaml
  15. /// </summary>
  16. public partial class AssignSettingView
  17. {
  18. private readonly ObservableCollection<AccountViewModel> _accountViewModels = new ObservableCollection<AccountViewModel>();
  19. public AssignSettingView()
  20. {
  21. InitializeComponent();
  22. AccountSelector.ItemsSource = _accountViewModels;
  23. LoadAccounts();
  24. LoadImageDescriptions();
  25. OnImageDescSelectionChanged(this,null);
  26. }
  27. private void OnAssignClick(object sender, RoutedEventArgs e)
  28. {
  29. if (!int.TryParse(AssignCount.Text, out int assignCount))
  30. {
  31. MessageBox.Show("请输入数字","错误",MessageBoxButton.OK, MessageBoxImage.Error);
  32. AssignCount.Focus();
  33. return;
  34. }
  35. if (!int.TryParse(DuplicateCount.Text, out int duplicateCount))
  36. {
  37. MessageBox.Show("请输入数字", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  38. DuplicateCount.Focus();
  39. return;
  40. }
  41. if (string.IsNullOrEmpty(ImageDesc.Text))
  42. {
  43. MessageBox.Show("请输入要分配的图像的标签", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  44. ImageDesc.Focus();
  45. return;
  46. }
  47. var description = ImageDesc.Text;
  48. var dataManager = ManagerContainer.GetManager<IDataManager>();
  49. var unAssignedImages = dataManager.GetUnAssignedLabelImages(assignCount, description);
  50. if(unAssignedImages.Count < assignCount)
  51. {
  52. assignCount = unAssignedImages.Count;
  53. }
  54. var duplicateImages = unAssignedImages.Take(duplicateCount).ToList();
  55. if(duplicateImages.Count < duplicateCount)
  56. {
  57. duplicateCount = duplicateImages.Count;
  58. }
  59. var assignImages = unAssignedImages.Skip(duplicateCount).ToList();
  60. var accounts = _accountViewModels.Where(x=>x.IsSelected).ToList();
  61. if (accounts.Count == 0)
  62. {
  63. MessageBox.Show("请输入要分配的人员。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  64. AccountSelector.Focus();
  65. return;
  66. }
  67. var eachCount = (assignCount - duplicateCount) / accounts.Count;
  68. if(eachCount < 0)
  69. {
  70. eachCount = 0;
  71. }
  72. if (duplicateCount > 0 && accounts.Count == 1)
  73. {
  74. MessageBox.Show("当共享图片数量大于0时,参与分配的人员必须大于1。", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  75. AccountSelector.Focus();
  76. return;
  77. }
  78. if (Application.Current.MainWindow != null)
  79. {
  80. Application.Current.MainWindow.IsEnabled = false;
  81. LogManager.WriteLog("开始分配图像资源...");
  82. Task.Run(() =>
  83. {
  84. try
  85. {
  86. var assignIndex = 0;
  87. var accountIndex = 0;
  88. foreach (var account in accounts)
  89. {
  90. var assignImageCount = 0;
  91. if (duplicateCount > 0 || eachCount > 0)
  92. {
  93. var virtualCase = new VirtualCase
  94. {
  95. Id = Guid.NewGuid().ToString("N").ToUpper(),
  96. Description = description,
  97. CreatedTime = DateTime.Now
  98. };
  99. dataManager.AddVirtualCase(virtualCase);
  100. var caseImageIndex = 0;
  101. var virtualCaseImages = new List<VirtualCaseImage>();
  102. foreach (var duplicateImage in duplicateImages)
  103. {
  104. var virtualCaseImage = new VirtualCaseImage
  105. {
  106. Id = Guid.NewGuid().ToString("N").ToUpper(),
  107. ImageId = duplicateImage.Id,
  108. ImageIndex = caseImageIndex,
  109. CreatedTime = DateTime.Now,
  110. CaseId = virtualCase.Id
  111. };
  112. duplicateImage.Assigned = true;
  113. duplicateImage.IsShared = true;
  114. virtualCaseImages.Add(virtualCaseImage);
  115. caseImageIndex++;
  116. assignImageCount++;
  117. }
  118. for (var i = 0; i < eachCount; i++)
  119. {
  120. var image = assignImages[assignIndex];
  121. var virtualCaseImage = new VirtualCaseImage
  122. {
  123. Id = Guid.NewGuid().ToString("N").ToUpper(),
  124. ImageId = image.Id,
  125. ImageIndex = caseImageIndex,
  126. CreatedTime = DateTime.Now,
  127. CaseId = virtualCase.Id
  128. };
  129. image.Assigned = true;
  130. virtualCaseImages.Add(virtualCaseImage);
  131. caseImageIndex++;
  132. assignIndex++;
  133. assignImageCount++;
  134. }
  135. dataManager.AddVirtualCaseImages(virtualCaseImages.ToArray());
  136. var assignedCaseCount = dataManager.GetVirtualCaseCount(account.Id);
  137. var assignedCase = new AssignedCase
  138. {
  139. Id = Guid.NewGuid().ToString("N").ToUpper(),
  140. AccountId = account.Id,
  141. CaseId = virtualCase.Id,
  142. CaseIndex = assignedCaseCount,
  143. CreatedTime = DateTime.Now,
  144. };
  145. dataManager.AddAssignedCase(assignedCase);
  146. LogManager.WriteLog($"分配了{duplicateImages.Count + assignImageCount}图像资源到 {account.FriendlyName}");
  147. accountIndex++;
  148. var progress = (int) ((float) accountIndex / accounts.Count * 100);
  149. LogManager.WriteLog($"分配图像资源...{progress}%");
  150. }
  151. }
  152. dataManager.UpdateLabelImages(duplicateImages.ToArray());
  153. dataManager.UpdateLabelImages(assignImages.ToArray());
  154. LogManager.WriteLog("分配图像资源完毕.");
  155. }
  156. catch (Exception ex)
  157. {
  158. LogManager.WriteLog($"分配图像资源失败:{ex.Message}.");
  159. }
  160. finally
  161. {
  162. Dispatcher.Invoke(() => { Application.Current.MainWindow.IsEnabled = true; });
  163. }
  164. });
  165. }
  166. }
  167. private void OnAccountViewModelIsSelectedChanged(object sender, EventArgs e)
  168. {
  169. var selectedNames = _accountViewModels.Where(x => x.IsSelected).Select(x => x.FriendlyName).ToArray();
  170. AccountSelector.Text = string.Join(",", selectedNames);
  171. }
  172. private void LoadAccounts()
  173. {
  174. foreach (var accountViewModel in _accountViewModels)
  175. {
  176. accountViewModel.IsSelectedChanged -= OnAccountViewModelIsSelectedChanged;
  177. }
  178. _accountViewModels.Clear();
  179. var dataManager = ManagerContainer.GetManager<IDataManager>();
  180. var accounts = dataManager.GetAccounts();
  181. foreach (var account in accounts)
  182. {
  183. var accountViewModel = new AccountViewModel
  184. {
  185. Id = account.Id,
  186. Name = account.Name,
  187. Description = account.Description,
  188. Password = account.Password,
  189. CreatedTime = account.CreatedTime,
  190. IsReviewer = account.IsReviewer
  191. };
  192. accountViewModel.IsSelectedChanged += OnAccountViewModelIsSelectedChanged;
  193. _accountViewModels.Add(accountViewModel);
  194. }
  195. AccountSelector.SelectAll();
  196. }
  197. private void LoadImageDescriptions()
  198. {
  199. var dataManager = ManagerContainer.GetManager<IDataManager>();
  200. var descriptions = dataManager.GetExistingLabels();
  201. ImageDesc.ItemsSource = descriptions;
  202. }
  203. private void OnImageDescSelectionChanged(object sender, EventArgs e)
  204. {
  205. int unassignedCount;
  206. var dataManager = ManagerContainer.GetManager<IDataManager>();
  207. if (ImageDesc.SelectedItem != null)
  208. {
  209. unassignedCount = dataManager.GetUnAssignedImageCount(ImageDesc.SelectedItem.ToString());
  210. }
  211. else
  212. {
  213. unassignedCount = dataManager.GetUnAssignedImageCount();
  214. }
  215. LeftCount.Content = $"剩余可分配数量: {unassignedCount}";
  216. }
  217. private void OnSelectUnSelectAllAccounts(object sender, bool e)
  218. {
  219. foreach (var accountViewModel in _accountViewModels)
  220. {
  221. accountViewModel.IsSelected = e;
  222. }
  223. }
  224. private void OnAccountsRefreshed(object sender, EventArgs e)
  225. {
  226. LoadAccounts();
  227. }
  228. private void OnImageDescRefreshed(object sender, EventArgs e)
  229. {
  230. LoadImageDescriptions();
  231. }
  232. }
  233. }