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

363 lines
15 KiB

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
{
/// <summary>
/// 生产班次设备情况统计-数据层对象
/// 创建人:李炳海 YZQ
/// 创建时间:2017.09.22
/// </summary>
public class LineShiftQTYCountDAL : BaseDAL
{
/// <summary>
/// 获取各班次设备生产情况
/// </summary>
public List<LineShiftEquQTY> GetEquQTYByShift(LineDayQTYCount condition)
{
List<LineShiftEquQTY> list = null;
return list;
}
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList(LineDayQTYCount condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "STATIS_DATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage<LineDayQTYCount>(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <returns>全部集合</returns>
public List<LineDayQTYCount> GetAllList(LineDayQTYCount condition)
{
List<LineDayQTYCount> list = new List<LineDayQTYCount>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<LineDayQTYCount>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
public List<LineDayQTYCount> GetAllList1(LineDayQTYCount condition)
{
List<LineDayQTYCount> list = new List<LineDayQTYCount>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql1(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<LineDayQTYCount>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取班次列表
/// <summary>
/// 获取班次列表
/// </summary>
/// <param name="condition"></param>
/// <param name="parameters"></param>
/// <returns></returns>
public List<T_QT_SHIFT> GetT_QT_SHIFTList()
{
List<T_QT_SHIFT> list = new List<T_QT_SHIFT>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = "select * from T_QT_SHIFT order by SHIFT_CODE";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<T_QT_SHIFT>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(LineDayQTYCount condition, ref List<DataParameter> 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<DataParameter> 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 获取导出的数据
/// <summary>
/// 获取导出的数据
/// </summary>
/// <param name="user">查询条件</param>
/// <returns>数据</returns>
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<LineDayQTYCount> 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<T>(IEnumerable<T> 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
}
}