天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

463 lines
17 KiB

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
{
/// <summary>
/// 模块名称:工作中心
/// 作 者:郭兆福
/// 编写日期:2017年05月09日
/// </summary>
public class WorkCenterDAL : BaseDAL
{
#region 判断工作中心是否已被使用
/// <summary>
/// 判断工作中心是否已被使用
/// </summary>
/// <param name="workCenterAry">工作中心信息</param>
/// <param name="tableList">需要校验表集合</param>
/// <param name="isDel">是否是删除</param>
/// <returns>true:使用 false:未使用</returns>
public bool IsWorkCenterUsed(string[] workCenterAry, List<string> tableList, bool isDel)
{
int count = 0;
StringBuilder sqlBuilder = new StringBuilder();
try
{
List<DataParameter> param = new List<DataParameter>();
// 创建检索条件
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;
}
}
/// <summary>
/// 创建检索条件
/// </summary>
/// <param name="workCenterAry">工作中心信息</param>
/// <param name="isDel">是否是删除</param>
/// <param name="parameters">参数信息</param>
/// <returns>检索条件</returns>
private string CreateSearchCondition(string[] workCenterAry, bool isDel, ref List<DataParameter> 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 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>信息</returns>
public WorkCenter Get(WorkCenter model)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
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<WorkCenter>(sqlChange, parameters.ToArray());
}
return model;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工作中心数据层-获取信息");
throw ex;
}
}
#endregion
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList(WorkCenter condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
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<WorkCenter>(sqlChange, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工作中心数据层-获取列表");
throw ex;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(WorkCenter condition, ref List<DataParameter> 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 信息是否重复
/// <summary>
/// 判断名称是否存在
/// </summary>
/// <param name="info"></param>
/// <returns>true:已存在;fasel:不存在。</returns>
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 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int Insert(WorkCenter model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert<WorkCenter>(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name=""></param>
/// <returns>更新行数</returns>
public int Update(WorkCenter model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update<WorkCenter>(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 删除
/// <summary>
/// 删除信息
/// </summary>
/// <param name="pidary">删除的主键</param>
/// <param name="userID">用户ID</param>
/// <returns>删除个数</returns>
public int Delete(string[] pidary, string userID)
{
if (null == pidary || pidary.Length == 0)
{
return 0;
}
StringBuilder sqlBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
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 工作中心下拉
/// <summary>
///
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List<WorkCenter> GetWorkCenterList(WorkCenter condition)
{
List<WorkCenter> list = new List<WorkCenter>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
list = session.GetList<WorkCenter>(sqlChange, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工作中心数据层-获取信息");
throw ex;
}
}
#endregion
}
}