using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.MESReport.Entity.LineQTY;
using QMAPP.DAL;
using System.Data;
using QMAPP.MESReport.Entity.Tables;
namespace QMAPP.MESReport.DAL.LineQTY
{
///
/// 生产班次设备情况统计-数据层对象
/// 创建人:李炳海 YZQ
/// 创建时间:2017.09.22
///
public class LineShiftQTYCountDAL : BaseDAL
{
///
/// 获取各班次设备生产情况
///
public List GetEquQTYByShift(LineDayQTYCount condition)
{
List list = null;
return list;
}
#region 获取列表
///
/// 获取列表
///
/// 条件
/// 数据页
/// 数据页
public DataPage GetList(LineDayQTYCount condition, DataPage page)
{
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "STATIS_DATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
///
/// 获取列表
///
/// 条件
/// 全部集合
public List GetAllList(LineDayQTYCount 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)
{
throw ex;
}
}
public List GetAllList1(LineDayQTYCount condition)
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = this.GetQuerySql1(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取班次列表
///
/// 获取班次列表
///
///
///
///
public List GetT_QT_SHIFTList()
{
List list = new List();
string sql = null;
List parameters = new List();
try
{
sql = "select * from T_QT_SHIFT order by SHIFT_CODE";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
///
/// 获取查询语句
///
/// 查询条件
/// 参数
/// 查询语句
private string GetQuerySql(LineDayQTYCount condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
//sqlBuilder.AppendLine("select sum(OK_QTY) OK_QTY,tsa.EQUIPMENT_CODE,eq.EQUIPMENT_NAME,tsa.SHIFT_CODE from dbo.T_SA_WORKCELLQTYCOUNT tsa inner join T_MD_EQUIPMENT eq on eq.EQUIPMENT_CODE=tsa.EQUIPMENT_CODE and eq.FLGDEL =0 ");
sqlBuilder.AppendLine("SELECT SUM(OK_QTY) OK_QTY ");
sqlBuilder.AppendLine(" ,SA.EQUIPMENT_CODE ");
sqlBuilder.AppendLine(" ,MH.MACHINENAME AS EQUIPMENT_NAME ");
sqlBuilder.AppendLine(" ,MH.MACHINENAME_SHORT ");
sqlBuilder.AppendLine(" ,SA.SHIFT_CODE ");
sqlBuilder.AppendLine(" FROM T_SA_WORKCELLQTYCOUNT SA ");
sqlBuilder.AppendLine(" LEFT JOIN T_BD_MACHINEINFO MH ");
sqlBuilder.AppendLine(" ON MH.MACHINECODDE=SA.EQUIPMENT_CODE ");
sqlBuilder.AppendLine(" WHERE SA.EQUIPMENT_CODE <>'' and SA.EQUIPMENT_CODE is not null ");
//select WORKCENTER_CODE,STATIS_DATE,NOK_QTY,OK_QTY from T_SA_WORKCELLQTYCOUNT
//whereBuilder.Append(" AND P.FLGDEL='0'");
////查询条件
//if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
//{
// whereBuilder.Append(" AND FACTORY_CODE = @FACTORY_CODE ");
// parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
//}
//if (string.IsNullOrEmpty(condition.START_DATE) == false)
//{
// whereBuilder.Append(" AND FACTORY_NAME LIKE @FACTORY_NAME ");
// parameters.Add(new DataParameter { ParameterName = "FACTORY_NAME", DataType = DbType.String, Value = EscapeValue(condition.FACTORY_NAME) + "%" });
//}
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
whereBuilder.Append(" AND STATIS_DATE >= @START_DATE ");
parameters.Add(new DataParameter { ParameterName = "START_DATE", DataType = DbType.DateTime, Value = condition.START_DATE });
}
if (string.IsNullOrEmpty(condition.END_DATE) == false)
{
whereBuilder.Append(" AND STATIS_DATE <= @END_DATE ");
parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = condition.END_DATE });
}
if (string.IsNullOrEmpty(condition.SHIFT_CODE) == false)
{
whereBuilder.Append(" AND SA.SHIFT_CODE = @SHIFT_CODE ");
parameters.Add(new DataParameter { ParameterName = "SHIFT_CODE", DataType = DbType.String, Value = condition.SHIFT_CODE });
}
if (string.IsNullOrEmpty(condition.EQUIPMENT_CODE) == false)
{
whereBuilder.Append(" AND SA.EQUIPMENT_CODE = @EQUIPMENT_CODE ");
parameters.Add(new DataParameter { ParameterName = "EQUIPMENT_CODE", DataType = DbType.String, Value = condition.EQUIPMENT_CODE });
}
if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false)
{
whereBuilder.Append(" AND SA.WORKCENTER_CODE = @WORKCENTER_CODE ");
parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE });
}
if (whereBuilder.Length > 0)
{
//sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
sqlBuilder.Append(whereBuilder);
}
sqlBuilder.AppendLine("GROUP BY SA.EQUIPMENT_CODE,MH.MACHINENAME,SA.SHIFT_CODE,MH.MACHINENAME_SHORT ");
sqlBuilder.AppendLine(" order by EQUIPMENT_NAME ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
private string GetQuerySql1(LineDayQTYCount condition, ref List parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
//sqlBuilder.AppendLine("select sum(OK_QTY) OK_QTY,tsa.EQUIPMENT_CODE,eq.EQUIPMENT_NAME,tsa.SHIFT_CODE from dbo.T_SA_WORKCELLQTYCOUNT tsa inner join T_MD_EQUIPMENT eq on eq.EQUIPMENT_CODE=tsa.EQUIPMENT_CODE and eq.FLGDEL =0 ");
sqlBuilder.AppendLine("SELECT SUM(OK_QTY) OK_QTY ,SHIFT_CODE ,convert(varchar(10),STATIS_DATE,120) as STATIS_DATE FROM T_SA_WORKCELLQTYCOUNT ");
//select WORKCENTER_CODE,STATIS_DATE,NOK_QTY,OK_QTY from T_SA_WORKCELLQTYCOUNT
//whereBuilder.Append(" AND P.FLGDEL='0'");
////查询条件
//if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false)
//{
// whereBuilder.Append(" AND FACTORY_CODE = @FACTORY_CODE ");
// parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE });
//}
//if (string.IsNullOrEmpty(condition.START_DATE) == false)
//{
// whereBuilder.Append(" AND FACTORY_NAME LIKE @FACTORY_NAME ");
// parameters.Add(new DataParameter { ParameterName = "FACTORY_NAME", DataType = DbType.String, Value = EscapeValue(condition.FACTORY_NAME) + "%" });
//}
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
var startDate = condition.START_DATE + " 00:00:00";
whereBuilder.Append(" AND STATIS_DATE >= @START_DATE ");
parameters.Add(new DataParameter { ParameterName = "START_DATE", DataType = DbType.DateTime, Value = startDate });
}
if (string.IsNullOrEmpty(condition.END_DATE) == false)
{
var endDate = condition.END_DATE + " 23:59:59";
whereBuilder.Append(" AND STATIS_DATE <= @END_DATE ");
parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = endDate });
}
if (string.IsNullOrEmpty(condition.SHIFT_CODE) == false)
{
whereBuilder.Append(" AND SHIFT_CODE = @SHIFT_CODE ");
parameters.Add(new DataParameter { ParameterName = "SHIFT_CODE", DataType = DbType.String, Value = condition.SHIFT_CODE });
}
if (string.IsNullOrEmpty(condition.EQUIPMENT_CODE) == false)
{
whereBuilder.Append(" AND EQUIPMENT_CODE = @EQUIPMENT_CODE ");
parameters.Add(new DataParameter { ParameterName = "EQUIPMENT_CODE", DataType = DbType.String, Value = condition.EQUIPMENT_CODE });
}
if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false)
{
whereBuilder.Append(" AND WORKCENTER_CODE = @WORKCENTER_CODE ");
parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
sqlBuilder.AppendLine("GROUP BY SHIFT_CODE,convert(varchar(10),STATIS_DATE,120) ");
sqlBuilder.AppendLine(" order by STATIS_DATE ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
///
/// 获取导出的数据
///
/// 查询条件
/// 数据
public DataTable GetExportData(LineDayQTYCount model)
{
var workcenter = "";
if (model.WORKCENTER_CODE == "INJECT")
{
workcenter = "注塑";
}
else if (model.WORKCENTER_CODE == "BORADPASSY")
{
workcenter = "门板";
}
else if (model.WORKCENTER_CODE == "BORAIPASSY")
{
workcenter = "仪表板";
}
List list;
if (model.Trend == 1)
{
list = GetAllList1(model);
}
else
{
list = GetAllList(model);
}
var shifts = GetT_QT_SHIFTList();
foreach (LineDayQTYCount item in list)
{
item.WORKCENTER_NAME = workcenter;
//处理人
if (!string.IsNullOrEmpty(item.SHIFT_CODE))
{
var shift = shifts.FirstOrDefault(x => x.SHIFT_CODE == item.SHIFT_CODE);
if (shift != null)
{
item.SHIFT_NAME = shift.SHIFT_NAME;
}
}
}
return CopyToDataTable(list);
}
//把list转换成DataTable
public static DataTable CopyToDataTable(IEnumerable array)
{
var ret = new DataTable();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
ret.Columns.Add(dp.Name);
foreach (T item in array)
{
var Row = ret.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
Row[dp.Name] = dp.GetValue(item);
ret.Rows.Add(Row);
}
return ret;
}
#endregion
}
}