using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.FJC.Entity.Basic;
using QMAPP.DAL;
using QMFrameWork.Log;
using System.Data;
namespace QMAPP.FJC.DAL.Basic
{
public class BomHdrDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public BomHdr Get(BomHdr model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(model);
}
return model;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-获取信息"
});
throw;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(BomHdr condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
#region 排序
//分页关键字段及排序
page.KeyName = "PID";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "UPDATEDATE DESC";
}
#endregion
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-获取列表"
});
throw;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(BomHdr condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT B.PID ");
sqlBuilder.AppendLine(" ,B.MATERIAL_CODE ");
sqlBuilder.AppendLine(" ,M.MATERIALNAME AS MATERIAL_NAME");
sqlBuilder.AppendLine(" ,B.DESCRIBE_TEXT ");
sqlBuilder.AppendLine(" ,B.DEL_FLG ");
sqlBuilder.AppendLine(" ,B.BOM_VERSION ");
sqlBuilder.AppendLine(" ,B.NODE_TYPE ");
sqlBuilder.AppendLine(" ,B.CREATEUSR ");
sqlBuilder.AppendLine(" ,B.CREATEDATE ");
sqlBuilder.AppendLine(" ,B.UPDATEUSR ");
sqlBuilder.AppendLine(" ,B.FLAGDEL ");
sqlBuilder.AppendLine(" ,B.UPDATEDATE ");
sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" FROM T_BD_BOM_HDR B ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_MATERIALINFO M ON M.MATERIALNO=B.MATERIAL_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=B.CREATEUSR ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=B.UPDATEUSR ");
whereBuilder.Append(" AND B.FLAGDEL='0'");
//查询条件
//BOM
if (string.IsNullOrEmpty(condition.DEL_FLG) == false)
{
whereBuilder.Append(" AND B.DEL_FLG= @DEL_FLG ");
parameters.Add(new DataParameter { ParameterName = "DEL_FLG", DataType = DbType.String, Value = condition.DEL_FLG });
}
//物料号
if (string.IsNullOrEmpty(condition.MATERIAL_CODE) == false)
{
whereBuilder.Append(" AND B.MATERIAL_CODE LIKE @MATERIAL_CODE ");
parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = "%" + condition.MATERIAL_CODE + "%" });
}
//BOM版本
if (string.IsNullOrEmpty(condition.BOM_VERSION) == false)
{
whereBuilder.Append(" AND B.BOM_VERSION= @BOM_VERSION ");
parameters.Add(new DataParameter { ParameterName = "BOM_VERSION", DataType = DbType.String, Value = condition.BOM_VERSION });
}
//BOM类型
if (string.IsNullOrEmpty(condition.NODE_TYPE) == false)
{
whereBuilder.Append(" AND B.NODE_TYPE= @NODE_TYPE ");
parameters.Add(new DataParameter { ParameterName = "NODE_TYPE", DataType = DbType.String, Value = condition.NODE_TYPE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 物料号信息是否重复
///
/// 判断是否存在
///
///
/// true:已存在;fasel:不存在。
public bool ExistsBomHdr(BomHdr model)
{
string PID = "";
int count = 0;
List parameters = new List();
StringBuilder sqlBuilder = new StringBuilder();
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
sqlBuilder.Append(" SELECT COUNT(*) FROM T_BD_BOM_HDR WHERE PID <> @PID AND MATERIAL_CODE =@MATERIAL_CODE ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = model.MATERIAL_CODE });
if (this.BaseSession != null)
{
count = Convert.ToInt32(BaseSession.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray()));
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray()));
}
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-信息是否重复"
});
throw;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(BomHdr model)
{
int count = 0;
try
{
if (this.BaseSession != null)
{
//插入基本信息
count = BaseSession.Insert(model);
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-插入信息"
});
throw;
}
}
#endregion
#region 删除Bom信息
///
/// 删除信息
///
/// Bom信息(ID)
/// 删除个数
public int DeleteBomHdr(BomHdr model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
if (this.BaseSession != null)
{
//删除基本信息
count = BaseSession.Delete(model);
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count = session.Delete(model);
}
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-删除Bom信息"
});
throw;
}
}
#endregion
#region Bom信息下拉
///
///
///
///
///
public List GetBomHdrList(BomHdr condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "BOM信息-列表"
});
throw;
}
}
#endregion
#region 配置BOM明细
#region 获取BOM明细列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage BomHdrConfigList(BomDetail condition, DataPage page)
{
string sql = "";
List parameters = new List();
try
{
sql = this.GetBomDetailListSql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "UPDATEDATE DESC";
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-获取列表"
});
throw;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetBomDetailListSql(BomDetail condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT B.PID ");
sqlBuilder.AppendLine(" ,B.BOM_HDR_PID ");
sqlBuilder.AppendLine(" ,B.ITEM_CODE ");
sqlBuilder.AppendLine(" ,B.MATERIAL_CODE ");
sqlBuilder.AppendLine(" ,B.MATERIAL_LAYER ");
sqlBuilder.AppendLine(" ,B.IS_ASSY ");
sqlBuilder.AppendLine(" ,B.UNIT_CODE ");
sqlBuilder.AppendLine(" ,B.ITEM_QTY ");
sqlBuilder.AppendLine(" ,B.REMARK ");
sqlBuilder.AppendLine(" ,B.FLAGDEL ");
sqlBuilder.AppendLine(" ,B.CREATEUSR ");
sqlBuilder.AppendLine(" ,B.CREATEDATE ");
sqlBuilder.AppendLine(" ,B.UPDATEUSR ");
sqlBuilder.AppendLine(" ,B.UPDATEDATE ");
sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" FROM T_BD_BOM_DETAIL B ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_BOM_HDR BH ON BH.PID=B.BOM_HDR_PID ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=B.CREATEUSR ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=B.UPDATEUSR ");
//查询条件
if (string.IsNullOrEmpty(condition.BOM_HDR_PID) == false)
{
whereBuilder.Append(" AND BH.PID= @BOM_HDR_PID ");
parameters.Add(new DataParameter { ParameterName = "BOM_HDR_PID", DataType = DbType.String, Value = condition.BOM_HDR_PID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取Bom明细列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public List BomDetailList(BomDetail condition)
{
string sql = "";
List parameters = new List();
List list = new List();
try
{
sql = this.GetBomDetailListSql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-获取Bom明细列表"
});
throw;
}
}
#endregion
#region 插入Bom明细信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int InsertBomDetail(BomDetail model)
{
int count = 0;
try
{
if (this.BaseSession != null)
{
//插入基本信息
count = BaseSession.Insert(model);
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-插入Bom明细信息"
});
throw;
}
}
#endregion
#region 获取Bom明细
///
/// 获取Bom明细
///
/// 条件
/// *信息
public BomDetail GetBomDetail(BomDetail model)
{
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(model);
}
return model;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-获取Bom明细"
});
throw;
}
}
#endregion
#region 删除Bom明细
///
/// 删除信息
///
/// Bom信息(ID)
/// 删除个数
public int DeleteBomDetail(BomDetail model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
StringBuilder whereBuilder = new StringBuilder();
int count = 0;
try
{
//构成查询语句
sqlBuilder.AppendLine(" DELETE FROM T_BD_BOM_DETAIL ");
if (string.IsNullOrEmpty(model.PID) == false)
{
whereBuilder.Append(" AND PID=@PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
}
if (string.IsNullOrEmpty(model.BOM_HDR_PID) == false)
{
whereBuilder.Append(" AND BOM_HDR_PID=@BOM_HDR_PID ");
parameters.Add(new DataParameter { ParameterName = "BOM_HDR_PID", DataType = DbType.String, Value = model.BOM_HDR_PID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
if (this.BaseSession != null)
{
//删除基本信息
count = BaseSession.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());
}
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "Bom信息维护数据层-删除"
});
throw;
}
}
#endregion
#endregion
}
}