using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMFrameWork.Log;
using QMAPP.DAL;
using QMAPP.FJC.Entity.Basic;
using System.Data;
using QMAPP.FJC.Entity;
namespace QMAPP.FJC.DAL.Basic
{
public class ProcessRouteDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public ProcessRoute Get(ProcessRoute 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(ProcessRoute 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("AREA") > -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;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(ProcessRoute condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine("SELECT T.PID ");
sqlBuilder.AppendLine(" ,T.ROUTE_CODE ");
sqlBuilder.AppendLine(" ,T.ROUTE_NAME ");
sqlBuilder.AppendLine(" ,T.REMARK ");
sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,T.CREATEDATE ");
sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" ,T.UPDATEDATE ");
sqlBuilder.AppendLine(" ,T.FLAGDEL ");
sqlBuilder.AppendLine(" FROM T_BD_PROCESS_ROUTE T "); ;
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=T.CREATEUSR ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=T.UPDATEUSR ");
whereBuilder.Append(" AND T.FLAGDEL='0'");
//查询条件
//工艺路线编码
if (string.IsNullOrEmpty(condition.ROUTE_CODE) == false)
{
whereBuilder.Append(" AND T.ROUTE_CODE = @ROUTE_CODE ");
parameters.Add(new DataParameter { ParameterName = "ROUTE_CODE", DataType = DbType.String, Value = condition.ROUTE_CODE });
}
//工艺路线名称
if (string.IsNullOrEmpty(condition.ROUTE_NAME) == false)
{
whereBuilder.Append(" AND T.ROUTE_NAME LIKE '%'+" + "@ROUTE_NAME" + "+'%'");
parameters.Add(new DataParameter { ParameterName = "ROUTE_NAME", DataType = DbType.String, Value = condition.ROUTE_NAME });
}
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 ExistsProcessRoute(ProcessRoute 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_PROCESS_ROUTE WHERE PID <> @PID AND ROUTE_CODE=@ROUTE_CODE AND FLAGDEL<>1 ";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("PID", PID),
new DataParameter { ParameterName = "ROUTE_CODE", Value = model.ROUTE_CODE }));
}
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(ProcessRoute 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(ProcessRoute 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(ProcessRoute model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
sqlBuilder.Append("UPDATE T_BD_PROCESS_ROUTE ");
sqlBuilder.Append("SET FLAGDEL = '1'");
sqlBuilder.Append("WHERE PID = @PID");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
if (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 = "工艺路线数据层-删除信息"
});
throw;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetProcessRouteWorkCellData(ProcessRouteWorkCell condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetProcessRouteWorkCellSql(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 = "工艺路线数据层-获取列表"
});
throw;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetProcessRouteWorkCellSql(ProcessRouteWorkCell condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine(@" SELECT A.PID
,A.ROUTE_PID
,B.ROUTE_CODE
,B.ROUTE_NAME
,A.WORKCELL_PID
,C.PROCESSNAME AS WORKCELL_NAME
,A.REMARK
,A.OPT_STANDARD
,A.BACKFLUSH
,A.CREATEUSR
,A.CREATEDATE
,A.UPDATEUSR
,A.UPDATEDATE
,E.USERNAME AS CREATEUSERNAME
,U.USERNAME AS UPDATEUSERNAME
,A.FLAGDEL
,STUFF((SELECT ','+RTRIM(S.PRE_WORKCELL_CODE)+'/'+S.PRE_WORKCELL_NAME FROM
T_BD_PROCESS_ROUTE_WORKCELL_SEQ S
WHERE S.ROUTE_PID=A.ROUTE_PID
AND S.PRW_PID=A.PID
AND FLAGDEL='0' FOR XML PATH('')),1,1,'') AS PARWORKCELLCODE
FROM T_BD_PROCESS_ROUTE_WORKCELL A
LEFT JOIN T_BD_PROCESS_ROUTE B ON B.PID=A.ROUTE_PID
LEFT JOIN T_BD_PROCESSINFO C ON C.PID=A.WORKCELL_PID
LEFT JOIN T_QM_USER E ON E.USERID=A.CREATEUSR
LEFT JOIN T_QM_USER U ON U.USERID=A.UPDATEUSR ");
if (string.IsNullOrEmpty(condition.ROUTE_PID) == false)
{
whereBuilder.Append(" AND A.ROUTE_PID = @ROUTE_PID");
parameters.Add(new DataParameter { ParameterName = "ROUTE_PID", DataType = DbType.String, Value = condition.ROUTE_PID });
}
whereBuilder.Append(" AND A.FLAGDEL<>1 ");
//查询条件
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 ExistsProcess(ProcessRouteWorkCell model)
{
List parameters = new List();
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
int count = 0;
try
{
sqlBuilder.Append("SELECT COUNT(*) FROM T_BD_PROCESS_ROUTE_WORKCELL T ");
//查询条件
if (string.IsNullOrEmpty(model.ROUTE_PID) == false)
{
whereBuilder.Append(" AND T.ROUTE_PID = @ROUTE_PID");
parameters.Add(new DataParameter { ParameterName = "ROUTE_PID", DataType = DbType.String, Value = model.ROUTE_PID });
}
if (string.IsNullOrEmpty(model.WORKCELL_PID) == false)
{
whereBuilder.Append(" AND T.WORKCELL_PID = @WORKCELL_PID");
parameters.Add(new DataParameter { ParameterName = "WORKCELL_PID", DataType = DbType.String, Value = model.WORKCELL_PID });
}
whereBuilder.Append(" AND T.FLAGDEL<>1 ");
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
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 = "工艺路线数据层-判断工序编号是否存在"
});
throw;
}
}
#endregion
#region 插入工序配置信息
///
/// 插入工序配置信息(单表)
///
/// 工序配置信息
/// 插入行数
public int InsertProcessRouteWorkCell(ProcessRouteWorkCell model)
{
int count = 0;
try
{
if (BaseSession != null)
{
//插入基本信息
count = BaseSession.Insert(model);
}
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 InsertProcessRouteWorkCellSeq(List list)
{
int count = 0;
try
{
if (BaseSession != null)
{
//插入基本信息
count = BaseSession.Insert(list);
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(list);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "工艺路线数据层-插入工序与前置工序中间表"
});
throw;
}
}
#endregion
#region 删除工序配置信息
///
/// 删除工序配置信息
///
/// 工序配置信息(ID)
/// 删除个数
public int DeleteProcessRouteWorkCell(ProcessRouteWorkCell model)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
sqlBuilder.Append(" UPDATE T_BD_PROCESS_ROUTE_WORKCELL SET FLAGDEL=@FLAGDEL ");
parameters.Add(new DataParameter() { ParameterName = "FLAGDEL", DataType = DbType.String, Value = EnumGeter.FLAGDEL.YES.GetHashCode().ToString() });
if (string.IsNullOrEmpty(model.ROUTE_PID) == false)
{
whereBuilder.Append(" AND ROUTE_PID=@ROUTE_PID");
parameters.Add(new DataParameter() { ParameterName = "ROUTE_PID", DataType = DbType.String, Value = model.ROUTE_PID });
}
if (string.IsNullOrEmpty(model.PID) == false)
{
whereBuilder.Append(" AND PID=@PID");
parameters.Add(new DataParameter() { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
}
//查询条件
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
if (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 = "工艺路线数据层-删除工序配置信息"
});
throw;
}
}
#endregion
#region 删除工序与前置工序中间表
///
/// 删除工序与前置工序中间表
///
/// 工序配置信息(ID)
/// 删除个数
public int DeleteProcessRouteWorkCellSeq(ProcessRouteWorkCell model)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
sqlBuilder.Append(" UPDATE T_BD_PROCESS_ROUTE_WORKCELL_SEQ SET FLAGDEL=@FLAGDEL ");
parameters.Add(new DataParameter() { ParameterName = "FLAGDEL", DataType = DbType.String, Value = EnumGeter.FLAGDEL.YES.GetHashCode().ToString() });
if (string.IsNullOrEmpty(model.ROUTE_PID) == false)
{
whereBuilder.Append(" AND ROUTE_PID=@ROUTE_PID");
parameters.Add(new DataParameter() { ParameterName = "ROUTE_PID", DataType = DbType.String, Value = model.ROUTE_PID });
}
if (string.IsNullOrEmpty(model.PID) == false)
{
whereBuilder.Append(" AND PRW_PID=@PRW_PID");
parameters.Add(new DataParameter() { ParameterName = "PRW_PID", DataType = DbType.String, Value = model.PID });
}
//查询条件
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
if (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 = "工艺路线数据层-删除工序与前置工序中间表"
});
throw;
}
}
#endregion
#region 获取工艺路线下拉列表
///
/// 获取工艺路线列表(下拉列表数据源)
///
/// 条件
/// 全部集合
public List GetProcessRouteList(ProcessRoute 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
public string GetRouteCodeByWorkcell(string workcellcode)
{
List parameters = new List();
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT MAX(ROUTE_CODE) FROM T_MD_PROCESS_ROUTE_WORKCELL WHERE WORKCELL_CODE=@workcellcode");
parameters.Add(new DataParameter("workcellcode", workcellcode));
using (var session = AppDataFactory.CreateMainSession())
{
return session.ExecuteSqlScalar(sql.ToString(), parameters.ToArray()) + "";
}
}
}
}