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年11月21日
///
public class ScheduleDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// *信息
public ScheduleEntity Get(ScheduleEntity model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get(model);
}
return model;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "数据层 - 获取信息");
throw ex;
}
}
public ScheduleEntity GetEntity(ScheduleEntity model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
var sql = "SELECT S.*,T.TEAM_NAME,SH.PRODUCESHIFTNAME AS SHIFT_NAME FROM T_QT_SCHEDULE S ";
sql += "LEFT JOIN T_QT_TEAM T ON T.TEAM_CODE=S.TEAM_CODE AND T.FACTORY_CODE=S.FACTORY_CODE ";
sql += "LEFT JOIN T_BD_PRODUCESHIFT SH ON SH.PRODUCESHIFTTCODE=S.SHIFT_CODE ";
sql += "WHERE S.PID='" + model .PID+ "'";
//获取信息
model = session.Get(sql, new List().ToArray());
}
return model;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "数据层 - 获取信息");
throw ex;
}
}
#endregion
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(ScheduleEntity 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 = "FACTORY_CODE DESC";
}
#endregion
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
//string sqlChange = this.ChangeSqlByDB(sql, session);
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "班组信息数据层 - 获取列表");
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(ScheduleEntity 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
// ");
sqlBuilder.AppendLine(@" SELECT S.* FROM T_QT_SCHEDULE S ");
sqlBuilder.AppendLine(@" LEFT JOIN T_QT_TEAM T ");
sqlBuilder.AppendLine(@" ON T.TEAM_CODE = S.TEAM_CODE ");
//查询条件
//whereBuilder.Append(" AND T.FLGDEL = '0'");
//工厂
if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
{
whereBuilder.Append(" AND S.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 S.TEAM_CODE = @TEAM_CODE");
parameters.Add(new DataParameter { ParameterName = "TEAM_CODE", DataType = DbType.String, Value = condition.TEAM_CODE });
}
//班组类型
if (string.IsNullOrEmpty(condition.TEAM_TYPE) == false)
{
whereBuilder.Append(" AND T.DEPT_CODE = @TEAM_TYPE");
parameters.Add(new DataParameter { ParameterName = "TEAM_TYPE", DataType = DbType.String, Value = condition.TEAM_TYPE });
}
//班次
if (string.IsNullOrEmpty(condition.SHIFT_CODE) == false)
{
whereBuilder.Append(" AND S.SHIFT_CODE = @SHIFT_CODE");
parameters.Add(new DataParameter { ParameterName = "SHIFT_CODE", DataType = DbType.String, Value = condition.SHIFT_CODE });
}
//当前日期
if (condition.NOW_DATE!=DateTime.MinValue)
{
whereBuilder.Append(" AND (S.START_DATE <@NOW_DATE AND S.END_DATE >@NOW_DATE)");
parameters.Add(new DataParameter { ParameterName = "NOW_DATE", DataType = DbType.DateTime, Value = condition.NOW_DATE });
}
//结束
//if (condition.END_DATE != DateTime.MinValue)
//{
// whereBuilder.Append(" AND END_DATE <@END_DATE");
// parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = condition.END_DATE });
//}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
///
/// 获取全部用户
///
/// 获取条件
/// 用户信息列表
public List GetAllSchedule(ScheduleEntity condition)
{
string sql = null;
List list = null;
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
sql = GetQuerySql(condition, ref parameters);
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw;
}
}
#region 判断班组是否存在
///
/// 判断名称是否存在
///
///
/// true:已存在;false:不存在。
//public bool ExistsTeam(ScheduleEntity 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 ";
// using (IDataSession session = AppDataFactory.CreateMainSession())
// {
// // 对应多种数据库
// string sqlChange = this.ChangeSqlByDB(sql, session);
// count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, new DataParameter("PID", PID),
// new DataParameter { ParameterName = "TEAM_CODE", Value = model.TEAM_CODE }));
// }
// if (count > 0)
// {
// return true;
// }
// else
// {
// return false;
// }
// }
// catch (Exception ex)
// {
// RecordExceptionLog(ex, "班组信息数据层-判断班组是否存在");
// throw ex;
// }
//}
#endregion
#region 插入信息
///
/// 插入信息(单表)
///
/// 班组信息
/// 插入行数
public int Insert(ScheduleEntity 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(ScheduleEntity 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(ScheduleEntity 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(ScheduleEntity 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
}
}