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.
321 lines
13 KiB
321 lines
13 KiB
4 years ago
|
using System;
|
||
|
using System.Collections.Generic;
|
||
|
using System.Linq;
|
||
|
using System.Text;
|
||
|
using QMAPP.DAL;
|
||
|
using QMFrameWork.Data;
|
||
|
using QMAPP.FJC.Entity.Operation;
|
||
|
using System.Data;
|
||
|
|
||
|
namespace QMAPP.FJC.DAL.Operation
|
||
|
{
|
||
|
public class MainCodeChangeDAL : BaseDAL
|
||
|
{
|
||
|
|
||
|
#region 获取列表
|
||
|
/// <summary>
|
||
|
/// 获取列表
|
||
|
/// </summary>
|
||
|
/// <param name="condition">条件</param>
|
||
|
/// <param name="page">数据页</param>
|
||
|
/// <returns>数据页</returns>
|
||
|
public DataPage GetList(MainCodeChange condition, DataPage page)
|
||
|
{
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
sql = this.GetQuerySql(condition, ref parameters);
|
||
|
//分页关键字段及排序
|
||
|
page.KeyName = "PID";
|
||
|
if (string.IsNullOrEmpty(page.SortExpression))
|
||
|
{
|
||
|
page.SortExpression = "CREATEDATE DESC";
|
||
|
}
|
||
|
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
page = session.GetDataPage<MainCodeChange>(sql, parameters.ToArray(), page);
|
||
|
}
|
||
|
return page;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region 获取查询语句
|
||
|
/// <summary>
|
||
|
/// 获取查询语句
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <param name="parameters">参数</param>
|
||
|
/// <returns>查询语句</returns>
|
||
|
private string GetQuerySql(MainCodeChange condition, ref List<DataParameter> parameters)
|
||
|
{
|
||
|
StringBuilder sqlBuilder = new StringBuilder();
|
||
|
StringBuilder whereBuilder = new StringBuilder();
|
||
|
try
|
||
|
{
|
||
|
//构成查询语句
|
||
|
sqlBuilder.AppendLine(" SELECT MC.PID,");
|
||
|
sqlBuilder.AppendLine(" MC.ORIGMAINCODE,");
|
||
|
sqlBuilder.AppendLine(" MC.NEWMAINCODE,");
|
||
|
sqlBuilder.AppendLine(" MC.MATERIALCODE,");
|
||
|
sqlBuilder.AppendLine(" MC.PRODUCTCODE,");
|
||
|
sqlBuilder.AppendLine(" MC.PRODUCELINE,");
|
||
|
sqlBuilder.AppendLine(" C.USERNAME AS CREATEUSERNAME, ");
|
||
|
sqlBuilder.AppendLine(" MC.CREATEUSER, ");
|
||
|
sqlBuilder.AppendLine(" CONVERT(VARCHAR(19),MC.CREATEDATE,120) AS CREATEDATE,");
|
||
|
sqlBuilder.AppendLine(" M.MATERIAL_NAME");
|
||
|
sqlBuilder.AppendLine(" FROM T_AW_MAINCODECHANGE MC");
|
||
|
sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL M on M.MATERIAL_CODE=MC.MATERIALCODE ");
|
||
|
sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=MC.CREATEUSER ");
|
||
|
//查询条件
|
||
|
|
||
|
if (string.IsNullOrEmpty(condition.MATERIALCODE) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND MC.MATERIALCODE LIKE '%'+" + "@MATERIALCODE" + "+'%' ");
|
||
|
parameters.Add(new DataParameter { ParameterName = "MATERIALCODE", DataType = DbType.String, Value = condition.MATERIALCODE });
|
||
|
}
|
||
|
|
||
|
//if (string.IsNullOrEmpty(condition.NEWMAINCODE) == false)
|
||
|
//{
|
||
|
// whereBuilder.Append(" AND MC.NEWMAINCODE LIKE '%'+" + "@NEWMAINCODE" + "+'%'");
|
||
|
// parameters.Add(new DataParameter { ParameterName = "NEWMAINCODE", DataType = DbType.String, Value = condition.NEWMAINCODE });
|
||
|
//}
|
||
|
|
||
|
//if (string.IsNullOrEmpty(condition.ORIGMAINCODE) == false)
|
||
|
//{
|
||
|
// whereBuilder.Append(" AND MC.ORIGMAINCODE LIKE '%'+" + "@ORIGMAINCODE" + "+'%'");
|
||
|
// parameters.Add(new DataParameter { ParameterName = "ORIGMAINCODE", DataType = DbType.String, Value = condition.ORIGMAINCODE });
|
||
|
//}
|
||
|
|
||
|
if (string.IsNullOrEmpty(condition.PRODUCTCODE) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND MC.PRODUCTCODE LIKE '%'+" + "@PRODUCTCODE" + "+'%'");
|
||
|
parameters.Add(new DataParameter { ParameterName = "PRODUCTCODE", DataType = DbType.String, Value = condition.PRODUCTCODE });
|
||
|
}
|
||
|
|
||
|
if (string.IsNullOrEmpty(condition.ORIGMAINCODE) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND MC.ORIGMAINCODE=@ORIGMAINCODE ");
|
||
|
parameters.Add(new DataParameter { ParameterName = "ORIGMAINCODE", DataType = DbType.String, Value = condition.ORIGMAINCODE });
|
||
|
}
|
||
|
|
||
|
|
||
|
if (string.IsNullOrEmpty(condition.NEWMAINCODE) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND MC.NEWMAINCODE=@NEWMAINCODE ");
|
||
|
parameters.Add(new DataParameter { ParameterName = "NEWMAINCODE", DataType = DbType.String, Value = condition.NEWMAINCODE });
|
||
|
}
|
||
|
|
||
|
|
||
|
//时间
|
||
|
if (condition.CREATEDATESTART != DateTime.MinValue)
|
||
|
{
|
||
|
whereBuilder.Append(" AND MC.CREATEDATE>@CREATEDATESTART ");
|
||
|
parameters.Add(new DataParameter { ParameterName = "CREATEDATESTART", DataType = DbType.String, Value = condition.CREATEDATESTART.ToString("yyyy-MM-dd HH:mm:ss") });
|
||
|
}
|
||
|
if (condition.CREATEDATEEND != DateTime.MinValue)
|
||
|
{
|
||
|
whereBuilder.Append(" AND MC.CREATEDATE<@CREATEDATEEND ");
|
||
|
parameters.Add(new DataParameter { ParameterName = "CREATEDATEEND", DataType = DbType.String, Value = condition.CREATEDATEEND.ToString("yyyy-MM-dd HH:mm:ss") });
|
||
|
}
|
||
|
|
||
|
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 bool ExistsMain(MainCodeChange model)
|
||
|
{
|
||
|
StringBuilder sqlBuilder = new StringBuilder();
|
||
|
StringBuilder whereBuilder = new StringBuilder();
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
int count = 0;
|
||
|
try
|
||
|
{
|
||
|
sqlBuilder.Append("SELECT COUNT(PID) FROM T_AW_MAINCODECHANGE");
|
||
|
|
||
|
if (string.IsNullOrEmpty(model.NEWMAINCODE) == false)
|
||
|
{
|
||
|
whereBuilder.Append(" AND NEWMAINCODE=@NEWMAINCODE");
|
||
|
parameters.Add(new DataParameter { ParameterName = "NEWMAINCODE", DataType = DbType.String, Value = model.NEWMAINCODE });
|
||
|
}
|
||
|
if (whereBuilder.Length > 0)
|
||
|
{
|
||
|
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
|
||
|
}
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray()));
|
||
|
}
|
||
|
if (count > 0)
|
||
|
{
|
||
|
return true;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
#region 插入信息
|
||
|
/// <summary>
|
||
|
/// 插入信息(单表)
|
||
|
/// </summary>
|
||
|
/// <param name="">信息</param>
|
||
|
/// <returns>插入行数</returns>
|
||
|
public int Insert(MainCodeChange model)
|
||
|
{
|
||
|
int count = 0;
|
||
|
try
|
||
|
{
|
||
|
if (BaseSession != null)
|
||
|
{
|
||
|
//插入基本信息
|
||
|
count = BaseSession.Insert<MainCodeChange>(model);
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
//插入基本信息
|
||
|
count = session.Insert<MainCodeChange>(model);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return count;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
#region 获取导出的数据
|
||
|
|
||
|
/// <summary>
|
||
|
/// 获取导出的数据
|
||
|
/// </summary>
|
||
|
/// <param name="user">查询条件</param>
|
||
|
/// <returns>数据</returns>
|
||
|
public DataTable GetExportData(MainCodeChange model)
|
||
|
{
|
||
|
DataTable dt = null;
|
||
|
string sql = null;
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
//构成查询语句
|
||
|
sql = this.GetQuerySql(model, ref parameters);
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
dt = session.GetTable(sql, parameters.ToArray());
|
||
|
dt.TableName = "T_AW_MAINCODECHANGE";
|
||
|
}
|
||
|
return dt;
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
#endregion
|
||
|
|
||
|
|
||
|
public List<MainCodeChange> GetChangeRecord(string origcode)
|
||
|
{
|
||
|
//string sql = "SELECT * FROM [T_AW_MAINCODECHANGE] WHERE ORIGMAINCODE=@origcode";
|
||
|
StringBuilder sql=new StringBuilder();
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
sql.AppendLine("WITH CTE([PID],[ORIGMAINCODE],[NEWMAINCODE],[PRODUCTCODE],[CREATEDATE]) ");
|
||
|
sql.AppendLine("AS ");
|
||
|
sql.AppendLine("( ");
|
||
|
sql.AppendLine("SELECT [PID],[ORIGMAINCODE],[NEWMAINCODE],[PRODUCTCODE],[CREATEDATE] ");
|
||
|
sql.AppendLine("FROM [T_AW_MAINCODECHANGE] ");
|
||
|
sql.AppendLine("WHERE ORIGMAINCODE=@origcode ");
|
||
|
sql.AppendLine("UNION ALL ");
|
||
|
sql.AppendLine("SELECT N.[PID],N.[ORIGMAINCODE],N.[NEWMAINCODE],N.[PRODUCTCODE],N.[CREATEDATE] ");
|
||
|
sql.AppendLine("FROM [T_AW_MAINCODECHANGE] AS N ");
|
||
|
sql.AppendLine("INNER JOIN CTE AS O ON N.ORIGMAINCODE = O.NEWMAINCODE ");
|
||
|
sql.AppendLine(") ");
|
||
|
sql.AppendLine("SELECT * FROM CTE ");
|
||
|
parameters.Add(new DataParameter("origcode", origcode));
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
return session.GetList<MainCodeChange>(sql.ToString(), parameters.ToArray()).ToList();
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
public List<MainCodeChange> GetChangeRecordFromNewCode(string newcode)
|
||
|
{
|
||
|
//string sql = "SELECT * FROM [T_AW_MAINCODECHANGE] WHERE ORIGMAINCODE=@origcode";
|
||
|
StringBuilder sql = new StringBuilder();
|
||
|
List<DataParameter> parameters = new List<DataParameter>();
|
||
|
try
|
||
|
{
|
||
|
sql.AppendLine("WITH CTE([PID],[ORIGMAINCODE],[NEWMAINCODE],[PRODUCTCODE],[CREATEUSER],[CREATEDATE]) ");
|
||
|
sql.AppendLine("AS ");
|
||
|
sql.AppendLine("( ");
|
||
|
sql.AppendLine("SELECT [PID],[ORIGMAINCODE],[NEWMAINCODE],[PRODUCTCODE],[CREATEUSER],[CREATEDATE] ");
|
||
|
sql.AppendLine("FROM [T_AW_MAINCODECHANGE] ");
|
||
|
sql.AppendLine("WHERE [NEWMAINCODE]=@newcode ");
|
||
|
sql.AppendLine("UNION ALL ");
|
||
|
sql.AppendLine("SELECT N.[PID],N.[ORIGMAINCODE],N.[NEWMAINCODE],N.[PRODUCTCODE],N.[CREATEUSER],N.[CREATEDATE] ");
|
||
|
sql.AppendLine("FROM [T_AW_MAINCODECHANGE] AS N ");
|
||
|
sql.AppendLine("INNER JOIN CTE AS O ON O.ORIGMAINCODE = N.NEWMAINCODE ");
|
||
|
sql.AppendLine(") ");
|
||
|
sql.AppendLine("SELECT CTE.* ");
|
||
|
sql.AppendLine(" ,U.USERNAME AS CREATEUSERNAME ");
|
||
|
sql.AppendLine(" FROM CTE ");
|
||
|
sql.AppendLine(" LEFT JOIN T_QM_USER AS U ");
|
||
|
sql.AppendLine(" ON U.USERID=CTE.CREATEUSER ");
|
||
|
parameters.Add(new DataParameter("newcode", newcode));
|
||
|
using (IDataSession session = AppDataFactory.CreateMainSession())
|
||
|
{
|
||
|
return session.GetList<MainCodeChange>(sql.ToString(), parameters.ToArray()).ToList();
|
||
|
}
|
||
|
}
|
||
|
catch (Exception ex)
|
||
|
{
|
||
|
throw ex;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|