MES总结:CBF.Common 文件Net下的有类型转换
MES总结:CBF.Common 文件Net下的有类型转换。
using System.Text;
using System.Data;
using System.ComponentModel;
using CBF.WMS.Common.Comm;
using CBF.WMS.Common.Constant;
using System.Reflection;
using System.Collections;
namespace CBF.WMS.Common.Net
{
public static class ConvertHelper
{
public static List<M> IListToList<M>(IList<M> tmpList)
{
List<M> list = new List<M>();
foreach (M table in tmpList)
{
list.Add(table);
}
return list;
}
/// <summary>
/// Ilist 转换成 DataTable
/// </summary>
/// <typeparam name="T">数据类型</typeparam>
/// <param name="i_objlist"></param>
/// <returns></returns>
public static DataTable ConvertToDataTable<T>(IList i_objlist)
{
if (i_objlist == null || i_objlist.Count <= 0)
{
return null;
}
DataTable dt = new DataTable(typeof(T).Name);
DataColumn column;
DataRow row;
PropertyInfo[] myPropertyInfo = typeof(T).GetProperties(BindingFlags.Public |
BindingFlags.Instance);
foreach (T t in i_objlist)
{
if (t == null)
{
continue;
}
row = dt.NewRow();
for (int i = 0, j = myPropertyInfo.Length; i < j; i++)
{
PropertyInfo pi = myPropertyInfo[i];
string name = pi.Name;
if (dt.Columns[name] == null)
{
column = new DataColumn(name, pi.PropertyType);
dt.Columns.Add(column);
}
row[name] = pi.GetValue(t, null);
}
dt.Rows.Add(row);
}
return dt;
}
public static DataTable ToDataTable(IList list)
{
DataTable result = new DataTable();
if (list.Count > 0)
{
PropertyInfo[] propertys = list[0].GetType().GetProperties();
foreach (PropertyInfo pi in propertys)
{
result.Columns.Add(pi.Name, pi.PropertyType);
}
for (int i = 0; i < list.Count; i++)
{
ArrayList tempList = new ArrayList();
foreach (PropertyInfo pi in propertys)
{
object obj = pi.GetValue(list[i], null);
tempList.Add(obj);
}
object[] array = tempList.ToArray();
result.LoadDataRow(array, true);
}
}
return result;
}
public static IList<T> ConvertListToIList<T>(
IList source
)
{
T[] output = new T[source.Count];
source.CopyTo(output, 0);
return output;
}
public static DataTable CreateTable<T>()
{
Type entityType = typeof(T);
DataTable table = new DataTable(entityType.Name);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (PropertyDescriptor prop in properties)
{
table.Columns.Add(prop.Name, prop.PropertyType);
}
return table;
}
public static T CreateItem<T>(DataRow row)
{
T obj = default(T);
if (row != null)
{
obj = Activator.CreateInstance<T>();
foreach (DataColumn column in row.Table.Columns)
{
PropertyInfo prop = obj.GetType().GetProperty(column.ColumnName);
try
{
object value = row[column.ColumnName];
prop.SetValue(obj, value, null);
}
catch (Exception ex)
{
// You can log something here
CLog.WriteLog(CMessage.ERR, ex);
throw;
}
}
}
return obj;
}
/// <summary>
/// 转换DataTa为IList<T>泛型
/// </summary>
/// <typeparam name="T">泛型类型</typeparam>
/// <param name="table">所需转换DataTab</param>
/// <returns>IList<T></returns>
public static IList<T> DataTableToList<T>(DataTable table)
{
if (table == null)
{
return null;
}
List<DataRow> rows = new List<DataRow>();
foreach (DataRow row in table.Rows)
{
rows.Add(row);
}
return ConvertTo<T>(rows);
}
public static IList<T> ConvertTo<T>(IList<DataRow> rows)
{
IList<T> list = null;
if (rows != null)
{
list = new List<T>();
foreach (DataRow row in rows)
{
T item = CreateItem<T>(row);
list.Add(item);
}
}
return list;
}
/// <summary>
/// 转换IList<T>为DataTable
/// </summary>
/// <typeparam name="T">泛型类型</typeparam>
/// <param name="list">泛型List集合</param>
/// <returns>Datatable 对象</returns>
public static DataTable ListToDataTable<T>(IList<T> list)
{
DataTable table = CreateTable<T>();
Type entityType = typeof(T);
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(entityType);
foreach (T item in list)
{
DataRow row = table.NewRow();
foreach (PropertyDescriptor prop in properties)
{
row[prop.Name] = prop.GetValue(item);
}
table.Rows.Add(row);
}
return table;
}
}
}
=============================递归菜单的应用(CBF.DAL.BaseInfo下的Bom_DAL.cs)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CBF.Model;
using NHibernate;
using NHibernate.Linq;
using System.Data;
using System.Collections;
using CBF.DataAccess;
using CBF.Integration;
using NHibernate.Criterion;
using CBF.Common.Constant;
using CBF.Common.Comm;
namespace CBF.DAL.BaseInfo
{
public class BOM_DAL : BaseDAL<BOM>
{
/// <summary>
/// 根据ID取表
/// </summary>
/// <param name="id">ID</param>
/// <returns>表</returns>
public CBF.Model.BOM GetBOMById(int id)
{
try
{
if (!this._session.IsOpen) this._session = NhbHelper.GetCurrentSession();
return this._session.Get<BOM>(id);
}
catch (Exception ex)
{
CLog.WriteLog(CMessage.ERR, ex);
}
return Activator.CreateInstance<BOM>();
}
public DataTable SelectStationList(string lanText)
{
string strSQL = "";
strSQL = @"select StaCode, StaName from mes_station";
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL);
return dtTmp;
}
public DataTable SelectMaterialList(string MaterialNO)
{
string strSQL = "";
strSQL = @"select distinct MaterialNO, MaterialName from MES_MaterialPacket where ('' = @MaterialNO or MaterialNO = @MaterialNO1)";
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { MaterialNO, MaterialNO });
return dtTmp;
}
public DataTable SelectItemList(string lanText)
{
string strSQL = "";
strSQL = @"select ItemNO, ItemName from mes_item";
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL);
return dtTmp;
}
public DataTable GetBomList(string prefix, string flag)
{
string strSQL = "";
if (flag == "like")
{
strSQL = string.Format(@"select * from MES_BOM where BOMNO like '{0}'", prefix);
}
else
{
strSQL = string.Format("select * from MES_BOM where BOMNO='{0}'", prefix);
}
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString, strSQL);
return dtTmp;
}
/// <summary>
/// 执行传入的SQL
/// </summary>
public int exeERPBOM(string strSQL)
{
return SQLHelper.ExecuteNoneQuery(CommonInfo.Instance.DefaultConnectString,
strSQL);
}
/// <summary>
/// 执行传入的SQL
/// </summary>
public DataTable LoadERPBOM(string strSQL)
{
return SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL);
}
/// <summary>
/// 最大子表ID号
/// </summary>
public DataRow GetMaxChildID()
{
string strSQL = @"select Id = isnull(max(id),1)+1 from mes_subitem ";
return SQLHelper.ExecuteDataRow(CommonInfo.Instance.DefaultConnectString,
strSQL);
}
/// <summary>
/// 删除子表记录
/// </summary>
public int DeleteChildDetailInfo(int ID)
{
string strSQL = @"delete mes_bomchild where ID = @ID";
return SQLHelper.ExecuteNoneQuery(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { ID });
}
/// <summary>
///删除BOM结构
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DataTable DeleteBOM(string[] aIDList)
{
string strSQL = string.Format("delete MES_BOM where BOMNO in (select BOMNO from MES_BOM where Id in ('{0}')) ", String.Join("','", aIDList));
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL);
return dtTmp;
}
/// <summary>
/// Insert into Route
/// </summary>
public int ChangeBOMInfo(BOM m_BOM, DataTable tblChild, Boolean IsUpdate, string @User)
{
int Flag = 0;
try
{
try
{
Begin();
SaveOrUpdate(m_BOM);
//插入从表
foreach (DataRow dr in tblChild.Rows)
{
BOMChild bomChild = new BOMChild();
if (dr.RowState == DataRowState.Added)
{
}
else
{
bomChild = GetTableById<BOMChild>(dr["id"].ToString());
}
bomChild.ParentNO = m_BOM.BOMNO;
bomChild.ItemNO = dr["ItemNO"].ToString();
//bomChild.ItemName = dr["ItemName"].ToString();
if (dr["Qty"] != null)
{
bomChild.Qty = Convert.ToInt32(dr["Qty"]);
}
bomChild.Unit = dr["Unit"].ToString();
bomChild.StaCode = dr["StaCode"].ToString();
bomChild.Specification = dr["Specification"].ToString();
if (dr["Scrap"] != null)
{
bomChild.Scrap = Convert.ToDouble(dr["Scrap"]);
}
bomChild.Remark = dr["remark"].ToString();
SaveOrUpdate(bomChild);
}
if (Commit() == true)
{
Flag = 1;
}
}
catch (Exception e)
{
Rollback();
}
}
catch (Exception e)
{
Flag = 0;
}
return Flag;
}
/// <summary>
/// 获取替换子表记录
/// </summary>
public DataTable SelectSubChildInfo(string bomNO,string itemNo)
{
string strSQL = @"select BOMNO,id,ItemNO,SubItemId,SubItemName, CheckDate,CheckMan,InvalidDate,State,remark
from Mes_subitem
where BOMNO = @bomNO and Itemno=@Subitemid
order by ID ";
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { bomNO, itemNo });
return dtTmp;
}
/// <summary>
/// 最大替换子表ID号
/// </summary>
public DataRow GetSubMaxChildID()
{
string strSQL = @"select Id = isnull(max(id),1)+1 from mes_bomchild ";
return SQLHelper.ExecuteDataRow(CommonInfo.Instance.DefaultConnectString,
strSQL);
}
/// <summary>
/// 根据物料编号获取BOM信息
/// </summary>
public DataTable GetBomByItemNo(string itemNo)
{
string strSQL = @"select * from dbo.MES_BOM where itemno=@ItemNo";
return SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL, itemNo);
}
/// <summary>
/// 删除物料替换子表记录
/// </summary>
public int DeleteSubChildInfo(int ID)
{
string strSQL = @"delete mes_subitem where id = @Id";
return SQLHelper.ExecuteNoneQuery(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { ID });
}
/// <summary>
/// 删除物料替换子表记录
/// </summary>
public int DeleteAllSubChildInfo(string bomNo, string subItemId)
{
string strSQL = @"delete mes_subitem where bomno=@BOMNo and subItemId = @subItemId";
return SQLHelper.ExecuteNoneQuery(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { bomNo,subItemId });
}
/// <summary>
/// Insert into Route
/// </summary>
public int ChangeSubInfo(BOM m_BOM, DataTable tblChild, Boolean IsUpdate, string @User)
{
int Flag = 0;
try
{
try
{
Begin();
foreach (DataRow dr in tblChild.Rows)
{
SubItem subItem = new SubItem();
if (dr.RowState == DataRowState.Added)
{
subItem.CreateMan = @User;
subItem.CreateTime = DateTime.Now;
}
else
{
subItem = GetTableById<SubItem>(dr["id"].ToString());
subItem.UpdateMan = @User;
subItem.UpdateTime = DateTime.Now;
}
subItem.BOMNO = m_BOM.BOMNO;
subItem.ItemNO = m_BOM.ItemNO;
subItem.SubItemName = dr["SubItemName"].ToString();
if (dr["checkdate"] != null)
{
subItem.CheckDate = Convert.ToDateTime(dr["checkdate"]);
}
subItem.SubItemId = dr["subitemid"].ToString();
if (dr["InvalidDate"] != null)
{
try
{
subItem.InvalidDate = Convert.ToDateTime("2059-12-31");
}
catch (Exception) { }
}
subItem.State = Convert.ToBoolean(dr["State"]);
subItem.Remark = dr["remark"].ToString();
subItem.CheckMan = dr["CheckMan"].ToString();
if (dr.RowState == DataRowState.Added)
{
subItem.CreateMan = @User;
subItem.CreateTime = DateTime.Now;
}
else
{
SubItem anothersubItem = GetTableById<SubItem>(dr["id"].ToString());
subItem.CreateMan = anothersubItem.CreateMan;
subItem.CreateTime = anothersubItem.CreateTime;
subItem.UpdateMan = @User;
subItem.UpdateTime = DateTime.Now;
}
SaveOrUpdate(subItem);
}
if (Commit() == true)
{
Flag = 1;
}
}
catch (Exception e)
{
Rollback();
}
}
catch (Exception e)
{
Flag = 0;
}
return Flag;
}
/// <summary>
/// 添加或更新替代料到所有的BOM含有这个物料的资料
/// </summary>
public int ChangeAllSubInfo(BOM m_BOM, DataTable tblChild, Boolean IsUpdate, string @User)
{
int Flag = 0;
try
{
try
{
//Begin();
DataTable bomDt = GetBomByItemNo(m_BOM.ItemNO);
for (int i = 0; i < bomDt.Rows.Count; i++)
{
foreach (DataRow dr in tblChild.Rows)
{
DeleteAllSubChildInfo(bomDt.Rows[i]["BOMNO"].ToString(),dr["subitemid"].ToString());
SubItem subItem = new SubItem();
//if (dr.RowState == DataRowState.Added)
//{
subItem.CreateMan = @User;
subItem.CreateTime = DateTime.Now;
//}
//else
//{
// DataTable dtSubItem = GetSubItem(bomDt.Rows[i]["BOMNO"].ToString(), dr["subitemid"].ToString());
// if (dtSubItem.Rows.Count > 0)
// {
// subItem.CreateMan = dtSubItem.Rows[0]["CreateMan"].ToString();
// subItem.CreateTime = Convert.ToDateTime(dtSubItem.Rows[0]["CreateTime"]);
subItem.UpdateMan = @User;
subItem.UpdateTime = DateTime.Now;
// }
//}
subItem.BOMNO = bomDt.Rows[i]["BOMNO"].ToString();
subItem.ItemNO = m_BOM.ItemNO;
subItem.SubItemName = dr["SubItemName"].ToString();
if (dr["checkdate"] != null)
{
subItem.CheckDate = Convert.ToDateTime(dr["checkdate"]);
}
subItem.SubItemId = dr["subitemid"].ToString();
if (dr["InvalidDate"] != null)
{
try
{
subItem.InvalidDate = Convert.ToDateTime("2059-12-31");
}
catch (Exception) { }
}
subItem.State = Convert.ToBoolean(dr["State"]);
subItem.Remark = dr["remark"].ToString();
subItem.CheckMan = dr["CheckMan"].ToString();
DSaveOrUpdate<SubItem>(subItem);
}
}
//if (Commit() == true)
//{
Flag = 1;
//}
}
catch (Exception e)
{
//Rollback();
Flag = 0;
}
}
catch (Exception e)
{
Flag = 0;
}
return Flag;
}
/// <summary>
/// 递归显示BOM树
/// </summary>
/// <param name="bom"></param>
/// <param name="nodes"></param>
public void ShowBOM(IList<CBF.Model.BOM> boms, IList<CBF.Model.BOMDisplay> listBoms,int parentId)
{
foreach (var b in boms)
{
BOMDisplay bOMToBOMDisplay = BOMToBOMDisplay(b, listBoms,parentId);
listBoms.Add(bOMToBOMDisplay);
//ICriteria criteria = this._session.CreateCriteria(typeof(CBF.Model.BOM));
//criteria.Add(Expression.In("ParentId", new List<string>() { b.Id.ToString() }));
string strSQL = string.Format(@"if exists(select * from mes_bom where parentid ='{0}')
begin
select * from mes_bom where parentid ='{1}'
end
else if exists(select * from MES_BOM where ItemNO = '{2}' and ParentID = 0)
begin
select * from MES_BOM where ParentID in(select ID from MES_BOM where ItemNO = '{3}' and ParentID = 0)
end
else
select * from mes_bom where parentid ='{4}'
", b.Id, b.Id, b.ItemNO, b.ItemNO,b.Id);
IList<CBF.Model.BOM> iboms = _session.CreateSQLQuery(strSQL).AddEntity(typeof(BOM)).List<BOM>();
if (iboms.Count > 0)
{
ShowBOM(iboms, listBoms, bOMToBOMDisplay.IId);
}
}
}
public DataTable GetSubItem(string BomNo, string ItemNO)
{
string strSQL = "select * from MES_SubItem where bomno=@BomNo and SubItemId=@ItemNo";
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { BomNo, ItemNO });
return dtTmp;
}
/// <summary>
/// 判断bom里面是否已存在当前物料
/// </summary>
/// <param name="BomNo">bom编号</param>
/// <param name="ItemNO">物料编号</param>
/// <returns></returns>
public DataTable ItemIsExist(string BomNo,string ItemNO)
{
string strSQL = "select * from dbo.FN_GetBOM (@BomNo, @ItemNo)";
DataTable dtTmp = SQLHelper.ExecuteDataTable(CommonInfo.Instance.DefaultConnectString,
strSQL, new object[] { BomNo,ItemNO });
return dtTmp;
}
/// <summary>
/// 获取ID获取根BOM
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public IList<CBF.Model.BOM> GetRootBOM(string id)
{
return _session.CreateSQLQuery("select * from mes_bom where parentid = '" + id + "'").AddEntity(typeof(BOM)).List<BOM>();
}
/// <summary>
/// BOM转换成BOMDisplay
/// </summary>
/// <param name="bom"></param>
/// <param name="boms"></param>
/// <param name="parentId"></param>
/// <returns></returns>
public BOMDisplay BOMToBOMDisplay(BOM bom, IList<BOMDisplay> boms,int parentId)
{
BOMDisplay bomDis = new BOMDisplay();
bomDis.IId = boms.Count + 1;
bomDis.Id = bom.Id;
bomDis.ItemNO = bom.ItemNO;
bomDis.ItemName = bom.ItemName;
bomDis.Unit = bom.Unit;
bomDis.Qty = bom.Qty;
bomDis.Yield = bom.Yield;
bomDis.Scrap = bom.Scrap;
bomDis.ParentId = parentId;
return bomDis;
}
/// <summary>
/// 批量删除
/// </summary>
/// <param name="ids">列表</param>
/// <returns>是否删除成功</returns>
public bool DDelete(int[] ids)
{
if (!this._session.IsOpen) this._session = NhbHelper.GetCurrentSession();
using (ITransaction tran = _session.BeginTransaction())
{
try
{
foreach (int id in ids)
{
BOM table = this._session.Get<BOM>(id);
if (!table.IsNull())
{
this._session.Delete(table);
}
}
this._session.Flush();
tran.Commit();
return true;
}
catch (Exception ex)
{
CLog.WriteLog(CMessage.ERR, ex);
tran.Rollback();
return false;
}
}
}
/// <summary>
/// 删除表(自动生成事务)
/// </summary>
/// <param name="id">ID</param>
public bool DDelete(int id)
{
if (!this._session.IsOpen) this._session = NhbHelper.GetCurrentSession();
using (ITransaction tran = _session.BeginTransaction())
{
try
{
BOM table = this._session.Get<BOM>(id);
if (!table.IsNull())
{
this._session.Delete(table);
this._session.Flush();
tran.Commit();
}
return true;
}
catch (Exception ex)
{
CLog.WriteLog(CMessage.ERR, ex);
tran.Rollback();
return false;
}
}
}
}
}
posted on 2016-04-26 10:09 chengjunde 阅读(341) 评论(0) 编辑 收藏 举报