using DataEntity.Share;
using DataRWDAL;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Xml;
using XCommon;
using XCommon.Log;
using XCore;
using XHandler.Class;
using XHandler.Class.DataEx;
using XHandler.View.ManualPick;
using XHandler.View.MethodProperty;
using XImagingXhandler.XDAL;
namespace XHandler.View.ManualCoating
{
///
/// 挑菌后挑菌信息设置页面
///
public partial class CoatingSetting : Window
{
// 挑菌的板位号
private string choiceLatticeId = string.Empty;
// 本画面的数据
private ObservableCollection choiceDatas = new ObservableCollection();
private CoatingFileBll coatingFileBll = new CoatingFileBll();
// 涂布耗材
private List lstCoatingLabwareText = new List();
private List lstCoatingLabwareValue = new List();
public event EventHandler okEvent;
public RunWnd launchView = null; // 运行界面
public XmlNode methodNode = null;
public DataTable dtChoiceParams;
// 前画面传过来数据
public ObservableCollection dgBacteriaCoordinates = null;
///
/// 构造函数
///
public CoatingSetting()
{
InitializeComponent();
this.Owner = (Window)Shared.Main;
}
///
/// 页面初始化
///
///
///
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
// 加载挑菌数据
InitializeChoiceData();
// 1:人工挑选菌落;0:成像系统自动挑选菌落
string choicemode = dtChoiceParams.Rows[9]["属性值"].ToString();
if (choicemode.Equals("0"))
{
Console.WriteLine("ManualPickBacteria do");
btnOK_Click(null, null);
}
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 加载挑菌数据
///
private void InitializeChoiceData()
{
// 挑菌的板位号
choiceLatticeId = ConfigurationManager.AppSettings["choiceLatticeId"].ToString();
if (methodNode != null)
{
// 涂布耗材
lstCoatingLabwareText = methodNode.SelectSingleNode("labware/text").InnerText.ToString().Split(',').ToList();
lstCoatingLabwareValue = methodNode.SelectSingleNode("labware/value").InnerText.ToString().Split(',').ToList();
// 涂布接种次数
launchView.choiceTimes = Convert.ToInt32(methodNode.SelectSingleNode("choiceTimes").InnerText);
if (launchView.choiceTimes == 1)
{
radioBtnTimes1.IsChecked = true;
}
else if (launchView.choiceTimes == 2)
{
radioBtnTimes2.IsChecked = true;
}
}
}
///
/// 确定
///
///
///
private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
okEvent?.Invoke(this, EventArgs.Empty);
// 将DataTable中数据写入到CSV文件中
// 把 ObservableCollection 类型转为DataTable
DataTable dt = coatingFileBll.ManualChoiceDataTypeChangeToDataTable(choiceDatas);
if (dt != null && dt.Rows.Count >= 0)
{
var pickResultPath = ConfigurationManager.AppSettings["PickResultPath"];
string directoryName = Path.GetDirectoryName(pickResultPath);
if (!Directory.Exists(directoryName))
{
Directory.CreateDirectory(directoryName);
}
string strPickResultFile = string.Format("{0}{1}", pickResultPath, ConfigurationManager.AppSettings["PickResultFile"]);
if (File.Exists(strPickResultFile))
{
File.Delete(strPickResultFile);
}
ExcelAndCsvHelper.WriteDataTableToCSV(dt, strPickResultFile);
}
this.Close();
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
#region 涂布/接种次数
///
/// 涂布/接种次数:1
///
///
///
private void radioBtnTimes1_Checked(object sender, RoutedEventArgs e)
{
try
{
#region
if (stackPanel != null)
{
stackPanel.Visibility = Visibility.Collapsed;
dgCoatingData.ItemsSource = null;//清空
choiceDatas.Clear();
// 挑菌数据
ObservableCollection choiceList = dgBacteriaCoordinates;
Labware labwareDes = new Labware();
int rowNum = 0;
int columnNum = 0;
XmlNodeList latticeDesXmlList = null;
if (launchView != null) // 运行界面
{
XmlNode latticeEnv = launchView.xmlDocument.SelectSingleNode("root/env");
// 目标板耗材Id
string labwareIdDes = lstCoatingLabwareValue[0];
// 来源皿barcode
string barcodeSrc = string.Empty;
if (dtChoiceParams != null)
{
barcodeSrc = dtChoiceParams.Rows[1][2].ToString();
}
// 目标板耗材信息
labwareDes = LabwareDB.GetLabware(labwareIdDes);
if (labwareDes != null && labwareDes.labware_id != "")
{
rowNum = (int)labwareDes.number_row;
columnNum = (int)labwareDes.number_column;
}
else
{
System.Windows.Application.Current.Dispatcher.Invoke(new Action(() =>
{
string strMsg = "Please set lattices! Later try again.";
PlsToolTipWin plsToolTipWin = new PlsToolTipWin(strMsg);
plsToolTipWin.Topmost = true;
plsToolTipWin.Height = this.ActualHeight;
plsToolTipWin.Width = this.ActualWidth;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
plsToolTipWin.Background = (System.Windows.Media.Brush)mybtn1_Brush;
plsToolTipWin.btnCancel.Visibility = Visibility.Hidden;
plsToolTipWin.ShowDialog();
this.Close();
return;
}));
}
// 获取涂布数据
GetCoatingData(latticeEnv, choiceList, barcodeSrc);
}
dgCoatingData.DataContext = choiceDatas;
dgCoatingData.ItemsSource = choiceDatas;
dgCoatingData.Columns[1].IsReadOnly = true; // 来源皿位置
dgCoatingData.Columns[5].IsReadOnly = true; // 目标板位置
dgCoatingData.Columns[3].IsReadOnly = true;
dgCoatingData.Columns[4].IsReadOnly = true;
}
#endregion
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
///
/// 涂布/接种次数:2
///
///
///
private void radioBtnTimes2_Checked(object sender, RoutedEventArgs e)
{
try
{
#region 双板涂布
if (stackPanel != null)
{
stackPanel.Visibility = Visibility.Visible;
dgCoatingData.ItemsSource = null;
choiceDatas.Clear();
// 挑菌数据
ObservableCollection choiceList = dgBacteriaCoordinates;
// 目标板耗材Id
string labwareIdDes = lstCoatingLabwareValue[0];
string srcBarcode = string.Empty;//来源皿barcode
if (dtChoiceParams != null)
{
srcBarcode = dtChoiceParams.Rows[1][2].ToString();
}
// 目标板耗材信息
XmlNode latticeEnv = launchView.xmlDocument.SelectSingleNode("root/env");
if (launchView != null)
{
// 获取涂布数据
GetCoatingData(latticeEnv, choiceList, srcBarcode);
}
dgCoatingData.DataContext = choiceDatas;
dgCoatingData.ItemsSource = choiceDatas;
dgCoatingData.Columns[1].IsReadOnly = true; // 来源皿位置
dgCoatingData.Columns[5].IsReadOnly = true; // 目标板位置
dgCoatingData.Columns[3].IsReadOnly = true;
dgCoatingData.Columns[4].IsReadOnly = true;
}
#endregion
}
catch (Exception ex)
{
LoggerHelper.ErrorLog("ERROR:", ex);
}
}
#endregion
private void dgCoatingData_BeginningEdit(object sender, DataGridBeginningEditEventArgs e)
{
}
private void dgCoatingData_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
{
//如果点击了放弃按钮或ESC
if (e.EditAction == DataGridEditAction.Cancel)
{
return;
}
//获取编辑模式显示的元素,因为使用了模板,因此这里是ContentPresenter类型
ContentPresenter cp = e.EditingElement as ContentPresenter;
if (cp != null && VisualTreeHelper.GetChildrenCount(cp) > 0)
{
//找到编辑框
TextBox textBox = VisualTreeHelper.GetChild(cp, 0) as TextBox;
//如果找到
if (textBox != null)
{
var a = choiceDatas;
var dataGrid = sender as DataGrid;
dataGrid.BeginEdit();
}
}
}
private void dgCoatingData_PreviewMouseMove(object sender, MouseEventArgs e)
{
}
private void DataGridCell_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is System.Windows.Shapes.Path)
{
Cursor = Cursors.Cross;
}
else
Cursor = Cursors.Arrow;
}
bool bGroup = false;
private void DataGridCell_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("Button down");
if (e.OriginalSource is System.Windows.Shapes.Path)
{
bGroup = true;
}
}
private void DataGridCell_PreviewMouseDoubleClick(object sender, MouseButtonEventArgs e)
{
Debug.WriteLine("Double click");
DataGridCell cell = sender as DataGridCell;
if ((cell.Column.DisplayIndex == 1) || (cell.Column.DisplayIndex == 2) || (cell.Column.DisplayIndex == 5)
|| (cell.Column.DisplayIndex == 6) || (cell.Column.DisplayIndex == 7))
{
cell.Column.IsReadOnly = false;
}
cell.Focus();
cell.IsSelected = true;
dgCoatingData.BeginEdit();
e.Handled = true;
}
private int FindRowIndex(DataGridRow row)
{
DataGrid dataGrid = ItemsControl.ItemsControlFromItemContainer(row) as DataGrid;
int index = dataGrid.ItemContainerGenerator.IndexFromContainer(row);
return index;
}
public DataGridCell GetDataGridCell(DataGridCellInfo cellInfo)
{
var cellContent = cellInfo.Column.GetCellContent(cellInfo.Item);
if (cellContent != null)
return (DataGridCell)cellContent.Parent;
return null;
}
private void DataGridCell_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
}
private void dgCoatingData_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count > 1)
{
int a = 0;
}
}
DataFillTips dataFillTips = null;
private void dgCoatingData_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (bGroup)
{
DataFillInfo info = GetSelectedCellData();
dataFillTips = new DataFillTips(info);
//dataFillTips.closeEvent += DataFillTips_closeEvent;
//dataFillTips.OKEvent += DataFillTips_OKEvent;
//Window wnd = Application.Current.MainWindow;
//Grid parent = Utilities.FindVisualChild(wnd);
//parent.Children.Add(dataFillTips);
dataFillTips.Height = this.ActualHeight;
dataFillTips.Width = this.ActualWidth;
dataFillTips.coatingSetting = this;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
dataFillTips.Background = (System.Windows.Media.Brush)mybtn1_Brush;
dataFillTips.ShowDialog();
}
bGroup = false;
}
public void DataFillTips_OKEvent()
{
if (dataFillTips != null)
{
DataFillInfo info = (DataFillInfo)dataFillTips.dataFillInfo;
if (info.nFrom > 0 && info.nTo > 0 && info.nFrom <= info.nTo)
{
for (int i = info.nFrom - 1; i < info.nTo; i++)
{
ManualChoiceData data = choiceDatas[i];
if (info.column == 1)
data.sourceplate = info.columnContent;
else if (info.column == 2)
data.sourceplate_num = info.columnContent;
else if (info.column == 5)
data.targetplate = info.columnContent;
else if (info.column == 6)
data.targetplate_num = info.columnContent;
else if (info.column == 7)
data.targetplate_well = info.columnContent;
}
}
//DataFillTips_closeEvent(sender, null);
}
}
private void DataFillTips_closeEvent(object sender, EventArgs e)
{
Window wnd = Application.Current.MainWindow;
Grid grid = Utilities.FindVisualChild(wnd);
UIElement element = sender as UIElement;
grid.Children.Remove(element);
}
///
/// 获取选中单元格数据
///
/// 返回第一个有内容的单元格里的内容
/// 返回选中的行list
private DataFillInfo GetSelectedCellData()
{
DataFillInfo dataFillInfo = new DataFillInfo();
foreach (var cell in dgCoatingData.SelectedCells)
{
ManualChoiceData data = cell.Item as ManualChoiceData;
dataFillInfo.column = cell.Column.DisplayIndex;
if (string.IsNullOrEmpty(dataFillInfo.columnContent))
{
if (dataFillInfo.column == 1)
{
dataFillInfo.columnName = Properties.Resources.strSourceDish;
dataFillInfo.columnContent = data.sourceplate;
}
else if (dataFillInfo.column == 2)
{
dataFillInfo.columnName = Properties.Resources.strSourceDishBarcode;
dataFillInfo.columnContent = data.sourceplate_num;
}
else if (dataFillInfo.column == 5)
{
dataFillInfo.columnName = Properties.Resources.strTargetBoard;
dataFillInfo.columnContent = data.targetplate;
}
else if (dataFillInfo.column == 6)
{
dataFillInfo.columnName = Properties.Resources.strDestBoardBarcode;
dataFillInfo.columnContent = data.targetplate_num;
}
else if (dataFillInfo.column == 7)
{
dataFillInfo.columnName = Properties.Resources.strTargetWell;
dataFillInfo.columnContent = data.targetplate_well;
}
}
DataGridCell dgc = GetDataGridCell(cell);
DataGridRow dataGridRow = Utilities.FindVisualParent(dgc);
int row = FindRowIndex(dataGridRow);
dataFillInfo.rows.Add(row);
}
if (dataFillInfo.rows.Count > 0)
{
dataFillInfo.nFrom = dataFillInfo.rows[0] + 1;
dataFillInfo.nTo = dataFillInfo.rows[dataFillInfo.rows.Count - 1] + 1;
}
return dataFillInfo;
}
///
/// 获取涂布数据
///
///
///
///
private void GetCoatingData(XmlNode latticeEnv, ObservableCollection choiceList, string barcodeSrc)
{
// 涂布耗材集合:count = 2
List lstCoatingLabware = new List();
Labware labwareDes = null;
#region 循环涂布耗材集合,添加耗材信息
foreach (string coatingLabwareId in lstCoatingLabwareValue)
{
if (string.IsNullOrEmpty(coatingLabwareId))
{
continue;
}
CoatingLabware coatingLab = new CoatingLabware();
// 获取目标板耗材信息
labwareDes = LabwareDB.GetLabware(coatingLabwareId);
if (!string.IsNullOrEmpty(labwareDes.labware_id))
{
coatingLab.RowNum = (int)labwareDes.number_row;
coatingLab.ColumnNum = (int)labwareDes.number_column;
}
// 耗材Id
coatingLab.LabwareId = coatingLabwareId;
// 所有涂布耗材台面信息
string xPath = string.Format("platform[labware_id='{0}']", coatingLabwareId); // 根据涂布耗材Id,查找台面上所有匹配的耗材
coatingLab.NodeList = latticeEnv.SelectNodes(xPath);
lstCoatingLabware.Add(coatingLab);
}
#endregion
#region 获取涂布使用的所有目标板的孔位信息
if (launchView.lstCoatingLabware == null)
{
foreach (CoatingLabware labwareType in lstCoatingLabware)
{
// 拍照节点选了两个涂布耗材,实际台面上没有
if (labwareType.NodeList == null || labwareType.NodeList.Count == 0)
{
continue;
}
labwareType.ListCoatingWell = new List();
foreach (XmlNode latticeDesXmlItem in labwareType.NodeList)
{
// 获取当前板所有孔
List lstCoatingWell = GetWellsForPlate(labwareType.RowNum, labwareType.ColumnNum, latticeDesXmlItem);
labwareType.ListCoatingWell.AddRange(lstCoatingWell);
}
}
launchView.lstCoatingLabware = lstCoatingLabware;
}
#endregion
// 循环挑菌数据
for (int choiceCurrentNum = 1; choiceCurrentNum <= choiceList.Count; choiceCurrentNum++)
{
// 根据涂布接种次数动态生成数据
for (int choiceTimeIndex = 0; choiceTimeIndex < launchView.choiceTimes; choiceTimeIndex++)
{
// 涂布数据
ManualChoiceData choiceData = new ManualChoiceData();
choiceData.is_new_plate_batch = choiceCurrentNum == 1 ? true : false;
CoatingLabware coatingLabware = launchView.lstCoatingLabware[choiceTimeIndex];
if (coatingLabware.ListCoatingWell == null)
{
continue;
}
// 获取目标板的信息
CoatingWell coatingWell = coatingLabware.ListCoatingWell.FirstOrDefault(it => !it.IsUsed);
// 之前一批的目标板已经被使用完了,需要新一批次的目标板
if (coatingWell == null)
{
foreach (var coatingWellItem in coatingLabware.ListCoatingWell)
{
coatingWellItem.IsUsed = false;
coatingWellItem.Barcode = string.Empty;
}
// 再取一次未使用的有效孔
coatingWell = coatingLabware.ListCoatingWell.FirstOrDefault(it => !it.IsUsed);
choiceData.is_new_plate_batch = true;
}
// 当前空已被使用
coatingWell.IsUsed = true;
choiceData.choice_id = Convert.ToInt32(choiceList[choiceCurrentNum - 1].bacteriacoordinate_id);
choiceData.sourceplate = choiceTimeIndex == 0 ? launchView.choiceSrcLabwareSname : choiceDatas[choiceDatas.Count - 1].sourceplate;
choiceData.sourceplate_num = choiceTimeIndex == 0 ? barcodeSrc : choiceDatas[choiceDatas.Count - 1].sourceplate_num;
choiceData.machine_coordinate = choiceTimeIndex == 0 ? choiceList[choiceCurrentNum - 1].coordinate_machine : choiceDatas[choiceDatas.Count - 1].machine_coordinate;
choiceData.pixel_coordinate = choiceList[choiceCurrentNum - 1].coordinate_pixel;
choiceData.targetplate = coatingWell.LatticeNum; // 方格编号
choiceData.targetplate_num = coatingWell.Barcode; // 板编号
choiceData.targetplate_well = coatingWell.Site; // 孔号
// 是否是一个板子最后的一个有效孔:最后一个菌时,一定是板子的最后一个有效孔!
choiceData.is_plate_last_well = choiceCurrentNum == choiceList.Count ? true : coatingWell.IsPlateLastWell;
choiceDatas.Add(choiceData);
}
}
}
///
/// 获取当前板所有孔
///
///
///
///
///
///
private List GetWellsForPlate(int nRow, int nCol, XmlNode latticeDesXml)
{
// TODO 需要和画面设定的有效孔位进行绑定
List lstCoatingWell = new List();
// 所有孔
int nRowAscii = 65;
for (int x = 1; x <= nCol; x++)
{
nRowAscii = 65;
for (int y = 1; y <= nRow; y++, nRowAscii++)
{
string strWell = string.Format("{0}{1}", Char.ConvertFromUtf32(nRowAscii), x);
CoatingWell coatingWell = new CoatingWell();
coatingWell.LatticeNum = latticeDesXml.SelectSingleNode("labware_sname").InnerText;
coatingWell.RowIndex = nRow;
coatingWell.ColIndex = nCol;
coatingWell.Site = strWell;
coatingWell.IsUsed = false;
coatingWell.Barcode = latticeDesXml.SelectSingleNode("labware_barcode").InnerText;
lstCoatingWell.Add(coatingWell);
}
}
// 一个板子最后的一个有效孔
if (lstCoatingWell.Count > 0)
{
lstCoatingWell.Last().IsPlateLastWell = true;
}
return lstCoatingWell;
}
#region Del
///
/// 关闭页面
///
///
///
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
#endregion
}
}