using CentralControl.App_Code; using CommonFunc; using CommonFunc.Tools; using HandyControl.Controls; using HandyControl.Tools.Extension; using SixLabors.ImageSharp.Formats; using System; using System.Collections; using System.Collections.Generic; using System.Data; using System.Diagnostics.Eventing.Reader; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Timers; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Markup; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; using XGL.Data.DBService; using XGL.Models; using XGL.Models.Model.OrderPrepare; using ComboBox = System.Windows.Controls.ComboBox; using MessageBox = System.Windows.MessageBox; namespace XGLFinishPro.Views { /// /// LanJu_Conversion_Report.xaml 的交互逻辑 /// public partial class LanJu_Conversion_Report : System.Windows.Window { private string belong_to = string.Empty; FormingMachineService dbService = new FormingMachineService(); private static double LineWidth = 30; private static double LineHeight = 30; private static double MainLineWidth = 150; private static double MainLineHeight = 50; string deviceCode = Utils.GetAppSetting("DeviceCode"); private string date = string.Empty; private string workId = string.Empty; private string productName = string.Empty; private bool IsFinal = false; private string shiftName = string.Empty; private string shiftCode = LoginUser.ShiftCode; public LanJu_Conversion_Report() { InitializeComponent(); } private string product_code = string.Empty; private MesTableSelf mesTableSelf; bool isUpdate = false; private List data = new List(); private readonly List _hours = new List() { "00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" }; private readonly List _minutes = new List { "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55" }; private List list = new List(); private List headerDic = new List() { "A1", "A2", "A3", "A4", "A5", "A6" }; private List DictDatas = new List(); private List MainType = new List(); private Dictionary> mainValueDic = new Dictionary> { }; private Dictionary> TypeDic = new Dictionary> { }; private List DefaultTypeDic = new List() { string.Empty, "√", "x" }; private void ToggleButton_Click(object sender, RoutedEventArgs e) { if (((ToggleButton)sender).TemplatedParent is ComboBox comboBox) { comboBox.IsDropDownOpen = !comboBox.IsDropDownOpen; } } private void MainWindow_OnLoaded(object sender, RoutedEventArgs e) { this.NickName.Text = LoginUser.UserName; //this.CheckDate.Text = date; try { shiftName = dbService.GetShiftById(shiftCode); this.Line.Text = dbService.GetBaseEquipment(Utils.GetAppSetting("DeviceCode")); product_code = string.Empty; DataTable dataTable = dbService.GetProOrderWorkorder(workId); if (dataTable != null && dataTable.Rows.Count > 0) { //productName = dataTable.Rows[0]["product_name"].ToString(); product_code = dataTable.Rows[0]["product_code"].ToString(); } else { } this.CheckDate.SelectedDate = DateTime.Parse(LoginUser.WorkDate); } catch { } LoadData(); } private void UpdateUi() { Dispatcher.Invoke(() => { double width = this.MainGrid.GetValidWidth(); double widthCount = this.MainGrid.ColumnDefinitions.Count - 0.4; var lineWidth = width / widthCount; double height = this.MainGrid.GetValidHeight(); double heightCount = this.MainGrid.RowDefinitions.Count; var lineHeight = height / heightCount; foreach (var item in this.MainGrid.Children) { if (item is ComboBox comboBox) { comboBox.Width = lineWidth - 10; comboBox.Height = lineHeight - 10; if (string.IsNullOrEmpty(productName)) { comboBox.IsEnabled = false; } else { comboBox.IsEnabled = !mesTableSelf.IsFinal; } } } this.Commit.Visibility = mesTableSelf.IsFinal ? Visibility.Collapsed : Visibility.Visible; this.IsFinalSubmit.Visibility = this.Commit.Visibility; this.Add.Visibility = this.Commit.Visibility; }); } private void LoadData(bool addLine = false) { isUpdate = true; list.Clear(); this.MainGrid.Children.Clear(); this.MainGrid.RowDefinitions.Clear(); this.MainGrid.ColumnDefinitions.Clear(); DictDatas = dbService.GetConversionReportType(); headerDic = DictDatas.Select(t => t.dict_label).ToList(); MainType = dbService.GetSelfCheckInfo("self_check_info"); var mainTypeDic = new Dictionary>(); foreach (var item in headerDic) { var defaultDic = MainType.Where(t => string.IsNullOrEmpty(t.remark)).Select(t => t.dict_value).ToList(); var typeDic = MainType.Where(t => t.remark == item).Select(t => t.dict_value).ToList(); defaultDic.AddRange(typeDic); if (!mainTypeDic.ContainsKey(item)) { mainTypeDic.Add(item, defaultDic); } } TypeDic = mainTypeDic; mesTableSelf = null; mesTableSelf = dbService.GetMesTableSelf(deviceCode, productName, this.CheckDate.Text, "ConversionReport"+ shiftName); if (mesTableSelf == null) { mesTableSelf = new MesTableSelf() { id = Guid.NewGuid().ToString(), line_code = deviceCode, product_name = productName, check_date = this.CheckDate.Text, product_code = product_code, factory_code = Utils.GetAppSetting("SiteCode"), del_flag = "0", product_date = this.CheckDate.Text, bz = string.Empty, report_name = "ConversionReport"+ shiftName }; if (!string.IsNullOrEmpty(productName)) { dbService.InsertMesTableSelf(mesTableSelf); } } belong_to = mesTableSelf.id; data = dbService.GetMesTableSelfDetial(belong_to, 7); this.ExceptionDesc.Text = mesTableSelf.bz; if (addLine) { int maxLine = data.Max(t => t.table_line) + 1; data.ForEach(t => { t.index++; }); data.Insert(0, new MesTableSelfDetialModel() { index = 0 }); data.RemoveAll(t => t.index == 7); } for (var i = 0; i < 7; i++) { foreach (var item in headerDic) { bool hasData = data.Where(t => t.index == i && t.info_name == item).Any(); if (!hasData) { int tableLine = 0; MesTableSelfDetialModel mesTableSelfDetialModel = data.Where(t => t.index == i).Where(t => t.table_line != 0).FirstOrDefault(); if (mesTableSelfDetialModel != null) { tableLine = mesTableSelfDetialModel.table_line; } else { try { tableLine = data.Max(t => t.table_line) + 1; } catch { } } data.Add(new MesTableSelfDetialModel() { index = i, table_line = tableLine, info_name = item, info_code = DictDatas.FirstOrDefault(t => t.dict_label == item)?.dict_code, }); } } } Dictionary> dic = new Dictionary>(); foreach (var item in data) { if (item.info_name == null || dic.Keys.Contains(item.info_name)) { continue; } var mesTableSelfDetialModels = data.Where(t => t.info_name == item.info_name).ToList(); var dictionary = mesTableSelfDetialModels.ToDictionary(t => t.index); dic.Add(item.info_name, dictionary); list.Add(new ConveterData() { label = item.info_name, value = MD5Provider.Hash(item.info_name), }); } mainValueDic = dic; list.ForEach(t => t.value = t.value.Insert(0, "A")); MakeHeaderData(); MainValueData(); UpdateUi(); isUpdate = false; } private void MainValueData() { for (var i = 2; i < headerDic.Count + 2; i++) { this.MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) }); var line = new Line() { Name = $"XLine{i}", X1 = 1.0, VerticalAlignment = i == (headerDic.Count + 1) ? VerticalAlignment.Bottom : VerticalAlignment.Top, Stroke = Brushes.Black, StrokeThickness = 1.0, Stretch = Stretch.Fill, }; Grid.SetRow(line, i); Grid.SetColumnSpan(line, 99); this.MainGrid.Children.Add(line); string key = headerDic.ToList()[i - 2]; bool hasType = TypeDic.TryGetValue(key, out var type); if (mainValueDic.TryGetValue(key, out var value)) { for (var j = 1; j <= value.Count; j++) { if (j > 7) { continue; } string startHour = "00"; string startMintue = "00"; string endHour = "00"; string endMintue = "00"; try { if (value[j - 1].check_time_s != null) { string[] startTime = value[j - 1].check_time_s.Split(':'); startHour = startTime[0]; startMintue = startTime[1]; } if (value[j - 1].check_time_e != null) { string[] endTime = value[j - 1].check_time_e.Split(':'); endHour = endTime[0]; endMintue = endTime[1]; } foreach (var item in this.MainGrid.Children) { if (item is StackPanel stackPanel) { foreach (var child in stackPanel.Children) { if (child is ComboBox comboBox2 && comboBox2.SelectedIndex == 0) { if (comboBox2.Name == "StartHour" + j) { comboBox2.SelectedItem = startHour; } if (comboBox2.Name == "StartMinute" + j) { comboBox2.SelectedItem = startMintue; } if (comboBox2.Name == "EndHour" + j) { comboBox2.SelectedItem = endHour; } if (comboBox2.Name == "EndMinute" + j) { comboBox2.SelectedItem = endMintue; } } } } } } catch { } var textBlock = new TextBlock() { Text = key, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20, TextWrapping = TextWrapping.Wrap, }; Grid.SetRow(textBlock, i); Grid.SetColumn(textBlock, 0); this.MainGrid.Children.Add(textBlock); string SelectedItem = string.Empty; try { SelectedItem = value[j - 1].check_result; } catch { } ComboBox comboBox = new ComboBox() { Name = $"{list.First(t => t.label == key).value}{j}", ItemsSource = hasType ? type.Distinct() : DefaultTypeDic.Distinct(), SelectedItem = SelectedItem, Width = MainLineWidth, Height = MainLineHeight }; comboBox.SelectionChanged += SelectionChanged; Grid.SetRow(comboBox, i); Grid.SetColumn(comboBox, j); this.MainGrid.Children.Add(comboBox); } for (var j = value.Count + 1; j <= 7; j++) { var textBlock = new TextBlock() { Text = key, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20, TextWrapping = TextWrapping.Wrap, }; Grid.SetRow(textBlock, i); Grid.SetColumn(textBlock, 0); this.MainGrid.Children.Add(textBlock); string SelectedItem = string.Empty; if (value.TryGetValue(j, out var outvValue)) { SelectedItem = outvValue.check_result; } ComboBox comboBox = new ComboBox() { Name = $"{list.First(t => t.label == key).value}{j}", ItemsSource = hasType ? type.Distinct() : DefaultTypeDic.Distinct(), SelectedItem = SelectedItem, Width = MainLineWidth, Height = MainLineHeight }; comboBox.SelectionChanged += SelectionChanged; Grid.SetRow(comboBox, i); Grid.SetColumn(comboBox, j); this.MainGrid.Children.Add(comboBox); } } else { for (var j = 1; j <= 7; j++) { var textBlock = new TextBlock() { Text = key, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20, TextWrapping = TextWrapping.Wrap, }; Grid.SetRow(textBlock, i); Grid.SetColumn(textBlock, 0); this.MainGrid.Children.Add(textBlock); string SelectedItem = string.Empty; ComboBox comboBox = new ComboBox() { Name = $"{list.First(t => t.label == key).value}{j}", ItemsSource = hasType ? type.Distinct() : DefaultTypeDic.Distinct(), SelectedItem = SelectedItem, Width = MainLineWidth, Height = MainLineHeight }; comboBox.SelectionChanged += SelectionChanged; Grid.SetRow(comboBox, i); Grid.SetColumn(comboBox, j); this.MainGrid.Children.Add(comboBox); } } } List batchCodes = dbService.GetBatchCode(workId); foreach (var item in this.MainGrid.Children) { if (item is ComboBox box) { string label = list.Where(t => t.value == box.Name.Substring(0, box.Name.Length - 1)).First().label; if (label.Contains("日期码") || label.Contains("生产批号")) { box.ItemsSource = batchCodes.Distinct(); } } } } private void MakeHeaderData() { for (var i = 0; i <= 8; i++) { if (i != 8) { this.MainGrid.ColumnDefinitions.Add(new ColumnDefinition() { Width = new GridLength(i == 0 ? 0.6 : 1.0, GridUnitType.Star) }); } } for (var i = 0; i <= 2; i++) { if (i != 2) { this.MainGrid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(1.0, GridUnitType.Star) }); } } for (var i = 0; i <= 8; i++) { var line = new Line() { Name = $"YLine{i}", Y1 = 1.0, HorizontalAlignment = i == 8 ? HorizontalAlignment.Right : HorizontalAlignment.Left, Stroke = Brushes.Black, StrokeThickness = 1.0, Stretch = Stretch.Fill, }; Grid.SetColumn(line, i); Grid.SetRowSpan(line, 99); this.MainGrid.Children.Add(line); } for (var i = 0; i <= headerDic.Count + 2; i++) { var line = new Line() { Name = $"XLine{i}", X1 = 1.0, VerticalAlignment = i == (headerDic.Count + 2) ? VerticalAlignment.Bottom : VerticalAlignment.Top, Stroke = Brushes.Black, StrokeThickness = 1.0, Stretch = Stretch.Fill, }; Grid.SetRow(line, i); if (i == 1) { Grid.SetColumn(line, 1); Grid.SetColumnSpan(line, 99); } Grid.SetColumnSpan(line, 99); this.MainGrid.Children.Add(line); } var productBlock = new TextBlock() { Text = "项目", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20 }; Grid.SetColumn(productBlock, 0); Grid.SetRow(productBlock, 0); Grid.SetRowSpan(productBlock, 2); this.MainGrid.Children.Add(productBlock); double timeHeight = LineHeight; double timeWidth = LineWidth; for (var i = 1; i <= 7; i++) { var hearBlock = new TextBlock() { Text = "检查时间", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20 }; Grid.SetColumn(hearBlock, i); Grid.SetRow(hearBlock, 0); this.MainGrid.Children.Add(hearBlock); #region 头部 var stackPanel = new StackPanel() { Orientation = Orientation.Horizontal, VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; var startHour = new ComboBox() { Name = $"StartHour{i}", ItemsSource = _hours.Distinct(), SelectedIndex = 0, Width = timeWidth, Height = timeHeight, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; startHour.SelectionChanged += SelectionChanged; stackPanel.Children.Add(startHour); var startBlock = new TextBlock() { Text = ":", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20 }; stackPanel.Children.Add(startBlock); var startMinute = new ComboBox() { Name = $"StartMinute{i}", ItemsSource = _minutes.Distinct(), SelectedIndex = 0, Width = timeWidth, Height = timeHeight, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; startMinute.SelectionChanged += SelectionChanged; stackPanel.Children.Add(startMinute); var toBlock = new TextBlock() { Text = "-", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20 }; stackPanel.Children.Add(toBlock); var endHour = new ComboBox() { Name = $"EndHour{i}", ItemsSource = _hours.Distinct(), SelectedIndex = 0, Width = timeWidth, Height = timeHeight, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; endHour.SelectionChanged += SelectionChanged; stackPanel.Children.Add(endHour); var endBlock = new TextBlock() { Text = ":", VerticalAlignment = VerticalAlignment.Center, HorizontalAlignment = HorizontalAlignment.Center, FontSize = 20 }; stackPanel.Children.Add(endBlock); var endMinute = new ComboBox() { Name = $"EndMinute{i}", ItemsSource = _minutes.Distinct(), SelectedIndex = 0, Width = timeWidth, Height = timeHeight, HorizontalAlignment = HorizontalAlignment.Center, VerticalAlignment = VerticalAlignment.Center, Margin = new Thickness(0, 0, 0, 0) }; endMinute.SelectionChanged += SelectionChanged; stackPanel.Children.Add(endMinute); Grid.SetColumn(stackPanel, i); Grid.SetRow(stackPanel, 1); this.MainGrid.Children.Add(stackPanel); #endregion } } private void SelectionChanged(object sender, SelectionChangedEventArgs e) { try { if (isUpdate) { return; } if (sender is ComboBox comboBox) { if (comboBox.Name.Contains("StartHour") || comboBox.Name.Contains("StartMinute") || comboBox.Name.Contains("EndHour") || comboBox.Name.Contains("EndMinute")) { string index = comboBox.Name.Substring(comboBox.Name.Length - 1, 1); string startHour = "00"; string startMintue = "00"; string endHour = "00"; string endMintue = "00"; foreach (var item in this.MainGrid.Children) { if (item is StackPanel stackPanel) { foreach (var child in stackPanel.Children) { if (child is ComboBox comboBox2) { if (comboBox2.Name == "StartHour" + index) { startHour = comboBox2.SelectedItem.ToString(); } if (comboBox2.Name == "StartMinute" + index) { startMintue = comboBox2.SelectedItem.ToString(); } if (comboBox2.Name == "EndHour" + index) { endHour = comboBox2.SelectedItem.ToString(); } if (comboBox2.Name == "EndMinute" + index) { endMintue = comboBox2.SelectedItem.ToString(); } } } } } foreach (var item in data.Where(t => t.index == int.Parse(index) - 1)) { item.check_time_s = startHour + ":" + startMintue; item.check_time_e = endHour + ":" + endMintue; item.belong_to = belong_to; } } else { string index = comboBox.Name.Substring(comboBox.Name.Length - 1, 1); string key = list.First(t => t.value == comboBox.Name.Substring(0, comboBox.Name.Length - 1)).label; var mesTableSelfDetialModel = data.Where(t => t.info_name == key && t.index == int.Parse(index) - 1).FirstOrDefault(); if (mesTableSelfDetialModel != null) { if (string.IsNullOrEmpty(mesTableSelfDetialModel.id)) { mesTableSelfDetialModel.id = Guid.NewGuid().ToString(); mesTableSelfDetialModel.belong_to = belong_to; } mesTableSelfDetialModel.check_result = comboBox.SelectedItem.ToString(); string startHour = "00"; string startMintue = "00"; string endHour = "00"; string endMintue = "00"; foreach (var item in this.MainGrid.Children) { if (item is StackPanel stackPanel) { foreach (var child in stackPanel.Children) { if (child is ComboBox comboBox2) { if (comboBox2.Name == "StartHour" + index) { startHour = comboBox2.SelectedItem.ToString(); } if (comboBox2.Name == "StartMinute" + index) { startMintue = comboBox2.SelectedItem.ToString(); } if (comboBox2.Name == "EndHour" + index) { endHour = comboBox2.SelectedItem.ToString(); } if (comboBox2.Name == "EndMinute" + index) { endMintue = comboBox2.SelectedItem.ToString(); } } } } } foreach (var item in data.Where(t => t.index == int.Parse(index) - 1)) { item.check_time_s = startHour + ":" + startMintue; item.check_time_e = endHour + ":" + endMintue; item.belong_to = belong_to; } } } } } catch { } } private void Refresh_Click(object sender, RoutedEventArgs e) { LoadData(); } private void Add_Click(object sender, RoutedEventArgs e) { LoadData(true); } private void Close_Click(object sender, RoutedEventArgs e) { this.Close(); } private void Submit_Click(object sender, RoutedEventArgs e) { string isFinal = "0"; if (sender is Button button) { isFinal = button.Content == "最终提交" ? "1" : "0"; } if (isFinal == "1") { var result = MessageBox.Show("是否将报表最终提交?", "提示", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result != MessageBoxResult.Yes) { return; } } foreach (var item in data) { if (item.table_line == 0) { item.table_line = 1; } if (string.IsNullOrEmpty(item.id)) { continue; } var mesTableSelf = dbService.GetMesTableSelfDetialById(item.id); if (mesTableSelf != null && (mesTableSelf.check_result != item.check_result || mesTableSelf.check_time_s != item.check_time_s || mesTableSelf.check_time_e != item.check_time_e)) { dbService.UpdateMesTableSelfDetial(item); } else if (mesTableSelf == null) { dbService.InsertMesTableSelfDetial(item); } } dbService.UpdateMesTableSelf(mesTableSelf.id, this.ExceptionDesc.Text, this.NickName.Text, isFinal); LoadData(); MessageBox.Show("提交成功"); } private void LeftCheckDate_SelectedDateChanged_1(object sender, SelectionChangedEventArgs e) { this.date = this.CheckDate.SelectedDate.Value.ToString("yyyy-MM-dd"); DataTable dataTable = dbService.GetProOrderWorkorder(deviceCode, date); List orderWorks = new List(); if (dataTable == null || dataTable.Rows.Count == 0) { MessageBox.Show("没有查询到工单信息!"); } else { foreach (DataRow row in dataTable.Rows) { orderWorks.Add(new OrderWork() { WorkId = row["workorder_id"].ToString(), WorkName = row["product_name"].ToString(), }); } } this.ProductName.ItemsSource = orderWorks?.Distinct(); this.ProductName.Items.Refresh(); this.workId = string.Empty; this.productName = string.Empty; LoadData(); } private void LeftProductName_SelectionChanged(object sender, SelectionChangedEventArgs e) { if (sender is ComboBox comboBox) { if (comboBox.SelectedItem is OrderWork orderWork) { this.workId = orderWork.WorkId; this.productName = orderWork.WorkName; } } LoadData(); } } }