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

265 lines
10 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.FJC.Entity.Basic;
using QMAPP.FJC.DAL.Basic;
namespace QMAPP.MESReport.DAL.LineQTY
{
/// <summary>
/// 安东呼叫统计分析
/// 于子清
/// 2017-11-13
/// </summary>
public class AnDongCallDAL : BaseDAL
{
public List<AnDongCallDModel> GetAllList(AnDongCallDModel condition)
{
List<AnDongCallDModel> list = new List<AnDongCallDModel>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<AnDongCallDModel>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
public List<AnDongCallDModel> GetRepairTimeAvg(AnDongCallDModel condition)
{
List<AnDongCallDModel> list = new List<AnDongCallDModel>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetSqlRepairTimeAvg(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<AnDongCallDModel>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
throw ex;
}
}
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(AnDongCallDModel condition, ref List<DataParameter> parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder sqlBuilder2 = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
string group = "";
//构成查询语句
sqlBuilder.AppendLine("select count(CALL_TYPE) NUM ,CALL_TYPE,tbd.MACHINENAME,tbd.MACHINENAME_SHORT from T_AD_CALLLOG tad inner join T_BD_MACHINEINFO tbd on tbd.MACHINECODDE = tad.MACHINECODDE ");
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
var startDate = condition.START_DATE + " 00:00:00";
whereBuilder.Append(" AND tad.CALL_TIME >= @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 tad.CALL_TIME <= @END_DATE ");
parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = endDate });
}
if (string.IsNullOrEmpty(condition.CALL_TYPE) == false)
{
whereBuilder.Append(" AND tad.CALL_TYPE = @CALL_TYPE ");
parameters.Add(new DataParameter { ParameterName = "CALL_TYPE", DataType = DbType.String, Value = condition.CALL_TYPE });
}
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
sqlBuilder.Append(" group by CALL_TYPE,tbd.MACHINENAME,tbd.MACHINENAME_SHORT ");
sqlBuilder.AppendLine(" order by tbd.MACHINENAME ");
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
private string GetSqlRepairTimeAvg(AnDongCallDModel condition, ref List<DataParameter> parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder sqlBuilder2 = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
string group = "";
//构成查询语句
//sqlBuilder.AppendLine("select count(CALL_TYPE) NUM ,CALL_TYPE,tbd.MACHINENAME,tbd.MACHINENAME_SHORT from T_AD_CALLLOG tad inner join T_BD_MACHINEINFO tbd on tbd.MACHINECODDE = tad.MACHINECODDE ");
sqlBuilder.AppendLine("SELECT MACHINECODDE,sum(datediff( MINUTE, CALL_TIME, COMPLETE_TIME ))as REPAIR_TIME,count(1) as NUM FROM T_AD_CALLLOG ");
sqlBuilder.AppendLine("where CALL_STATE='3' and call_type='0' and (MACHINECODDE !='' and MACHINECODDE is not null) ");
group = " group by MACHINECODDE ";
if (string.IsNullOrEmpty(condition.START_DATE) == false)
{
var startDate = condition.START_DATE + " 00:00:00";
whereBuilder.Append(" AND COMPLETE_TIME >= @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 COMPLETE_TIME <= @END_DATE ");
parameters.Add(new DataParameter { ParameterName = "END_DATE", DataType = DbType.DateTime, Value = endDate });
}
//if (string.IsNullOrEmpty(condition.CALL_TYPE) == false)
//{
// whereBuilder.Append(" AND tad.CALL_TYPE = @CALL_TYPE ");
// parameters.Add(new DataParameter { ParameterName = "CALL_TYPE", DataType = DbType.String, Value = condition.CALL_TYPE });
//}
if (whereBuilder.Length > 0)
{
//sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
sqlBuilder.Append(whereBuilder);
}
sqlBuilder.Append(group);
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 获取导出的数据
/// <summary>
/// 获取导出的数据
/// </summary>
/// <param name="user">查询条件</param>
/// <returns>数据</returns>
public DataTable GetExportData(AnDongCallDModel model)
{
List<AnDongCallDModel> list = null;
if (model.selectExcel==0)
{
list = GetAllList(model);
foreach (AnDongCallDModel item in list)
{
//呼叫类型名称
if (!string.IsNullOrEmpty(item.CALL_TYPE))
{
if (item.CALL_TYPE == "0")
{
item.CALL_TYPE_NAME = "维修";
}
else if (item.CALL_TYPE == "1")
{
item.CALL_TYPE_NAME = "质量";
}
else if (item.CALL_TYPE == "2")
{
item.CALL_TYPE_NAME = "物料";
}
else if (item.CALL_TYPE == "3")
{
item.CALL_TYPE_NAME = "工程";
}
}
//设备名称
if (!string.IsNullOrEmpty(item.MACHINENAME))
{
if (string.IsNullOrEmpty(item.MACHINENAME_SHORT))
{
item.MACHINENAME_SHORT = item.MACHINENAME;
}
}
}
}
else if (model.selectExcel == 1)
{
list = GetRepairTimeAvg(model);
var machineInfos = new MachineInfoDAL().GetList(new MachineInfo());
foreach (AnDongCallDModel item in list)
{
//处理人
if (!string.IsNullOrEmpty(item.MACHINECODDE))
{
var machineInfo = machineInfos.FirstOrDefault(x => x.MACHINECODDE == item.MACHINECODDE);
if (machineInfo != null)
{
if (!string.IsNullOrEmpty(machineInfo.MACHINENAME_SHORT))
{
item.MACHINENAME = machineInfo.MACHINENAME_SHORT;
}
else
{
item.MACHINENAME = machineInfo.MACHINENAME;
}
}
else
{
item.MACHINENAME = item.MACHINECODDE;
}
}
}
}
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
}
}