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 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(MainCodeChange condition, DataPage page)
{
string sql = null;
List parameters = new List();
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(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(MainCodeChange condition, ref List 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 校验
///
/// 校验
///
///
///
public bool ExistsMain(MainCodeChange model)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
List parameters = new List();
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 插入信息
///
/// 插入信息(单表)
///
/// 信息
/// 插入行数
public int Insert(MainCodeChange model)
{
int count = 0;
try
{
if (BaseSession != null)
{
//插入基本信息
count = BaseSession.Insert(model);
}
else
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert(model);
}
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(MainCodeChange model)
{
DataTable dt = null;
string sql = null;
List parameters = new List();
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 GetChangeRecord(string origcode)
{
//string sql = "SELECT * FROM [T_AW_MAINCODECHANGE] WHERE ORIGMAINCODE=@origcode";
StringBuilder sql=new StringBuilder();
List parameters = new List();
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(sql.ToString(), parameters.ToArray()).ToList();
}
}
catch (Exception ex)
{
throw ex;
}
}
public List GetChangeRecordFromNewCode(string newcode)
{
//string sql = "SELECT * FROM [T_AW_MAINCODECHANGE] WHERE ORIGMAINCODE=@origcode";
StringBuilder sql = new StringBuilder();
List parameters = new List();
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(sql.ToString(), parameters.ToArray()).ToList();
}
}
catch (Exception ex)
{
throw ex;
}
}
}
}