using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
using QMFrameWork.Log;
using System.Data;
using QMAPP.DAL;
namespace QMAPP.MD.DAL
{
///
/// 模块名称:班组信息
/// 作 者:郭兆福
/// 编写日期:2017年05月17日
///
public class TeamDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public Team Get(Team model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(model);
}
return model;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "班组信息数据层 - 获取信息");
throw ex;
}
}
///
/// 根据检索条件得到班组名称
///
/// 条件
/// 班组名称
public string GetTeamName(Team condition)
{
string teamName = "";
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);
Team model = session.Get(sqlChange, parameters.ToArray());
if (null != model)
{
teamName = model.TEAM_NAME;
}
}
return teamName;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "班组信息数据层 - 获取班组名称");
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(Team 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;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(Team condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
sqlBuilder.AppendLine(@" SELECT T.PID
,T.FACTORY_CODE
,F.FACTORY_NAME
,T.DEPT_CODE
,T.TEAM_CODE
,T.TEAM_NAME
,T.TEAM_LEADER
,T.REMARK
,T.CREATEUSER
,T.CREATEDATE
,T.UPDATEUSER
,T.UPDATEDATE
,T.FLGDEL
FROM T_QT_TEAM T
LEFT JOIN T_MD_FACTORY F
ON T.FACTORY_CODE = F.FACTORY_CODE
");
//查询条件
whereBuilder.Append(" AND T.FLGDEL = '0'");
//班组编号
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND T.FACTORY_CODE = @FACTORY_CODE");
parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
}
//班组编号
if (string.IsNullOrEmpty(condition.TEAM_CODE) == false)
{
whereBuilder.Append(" AND T.TEAM_CODE = @TEAM_CODE");
parameters.Add(new DataParameter { ParameterName = "TEAM_CODE", DataType = DbType.String, Value = condition.TEAM_CODE });
}
//班组名称
if (string.IsNullOrEmpty(condition.TEAM_NAME) == false)
{
whereBuilder.Append(" AND T.TEAM_NAME LIKE @TEAM_NAME");
parameters.Add(new DataParameter { ParameterName = "TEAM_NAME", DataType = DbType.String, Value = EscapeValue(condition.TEAM_NAME) + "%" });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
//sqlBuilder.AppendLine(" order by T.TEAM_CODE ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
///
/// 获取全部用户
///
/// 获取条件
/// 用户信息列表
public List GetAllTeam(Team condition)
{
string sql = null;
List list = null;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = "SELECT * FROM T_QT_TEAM ";
if (!string.IsNullOrEmpty(condition.FACTORY_CODE))
{
sql += " where FACTORY_CODE = '"+condition.FACTORY_CODE+"'" ;
}
sql += " ORDER BY TEAM_CODE ";
list = session.GetList(sql, new List().ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#region 判断班组是否存在
///
/// 判断名称是否存在
///
///
/// true:已存在;false:不存在。
public bool ExistsTeam(Team model)
{
string PID = "";
int count = 0;
string sql = null;
try
{
if (string.IsNullOrEmpty(model.PID) == false)
{
PID = model.PID;
}
sql = "SELECT COUNT(*) FROM T_QT_TEAM WHERE PID <> @PID AND TEAM_CODE=@TEAM_CODE AND FACTORY_CODE=@FACTORY_CODE";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange,
new DataParameter("PID", PID),
new DataParameter("TEAM_CODE", model.TEAM_CODE),
new DataParameter("FACTORY_CODE", model.FACTORY_CODE)));
}
return count > 0;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "班组信息数据层-判断班组是否存在");
throw ex;
}
}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 班组信息
/// 插入行数
public int Insert(Team 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(Team 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(Team 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 班组是否使用
// ///
// /// 班组是否使用
// ///
// ///
// /// true:已使用;fasel:未使用。
// public bool IsUsing(Team model)
// {
// int count = 0;
// StringBuilder sqlBuilder = new StringBuilder();
// try
// {
// sqlBuilder.AppendLine(" SELECT COUNT(*) FROM T_QT_TEAM_MEMBER T WHERE T.TEAM_PID=@TEAMID ");
// using (IDataSession session = AppDataFactory.CreateMainSession())
// {
// count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(),
// new DataParameter { ParameterName = "TEAMID", Value = model.PID }));
// }
// if (count > 0)
// {
// return true;
// }
// else
// {
// return false;
// }
// }
// catch (Exception ex)
// {
// throw ex;
// }
// }
// #endregion
// #region 获取班组人员列表
// ///
// /// 获取班组人员列表
// ///
// /// 条件
// /// 数据页
// /// 数据页
// public DataPage GetTeamWorkerList(TeamMember condition, DataPage page)
// {
// string sql = null;
// List parameters = new List();
// try
// {
// sql = this.GetTeamWorkerQuerySql(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)
// {
// throw ex;
// }
// }
// ///
// /// 获取列表
// ///
// /// 条件
// /// 全部集合
// public List GetTeamWorker(TeamMember condition)
// {
// List list = new List();
// string sql = null;
// List parameters = new List();
// try
// {
// sql = this.GetTeamWorkerQuerySql(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
// #region 获取班组成员查询语句
// ///
// /// 获取班组成员查询语句
// ///
// /// 查询条件
// /// 参数
// /// 查询语句
// private string GetTeamWorkerQuerySql(TeamMember condition, ref List parameters)
// {
// StringBuilder sqlBuilder = new StringBuilder();
// StringBuilder whereBuilder = new StringBuilder();
// try
// {
// sqlBuilder.AppendLine(@" SELECT T.PID
// ,T.MEMBER_CODE
// ,T.MEMBER_NAME
// ,T.WORK_STATION
// ,T.TEAM_PID
// ,T.CREATEUSR
// ,T.CREATEDATE
// ,T.UPDATEDATE
// ,T.FLAGDEL
// FROM T_QT_TEAM_MEMBER T ");
// //查询条件
// //班组主键
// if (string.IsNullOrEmpty(condition.TEAM_PID) == false)
// {
// whereBuilder.Append(" AND T.TEAM_PID = @TEAMID ");
// parameters.Add(new DataParameter { ParameterName = "TEAMID", DataType = DbType.String, Value = condition.TEAM_PID });
// }
// if (whereBuilder.Length > 0)
// {
// sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
// }
// return sqlBuilder.ToString();
// }
// catch (Exception ex)
// {
// throw ex;
// }
// }
// #endregion
}
}