CreateImageCaseWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using AIPlatform.Protocol.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. namespace aipdev
  9. {
  10. /// <summary>
  11. /// Interaction logic for CreateImageCaseWindow.xaml
  12. /// </summary>
  13. public partial class CreateImageCaseWindow : Window
  14. {
  15. /// <summary>
  16. /// Gets the organization.
  17. /// </summary>
  18. public Organization Organization { get; private set; }
  19. /// <summary>
  20. /// Gets the image category.
  21. /// </summary>
  22. public ImageCategory ImageCategory { get; private set; }
  23. /// <summary>
  24. /// Gets the image batch.
  25. /// </summary>
  26. public ImageCategory ImageBatch { get; private set; }
  27. /// <summary>
  28. /// Gets the image case name.
  29. /// </summary>
  30. public string ImageCaseName { get; private set; }
  31. /// <summary>
  32. /// Gets if dialog is filled and OK.
  33. /// </summary>
  34. public bool Ok { get; private set; }
  35. /// <summary>
  36. /// Gets the selected organization id.
  37. /// </summary>
  38. private long SelectedOrganizationId { get; set; }
  39. /// <summary>
  40. /// Gets the selected image category id.
  41. /// </summary>
  42. private long SelectedImageCategoryId { get; set; }
  43. /// <summary>
  44. /// Gets the selected image batch id.
  45. /// </summary>
  46. private long SelectedImageBatchId { get; set; }
  47. private List<ImageCategory> _lstImageCategories = new List<ImageCategory>();
  48. private List<ImageCategory> _lstImageBatches = new List<ImageCategory>();
  49. private string _imageCategory;
  50. private string _imageBatch;
  51. private EntityBase _developer;
  52. public CreateImageCaseWindow(long organizationId, long categoryId, long batchId, EntityBase developer)
  53. {
  54. InitializeComponent();
  55. SelectedOrganizationId = organizationId;
  56. SelectedImageCategoryId = categoryId;
  57. SelectedImageBatchId = batchId;
  58. _developer = developer;
  59. Loaded += OnLoaded;
  60. txtImageCase.Focus();
  61. txtImageCase.Text = "老数据导入_";
  62. txtImageCase.SelectionStart = txtImageCase.Text.Length;
  63. }
  64. private async void OnLoaded(object sender, RoutedEventArgs e)
  65. {
  66. try
  67. {
  68. cbbOrganizations.SelectionChanged -= OnOrganizationSelectionChanged;
  69. cbbImageCategories.SelectionChanged -= cbbImageCategories_SelectionChanged;
  70. cbbImageBatches.SelectionChanged -= OnImageBatchSelectionChanged;
  71. cbbOrganizations.Items.Clear();
  72. var organizations = await DeveloperManager.Shared.GetOrganizationsAsync();
  73. if (organizations != null)
  74. {
  75. foreach (var item in organizations)
  76. {
  77. cbbOrganizations.Items.Add(item);
  78. }
  79. if (organizations.Count > 0)
  80. {
  81. cbbOrganizations.SelectedIndex = organizations.FindIndex(x => x.Id == SelectedOrganizationId);
  82. //ImageCategories
  83. var categories = await DeveloperManager.Shared.GetImageCategoriesByDeveloperAsync(SelectedOrganizationId, _developer);
  84. if (categories != null)
  85. {
  86. cbbImageCategories.Items.Clear();
  87. foreach (var item in categories)
  88. {
  89. cbbImageCategories.Items.Add(item);
  90. }
  91. if (categories.Count > 0)
  92. {
  93. cbbImageCategories.SelectedIndex = categories.FindIndex(x => x.Id == SelectedImageCategoryId);
  94. //ImageBtaches
  95. var imageBtaches = await DeveloperManager.Shared.GetImageCategoriesAsync(SelectedImageCategoryId);
  96. if (imageBtaches != null)
  97. {
  98. cbbImageBatches.Items.Clear();
  99. foreach (var subItem in imageBtaches)
  100. {
  101. cbbImageBatches.Items.Add(subItem);
  102. }
  103. if (imageBtaches.Count > 0)
  104. {
  105. cbbImageBatches.SelectedIndex = imageBtaches.FindIndex(x => x.Id == SelectedImageBatchId);
  106. }
  107. _lstImageBatches = imageBtaches;
  108. }
  109. }
  110. _lstImageCategories = categories;
  111. }
  112. }
  113. }
  114. _imageCategory = cbbImageCategories.Text;
  115. _imageBatch = cbbImageBatches.Text;
  116. }
  117. catch (Exception ex)
  118. {
  119. MessageBox.Show(Application.Current.MainWindow, $"加载数据失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  120. }
  121. finally
  122. {
  123. cbbOrganizations.SelectionChanged += OnOrganizationSelectionChanged;
  124. cbbImageCategories.SelectionChanged += cbbImageCategories_SelectionChanged;
  125. cbbImageBatches.SelectionChanged += OnImageBatchSelectionChanged;
  126. }
  127. }
  128. private void OnOkClick(object sender, RoutedEventArgs e)
  129. {
  130. if (cbbOrganizations.SelectedIndex == -1)
  131. {
  132. MessageBox.Show(Application.Current.MainWindow, "请选择组织", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  133. return;
  134. }
  135. if (cbbImageCategories.SelectedIndex == -1)
  136. {
  137. MessageBox.Show(Application.Current.MainWindow, "请选择图像大类", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  138. return;
  139. }
  140. if (cbbImageBatches.SelectedIndex == -1)
  141. {
  142. MessageBox.Show(Application.Current.MainWindow, "请选择图像批次", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  143. return;
  144. }
  145. if (string.IsNullOrWhiteSpace(txtImageCase.Text) || string.IsNullOrEmpty(txtImageCase.Text))
  146. {
  147. MessageBox.Show(Application.Current.MainWindow, "请填写用例名称", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  148. return;
  149. }
  150. Organization = cbbOrganizations.SelectedItem as Organization;
  151. ImageCategory = cbbImageCategories.SelectedItem as ImageCategory;
  152. ImageBatch = cbbImageBatches.SelectedItem as ImageCategory;
  153. ImageCaseName = txtImageCase.Text.Trim();
  154. Ok = true;
  155. Close();
  156. }
  157. private void OnCloseClick(object sender, RoutedEventArgs e)
  158. {
  159. Ok = false;
  160. Close();
  161. }
  162. private async void OnOrganizationSelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
  163. {
  164. try
  165. {
  166. cbbImageCategories.Items.Clear();
  167. var parent = cbbOrganizations.SelectedItem as Organization;
  168. if (parent != null)
  169. {
  170. await OnSelectionChanged(parent.Id, cbbImageCategories);
  171. _lstImageCategories = new List<ImageCategory>();
  172. foreach (var item in cbbImageCategories.Items)
  173. {
  174. var newitem = (ImageCategory)item;
  175. _lstImageCategories.Add(newitem);
  176. }
  177. }
  178. _imageCategory = cbbImageCategories.Text;
  179. _imageBatch = cbbImageBatches.Text;
  180. }
  181. catch (Exception ex)
  182. {
  183. MessageBox.Show(Application.Current.MainWindow, $"选择组织失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  184. }
  185. }
  186. private async void cbbImageCategories_SelectionChanged(object sender, SelectionChangedEventArgs e)
  187. {
  188. cbbImageBatches.Items.Clear();
  189. var parent = cbbImageCategories.SelectedItem as ImageCategory;
  190. if (parent != null)
  191. {
  192. await OnSelectionChanged(parent.Id, cbbImageBatches);
  193. _lstImageBatches = new List<ImageCategory>();
  194. foreach (var item in cbbImageBatches.Items)
  195. {
  196. var newitem = (ImageCategory)item;
  197. _lstImageBatches.Add(newitem);
  198. }
  199. }
  200. if (this.cbbImageCategories.SelectedValue != null)
  201. {
  202. _imageCategory = this.cbbImageCategories.SelectedValue.ToString();
  203. }
  204. }
  205. private void OnImageBatchSelectionChanged(object sender, SelectionChangedEventArgs e)
  206. {
  207. if (cbbImageBatches.SelectedValue != null)
  208. {
  209. _imageBatch = cbbImageBatches.SelectedValue.ToString();
  210. }
  211. }
  212. private async Task OnSelectionChanged(long parentId, ComboBox targetCbb)
  213. {
  214. try
  215. {
  216. var categories = new List<ImageCategory>();
  217. if (targetCbb.Name == "cbbImageCategories")
  218. {
  219. categories = await DeveloperManager.Shared.GetImageCategoriesByDeveloperAsync(parentId, _developer);
  220. }
  221. else
  222. {
  223. categories = await DeveloperManager.Shared.GetImageCategoriesAsync(parentId);
  224. }
  225. if (categories != null)
  226. {
  227. targetCbb.Items.Clear();
  228. foreach (var item in categories)
  229. {
  230. targetCbb.Items.Add(item);
  231. }
  232. if (categories.Count > 0)
  233. {
  234. targetCbb.SelectedIndex = 0;
  235. }
  236. }
  237. }
  238. catch (Exception ex)
  239. {
  240. MessageBox.Show(Application.Current.MainWindow, $"选择失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  241. }
  242. }
  243. #region 模糊查询方法提供
  244. private void OnImageCategoryKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  245. {
  246. if (_imageCategory != this.cbbImageCategories.Text)
  247. {
  248. this.OnInputFilter(this.cbbImageCategories, _lstImageCategories);
  249. _imageCategory = this.cbbImageCategories.Text;
  250. }
  251. }
  252. private void OnImageBatchKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
  253. {
  254. }
  255. private void OnInputFilter(ComboBox targetCbb, List<ImageCategory> list)
  256. {
  257. try
  258. {
  259. TextBox textBox = (TextBox)targetCbb.Template.FindName("PART_EditableTextBox", targetCbb);
  260. int startIndex = textBox.SelectionStart;
  261. string searchText = targetCbb.Text.Trim();
  262. targetCbb.Items.Clear();
  263. targetCbb.Text = searchText;
  264. List<ImageCategory> newlist = list;
  265. if (!string.IsNullOrEmpty(searchText) && list.Count > 0)
  266. {
  267. newlist = list.Where(x => x.Name.ToLower().Contains(searchText.ToLower())).ToList();
  268. }
  269. foreach (var item in newlist)
  270. {
  271. targetCbb.Items.Add(item);
  272. }
  273. targetCbb.IsDropDownOpen = true;
  274. textBox.SelectionStart = startIndex;
  275. textBox.Select(startIndex, 0);
  276. }
  277. catch (Exception ex)
  278. {
  279. MessageBox.Show(Application.Current.MainWindow, $"选择失败:{ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  280. }
  281. }
  282. #endregion
  283. }
  284. }