天津投入产出系统后端
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.
 
 
 
 
 
 

530 lines
19 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
using System.Data;
using QMAPP.Entity;
using QMFrameWork.Log;
using QMAPP.DAL;
namespace QMAPP.MD.DAL
{
/// <summary>
/// 模块名称:工厂
/// 作 者:郭兆福
/// 编写日期:2017年05月04日
/// </summary>
public class FactoryDAL : BaseDAL
{
#region 判断工厂是否已被使用
/// <summary>
/// 判断工厂是否已被使用
/// </summary>
/// <param name="factoryAry">工厂信息</param>
/// <param name="tableList">需要校验表集合</param>
/// <param name="isDel">是否是删除</param>
/// <returns>true:使用 false:未使用</returns>
public bool IsFactoryUsed(string[] factoryAry, List<string> tableList, bool isDel)
{
int count = 0;
StringBuilder sqlBuilder = new StringBuilder();
try
{
List<DataParameter> param = new List<DataParameter>();
// 创建检索条件
string sqlWhere = CreateSearchCondition(factoryAry, isDel, ref param);
// 删除情况 根据主键先取出工厂编码
if (isDel == true)
{
sqlBuilder.AppendLine(" WITH P ");
sqlBuilder.AppendLine(" AS ( SELECT FACTORY_CODE FROM T_MD_FACTORY WHERE PID IN (");
for (int i = 0; i < factoryAry.Length; i++)
{
if (i != 0)
{
sqlBuilder.AppendLine(" , ");
}
String paramname = "PID" + i.ToString();
sqlBuilder.AppendLine("@" + paramname);
param.Add(new DataParameter { ParameterName = paramname, Value = factoryAry[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.FACTORY_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="factoryAry">工厂信息</param>
/// <param name="isDel">是否是删除</param>
/// <param name="parameters">参数信息</param>
/// <returns>检索条件</returns>
private string CreateSearchCondition(string[] factoryAry, 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.FACTORY_CODE = B.FACTORY_CODE");
}
else
{
sqlBuilderWhere.AppendLine(" WHERE A.FLGDEL = '0'");
for (int i = 0; i < factoryAry.Length; i++)
{
if (i == 0)
{
sqlBuilderWhere.AppendLine(" AND A.FACTORY_CODE IN ( ");
}
else
{
sqlBuilderWhere.AppendLine(" , ");
}
String paramname = "FACTORY_CODE" + i.ToString();
sqlBuilderWhere.AppendLine("@" + paramname);
if (i == factoryAry.Length - 1)
{
sqlBuilderWhere.AppendLine(" ) ");
}
parameters.Add(new DataParameter { ParameterName = paramname, Value = factoryAry[i] });
}
}
return sqlBuilderWhere.ToString();
}
#endregion
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>信息</returns>
public Factory Get(Factory model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get<Factory>(model);
}
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(Factory condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "FACTORY_CODE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
page = session.GetDataPage<Factory>(sqlChange, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工厂信息数据层-获取列表");
throw ex;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <returns>全部集合</returns>
public List<Factory> GetAllList(Factory condition)
{
List<Factory> list = new List<Factory>();
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<Factory>(sqlChange, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工厂信息数据层-获取信息");
throw ex;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(Factory condition, ref List<DataParameter> parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT P.PID ");
sqlBuilder.AppendLine(" ,P.FACTORY_CODE ");
sqlBuilder.AppendLine(" ,P.FACTORY_NAME ");
sqlBuilder.AppendLine(" ,P.FACTORY_SHORT ");
sqlBuilder.AppendLine(" ,P.REMARK ");
sqlBuilder.AppendLine(" ,R.CORP_CODE ");
sqlBuilder.AppendLine(" ,R.CORP_NAME ");
sqlBuilder.AppendLine(" ,P.FLGDEL ");
sqlBuilder.AppendLine(" ,P.CREATEUSER ");
sqlBuilder.AppendLine(" ,P.CREATEDATE ");
sqlBuilder.AppendLine(" ,P.UPDATEUSER ");
sqlBuilder.AppendLine(" ,P.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_MD_FACTORY P ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_CORP R ON P.CORP_CODE=R.CORP_CODE ");
whereBuilder.Append(" AND P.FLGDEL='0'");
//查询条件
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND FACTORY_CODE = @FACTORY_CODE ");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
if (string.IsNullOrEmpty(condition.FACTORY_NAME) == false)
{
whereBuilder.Append(" AND FACTORY_NAME LIKE @FACTORY_NAME ");
parameters.Add(new DataParameter { ParameterName = "FACTORY_NAME", DataType = DbType.String, Value = EscapeValue(condition.FACTORY_NAME) + "%" });
}
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 ExistsFactory(Factory 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_FACTORY ");
sqlBuilder.AppendLine(" WHERE PID <> @PID 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 { ParameterName = "FACTORY_CODE", Value = model.FACTORY_CODE }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int Insert(Factory model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert<Factory>(model);
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name=""></param>
/// <returns>更新行数</returns>
public int Update(Factory model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update<Factory>(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_FACTORY ");
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="workcellCode"></param>
/// <returns></returns>
public Factory GetByWorkcell(string workcellCode)
{
Factory model;
StringBuilder sqlBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
sqlBuilder.Append(" SELECT F.* ");
sqlBuilder.Append(" FROM T_MD_FACTORY F ");
sqlBuilder.Append(" JOIN T_MD_WORKCENTER CT ");
sqlBuilder.Append(" ON F.FACTORY_CODE = CT.FACTORY_CODE AND CT.FLGDEL = '0' ");
sqlBuilder.Append(" JOIN T_MD_WORKCELL CL ");
sqlBuilder.Append(" ON CT.WORKCENTER_CODE = CL.WORKCENTER_CODE AND CL.FLGDEL = '0' ");
sqlBuilder.Append(" WHERE F.FLGDEL = '0' AND CL.WORKCELL_CODE = @WORKCELL_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKCELL_CODE", DataType = DbType.String, Value = workcellCode });
try
{
if (null != BaseSession)
{
string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), BaseSession);
model = BaseSession.Get<Factory>(sqlChange, parameters.ToArray());
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session);
//获取信息
model = session.Get<Factory>(sqlChange, parameters.ToArray());
}
}
return model;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工厂信息数据层-根据工序获取信息");
throw ex;
}
}
#endregion
#region 根据工作中心获取工厂
/// <summary>
/// 根据工序获取工厂
/// </summary>
/// <param name="workcellCode"></param>
/// <returns></returns>
public string GetFactoryWithWorkcenter(string workcentetinfo)
{
string factorycode;
StringBuilder sqlBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
sqlBuilder.Append(" SELECT F.FACTORY_CODE ");
sqlBuilder.Append(" FROM T_MD_FACTORY F ");
sqlBuilder.Append(" JOIN T_MD_WORKCENTER CT ");
sqlBuilder.Append(" ON F.FACTORY_CODE = CT.FACTORY_CODE AND CT.FLGDEL = '0' ");
sqlBuilder.Append(" WHERE F.FLGDEL = '0' AND CT.WORKCENTER_CODE = @WORKCENTER_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = workcentetinfo });
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session);
//获取信息
factorycode = session.ExecuteSqlScalar(sqlChange, parameters.ToArray())+"";
}
return factorycode;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工厂信息数据层-根据工作中心获取信息");
throw ex;
}
}
#endregion
}
}