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;
using QMFrameWork.Log;
namespace QMAPP.FJC.DAL.Operation
{
public class ProductOutDAL : BaseDAL
{
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(ProductOut 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;
}
}
public List GetList(string productcode)
{
//string sql = "SELECT * FROM [T_AW_PRODUCTOUT] AS O WHERE [PRODUCT_CODE] =@productcode ORDER BY [CREATEDATE]";
List parameters = new List();
try
{
StringBuilder sql = new StringBuilder();
sql.AppendLine("SELECT O.* ");
sql.AppendLine(" ,R.OUTREASON_NAME");
sql.AppendLine(" ,U.USERNAME AS CREATEUSERNAME ");
sql.AppendLine(" FROM T_AW_PRODUCTOUT AS O ");
sql.AppendLine(" LEFT JOIN T_QM_USER AS U ");
sql.AppendLine(" ON O.CREATEUSER=U.USERID ");
sql.AppendLine(" LEFT JOIN T_MD_OUTREASON AS R ");
sql.AppendLine(" ON R.OUTREASON_CODE=O.OUTREASON ");
sql.AppendLine(" WHERE O.[PRODUCT_CODE] =@productcode");
sql.AppendLine(" ORDER BY [CREATEDATE]");
parameters.Add(new DataParameter("productcode", productcode));
using (IDataSession session = AppDataFactory.CreateMainSession())
{
return session.GetList(sql.ToString(), parameters.ToArray()).ToList();
}
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(ProductOut condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.Append("SELECT A.PID,");
sqlBuilder.Append("A.PRODUCT_CODE,");
sqlBuilder.Append("A.MATERIAL_CODE,");
sqlBuilder.Append("A.WORKCENTER_CODE,");
sqlBuilder.Append("WC.WORKCENTER_NAME,");
sqlBuilder.Append("A.WORKCELL_CODE,");
sqlBuilder.Append("W.WORKCELL_NAME,");
sqlBuilder.Append("A.OUTREASON,");
sqlBuilder.Append("A.SHIFT_CODE,");
sqlBuilder.Append("A.CREATEUSER,");
sqlBuilder.Append("CONVERT(VARCHAR(19),A.CREATEDATE,120) AS CREATEDATE,");
sqlBuilder.Append("A.UPDATEUSER,");
sqlBuilder.Append("CONVERT(VARCHAR(19),A.UPDATEDATE,120) AS UPDATEDATE,");
sqlBuilder.Append("M.MATERIAL_NAME,");
sqlBuilder.Append("S.PRODUCESHIFTNAME AS SHIFT_NAME,");
sqlBuilder.Append("MC.MATERIAL_TYPE_CODE,");
sqlBuilder.Append("MC.MATERIAL_TYPE_NAME,");
sqlBuilder.Append("C.USERNAME AS CREATEUSERNAME ");
sqlBuilder.Append("FROM T_AW_PRODUCTOUT A ");
sqlBuilder.Append("LEFT JOIN T_QM_USER C ON C.USERID=A.CREATEUSER ");
sqlBuilder.Append("LEFT JOIN T_MD_MATERIAL M ON M.MATERIAL_CODE=A.MATERIAL_CODE ");
sqlBuilder.Append("LEFT JOIN T_MD_MATERIAL_CLASS MC on MC.MATERIAL_TYPE_CODE=M.MATERIAL_TYPE_CODE ");
sqlBuilder.Append("LEFT JOIN T_BD_PRODUCESHIFT S ON S.PRODUCESHIFTTCODE=A.SHIFT_CODE ");
sqlBuilder.Append("LEFT JOIN T_MD_WORKCENTER WC ON WC.WORKCENTER_CODE=A.WORKCENTER_CODE ");
sqlBuilder.Append("LEFT JOIN T_MD_WORKCELL W ON W.WORKCELL_CODE=A.WORKCELL_CODE ");
//查询条件
if (condition.CREATEDATESTART != null && condition.CREATEDATESTART != DateTime.MinValue)
{
whereBuilder.Append(" AND A.CREATEDATE>@CREATEDATESTART ");
parameters.Add(new DataParameter { ParameterName = "CREATEDATESTART", DataType = DbType.String, Value = condition.CREATEDATESTART.ToString("yyyy-MM-dd HH:mm:ss") });
}
if (condition.CREATEDATEEND != null && condition.CREATEDATEEND != DateTime.MinValue)
{
whereBuilder.Append(" AND A.CREATEDATE<@CREATEDATEEND ");
parameters.Add(new DataParameter { ParameterName = "CREATEDATEEND", DataType = DbType.String, Value = condition.CREATEDATEEND.ToString("yyyy-MM-dd HH:mm:ss") });
}
if (string.IsNullOrEmpty(condition.PRODUCT_CODE) == false)
{
whereBuilder.Append(" AND A.PRODUCT_CODE LIKE '%'+" + "@PRODUCT_CODE" + "+'%' ");
parameters.Add(new DataParameter { ParameterName = "PRODUCT_CODE", DataType = DbType.String, Value = condition.PRODUCT_CODE });
}
if (string.IsNullOrEmpty(condition.MATERIAL_TYPE_CODE) == false)
{
whereBuilder.Append(" AND MC.MATERIAL_TYPE_CODE =@MATERIAL_TYPE_CODE");
parameters.Add(new DataParameter { ParameterName = "MATERIAL_TYPE_CODE", DataType = DbType.String, Value = condition.MATERIAL_TYPE_CODE });
}
if (string.IsNullOrEmpty(condition.MATERIAL_CODE) == false)
{
whereBuilder.Append(" AND M.MATERIAL_CODE =@MATERIAL_CODE");
parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MATERIAL_CODE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入出库信息
///
/// 插入出库信息
///
/// 信息
/// 插入行数
public int InsertProductOut(ProductOut model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
try
{
session.OpenTs();
//插入出库表
count = session.Insert(model);
if (count > 0)
{
//更新本体表
count = session.Update(model.productList);
}
session.CommitTs();
}
catch (Exception)
{
session.RollbackTs();
throw;
}
}
return count;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(ProductOut 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 = "ProductOutExp";
}
return dt;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "信息导出获取数据错误!"
});
throw ex;
}
}
#endregion
#region 获取列表出库原因
///
/// 获取列表出库原因
///
/// 条件
/// 列表
public List GetOutReasonList(OutReason condition)
{
string sql = null;
List parameters = new List();
List listProcessInfo = new List();
try
{
sql = "SELECT * FROM T_MD_OUTREASON";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
// 对应多种数据库
string sqlChange = this.ChangeSqlByDB(sql, session);
listProcessInfo = session.GetList(sqlChange, parameters.ToArray()).ToList();
}
return listProcessInfo;
}
catch (Exception ex)
{
RecordExceptionLog(ex, "出库信息-获取列表");
throw ex;
}
}
#endregion
}
}