using CnasSynchronousCommon;
using CnasSynchronusClient;
using CnasSynchrousModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Serialization;
namespace CNAS_BalanceClient
{
public class BalanceMainOperation
{
ILaboratoryTestOpera iopera;
///
/// 根据当前切换的tab页,切换当前数据类型标识
///
///
///
public void UpdateCurrentDataType(System.Windows.Forms.TabControlEventArgs e, ref string CurrentDataType)
{
//ARB 全水分;Moisture 水分;Ash 灰分;Volatile 挥发分;Crucible 坩埚;Heat 发热量:Sulfur 测硫;Hydrogen 测氢
switch (e.TabPage.Name)
{
case "tabpAllWater":
CurrentDataType = "ARB";
break;
case "tabpWater":
CurrentDataType = "Moisture";
break;
case "tabpAsh":
CurrentDataType = "Ash";
break;
case "tabpVoliate":
CurrentDataType = "Volatile";
break;
case "tabpCrucible":
CurrentDataType = "Crucible";
break;
case "tabpHeat":
CurrentDataType = "Heat";
break;
case "tabpSulfur":
CurrentDataType = "Sulfur";
break;
case "tabpHydrogen":
CurrentDataType = "Hydrogen";
break;
}
}
///
/// 根据类型绑定datagridview
///
///
///
///
public void BindData(System.Windows.Forms.DataGridView dgv, List laboratoryTests, string currentDataType)
{
dgv.DataSource = new BindingList();
var query = laboratoryTests.Where(s => s.DataType == currentDataType && s.OperationType != "delete").ToList();
dgv.DataSource = new BindingList(query);
}
///
/// 绑定坩埚数据源
///
///
///
///
public void BindCrucibleData(DataGridView dgv, List laboratoryTests, string currentDataType)
{
dgv.DataSource = new BindingList();
var query = laboratoryTests.Where(s => s.DataType == currentDataType &&(s.Crucible_Number==null||s.Crucible_Number=="")&& s.OperationType != "delete").ToList();
dgv.DataSource = new BindingList(query);
}
///
/// 删除当前选中行
///
///
public void DeleteData(System.Windows.Forms.DataGridView dgv, List laboratoryTests, string currentDataType)
{
//当前选中行
if (dgv.CurrentCell == null) return;
if (dgv.Rows[dgv.CurrentCell.RowIndex].Cells[0].Value == null) return;
string strID = dgv.Rows[dgv.CurrentCell.RowIndex].Cells[0].Value.ToString(); //要求每个dgv的第一列必须为ID字段
var queryitem = laboratoryTests.Where(s => s.GUID == strID).ToList();
if (queryitem.Count == 1)
{
queryitem[0].OperationType = "delete";
dgv.Rows.Remove(dgv.CurrentRow);
}
}
///
/// 增加新数据(全水分/水分/灰分/挥发分/...)
///
///
///
///
public ReturnValue AddNewData(string strCode, string strType)
{
ReturnValue returnValue = new ReturnValue();
if (strCode == "" || strType == "")
{
returnValue.StrErrorMsg = string.Format("参数为空,{0}", strCode == "" ? "样品编码" : "化验类型");
return null;
}
//ARB 全水分;Moisture 水分;Ash 灰分;Volatile 挥发分;Crucible 坩埚;Heat 发热量:Sulfur 测硫;Hydrogen 测氢
switch (strType)
{
case "ARB":
iopera = new QuanShuiFenOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
case "Moisture":
iopera = new ShuiFenOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
case "Ash":
iopera = new HuiFenOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
case "Volatile":
iopera = new HuiFaFenOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
case "Crucible|Heat":
iopera = new CrucibleHeatOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
case "Crucible|Sulfur":
iopera = new CrucibleSulfurOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
case "Crucible|Hydrogen":
iopera = new CrucibleHydrogenOpera();
returnValue = iopera.CreateLaboratoryTestData(strCode);
break;
}
return returnValue;
}
///
/// 获取自动编码
///
///
///
public string GetAutoCode(List laboratoryTests)
{
string strCode= "T";
string strNewCode = DateTime.Now.ToString("yyyyMMdd") + "01";
if (laboratoryTests.Count > 0)
{
var query = laboratoryTests.OrderByDescending(s => s.Auto_Code).ToList();
string strMaxCode = query[0].Auto_Code;
if (strMaxCode != "")
{
long maxIndex = Convert.ToInt64(strMaxCode.Substring(1, strMaxCode.Length - 1));
if (maxIndex >= Convert.ToInt64(strNewCode))
strCode += (maxIndex + 1).ToString();
else
strCode += strNewCode;
}
else
strCode += strNewCode;
}
else
{
strCode += strNewCode;
}
return strCode;
}
///
/// 绘制序号列
///
///
///
public void DrawFirstColumnIndex(DataGridView dgv, DataGridViewRowPostPaintEventArgs e)
{
Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
e.RowBounds.Location.Y,
dgv.RowHeadersWidth - 4,
e.RowBounds.Height);
TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
dgv.RowHeadersDefaultCellStyle.Font,
rectangle,
dgv.RowHeadersDefaultCellStyle.ForeColor,
TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
}
///
/// 提交编辑列
///
///
///
///
public void CommitEditColumn(DataGridView dgv, List laboratoryTests, DataGridViewCellEventArgs e)
{
if (dgv.CurrentCell == null) return;
if (dgv.CurrentCell.Value == null) return;
if (e.ColumnIndex == 2) //仪器编号
{
string strInputValue = dgv.CurrentCell.Value.ToString();
string strCurrentID = dgv.Rows[dgv.CurrentCell.RowIndex].Cells[0].Value.ToString();
var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList();
if (query.Count == 1)
{
query[0].Instrument_Number = strInputValue;
if (query[0].OperationType == null || query[0].OperationType == "")
query[0].OperationType = "update";
}
}
if (e.ColumnIndex == 3) //坩埚编号
{
string strInputValue = dgv.CurrentCell.Value.ToString();
string strCurrentID = dgv.Rows[dgv.CurrentCell.RowIndex].Cells[0].Value.ToString();
var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList();
if (query.Count == 1)
{
query[0].Crucible_Number = strInputValue;
if (query[0].OperationType == null || query[0].OperationType == "")
query[0].OperationType = "update";
}
}
if (e.ColumnIndex == 12) //颗粒度
{
string strInputValue = dgv.CurrentCell.Value.ToString();
string strCurrentID = dgv.Rows[dgv.CurrentCell.RowIndex].Cells[0].Value.ToString();
var query = laboratoryTests.Where(s => s.GUID == strCurrentID).ToList();
if (query.Count == 1)
{
query[0].Granularity = strInputValue;
if (query[0].OperationType == null || query[0].OperationType == "")
query[0].OperationType = "update";
}
}
}
///
/// 把天平端数据更改到显示端和存储端
///
///
///
///
///
public void UpdateQuanShuiFenBalanceWeight(System.Windows.Forms.DataGridView dgvQuanShuiFen, List lstWeightColumnIndex, double balanceWeight, List laboratoryTests,string strType)
{
lstWeightColumnIndex = new List() { "4", "5", "7", "8", "9" }; //全水分的这些列从天平端获取
if (lstWeightColumnIndex.Contains(dgvQuanShuiFen.CurrentCell.ColumnIndex.ToString()))
{
string strCurrentValue = dgvQuanShuiFen.CurrentCell.Value.ToString();
if (strCurrentValue != ""&&strCurrentValue!="0")
{
if (MessageBox.Show("单元格已经存在数据,继续将覆盖这个数据,是否继续?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
switch (strType)
{
case "ARB":
new QuanShuiFenUIOperation().UpdateQuanShuiFenData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
break;
case "Moisture":
new ShuiFenUIOperation().UpdateShuiFenData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
break;
case "Ash":
new HuiFenUIOperation().UpdateHuifenData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
break;
}
}
}
else
{
switch (strType)
{
case "ARB":
new QuanShuiFenUIOperation().UpdateQuanShuiFenData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
break;
case "Moisture":
new ShuiFenUIOperation().UpdateShuiFenData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
break;
case "Ash":
new HuiFenUIOperation().UpdateHuifenData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
break;
}
}
}
}
///
/// 将天平传输的重量插入数据中
///
///
///
///
///
public void UpdateVolatileBalanceWeight(System.Windows.Forms.DataGridView dgvQuanShuiFen, List lstWeightColumnIndex, double balanceWeight, List laboratoryTests)
{
lstWeightColumnIndex = new List() { "4", "5", "7" }; //全水分的这些列从天平端获取
if (lstWeightColumnIndex.Contains(dgvQuanShuiFen.CurrentCell.ColumnIndex.ToString()))
{
string strCurrentValue = dgvQuanShuiFen.CurrentCell.Value.ToString();
if (strCurrentValue != "" && strCurrentValue != "0")
{
if (MessageBox.Show("单元格已经存在数据,继续将覆盖这个数据,是否继续?", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
new HuiFaFenUIOperation().UpdateVolatileData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
}
}
else
{
new HuiFaFenUIOperation().UpdateVolatileData(dgvQuanShuiFen, balanceWeight, laboratoryTests);
}
}
}
///
/// 检查输入百分比大小是否是100内小数
///
///
///
public bool CheckInputPercent(string strInput)
{
bool bIfSuccess = true;
double value = 0;
if (double.TryParse(strInput, out value))
{
if (value < 0 || value > 100)
{
MessageBox.Show("输入值范围为0~100", "提示");
bIfSuccess = false;
}
}
else
{
MessageBox.Show("输入值范围为0~100的数字", "提示");
bIfSuccess = false;
}
return bIfSuccess;
}
///
/// 获取当前选中项的GUID集合
///
///
///
public List GetSelectItemID(DataGridView dgv)
{
List lstSelectID = new List();
for (int j = 0; j < dgv.SelectedRows.Count; j++)
{
string strID = dgv.SelectedRows[j].Cells[0].Value.ToString();
lstSelectID.Add(strID);
}
return lstSelectID;
}
///
/// 将天平传输来的重量插入坩埚架数据中
///
///
///
///
///
///
public void UpdateCrucibleBalanceWeight(Dictionary DstCurrentLeftGrids,DataGridView dgvCrucibleShelf, double defaultdouble,string currentCrucibleDataType,List laboratoryTests)
{
new CrucibleUIOperation().UpdateCrucibleBalanceWeight(DstCurrentLeftGrids, dgvCrucibleShelf, defaultdouble, currentCrucibleDataType, laboratoryTests);
}
///
/// 新增一组坩埚架数据
///
///
///
///
///
///
public void AddCrucibleShelfData(Dictionary DictCrucuibleGrids,DataGridView dgvCrucibleShelf, string currentCrucibleDataType,string strTypeTitle,List laboratoryTests)
{
new CrucibleUIOperation().AddCrucibleShelfData(DictCrucuibleGrids, dgvCrucibleShelf, currentCrucibleDataType, strTypeTitle, laboratoryTests);
}
}
}