using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMAPP.DAL;
using QMAPP.MD.Entity;
using QMFrameWork.Data;
using QMFrameWork.Log;
using System.Data;
using QMAPP.Entity;
using QMAPP.MD.Entity.Sys;
using QMAPP.Entity.Sys;
namespace QMAPP.MD.DAL
{
///
/// 模 块:逻辑矩阵
/// 作 者:郭兆福
/// 时 间:2018年04月20日
///
public class UserWithMachineDAL : BaseDAL
{
#region 获取信息
///
/// 获取信息
///
/// 条件
/// 信息
public UserWithMachine Get(UserWithMachine model)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_MD_USERWITHMACHINE WHERE '1'='1'";
if (string.IsNullOrEmpty(model.PID) == false)
{
sql += " AND PID = @PID";
parameters.Add(new DataParameter("PID", model.PID));
}
if (string.IsNullOrEmpty(model.USERID) == false)
{
sql += " AND USERID = @USERID";
parameters.Add(new DataParameter("USERID", model.USERID));
}
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(UserWithMachine 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 = "CREATEDATE DESC";
}
#endregion
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "逻辑矩阵数据层-获取分页列表");
throw ex;
}
}
///
/// 获取列表
///
/// 条件
/// 全部集合
public List GetList(UserWithMachine condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(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 插入信息
public int InsertUserWithMachine(List list)
{
ImportMessage em = new ImportMessage();
int count = 0;
List parameters = new List();
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//设置祖先对象数据会话
session.OpenTs();
//闫永刚
//新增之前先删除
string sql = string.Format("delete from T_MD_USERWITHMACHINE where USERID='{0}'", list[0].USERID);
session.ExecuteSql(sql, new List().ToArray());
foreach (UserWithMachine info in list)
{
count = session.Insert(info);
}
session.CommitTs();
}
}
catch (Exception ex)
{
throw ex;
}
return count;
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(UserWithMachine condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
if (string.IsNullOrEmpty(condition.EDITFLAG) == false)
{
sqlBuilder.AppendLine(" SELECT U.PID ");
sqlBuilder.AppendLine(" ,U.USERID ");
sqlBuilder.AppendLine(" ,U.MACHINEID ");
sqlBuilder.AppendLine(" ,A.USERNAME ");
sqlBuilder.AppendLine(" ,A.LOGINUSERID AS USERCODE ");
sqlBuilder.AppendLine(" ,M.MACHINECODDE AS MACHINE_CODE ");
sqlBuilder.AppendLine(" ,B.USERNAME AS CREATEUSERNAME ");
sqlBuilder.AppendLine(" ,C.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.CREATEUSER ");
sqlBuilder.AppendLine(" ,U.CREATEDATE ");
sqlBuilder.AppendLine(" ,U.UPDATEUSER ");
sqlBuilder.AppendLine(" ,U.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_MD_USERWITHMACHINE U ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_MACHINEINFO M ON M.PID = U.MACHINEID ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER A ON A.USERID = U.USERID ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER B ON B.USERID = U.CREATEUSER ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID = U.UPDATEUSER ");
}
else{
//sqlBuilder.AppendLine(" SELECT U.PID ");
sqlBuilder.AppendLine(" SELECT DISTINCT ");
sqlBuilder.AppendLine(" U.USERID ");
//sqlBuilder.AppendLine(" ,U.MACHINEID ");
sqlBuilder.AppendLine(" ,A.USERNAME ");
sqlBuilder.AppendLine(" ,A.LOGINUSERID AS USERCODE ");
//sqlBuilder.AppendLine(" ,M.MACHINECODDE AS MACHINE_CODE ");
sqlBuilder.AppendLine(" ,B.USERNAME AS CREATEUSERNAME ");
//sqlBuilder.AppendLine(" ,C.USERNAME AS UPDATEUSERNAME ");
sqlBuilder.AppendLine(" ,U.CREATEUSER ");
sqlBuilder.AppendLine(" ,U.CREATEDATE ");
//sqlBuilder.AppendLine(" ,U.UPDATEUSER ");
//sqlBuilder.AppendLine(" ,U.UPDATEDATE ");
sqlBuilder.AppendLine(" FROM T_MD_USERWITHMACHINE U ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_MACHINEINFO M ON M.PID = U.MACHINEID ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER A ON A.USERID = U.USERID ");
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER B ON B.USERID = U.CREATEUSER ");
//sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID = U.UPDATEUSER ");
}
//查询条件
if (string.IsNullOrEmpty(condition.USERID) == false)
{
whereBuilder.Append(" AND U.USERID = @USERID ");
parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = condition.USERID });
}
//
if (string.IsNullOrEmpty(condition.USERCODE) == false)
{
whereBuilder.Append(" AND A.LOGINUSERID = @USERCODE ");
parameters.Add(new DataParameter { ParameterName = "USERCODE", DataType = DbType.String, Value = condition.USERCODE });
}
//
if (string.IsNullOrEmpty(condition.USERNAME) == false)
{
whereBuilder.Append(" AND A.USERNAME = @USERNAME ");
parameters.Add(new DataParameter { ParameterName = "USERNAME", DataType = DbType.String, Value = condition.USERNAME });
}
//
if (string.IsNullOrEmpty(condition.MACHINEID) == false)
{
whereBuilder.Append(" AND U.MACHINEID = @MACHINEID ");
parameters.Add(new DataParameter { ParameterName = "MACHINEID", DataType = DbType.String, Value = condition.MACHINEID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 更新信息
///
/// 更新信息
///
/// 信息
/// 更新行数
public int Update(UserWithMachine 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 编辑时删除信息
///
/// 编辑时删除信息
///
/// 逻辑矩
/// 删除个数
public int EditDeleteUserWithMachine(UserWithMachine model)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("DELETE FROM T_MD_USERWITHMACHINE ");
//设备
if (string.IsNullOrEmpty(model.MACHINEID) == false)
{
whereBuilder.Append(" AND MACHINEID = @MACHINEID ");
parameters.Add(new DataParameter { ParameterName = "MACHINEID", DataType = DbType.String, Value = model.MACHINEID });
}
//用户id
if (string.IsNullOrEmpty(model.USERID) == false)
{
whereBuilder.Append(" AND USERID = @USERID ");
parameters.Add(new DataParameter { ParameterName = "USERID", DataType = DbType.String, Value = model.USERID });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
string sqlChange = ChangeSqlByDB(sqlBuilder.ToString(), session);
count = session.ExecuteSql(sqlChange, parameters.ToArray());
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "逻辑矩阵数据层-删除信息");
throw ex;
}
}
#endregion
#region 判断信息是否存在
///
/// 判断名称是否存在
///
///
/// true:已存在;fasel:不存在。
public bool ExistsMateriel(UserWithMachine model)
{
//string PID = "";
int count = 0;
string sql = null;
try
{
sql = "SELECT COUNT(*) FROM T_MD_USERWITHMACHINE WHERE USERID = @USERID AND MACHINEID=@MACHINEID";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sqlChange = ChangeSqlByDB(sql, session);
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, new DataParameter { ParameterName = "USERID", Value = model.USERID },
new DataParameter { ParameterName = "MACHINEID", Value = model.MACHINEID }));
}
if (count > 0)
{
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
RecordExceptionLog(ex, "逻辑矩阵数据层-判断是否存在");
throw ex;
}
}
#endregion
#region 用户名称下拉
///
/// 用户名称下拉
///
///
///
public List GetUserInfoList(User condition)
{
List list = new List();
List parameters = new List();
try
{
StringBuilder sqlBuilder = new StringBuilder();
//构成查询语句
sqlBuilder.Append("SELECT * ");
sqlBuilder.Append("FROM T_QM_USER ");
sqlBuilder.Append("WHERE ActivateFlg = @ActivateFlg AND LOGINUSERID<>'ADMIN' ");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
parameters.Add(new DataParameter { ParameterName = "ActivateFlg", DataType = DbType.String, Value = condition.ActivateFlg });
list = session.GetList(sqlBuilder.ToString(), parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "逻辑矩阵数据层-列表"
});
throw;
}
}
///
/// 用户名称下拉
///
///
///
public List GetUserInfoListForEdit(User condition)
{
List list = new List();
List parameters = new List();
try
{
StringBuilder sqlBuilder = new StringBuilder();
//构成查询语句
sqlBuilder.Append("SELECT * ");
sqlBuilder.Append("FROM T_QM_USER U ");
sqlBuilder.Append("WHERE U.ActivateFlg = @ActivateFlg AND U.LOGINUSERID<>'ADMIN' and not exists(select 1 from T_MD_USERWITHMACHINE um where um.userid=U.USERID) ");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
parameters.Add(new DataParameter { ParameterName = "ActivateFlg", DataType = DbType.String, Value = condition.ActivateFlg });
list = session.GetList(sqlBuilder.ToString(), parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "逻辑矩阵数据层-列表"
});
throw;
}
}
#endregion
public List GetUserAbility(string userid)
{
string sql = null;
List parameters = new List();
try
{
sql = "SELECT * FROM T_MD_USERWITHMACHINE WHERE USERID=@userid";
parameters.Add(new DataParameter("userid", userid));
using (IDataSession session = AppDataFactory.CreateMainSession())
{
string sqlChange = this.ChangeSqlByDB(sql, session);
//获取信息
return session.GetList(sqlChange, parameters.ToArray()).ToList();
}
}
catch (Exception ex)
{
RecordExceptionLog(ex, "用户设备数据层-获取信息");
throw ex;
}
}
}
}