using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
using QMFrameWork.Log;
using System.Data;
using QMAPP.DAL;
namespace QMAPP.MD.DAL
{
///
/// 模块名称:物料分类
/// 作 者:郭兆福
/// 编写日期:2017年05月10日
///
public class MaterialClassDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public MaterialClass Get(MaterialClass model)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_MD_MATERIAL_CLASS WHERE '1'='1'";
if (string.IsNullOrEmpty(model.PID) == false)
{
sql += " AND PID = @PID";
parameters.Add(new DataParameter("PID", model.PID));
}
if (string.IsNullOrEmpty(model.MATERIAL_TYPE_CODE) == false)
{
sql += " AND MATERIAL_TYPE_CODE = @MATERIAL_TYPE_CODE";
parameters.Add(new DataParameter("MATERIAL_TYPE_CODE", model.MATERIAL_TYPE_CODE));
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sqlChange = this.ChangeSqlByDB(sql, session);
//获取信息
model = session.Get(sqlChange, parameters.ToArray());
}
return model;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料号信息数据层-获取信息");
throw ex;
}
}
#endregion
#region 获取分页列表
///
/// 获取分页列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(MaterialClass 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)
{
RecordExceptionLog(ex, "物料号类型信息数据层-获取分页列表");
throw ex;
}
}
///
/// 获取列表
///
/// 条件
/// 全部集合
public List GetList(MaterialClass 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(MaterialClass condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine(" SELECT M.PID ");
sqlBuilder.AppendLine(" ,M.FACTORY_CODE ");
sqlBuilder.AppendLine(" ,F.FACTORY_NAME ");
sqlBuilder.AppendLine(" ,M.MATERIAL_TYPE_CODE ");
sqlBuilder.AppendLine(" ,M.MATERIAL_TYPE_NAME ");
sqlBuilder.AppendLine(" ,M.MATERIAL_ATTRIBUTE ");
sqlBuilder.AppendLine(" ,M.LAYER_NUM ");
sqlBuilder.AppendLine(" ,M.SEQ_NUM ");
sqlBuilder.AppendLine(" ,M.UP_MATERIAL_TYPE_CODE ");
sqlBuilder.AppendLine(" ,M.REMARK ");
sqlBuilder.AppendLine(" ,M.FLGDEL ");
sqlBuilder.AppendLine(" ,M.CREATEUSER ");
sqlBuilder.AppendLine(" ,M.CREATEDATE ");
sqlBuilder.AppendLine(" ,M.UPDATEUSER ");
sqlBuilder.AppendLine(" ,M.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_MD_MATERIAL_CLASS M ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=M.FACTORY_CODE ");
whereBuilder.Append(" AND M.FLGDEL='0'");
//查询条件
//工厂
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND M.FACTORY_CODE = @FACTORY_CODE");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
//物料分类编号
if (string.IsNullOrEmpty(condition.MATERIAL_TYPE_CODE) == false)
{
whereBuilder.Append(" AND MATERIAL_TYPE_CODE = @MATERIAL_TYPE_CODE");
parameters.Add(new DataParameter { ParameterName = "MATERIAL_TYPE_CODE", DataType = DbType.String, Value = condition.MATERIAL_TYPE_CODE });
}
if (string.IsNullOrEmpty(condition.MATERIAL_ATTRIBUTE) == false)
{
whereBuilder.Append(" AND MATERIAL_ATTRIBUTE = @MATERIAL_ATTRIBUTE");
parameters.Add(new DataParameter { ParameterName = "MATERIAL_ATTRIBUTE", DataType = DbType.String, Value = condition.MATERIAL_ATTRIBUTE });
}
if (string.IsNullOrEmpty(condition.MaterialAttributes) == false)
{
whereBuilder.Append(" AND MATERIAL_ATTRIBUTE IN "+condition.MaterialAttributes+"");
//parameters.Add(new DataParameter { ParameterName = "MaterialAttributes", DataType = DbType.String, Value = condition.MaterialAttributes });
}
if (string.IsNullOrEmpty(condition.UpTypeCodes) == false)
{
whereBuilder.Append(" AND UP_MATERIAL_TYPE_CODE IN " + condition.UpTypeCodes + "");
//parameters.Add(new DataParameter { ParameterName = "MaterialAttributes", DataType = DbType.String, Value = condition.MaterialAttributes });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取同一级别的所有组织机构树菜单列表
///
/// 获取同一级别的所有组织机构树菜单列表
///
/// 条件
/// 同级别菜单列表
public List GetSubTreeList(MaterialClass materialclass)
{
List parameters = new List();
List list = null;
string sql = null;
try
{
sql = "SELECT * FROM T_MD_MATERIAL_CLASS WHERE UP_MATERIAL_TYPE_CODE=@UP_MATERIAL_TYPE_CODE";
parameters.Add(new DataParameter { ParameterName = "UP_MATERIAL_TYPE_CODE", DataType = DbType.String, Value = materialclass.UP_MATERIAL_TYPE_CODE });
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sql, session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region 物料类型下拉
///
///
///
///
///
public List GetMaterialClassList(MaterialClass condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sqlChange = ChangeSqlByDB(sql, session);
list = session.GetList(sqlChange, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料信息-列表");
throw ex;
}
}
#endregion
#region 物料类型下拉(winform注塑下来)
///
///
///
///
///
public List GetMaterialClassWithType(MaterialClass condition, WorkCell workcellmodel)
{
List list = new List();
string sql = null;
List parameters = new List();
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.AppendFormat(@"SELECT DISTINCT MC.*
FROM T_MD_WORKCELL WC ,T_MD_PROCESS_ROUTE_WORKCELL PRW,T_MD_PROCESS_ROUTE PR
,T_MD_MATERIAL_ROUTE MR
,T_MD_PBOM MP
,T_MD_MATERIAL M
,T_MD_MATERIAL_CLASS MC
WHERE WC.WORKCENTER_CODE IN (SELECT DISTINCT WORKCENTER FROM T_PP_ORDERINDENTITY WHERE ORDER_TYPE='{0}' ) AND
WC.WORKCELL_CODE=PRW.WORKCELL_CODE and PRW.ROUTE_CODE=PR.ROUTE_CODE
AND PR.ROUTE_CODE=MR.ROUTE_CODE
and MR.MATERIAL_CODE=MP.MATERIAL_CODE
AND MP.MATERIAL_CODE=M.MATERIAL_CODE
AND M.MATERIAL_TYPE_CODE=MC.MATERIAL_TYPE_CODE
AND MC.MATERIAL_ATTRIBUTE='1'", workcellmodel.WORKCELL_CODE);
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料信息-列表");
throw ex;
}
}
public List GetMaterialClassForOrder(string ordertype)
{
List list = new List();
string sql = null;
List parameters = new List();
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.AppendFormat(@"SELECT MC.*
FROM [T_MD_MATERIAL_CLASS] AS MC
LEFT JOIN [T_PP_ORDERINDENTITY] AS OI
ON OI.[MATERIALTYPE]=MC.[MATERIAL_TYPE_CODE]
WHERE OI.ORDER_TYPE='{0}'", ordertype);
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料信息-列表");
throw ex;
}
}
#endregion
#region 物料类型下拉(winform批量报废)
///
///
///
///
///
public List GetMaterialClassToMendRecorderWasteForm()
{
List list = new List();
string sql = null;
List parameters = new List();
StringBuilder sqlBuilder = new StringBuilder();
//查询父类字段UP_MATERIAL_TYPE_CODE里没有MATERIAL_TYPE_CODE的数据-最底层数据
sqlBuilder.AppendFormat(@"
SELECT * FROM [dbo].[T_MD_MATERIAL_CLASS]
where FLGDEL='0' AND MATERIAL_TYPE_CODE not in
( SELECT UP_MATERIAL_TYPE_CODE FROM [dbo].[T_MD_MATERIAL_CLASS]
group by UP_MATERIAL_TYPE_CODE )");
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料信息-列表");
throw ex;
}
}
#endregion
#region 获取SEQ_NUM最大号
///
/// 获取信息
///
/// 条件
/// *信息
public MaterialClass GetMaxSeqNum(MaterialClass model)
{
try
{
List parameters = new List();
var sql = " SELECT max(seq_num) as seq_num FROM T_MD_MATERIAL_CLASS where UP_MATERIAL_TYPE_CODE='" + model.UP_MATERIAL_TYPE_CODE + "' ";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.Get(sql, parameters.ToArray());
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 物料号信息
/// 插入行数
public int Insert(MaterialClass model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料号信息数据层-插入信息");
throw ex;
}
}
#endregion
#region 删除信息
///
/// 删除信息
///
/// 信息
/// 插入行数
public int Delete(string pid)
{
List parameters = new List();
int count = 0;
try
{
var sql = "delete from T_MD_MATERIAL_CLASS WHERE PID = '" + pid + "'";
if (this.BaseSession != null)
{
//删除基本信息
count = BaseSession.ExecuteSql(sql, parameters.ToArray());
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count = session.ExecuteSql(sql, parameters.ToArray());
}
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "删除物料类型信息");
throw ex;
}
}
#endregion
}
}