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

635 lines
28 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using QMFrameWork.Data;
using QMAPP.FJC.Entity.Basic;
using System.Data;
using QMFrameWork.Log;
using QMAPP.FJC.Entity.ProduceManage;
namespace QMAPP.FJC.DAL.ProduceManage
{
/// <summary>
/// 模块编号:M4-1
/// 作 用:生产记录数据层
/// 作 者:张敬贺
/// 编写日期:2015年06月04日
///</summary>
public class ProducePlanDAL
{
#region 获取信息
/// <summary>
/// 获取信息
/// </summary>
/// <param name="">条件</param>
/// <returns>*信息</returns>
public ProducePlan Get(ProducePlan model)
{
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//获取信息
model = session.Get<ProducePlan>(model);
}
return model;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "生产记录维护数据层-获取信息"
});
throw;
}
}
#endregion
#region 获取列表
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public DataPage GetList(ProducePlan condition, DataPage page)
{
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
//分页关键字段及排序
page.KeyName = "PID";
page.SortExpression = "UPDATEDATE DESC";
using (IDataSession session = AppDataFactory.CreateMainSession())
{
page = session.GetDataPage<ProducePlan>(sql, parameters.ToArray(), page);
}
return page;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "生产记录维护数据层-获取列表"
});
throw;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <returns>全部集合</returns>
public List<ProducePlan> GetList(ProducePlan condition)
{
List<ProducePlan> list = new List<ProducePlan>();
string sql = null;
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySql(condition, ref parameters);
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<ProducePlan>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "生产记录维护数据层-获取列表"
});
throw;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public List<ProducePlan> GetTotalMain(ProducePlan condition)
{
string sql = null;
List<ProducePlan> list = new List<ProducePlan>();
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySqlForTotalMain(condition, ref parameters);
//分页关键字段及排序
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<ProducePlan>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "获取列表"
});
throw;
}
}
/// <summary>
/// 获取列表
/// </summary>
/// <param name="condition">条件</param>
/// <param name="page">数据页</param>
/// <returns>数据页</returns>
public List<ProducePlan> GetTotalProduct(ProducePlan condition)
{
string sql = null;
List<ProducePlan> list = new List<ProducePlan>();
List<DataParameter> parameters = new List<DataParameter>();
try
{
sql = this.GetQuerySqlForTotalProduct(condition, ref parameters);
//分页关键字段及排序
using (IDataSession session = AppDataFactory.CreateMainSession())
{
list = session.GetList<ProducePlan>(sql, parameters.ToArray()).ToList();
}
return list;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "获取列表"
});
throw;
}
}
#endregion
#region 获取查询语句
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySql(ProducePlan condition, ref List<DataParameter> parameters)
{
StringBuilder sqlBuilder = new StringBuilder();
StringBuilder whereBuilder = new StringBuilder();
try
{
//构成查询语句
sqlBuilder.AppendLine(@" SELECT PP.PID
,PP.PROCESSTYPE
,PP.PRODUCTTYPE
,PP.COLOR
,PP.HB
,PP.MACHINECODE
,PP.PLANCODE
,PP.PRODUCELINE
,PP.MINPRODUCECOUNT
,PP.PRODUCECOUNT
,PP.PLANSTATUS
,PP.MEMO
,PP.PRODUCESHIFTTCODE
,PP.PLANDATE
,PP.CREATETYPE
,PP.CREATEUSER
,PP.CREATEDATE
,PP.UPDATEUSER
,PP.UPDATEDATE
,C.USERNAME AS CREATEUSERNAME
,U.USERNAME AS UPDATEUSERNAME
,M.MACHINENAME AS MACHINENAME
FROM T_AW_PRODUCEPLAN PP
LEFT JOIN T_BD_MACHINEINFO M ON PP.MACHINECODE = M.MACHINECODDE
LEFT JOIN T_QM_USER C ON C.USERID = PP.CREATEUSER
LEFT JOIN T_QM_USER U ON U.USERID = PP.UPDATEUSER ");
//查询条件
//生产计划号
if (string.IsNullOrEmpty(condition.PLANCODE) == false)
{
whereBuilder.Append(" AND PLANCODE = @PLANCODE ");
parameters.Add(new DataParameter { ParameterName = "PLANCODE", DataType = DbType.String, Value = condition.PLANCODE });
}
//工序类别
if (string.IsNullOrEmpty(condition.PROCESSTYPE) == false)
{
whereBuilder.Append(" AND PROCESSTYPE = @PROCESSTYPE ");
parameters.Add(new DataParameter { ParameterName = "PROCESSTYPE", DataType = DbType.String, Value = condition.PROCESSTYPE });
}
//零件类别
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
whereBuilder.Append(" AND PRODUCTTYPE = @PRODUCTTYPE ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
//颜色
if (string.IsNullOrEmpty(condition.COLOR) == false)
{
whereBuilder.Append(" AND COLOR = @COLOR ");
parameters.Add(new DataParameter { ParameterName = "COLOR", DataType = DbType.String, Value = condition.COLOR });
}
//高低配
if (string.IsNullOrEmpty(condition.HB) == false)
{
whereBuilder.Append(" AND HB = @HB ");
parameters.Add(new DataParameter { ParameterName = "HB", DataType = DbType.String, Value = condition.HB });
}
//设备编码
if (string.IsNullOrEmpty(condition.MACHINECODE) == false)
{
whereBuilder.Append(" AND MACHINECODE = @MACHINECODE ");
parameters.Add(new DataParameter { ParameterName = "MACHINECODE", DataType = DbType.String, Value = condition.MACHINECODE });
}
//计划状态
if (string.IsNullOrEmpty(condition.PLANSTATUS) == false)
{
whereBuilder.Append(" AND PLANSTATUS = @PLANSTATUS ");
parameters.Add(new DataParameter { ParameterName = "PLANSTATUS", DataType = DbType.String, Value = condition.PLANSTATUS });
}
if (condition.STARTCREATEDATE != DateTime.MinValue)
{
whereBuilder.Append(" AND PP.CREATEDATE >= @STARTCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "STARTCREATEDATE", Value = condition.STARTCREATEDATE.Date.ToString("yyyy-MM-dd HH:mm:ss") });
}
if (condition.ENDCREATEDATE != DateTime.MinValue)
{
whereBuilder.Append(" AND PP.CREATEDATE < @ENDCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "ENDCREATEDATE", Value = condition.ENDCREATEDATE.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") });
}
whereBuilder.Append(" AND PASTFLAG='0'");
if (whereBuilder.Length > 0)
{
sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4));
}
return sqlBuilder.ToString();
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySqlForTotalMain(ProducePlan condition, ref List<DataParameter> parameters)
{
StringBuilder whereBuilder = new StringBuilder();
try
{
StringBuilder opWhere = new StringBuilder();
StringBuilder planWhere = new StringBuilder();
if (condition.STARTCREATEDATE != DateTime.MinValue)
{
opWhere.Append(" AND OPERATEDDATE >= @STARTCREATEDATE");
planWhere.Append(" AND PLANDATE >= @STARTCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "STARTCREATEDATE", Value = condition.STARTCREATEDATE.Date.ToString("yyyy-MM-dd HH:mm:ss") });
}
if (condition.ENDCREATEDATE != DateTime.MinValue)
{
whereBuilder.Append(" AND OPERATEDDATE < @ENDCREATEDATE");
whereBuilder.Append(" AND PLANDATE < @ENDCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "ENDCREATEDATE", Value = condition.ENDCREATEDATE.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") });
}
//工序类别
if (string.IsNullOrEmpty(condition.PROCESSTYPE) == false)
{
opWhere.Append(" AND PROCESSTYPE = @PROCESSTYPE ");
planWhere.Append(" AND PROCESSTYPE = @PROCESSTYPE ");
parameters.Add(new DataParameter { ParameterName = "PROCESSTYPE", DataType = DbType.String, Value = condition.PROCESSTYPE });
}
//零件类别
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
opWhere.Append(" AND PRODUCTTYPE = @PRODUCTTYPE ");
planWhere.Append(" AND PRODUCTTYPE = @PRODUCTTYPE ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
//颜色
if (string.IsNullOrEmpty(condition.COLOR) == false)
{
planWhere.Append(" AND COLOR = @COLOR ");
parameters.Add(new DataParameter { ParameterName = "COLOR", DataType = DbType.String, Value = condition.COLOR });
}
//高低配
if (string.IsNullOrEmpty(condition.HB) == false)
{
planWhere.Append(" AND HB = @HB ");
parameters.Add(new DataParameter { ParameterName = "HB", DataType = DbType.String, Value = condition.HB });
}
//设备编码
if (string.IsNullOrEmpty(condition.MACHINECODE) == false)
{
planWhere.Append(" AND MACHINECODE = @MACHINECODE ");
parameters.Add(new DataParameter { ParameterName = "MACHINECODE", DataType = DbType.String, Value = condition.MACHINECODE });
}
//构成查询语句
string sql = @" select t.*,A_COUNT +B_COUNT + C_COUNT as PLAN_TOTAL,A_COMPLETE + B_COMPLETE + C_COMPLETE as COMPLETE_TOTAL,A_FACT+ B_FACT+ C_FACT as FACT_TOTAL from (
select machinename ,machinecode, producttype , HB,COLOR
,sum(case PRODUCESHIFTTCODE when 'A' then PRODUCECOUNT else 0 end) as A_COUNT , sum (case PRODUCESHIFTTCODE when 'A' then completecount else 0 end) as A_COMPLETE
,A_FACT = (select count(1) from T_AW_MAINOPERATION A,T_AW_MAIN MA where MA.PID = A.PDID and AA.machinecode = A.machinecodde and A.operatestate = 1 and A.PRODUCESHIFTTCODE = 'A'
and MA.HB = AA.HB and MA.COLOR = AA.COLOR
"
+ opWhere.ToString()
+ @" )
,sum(case PRODUCESHIFTTCODE when 'B' then PRODUCECOUNT else 0 end) as B_COUNT , sum (case PRODUCESHIFTTCODE when 'B' then completecount else 0 end) as B_COMPLETE
,B_FACT = (select count(1) from T_AW_MAINOPERATION B,T_AW_MAIN MB where MB.PID = B.PDID and AA.machinecode = B.machinecodde and B.operatestate = 1 and B.PRODUCESHIFTTCODE = 'B'
and MB.HB = AA.HB and MB.COLOR = AA.COLOR
"
+ opWhere.ToString()
+ @" )
,sum(case PRODUCESHIFTTCODE when 'C' then PRODUCECOUNT else 0 end) as C_COUNT , sum (case PRODUCESHIFTTCODE when 'C' then completecount else 0 end) as C_COMPLETE
,C_FACT = (select count(1) from T_AW_MAINOPERATION C,T_AW_MAIN MC where MC.PID = C.PDID and AA.machinecode = C.machinecodde and C.operatestate = 1 and C.PRODUCESHIFTTCODE = 'C'
and MC.HB = AA.HB and MC.COLOR = AA.COLOR
"
+ opWhere.ToString()
+ @" )
from T_AW_PRODUCEPLAN AA where 1=1
"
+ planWhere.ToString()
+ @" group by machinename,machinecode,producttype,HB,COLOR
) t
";
return sql;
}
catch (Exception ex)
{
throw ex;
}
}
/// <summary>
/// 获取查询语句
/// </summary>
/// <param name="user">查询条件</param>
/// <param name="parameters">参数</param>
/// <returns>查询语句</returns>
private string GetQuerySqlForTotalProduct(ProducePlan condition, ref List<DataParameter> parameters)
{
StringBuilder whereBuilder = new StringBuilder();
try
{
StringBuilder opWhere = new StringBuilder();
StringBuilder planWhere = new StringBuilder();
if (condition.STARTCREATEDATE != DateTime.MinValue)
{
opWhere.Append(" AND OPERATEDDATE >= @STARTCREATEDATE");
planWhere.Append(" AND PLANDATE >= @STARTCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "STARTCREATEDATE", Value = condition.STARTCREATEDATE.Date.ToString("yyyy-MM-dd HH:mm:ss") });
}
if (condition.ENDCREATEDATE != DateTime.MinValue)
{
whereBuilder.Append(" AND OPERATEDDATE < @ENDCREATEDATE");
whereBuilder.Append(" AND PLANDATE < @ENDCREATEDATE");
parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "ENDCREATEDATE", Value = condition.ENDCREATEDATE.Date.AddDays(1).ToString("yyyy-MM-dd HH:mm:ss") });
}
//工序类别
if (string.IsNullOrEmpty(condition.PROCESSTYPE) == false)
{
opWhere.Append(" AND PROCESSTYPE = @PROCESSTYPE ");
planWhere.Append(" AND PROCESSTYPE = @PROCESSTYPE ");
parameters.Add(new DataParameter { ParameterName = "PROCESSTYPE", DataType = DbType.String, Value = condition.PROCESSTYPE });
}
//零件类别
if (string.IsNullOrEmpty(condition.PRODUCTTYPE) == false)
{
opWhere.Append(" AND PRODUCTTYPE = @PRODUCTTYPE ");
planWhere.Append(" AND PRODUCTTYPE = @PRODUCTTYPE ");
parameters.Add(new DataParameter { ParameterName = "PRODUCTTYPE", DataType = DbType.String, Value = condition.PRODUCTTYPE });
}
//颜色
if (string.IsNullOrEmpty(condition.COLOR) == false)
{
planWhere.Append(" AND COLOR = @COLOR ");
parameters.Add(new DataParameter { ParameterName = "COLOR", DataType = DbType.String, Value = condition.COLOR });
}
//高低配
if (string.IsNullOrEmpty(condition.HB) == false)
{
planWhere.Append(" AND HB = @HB ");
parameters.Add(new DataParameter { ParameterName = "HB", DataType = DbType.String, Value = condition.HB });
}
//设备编码
if (string.IsNullOrEmpty(condition.MACHINECODE) == false)
{
planWhere.Append(" AND MACHINECODE = @MACHINECODE ");
parameters.Add(new DataParameter { ParameterName = "MACHINECODE", DataType = DbType.String, Value = condition.MACHINECODE });
}
//构成查询语句
string sql = @" select t.*,A_COUNT +B_COUNT + C_COUNT as PLAN_TOTAL,A_COMPLETE + B_COMPLETE + C_COMPLETE as COMPLETE_TOTAL,A_FACT+ B_FACT+ C_FACT as FACT_TOTAL from (
select machinename ,machinecode, producttype , HB,COLOR
,sum(case PRODUCESHIFTTCODE when 'A' then PRODUCECOUNT else 0 end) as A_COUNT , sum (case PRODUCESHIFTTCODE when 'A' then completecount else 0 end) as A_COMPLETE
,A_FACT = (select count(1) from T_AW_MAINOPERATION A,T_AW_PRODUCTATTRIBUTE PA where PA.MPPID = A.PDID and AA.machinecode = A.machinecodde and A.operatestate = 1 and A.PRODUCESHIFTTCODE = 'A'
and PA.VAL3 = AA.HB and PA.VAL4 = AA.COLOR
"
+ opWhere.ToString()
+ @" )
,sum(case PRODUCESHIFTTCODE when 'B' then PRODUCECOUNT else 0 end) as B_COUNT , sum (case PRODUCESHIFTTCODE when 'B' then completecount else 0 end) as B_COMPLETE
,B_FACT = (select count(1) from T_AW_MAINOPERATION B,T_AW_PRODUCTATTRIBUTE PB where PB.MPPID = B.PDID and AA.machinecode = B.machinecodde and B.operatestate = 1 and B.PRODUCESHIFTTCODE = 'B'
and PB.VAL3 = AA.HB and PB.VAL4 = AA.COLOR
"
+ opWhere.ToString()
+ @" )
,sum(case PRODUCESHIFTTCODE when 'C' then PRODUCECOUNT else 0 end) as C_COUNT , sum (case PRODUCESHIFTTCODE when 'C' then completecount else 0 end) as C_COMPLETE
,C_FACT = (select count(1) from T_AW_MAINOPERATION C,T_AW_PRODUCTATTRIBUTE PC where PC.MPPID = C.PDID and AA.machinecode = C.machinecodde and C.operatestate = 1 and C.PRODUCESHIFTTCODE = 'C'
and PC.VAL3 = AA.HB and PC.VAL4 = AA.COLOR
"
+ opWhere.ToString()
+ @" )
from T_AW_PRODUCEPLAN AA where 1=1
"
+ planWhere.ToString()
+ @" group by machinename,machinecode,producttype,HB,COLOR
) t
";
return sql;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
#region 插入信息
/// <summary>
/// 插入信息(单表)
/// </summary>
/// <param name="">信息</param>
/// <returns>插入行数</returns>
public int Insert(ProducePlan model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//插入基本信息
count = session.Insert<ProducePlan>(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "生产记录维护数据层-插入信息"
});
throw;
}
}
#endregion
#region 更新信息
/// <summary>
/// 更新信息
/// </summary>
/// <param name=""></param>
/// <returns>更新行数</returns>
public int Update(ProducePlan model)
{
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//更新基本信息
count = session.Update<ProducePlan>(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "生产记录维护数据层-更新信息"
});
throw;
}
}
#endregion
#region 删除
/// <summary>
/// 删除信息
/// </summary>
/// <param name="model">生产记录(ID)</param>
/// <returns>删除个数</returns>
public int Delete(ProducePlan model)
{
StringBuilder sqlBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
int count = 0;
try
{
using (IDataSession session = AppDataFactory.CreateMainSession())
{
//删除基本信息
sqlBuilder.Append("DELETE FROM T_AW_PRODUCEPLAN WHERE PID=@PID ");
parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID });
count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray());
session.Delete<ProducePlan>(model);
}
return count;
}
catch (Exception ex)
{
LogManager.LogHelper.Error(new LogInfo()
{
ErrorInfo = ex,
Tag = ex.StackTrace,
Info = "生产记录维护数据层-删除信息"
});
throw;
}
}
#endregion
#region 获取计划号
/// <summary>
/// 获取计划号(如果planCode为空,默认为当天第一条,eg:B92015061001。不为空时取最大值加1
/// </summary>
/// <param name="condition">查询条件</param>
/// <returns>查询语句</returns>
public string GetPlanCode()
{
StringBuilder sqlBuilder = new StringBuilder();
List<DataParameter> parameters = new List<DataParameter>();
string planCode = "B9" + DateTime.Now.ToString("yyyyMMdd");
try
{
//构成查询语句
sqlBuilder.AppendLine(@" SELECT max(PLANCODE) FROM T_AW_PRODUCEPLAN WHERE PLANCODE LIKE '" + planCode + "%" + "' ");
using (IDataSession session = AppDataFactory.CreateMainSession())
{
planCode = session.ExecuteSqlScalar(sqlBuilder.ToString(), parameters.ToArray()).ToString();
}
return planCode;
}
catch (Exception ex)
{
throw ex;
}
}
#endregion
}
}