using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using QMAPP.DAL;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
namespace QMAPP.MD.DAL
{
///
/// 模块名称:工位
/// 作 者:郭兆福
/// 编写日期:2017年05月11日
///
public class WorkLocDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public WorkLoc Get(WorkLoc model)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_MD_WORKLOC WHERE FLGDEL='0' ";
if (string.IsNullOrEmpty(model.PID) == false)
{
sql += " AND PID = @PID";
parameters.Add(new DataParameter("PID", model.PID));
}
if (string.IsNullOrEmpty(model.WORKLOC_CODE) == false)
{
sql += " AND WORKLOC_CODE = @WORKLOC_CODE";
parameters.Add(new DataParameter("WORKLOC_CODE", model.WORKLOC_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(WorkLoc 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())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
page = session.GetDataPage(sqlChange, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-获取列表");
throw ex;
}
}
///
/// 获取列表
///
/// 条件
/// 列表
public List GetList(WorkLoc condition)
{
string sql = null;
List parameters = new List();
List listProcessInfo = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
listProcessInfo = session.GetList(sqlChange, parameters.ToArray()).ToList();
}
return listProcessInfo;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-获取列表");
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(WorkLoc condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT M.PID ");
sqlBuilder.AppendLine(" ,M.WORKLOC_CODE ");
sqlBuilder.AppendLine(" ,M.WORKLOC_NAME ");
sqlBuilder.AppendLine(" ,M.WORKCELL_CODE ");
sqlBuilder.AppendLine(" ,W.WORKCELL_NAME ");
sqlBuilder.AppendLine(" ,M.WORKCENTER_CODE ");
sqlBuilder.AppendLine(" ,M.FACTORY_CODE ");
sqlBuilder.AppendLine(" ,C.WORKCENTER_NAME ");
sqlBuilder.AppendLine(" ,F.FACTORY_NAME ");
sqlBuilder.AppendLine(" ,M.REMARK ");
sqlBuilder.AppendLine(" ,M.CREATEUSER ");
sqlBuilder.AppendLine(" ,M.CREATEDATE ");
sqlBuilder.AppendLine(" ,M.UPDATEUSER ");
sqlBuilder.AppendLine(" ,M.UPDATEDATE ");
sqlBuilder.AppendLine(" ,M.FLGDEL ");
sqlBuilder.AppendLine(" FROM T_MD_WORKLOC M ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKCELL W ON M.WORKCELL_CODE=W.WORKCELL_CODE ");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKCENTER C ON C.WORKCENTER_CODE=M.WORKCENTER_CODE AND C.FACTORY_CODE=M.FACTORY_CODE");
sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=M.FACTORY_CODE ");
//查询条件
//工位
if (string.IsNullOrEmpty(condition.WORKLOC_CODE) == false)
{
whereBuilder.Append(" AND M.WORKLOC_CODE = @WORKLOC_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKLOC_CODE", DataType = DbType.String, Value = condition.WORKLOC_CODE });
}
//工序
if (string.IsNullOrEmpty(condition.WORKCELL_CODE) == false)
{
whereBuilder.Append(" AND M.WORKCELL_CODE = @WORKCELL_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKCELL_CODE", DataType = DbType.String, Value = condition.WORKCELL_CODE });
}
//工厂
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.WORKCENTER_CODE) == false)
{
whereBuilder.Append(" AND M.WORKCENTER_CODE = @WORKCENTER_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE });
}
whereBuilder.Append(" AND M.FLGDEL ='0'");
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 判断工位编号是否重复
///
/// 判断工位编号是否重复
///
/// 工位
/// true:已存在;false:不存在。
public bool IsExistsWorkLocCode(WorkLoc model)
{
string PID = "";
int count = 0;
string sql = null;
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
List paramList = new List();
sql = "SELECT COUNT(*) FROM T_MD_WORKLOC WHERE PID <> @PID AND FACTORY_CODE=@FACTORY_CODE AND WORKLOC_CODE=@WORKLOC_CODE";
paramList.Add(new DataParameter { ParameterName = "PID", Value = model.PID });
paramList.Add(new DataParameter { ParameterName = "FACTORY_CODE", Value = model.FACTORY_CODE });
paramList.Add(new DataParameter { ParameterName = "WORKLOC_CODE", Value = model.WORKLOC_CODE });
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, paramList.ToArray()));
}
return count == 0 ? false : true;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-判断编号名称是否存在");
throw ex;
}
}
#endregion
#region 判断工位编号是否被用
///
/// 判断工位编号是否被用
///
/// 工位
/// true:已用;false:未用。
public bool IsUsedWorkLocCode(string[] pid)
{
int count = 0;
StringBuilder sqlBuilder = new StringBuilder();
try
{
List paramList = new List();
sqlBuilder.Append(" SELECT COUNT(*) FROM T_MD_WORKCELL_EQUIPMENT WHERE FLGDEL = '0' ");
sqlBuilder.Append(" AND WORKLOC_CODE IN ( ");
sqlBuilder.Append(" SELECT WORKLOC_CODE FROM T_MD_WORKLOC ");
for (int i = 0; i < pid.Length; i++)
{
if (i == 0)
{
sqlBuilder.Append(" WHERE PID IN (");
}
else
{
sqlBuilder.Append(" ,");
}
string param = "PID" + i.ToString();
sqlBuilder.Append(" @").Append(param);
paramList.Add(new DataParameter(param,DbType.String,pid[i]));
if (i == pid.Length - 1)
{
sqlBuilder.Append(" ) ");
}
}
sqlBuilder.Append(" ) ");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, paramList.ToArray()));
}
return count == 0 ? false : true;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-判断编号是否被用");
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(WorkLoc 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 Update(WorkLoc model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update(model);
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-更新信息");
throw ex;
}
}
#endregion
#region 删除
///
/// 删除信息
///
/// 物料号信息(ID)
/// 删除个数
public int Delete(WorkLoc model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count = session.Delete(model);
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-删除");
throw ex;
}
}
#endregion
#region 工位设备相关操作
///
/// 工位设备信息的取得
///
///
///
///
public DataPage GetWorkLocEquipmentList(WorkCellEquipment condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetWorkLocEquipmentQuerySql(condition, ref parameters);
#region 排序
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "UPDATEDATE DESC";
#endregion
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;
}
}
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetWorkLocEquipmentQuerySql(WorkCellEquipment condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(" SELECT M.PID ");
sqlBuilder.AppendLine(" ,M.WORKLOC_CODE ");
sqlBuilder.AppendLine(" ,M.EQUIPMENT_CODE ");
sqlBuilder.AppendLine(" ,E.EQUIPMENT_NAME ");
sqlBuilder.AppendLine(" ,M.CREATEUSER ");
sqlBuilder.AppendLine(" ,M.CREATEDATE ");
sqlBuilder.AppendLine(" ,M.UPDATEUSER ");
sqlBuilder.AppendLine(" ,M.UPDATEDATE ");
sqlBuilder.AppendLine(" ,M.FLGDEL ");
sqlBuilder.AppendLine(" FROM T_MD_WORKCELL_EQUIPMENT M ");
sqlBuilder.AppendLine(" JOIN T_MD_EQUIPMENT E ON M.EQUIPMENT_CODE = E.EQUIPMENT_CODE AND E.FLGDEL ='0'");
//查询条件
//工位
if (string.IsNullOrEmpty(condition.WORKLOC_CODE) == false)
{
whereBuilder.Append(" AND M.WORKLOC_CODE = @WORKLOC_CODE");
parameters.Add(new DataParameter { ParameterName = "WORKLOC_CODE", DataType = DbType.String, Value = condition.WORKLOC_CODE });
}
whereBuilder.Append(" AND M.FLGDEL = '0'");
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int InsertWorkLocEquip(WorkCellEquipment model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位设备-插入信息");
throw ex;
}
}
///
/// 判断工位编号是否重复
///
/// 工位
/// true:已存在;false:不存在。
public bool IsExistsWorkLocEquip(WorkCellEquipment model)
{
string PID = "";
int count = 0;
string sql = null;
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
List paramList = new List();
sql = "SELECT COUNT(*) FROM T_MD_WORKCELL_EQUIPMENT WHERE PID <> @PID AND WORKLOC_CODE = @WORKLOC_CODE AND EQUIPMENT_CODE = @EQUIPMENT_CODE";
paramList.Add(new DataParameter { ParameterName = "PID", Value = model.PID });
paramList.Add(new DataParameter { ParameterName = "WORKLOC_CODE", Value = model.WORKLOC_CODE });
paramList.Add(new DataParameter { ParameterName = "EQUIPMENT_CODE", Value = model.EQUIPMENT_CODE });
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, paramList.ToArray()));
}
return count == 0 ? false : true;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位设备-判断工位设备是否存在");
throw ex;
}
}
///
/// 删除信息
///
/// 物料号信息(ID)
/// 删除个数
public int DeleteWorkLocEquip(WorkCellEquipment model)
{
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
count = session.Delete(model);
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位设备信息-删除");
throw ex;
}
}
#endregion
#region 工位信息下拉
///
///
///
///
///
public List GetWorkLocFromWorkCellList(WorkLoc model, string routecode)
{
List list = new List();
string sql = null;
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sqlBuilder.AppendLine("SELECT * FROM T_MD_WORKLOC ");
sqlBuilder.AppendLine(" WHERE WORKCELL_CODE=@routecode");
parameters.Add(new DataParameter { ParameterName = "routecode", DataType = DbType.String, Value = routecode });
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-下拉列表");
throw ex;
}
}
public List GetWorkLocFromWorkCenterCodeList(string workCenterCode)
{
List list = new List();
string sql = null;
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sqlBuilder.AppendLine("SELECT * FROM T_MD_WORKLOC ");
sqlBuilder.AppendLine(" where WORKCELL_CODE in ( select WORKCELL_CODE from T_MD_WORKCELL where WORKCENTER_CODE=@workCenterCode)");
parameters.Add(new DataParameter { ParameterName = "workCenterCode", DataType = DbType.String, Value = workCenterCode });
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工位信息-下拉列表");
throw ex;
}
}
#endregion
#region 根据工位信息获得工序信息
///
///
///
///
///
public List GetWorkCellFromWorkLocList(WorkLoc model, string routecode)
{
List list = new List();
string sql = null;
StringBuilder sqlBuilder = new StringBuilder();
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sqlBuilder.AppendLine("SELECT * FROM T_MD_WORKLOC ");
sqlBuilder.AppendLine(" WHERE WORKLOC_CODE=@routecode");
parameters.Add(new DataParameter { ParameterName = "routecode", DataType = DbType.String, Value = routecode });
sql = this.ChangeSqlByDB(sqlBuilder.ToString(), session);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "工序信息");
throw ex;
}
}
#endregion
}
}