using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
using QMAPP.DAL;
using QMFrameWork.Log;
using System.Data;
namespace QMAPP.MD.DAL
{
///
/// 模块名称:工作中心
/// 作 者:郭兆福
/// 编写日期:2017年05月09日
///
public class WorkCenterDAL : BaseDAL
{
#region 判断工作中心是否已被使用
///
/// 判断工作中心是否已被使用
///
/// 工作中心信息
/// 需要校验表集合
/// 是否是删除
/// true:使用 false:未使用
public bool IsWorkCenterUsed(string[] workCenterAry, List tableList, bool isDel)
{
int count = 0;
StringBuilder sqlBuilder = new StringBuilder();
try
{
List param = new List();
// 创建检索条件
string sqlWhere = CreateSearchCondition(workCenterAry, isDel, ref param);
// 删除情况 根据主键先取出工厂编码
if (isDel == true)
{
sqlBuilder.AppendLine(" WITH P ");
sqlBuilder.AppendLine(" AS ( SELECT WORKCENTER_CODE FROM T_MD_WORKCENTER WHERE PID IN (");
for (int i = 0; i < workCenterAry.Length; i++)
{
if (i != 0)
{
sqlBuilder.AppendLine(" , ");
}
String paramname = "PID" + i.ToString();
sqlBuilder.AppendLine("@" + paramname);
param.Add(new DataParameter { ParameterName = paramname, Value = workCenterAry[i] });
}
sqlBuilder.AppendLine(" ))");
}
// 相关表的连接
for (int i = 0; i < tableList.Count; i++)
{
if (i == 0)
{
sqlBuilder.AppendLine(" SELECT COUNT(*) FROM ( ");
}
else
{
sqlBuilder.AppendLine(" UNION ALL ");
}
sqlBuilder.AppendLine(" SELECT A.WORKCENTER_CODE FROM ");
sqlBuilder.AppendLine(tableList[i] + " A ");
// 每个表追加检索条件
sqlBuilder.AppendLine(sqlWhere.ToString());
if (i == tableList.Count - 1)
{
sqlBuilder.AppendLine(" ) AS TEMPA ");
}
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange,
param.ToArray()));
}
return count == 0 ? false : true;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 创建检索条件
///
/// 工作中心信息
/// 是否是删除
/// 参数信息
/// 检索条件
private string CreateSearchCondition(string[] workCenterAry, bool isDel, ref List parameters)
{
// 组织检索条件
StringBuilder sqlBuilderWhere = new StringBuilder();
if (isDel == true)
{
sqlBuilderWhere.AppendLine(" , P B ");
sqlBuilderWhere.AppendLine(" WHERE A.FLGDEL = '0'");
sqlBuilderWhere.AppendLine(" AND A.WORKCENTER_CODE = B.WORKCENTER_CODE");
}
else
{
sqlBuilderWhere.AppendLine(" WHERE A.FLGDEL = '0'");
for (int i = 0; i < workCenterAry.Length; i++)
{
if (i == 0)
{
sqlBuilderWhere.AppendLine(" AND A.WORKCENTER_CODE IN ( ");
}
else
{
sqlBuilderWhere.AppendLine(" , ");
}
String paramname = "WORKCENTER_CODE" + i.ToString();
sqlBuilderWhere.AppendLine("@" + paramname);
if (i == workCenterAry.Length - 1)
{
sqlBuilderWhere.AppendLine(" ) ");
}
parameters.Add(new DataParameter { ParameterName = paramname, Value = workCenterAry[i] });
}
}
return sqlBuilderWhere.ToString();
}
#endregion
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public WorkCenter Get(WorkCenter model)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_MD_WORKCENTER WHERE '1'='1'";
if (string.IsNullOrEmpty(model.PID) == false)
{
sql += " AND PID = @PID";
parameters.Add(new DataParameter("PID", model.PID));
}
if (string.IsNullOrEmpty(model.WORKCENTER_CODE) == false)
{
sql += " AND WORKCENTER_CODE = @WORKCENTER_CODE";
parameters.Add(new DataParameter("WORKCENTER_CODE", model.WORKCENTER_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(WorkCenter condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
if (string.IsNullOrEmpty(page.SortExpression))
{
page.SortExpression = "FACTORY_CODE ASC";
}
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
page = session.GetDataPage(sqlChange, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工作中心数据层-获取列表");
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(WorkCenter condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT W.PID ");
sqlBuilder.AppendLine(" ,W.WORKCENTER_CODE ");
sqlBuilder.AppendLine(" ,W.CENTER_TYPE ");
sqlBuilder.AppendLine(" ,W.WORKCENTER_NAME ");
sqlBuilder.AppendLine(" ,W.FACTORY_CODE ");
sqlBuilder.AppendLine(" ,F.FACTORY_NAME ");
sqlBuilder.AppendLine(" ,W.REMARK ");
sqlBuilder.AppendLine(" ,W.FLGDEL ");
sqlBuilder.AppendLine(" ,W.CREATEUSER ");
sqlBuilder.AppendLine(" ,W.CREATEDATE ");
sqlBuilder.AppendLine(" ,W.UPDATEUSER ");
sqlBuilder.AppendLine(" ,W.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_MD_WORKCENTER W ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=W.FACTORY_CODE ");
whereBuilder.Append(" AND W.FLGDEL='0'");
//查询条件
if (string.IsNullOrEmpty(condition.CENTER_TYPE) == false)
{
whereBuilder.Append(" AND CENTER_TYPE = @CENTER_TYPE ");
parameters.Add(new DataParameter { ParameterName = "CENTER_TYPE", DataType = DbType.String, Value = condition.CENTER_TYPE });
}
if (string.IsNullOrEmpty(condition.WORKCENTER_NAME) == false)
{
whereBuilder.Append(" AND W.WORKCENTER_NAME LIKE @WORKCENTER_NAME ");
parameters.Add(new DataParameter { ParameterName = "WORKCENTER_NAME", DataType = DbType.String, Value = EscapeValue(condition.WORKCENTER_NAME) + "%" });
}
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND W.FACTORY_CODE = @FACTORY_CODE ");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
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 ExistsWorkCenter(WorkCenter model)
{
string PID = "";
int count = 0;
StringBuilder sqlBuilder = new StringBuilder();
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
sqlBuilder.AppendLine("SELECT COUNT(*) FROM T_MD_WORKCENTER ");
sqlBuilder.AppendLine(" WHERE PID <> @PID AND WORKCENTER_CODE=@WORKCENTER_CODE AND FACTORY_CODE=@FACTORY_CODE");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange,
new DataParameter("PID", PID),
new DataParameter("WORKCENTER_CODE", model.WORKCENTER_CODE),
new DataParameter("FACTORY_CODE", model.FACTORY_CODE)));
}
return count > 0;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(WorkCenter model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
///
/// 更新行数
public int Update(WorkCenter model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 删除
///
/// 删除信息
///
/// 删除的主键
/// 用户ID
/// 删除个数
public int Delete(string[] pidary, string userID)
{
if (null == pidary || pidary.Length == 0)
{
return 0;
}
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("UPDATE T_MD_WORKCENTER ");
sqlBuilder.Append("SET FLGDEL = '1'");
sqlBuilder.Append(" ,UPDATEUSER = @UPDATEUSER ");
sqlBuilder.Append(" ,UPDATEDATE = @UPDATEDATE ");
sqlBuilder.Append(" WHERE ");
for (int i = 0; i < pidary.Length; i++)
{
if (i == 0)
{
sqlBuilder.Append(" PID IN ( ");
}
else
{
sqlBuilder.Append(" , ");
}
string paraname = "PID" + i.ToString();
sqlBuilder.Append("@" + paraname);
if (i == pidary.Length - 1)
{
sqlBuilder.Append(" ) ");
}
parameters.Add(new DataParameter { ParameterName = paraname, DataType = DbType.String, Value = pidary[i] });
}
parameters.Add(new DataParameter { ParameterName = "UPDATEUSER", DataType = DbType.String, Value = userID });
parameters.Add(new DataParameter { ParameterName = "UPDATEDATE", DataType = DbType.DateTime, Value = DateTime.Now });
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = session.ExecuteSql(sqlChange, parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 工作中心下拉
///
///
///
///
///
public List GetWorkCenterList(WorkCenter 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 = this.ChangeSqlByDB(sql, session);
list = session.GetList(sqlChange, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工作中心数据层-获取信息");
throw ex;
}
}
#endregion
}
}