using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.FJC.Entity.Basic;
using QMFrameWork.Data;
using System.Data;
using QMFrameWork.Log;
namespace QMAPP.FJC.DAL.Basic
{
///
/// 模块编号:M2-1
/// 作 用:物料号信息数据层
/// 作 者:王丹丹
/// 编写日期:2015年05月26日
///
public class MaterielDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public Materiel Get(Materiel model)
{
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 = "物料号信息数据层-获取信息"
});
throw;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Materiel 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";
}
else
{
//按照标题排序
if (page.SortExpression.IndexOf("PROCESSTYPETXT") > -1)
{
page.SortExpression = page.SortExpression.Replace("PROCESSTYPETXT", "PROCESSTYPE");
}
if (page.SortExpression.IndexOf("MATERIALTYPETXT") > -1)
{
page.SortExpression = page.SortExpression.Replace("MATERIALTYPETXT", "MATERIALTYPE");
}
}
#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 = "物料号信息数据层-获取列表"
});
throw;
}
}
///
/// 获取列表
///
/// 条件
/// 全部集合
public List GetList(Materiel 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 = "物料号信息数据层-获取列表"
});
throw;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(Materiel condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine(" SELECT M.PID ");
sqlBuilder.AppendLine(" ,M.MATERIALNAME ");
sqlBuilder.AppendLine(" ,M.MATERIALNO ");
sqlBuilder.AppendLine(" ,M.MATERIALTYPE ");
sqlBuilder.AppendLine(" ,M.PROCESSTYPE ");
sqlBuilder.AppendLine(" ,M.WEIGHT ");
sqlBuilder.AppendLine(" ,M.PRODUCELINE ");
sqlBuilder.AppendLine(" ,M.COLOR ");
sqlBuilder.AppendLine(" ,M.HAndL ");
sqlBuilder.AppendLine(" ,M.LIMITTIME ");
sqlBuilder.AppendLine(" ,M.MEMO ");
sqlBuilder.AppendLine(" ,M.CREATEUSER ");
sqlBuilder.AppendLine(" ,M.CREATEDATE ");
sqlBuilder.AppendLine(" ,M.UPDATEUSER ");
sqlBuilder.AppendLine(" ,M.UPDATEDATE ");
sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" FROM T_BD_MATERIALINFO M ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=M.CREATEUSER ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=M.UPDATEUSER ");
//查询条件
//物料号
if (string.IsNullOrEmpty(condition.MATERIALNO) == false)
{
whereBuilder.Append(" AND MATERIALNO LIKE '%'+" + "@MATERIALNO" + "+'%'");
parameters.Add(new DataParameter { ParameterName = "MATERIALNO", DataType = DbType.String, Value = condition.MATERIALNO });
}
//物料名称
if (string.IsNullOrEmpty(condition.MATERIALNAME) == false)
{
whereBuilder.Append(" AND MATERIALNAME LIKE '%'+" + "@MATERIALNAME" + "+'%'");
parameters.Add(new DataParameter { ParameterName = "MATERIALNAME", DataType = DbType.String, Value = condition.MATERIALNAME });
}
//物料类别
if (string.IsNullOrEmpty(condition.MATERIALTYPE) == false)
{
whereBuilder.Append(" AND MATERIALTYPE = @MATERIALTYPE ");
parameters.Add(new DataParameter { ParameterName = "MATERIALTYPE", DataType = DbType.String, Value = condition.MATERIALTYPE });
}
//工序类别
if (string.IsNullOrEmpty(condition.PROCESSTYPE) == false)
{
whereBuilder.Append(" AND PROCESSTYPE = @PROCESSTYPE ");
parameters.Add(new DataParameter { ParameterName = "PROCESSTYPE", DataType = DbType.String, Value = condition.PROCESSTYPE });
}
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 ExistsMateriel(Materiel model)
{
string PID = "";
int count = 0;
string sql = null;
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
sql = "SELECT COUNT(*) FROM T_BD_MATERIALINFO WHERE PID <> @PID AND MATERIALNO=@MATERIALNO";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("PID", PID),
new DataParameter { ParameterName = "MATERIALNO", Value = model.MATERIALNO }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "物料号信息数据层-判断名称是否存在"
});
throw;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 物料号信息
/// 插入行数
public int Insert(Materiel model)
{
int count = 0;
try
{
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 = "物料号信息数据层-插入信息"
});
throw;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
/// 物料号信息
/// 更新行数
public int Update(Materiel model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "物料号信息数据层-更新信息"
});
throw;
}
}
#endregion
#region 删除信息
///
/// 删除信息
///
/// 物料号信息(ID)
/// 删除个数
public int Delete(Materiel model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
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 = "物料号信息数据层-删除信息"
});
throw;
}
}
#endregion
#region 物料号是否使用
///
/// 物料号是否使用
///
///
/// true:已使用;fasel:未使用。
public bool IsUsing(Materiel model)
{
int count = 0;
string sql = null;
StringBuilder sqlBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine(" SELECT COUNT(*) FROM T_AW_INJECTIONRECORDER ");
sqlBuilder.AppendLine(" WHERE MATERIALCODE=(SELECT MATERIALNO ");
sqlBuilder.AppendLine(" FROM T_BD_MATERIALINFO WHERE PID=@PID ) ");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(),
new DataParameter { ParameterName = "PID", Value = model.PID }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "物料号信息数据层-物料号是否使用"
});
throw;
}
}
#endregion
#region 物料信息下拉
///
///
///
///
///
public List GetMaterielList(Materiel 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 = "物料信息-列表"
});
throw;
}
}
#endregion
#region PBOM头物料信息下拉
///
/// PBOM头物料信息下拉
///
///
///
public List GetPbomMaterialList(Materiel condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetPbomMaterialQuerySql(ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取PBOM头物料查询语句
///
/// 获取PBOM头物料查询语句
///
/// 参数
/// 查询语句
private string GetPbomMaterialQuerySql(ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine(" SELECT M.PID ");
sqlBuilder.AppendLine(" ,M.MATERIAL_CODE as MATERIALNO ");
sqlBuilder.AppendLine(" ,M.MATERIAL_NAME as MATERIALNAME ");
sqlBuilder.AppendLine(" FROM T_MD_MATERIAL M ");
sqlBuilder.AppendLine(" JOIN T_MD_PBOM T ON M.MATERIAL_CODE = T.MATERIAL_CODE AND T.FLGDEL='0' ");
whereBuilder.Append(" AND M.FLGDEL='0'");
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}