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

477 lines
18 KiB

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
{
/// <summary>
/// 模 块:逻辑矩阵
/// 作 者:郭兆福
/// 时 间:2018年04月20日
/// </summary>
public class UserWithMachineDAL : BaseDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>信息</returns>
public UserWithMachine Get(UserWithMachine model)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
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<UserWithMachine>(sqlChange, parameters.ToArray());
}
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(UserWithMachine condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
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<UserWithMachine>(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "逻辑矩阵数据层-获取分页列表");
throw ex;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <returns>全部集合</returns>
public List<UserWithMachine> GetList(UserWithMachine condition)
{
List<UserWithMachine> list = new List<UserWithMachine>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<UserWithMachine>(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<UserWithMachine> list)
{
ImportMessage em = new ImportMessage();
int count = 0;
List<DataParameter> parameters = new List<DataParameter>();
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<DataParameter>().ToArray());
foreach (UserWithMachine info in list)
{
count = session.Insert<UserWithMachine>(info);
}
session.CommitTs();
}
}
catch (Exception ex)
{
throw ex;
}
return count;
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(UserWithMachine condition, ref List<DataParameter> 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 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name="model">信息</param>
/// <returns>更新行数</returns>
public int Update(UserWithMachine model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update<UserWithMachine>(model);
}
return count;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "物料号信息数据层-更新信息");
throw ex;
}
}
#endregion
#region 编辑时删除信息
/// <summary>
/// 编辑时删除信息
/// </summary>
/// <param name="model">逻辑矩</param>
/// <returns>删除个数</returns>
public int EditDeleteUserWithMachine(UserWithMachine model)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
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 判断信息是否存在
/// <summary>
/// 判断名称是否存在
/// </summary>
/// <param name="info"></param>
/// <returns>true:已存在;fasel:不存在。</returns>
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 用户名称下拉
/// <summary>
/// 用户名称下拉
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List<User> GetUserInfoList(User condition)
{
List<User> list = new List<User>();
List<DataParameter> parameters = new List<DataParameter>();
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<User>(sqlBuilder.ToString(), parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "逻辑矩阵数据层-列表"
});
throw;
}
}
/// <summary>
/// 用户名称下拉
/// </summary>
/// <param name="condition"></param>
/// <returns></returns>
public List<User> GetUserInfoListForEdit(User condition)
{
List<User> list = new List<User>();
List<DataParameter> parameters = new List<DataParameter>();
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<User>(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<UserWithMachine> GetUserAbility(string userid)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
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<UserWithMachine>(sqlChange, parameters.ToArray()).ToList();
}
}
catch (Exception ex)
{
RecordExceptionLog(ex, "用户设备数据层-获取信息");
throw ex;
}
}
}
}