using DataEntity.Share;
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.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Xml;
using XCommon.Log;
using XCore;
using XHandler.Class;
using XHandler.Class.DataEx;
using XHandler.View.ManualCoating;
using XImagingXhandler.XDAL;
using DataRWDAL;
using XHandler.View.MethodProperty;
using XHandler.Controls.Run.Com;
using XCoreBLL;
namespace XHandler.View.ManualPick
{
///
/// 切胶转移孔位设置 的交互逻辑
///
public partial class CutSetting : Window
{
// 挑菌的板位号
private string choiceLatticeId = string.Empty;
// 本画面的数据
private ObservableCollection choiceDatas = new ObservableCollection();
private CoatingFileBll coatingFileBll = new CoatingFileBll();
CutGumFileBll cutGumFileBll = new CutGumFileBll();
// 涂布耗材
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;
bool bGroup = false;
// 前画面传过来数据
public ObservableCollection dgBacteriaCoordinates = null;
public CutSetting()
{
InitializeComponent();
this.Owner = (Window)Shared.Main;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
// 加载挑菌数据
InitializeChoiceData();
// 1:人工挑选菌落;0:成像系统自动挑选菌落
string choicemode = dtChoiceParams==null?"1":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();
}
try
{
#region
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);
}
}
private void btnOK_Click(object sender, RoutedEventArgs e)
{
try
{
okEvent?.Invoke(this, EventArgs.Empty);
// 将DataTable中数据写入到CSV文件中
// 把 ObservableCollection 类型转为DataTable
DataTable dt = cutGumFileBll.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);
}
}
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 dgCoatingData_SelectedCellsChanged(object sender, SelectedCellsChangedEventArgs e)
{
if (e.AddedCells.Count > 1)
{
int a = 0;
}
}
DataFillItems dataFillItems = null;
private void dgCoatingData_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (bGroup)
{
DataFillInfo info = GetSelectedCellData();
dataFillItems = new DataFillItems(info);
dataFillItems.Height = this.ActualHeight;
dataFillItems.Width = this.ActualWidth;
dataFillItems.cutSetting = this;
SolidColorBrush mybtn1_Brush = new SolidColorBrush(System.Windows.Media.Color.FromArgb(0, 0, 0, 0));
dataFillItems.Background = (System.Windows.Media.Brush)mybtn1_Brush;
dataFillItems.ShowDialog();
}
bGroup = false;
}
public void DataFillTips_OKEvent()
{
if (dataFillItems != null)
{
DataFillInfo info = (DataFillInfo)dataFillItems.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;
}
}
}
}
///
/// 获取选中单元格数据
///
/// 返回第一个有内容的单元格里的内容
/// 返回选中的行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 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 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", coatingLabwareId);
//XmlNode axmlNode = latticeEnv.SelectSingleNode(xPath);
coatingLab.NodeList = latticeEnv.SelectNodes("platform");
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)
{
XmlNode xnodeLabware = latticeDesXmlItem.SelectSingleNode("labware[@id=1]");
if (xnodeLabware != null)
{
string strLabwareTxt = xnodeLabware.SelectSingleNode("labware_id").InnerText;
if (strLabwareTxt == labwareType.LabwareId)
{
// 获取当前板所有孔
List lstCoatingWell = GetWellsForPlate(labwareType.RowNum, labwareType.ColumnNum, xnodeLabware);
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);
launchView.choiceSrcLabwareSname = "P1";//coatingLabware.LabwarePositon;
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;
}
private void DataGridCell_PreviewMouseMove(object sender, MouseEventArgs e)
{
if (e.OriginalSource is System.Windows.Shapes.Path)
{
Cursor = Cursors.Cross;
}
else
Cursor = Cursors.Arrow;
}
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;
}
}
}