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; namespace QMAPP.FJC.DAL.Basic { /// /// 模块编号:M2-8 /// 作 用:加工参数标准值设定数据层 /// 作 者:王丹丹 /// 编写日期:2015年06月03日 /// public class ProcessParameterDAL { #region 获得指定列的加工参数数据 /// /// 获得指定列的加工参数数据 /// /// 条件 /// 返回加工参数表 public DataTable GetData(QMAPP.FJC.Entity.Basic.ProcessParameter condition) { try { StringBuilder sbSql = new StringBuilder(); sbSql.AppendFormat(" SELECT TOP {0} {1} FROM {2} WHERE 1=1 ", condition.MonitorCount, condition.SelectColumn, condition.PARATABLENAME); if(condition.MVStart != 0) { sbSql.AppendFormat(" AND {0} >={1}",condition.SelectColumn,condition.MVStart); } if(condition.MVEnd != 0) { sbSql.AppendFormat(" AND {0} <={1}",condition.SelectColumn,condition.MVEnd); } sbSql.Append(" ORDER BY CREATEDATE DESC "); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetTable(sbSql.ToString(), new DataParameter[] { }); } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数数据层-获取加工参数标准值" }); throw; } } #endregion #region 获取信息 /// /// 获取信息 /// /// 条件 /// *信息 public QMAPP.FJC.Entity.Basic.ProcessParameter Get(QMAPP.FJC.Entity.Basic.ProcessParameter model) { try { List listDataPara= new List(); string sql = GetQuerySql(model, ref listDataPara); using (IDataSession session = AppDataFactory.CreateMainSession()) { //获取信息 model = session.Get(sql, listDataPara.ToArray()); } return model; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-获取加工参数标准值" }); throw; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(QMAPP.FJC.Entity.Basic.ProcessParameter condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); #region 排序 //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "UPDATEDATE DESC"; } else { //按照标题排序 if (page.SortExpression.IndexOf("PROCESSTYPETXT") > -1) { page.SortExpression = page.SortExpression.Replace("PROCESSTYPETXT", "PROCESSTYPE"); } } #endregion using (IDataSession session = AppDataFactory.CreateMainSession()) { page = session.GetDataPage(sql, parameters.ToArray(), page); } return page; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-获取列表" }); throw; } } /// /// 获取列表 /// /// 条件 /// 数据页 public List GetList(QMAPP.FJC.Entity.Basic.ProcessParameter 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; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(QMAPP.FJC.Entity.Basic.ProcessParameter condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.AppendLine(" SELECT PP.PID "); sqlBuilder.AppendLine(" ,PP.PARADESCRIBE "); sqlBuilder.AppendLine(" ,PP.PARANAME "); sqlBuilder.AppendLine(" ,PP.PARATABLENAME "); sqlBuilder.AppendLine(" ,PP.MACHINEID "); sqlBuilder.AppendLine(" ,PP.MACHINECODDE "); sqlBuilder.AppendLine(" ,PP.WORKCELL_CODE "); sqlBuilder.AppendLine(" ,PP.PROCESSTYPE "); sqlBuilder.AppendLine(" ,PP.PRODUCELINE "); sqlBuilder.AppendLine(" ,PP.MAXVALUE "); sqlBuilder.AppendLine(" ,PP.MINVALUE "); sqlBuilder.AppendLine(" ,PP.MEMO "); sqlBuilder.AppendLine(" ,PP.SHOWINDEX "); sqlBuilder.AppendLine(" ,PP.CREATEUSER "); sqlBuilder.AppendLine(" ,PP.CREATEDATE "); sqlBuilder.AppendLine(" ,PP.UPDATEUSER "); sqlBuilder.AppendLine(" ,PP.UPDATEDATE "); sqlBuilder.AppendLine(" ,PP.ALIGNVALUE "); sqlBuilder.AppendLine(" ,PP.WIDTH "); sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME "); sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME "); sqlBuilder.AppendLine(" FROM T_BD_ProcessParameter PP "); sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=PP.CREATEUSER "); sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=PP.UPDATEUSER "); //查询条件 //主键 if (string.IsNullOrEmpty(condition.PID) == false) { whereBuilder.Append(" AND PP.PID = @PID "); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = condition.PID }); } //设备编号 if (string.IsNullOrEmpty(condition.MACHINEID) == false) { whereBuilder.Append(" AND PP.MACHINEID = @MACHINEID "); parameters.Add(new DataParameter { ParameterName = "MACHINEID", DataType = DbType.String, Value = condition.MACHINEID }); } if (string.IsNullOrEmpty(condition.MACHINECODDE) == false) { whereBuilder.Append(" AND PP.MACHINECODDE = @MACHINECODDE "); parameters.Add(new DataParameter { ParameterName = "MACHINECODDE", DataType = DbType.String, Value = condition.MACHINECODDE }); } if (string.IsNullOrEmpty(condition.WORKCELL_CODE) == false) { whereBuilder.Append(" AND PP.WORKCELL_CODE = @WORKCELL_CODE "); parameters.Add(new DataParameter { ParameterName = "WORKCELL_CODE", DataType = DbType.String, Value = condition.WORKCELL_CODE }); } //工序类别 if (string.IsNullOrEmpty(condition.PROCESSTYPESEARCH) == false || string.IsNullOrEmpty(condition.PROCESSTYPE) == false) { whereBuilder.Append(" AND PP.PROCESSTYPE = @PROCESSTYPE "); if (string.IsNullOrEmpty(condition.PROCESSTYPESEARCH) == false) { parameters.Add(new DataParameter { ParameterName = "PROCESSTYPE", DataType = DbType.String, Value = condition.PROCESSTYPESEARCH }); } if (string.IsNullOrEmpty(condition.PROCESSTYPE) == false) { parameters.Add(new DataParameter { ParameterName = "PROCESSTYPE", DataType = DbType.String, Value = condition.PROCESSTYPE }); } } //参数名称 if (string.IsNullOrEmpty(condition.PARANAME) == false) { whereBuilder.Append(" AND PP.PARANAME LIKE '%'+" + "@PARANAME" + "+'%'"); parameters.Add(new DataParameter { ParameterName = "PARANAME", DataType = DbType.String, Value = condition.PARANAME }); } if (string.IsNullOrEmpty(condition.PARADESCRIBE) == false) { whereBuilder.Append(" AND PP.PARADESCRIBE LIKE '%'+" + "@PARADESCRIBE" + "+'%'"); parameters.Add(new DataParameter { ParameterName = "PARADESCRIBE", DataType = DbType.String, Value = condition.PARADESCRIBE }); } //工序类别(注塑,用于设备加工记录查询) if (string.IsNullOrEmpty(condition.PROCESSTYPES) == false) { whereBuilder.Append(" AND PP.PROCESSTYPE in (" + condition.PROCESSTYPES + ")"); //parameters.Add(new DataParameter { ParameterName = "PROCESSTYPES", DataType = DbType.String, Value = condition.PROCESSTYPES }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } sqlBuilder.Append(" ORDER BY PP.SHOWINDEX,PP.PARANAME"); return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region 判断信息是否重复 /// /// 判断信息是否重复 /// /// /// true:已存在;fasel:不存在。 public bool ExistsProcessParameter(QMAPP.FJC.Entity.Basic.ProcessParameter model) { string PID = ""; int count = 0; string sql = null; try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sql = "SELECT COUNT(*) FROM T_BD_ProcessParameter WHERE PID <> @PID AND MACHINECODDE=@MACHINECODDE AND PARANAME=@PARANAME "; using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("PID", PID), new DataParameter { ParameterName = "MACHINECODDE", Value = model.MACHINECODDE }, new DataParameter { ParameterName = "PARANAME", Value = model.PARANAME })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-判断信息是否重复" }); throw; } } /// /// 校验数据库中表是否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsParaTableName(QMAPP.FJC.Entity.Basic.ProcessParameter model) { int count = 0; string sql = null; try { sql = "SELECT COUNT(*) FROM DBO.SYSOBJECTS WHERE NAME=@PARATABLENAME "; using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("PARATABLENAME", model.PARATABLENAME))); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-校验数据库中表是否存在" }); throw; } } /// /// 校验数据库中表对应的字段否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsParaName(QMAPP.FJC.Entity.Basic.ProcessParameter model) { int count = 0; string sql = null; try { sql = "SELECT COUNT(*) FROM SYSCOLUMNS WHERE ID=OBJECT_ID(@PARATABLENAME) AND NAME=@PARANAME "; using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sql, new DataParameter("PARATABLENAME", model.PARATABLENAME), new DataParameter("PARANAME", model.PARANAME)) ); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-校验数据库中表对应的字段否存在" }); throw; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int Insert(QMAPP.FJC.Entity.Basic.ProcessParameter model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } return count; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-插入信息" }); throw; } } #endregion #region 更新信息 /// /// 更新信息 /// /// /// 更新行数 public int Update(QMAPP.FJC.Entity.Basic.ProcessParameter model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //更新基本信息 count = session.Update(model); } return count; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-更新信息" }); throw; } } #endregion #region 删除信息 /// /// 删除信息 /// /// 加工参数标准值(ID) /// 删除个数 public int Delete(QMAPP.FJC.Entity.Basic.ProcessParameter model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //删除基本信息 count = session.Delete(model); } return count; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-删除信息" }); throw; } } #endregion public DataPage GetParamList(ProcessParameter condition, DataPage page) { List parameters = new List(); try { if (string.IsNullOrEmpty(condition.PARATABLENAME)) { throw new Exception("未确定参数表名称!"); } StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT PA0.PID "); sql.AppendLine(" ,PA0.PDID "); sql.AppendLine(" ,PA0.PRODUCTCODE "); sql.AppendLine(" ,PA0.MOID "); sql.AppendLine(" ,PA0.MACHINENAME "); sql.AppendLine(" ,PA0.MACHINECODDE "); sql.AppendLine(" ,PA0.MOLDNUMBER "); sql.AppendLine(" ,PA0.CREATEDATE "); sql.AppendLine(","+condition.Fileds+" "); sql.AppendLine("FROM "); var tables = condition.PARATABLENAME.Split(','); for (int i = 0; i < tables.Length; i++) { if (i == 0) { sql.AppendLine(string.Format("{0} AS PA{1} ", tables[i], i)); } else { sql.AppendLine(string.Format("LEFT JOIN {0} AS PA{1} ", tables[i], i)); sql.AppendLine(string.Format("ON PA0.PID = PA{1}.PID ", tables[i], i)); } } sql.AppendLine("WHERE 1=1 "); if (!string.IsNullOrEmpty(condition.MACHINECODDE)) { sql.AppendLine("AND PA0.MACHINECODDE=@MACHINECODDE "); parameters.Add(new DataParameter("MACHINECODDE", condition.MACHINECODDE)); } if (!string.IsNullOrEmpty(condition.MouldCode)) { sql.AppendLine("AND PA0.MOLDNUMBER=@MouldCode "); parameters.Add(new DataParameter("MouldCode", condition.MouldCode)); } if (!string.IsNullOrEmpty(condition.ProductCode)) { sql.AppendLine("AND PA0.PRODUCTCODE=@ProductCode "); parameters.Add(new DataParameter("ProductCode", condition.ProductCode)); } if (!string.IsNullOrEmpty(condition.CREATEDATESTART)) { sql.AppendLine(" AND PA0.CREATEDATE >= @CREATEDATESTART"); parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "CREATEDATESTART", Value = condition.CREATEDATESTART }); } if (!string.IsNullOrEmpty(condition.CREATEDATEEND)) { sql.AppendLine(" AND PA0.CREATEDATE <= @CREATEDATEEND"); parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "CREATEDATEEND", Value = condition.CREATEDATEEND }); } #region 排序 //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "CREATEDATE DESC"; } #endregion using (IDataSession session = AppDataFactory.CreateMainSession()) { page = session.GetDataPage(sql.ToString(), parameters.ToArray(), page); } return page; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-获取列表" }); throw; } } public DataTable GetParameter(ProcessParameter condition) { List parameters = new List(); try { if (string.IsNullOrEmpty(condition.PARATABLENAME)) { throw new Exception("未确定参数表名称!"); } StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT PA0.PID "); sql.AppendLine(" ,PA0.PDID "); sql.AppendLine(" ,PA0.PRODUCTCODE "); sql.AppendLine(" ,PA0.MOID "); sql.AppendLine(" ,PA0.MACHINENAME "); sql.AppendLine(" ,PA0.MACHINECODDE "); sql.AppendLine(" ,PA0.MOLDNUMBER "); sql.AppendLine(" ,PA0.CREATEDATE "); sql.AppendLine("," + condition.Fileds + " "); sql.AppendLine("FROM "); var tables = condition.PARATABLENAME.Split(','); for (int i = 0; i < tables.Length; i++) { if (i == 0) { sql.AppendLine(string.Format("{0} AS PA{1} ", tables[i], i)); } else { sql.AppendLine(string.Format("LEFT JOIN {0} AS PA{1} ", tables[i], i)); sql.AppendLine(string.Format("ON PA0.PID = PA{1}.PID ", tables[i], i)); } } sql.AppendLine("WHERE 1=1 "); if (!string.IsNullOrEmpty(condition.MACHINECODDE)) { sql.AppendLine("AND PA0.MACHINECODDE=@MACHINECODDE "); parameters.Add(new DataParameter("MACHINECODDE", condition.MACHINECODDE)); } if (!string.IsNullOrEmpty(condition.MouldCode)) { sql.AppendLine("AND PA0.MOLDNUMBER=@MouldCode "); parameters.Add(new DataParameter("MouldCode", condition.MouldCode)); } if (!string.IsNullOrEmpty(condition.ProductCode)) { sql.AppendLine("AND PA0.PRODUCTCODE=@ProductCode "); parameters.Add(new DataParameter("ProductCode", condition.ProductCode)); } if (!string.IsNullOrEmpty(condition.CREATEDATESTART)) { sql.AppendLine(" AND PA0.CREATEDATE >= @CREATEDATESTART"); parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "CREATEDATESTART", Value = condition.CREATEDATESTART }); } if (!string.IsNullOrEmpty(condition.CREATEDATEEND)) { sql.AppendLine(" AND PA0.CREATEDATE <= @CREATEDATEEND"); parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "CREATEDATEEND", Value = condition.CREATEDATEEND }); } using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetTable(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-获取列表" }); throw; } } public DataTable GetParamExport(ProcessParameter condition) { List parameters = new List(); try { if (string.IsNullOrEmpty(condition.PARATABLENAME)) { throw new Exception("未确定参数表名称!"); } StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT PA0.PID "); sql.AppendLine(" ,PA0.PDID "); sql.AppendLine(" ,PA0.PRODUCTCODE "); sql.AppendLine(" ,PA0.MOID "); sql.AppendLine(" ,PA0.MACHINENAME "); sql.AppendLine(" ,PA0.MACHINECODDE "); sql.AppendLine(" ,PA0.MOLDNUMBER "); sql.AppendLine(" ,PA0.CREATEDATE "); sql.AppendLine(","+condition.Fileds+" "); sql.AppendLine("FROM "); var tables = condition.PARATABLENAME.Split(','); for (int i = 0; i < tables.Length; i++) { if (i == 0) { sql.AppendLine(string.Format("{0} AS PA{1} ", tables[i], i)); } else { sql.AppendLine(string.Format("LEFT JOIN {0} AS PA{1} ", tables[i], i)); sql.AppendLine(string.Format("ON PA0.PID = PA{1}.PID ", tables[i], i)); } } sql.AppendLine("WHERE 1=1 "); if (!string.IsNullOrEmpty(condition.MACHINECODDE)) { sql.AppendLine("AND PA0.MACHINECODDE=@MACHINECODDE "); parameters.Add(new DataParameter("MACHINECODDE", condition.MACHINECODDE)); } if (!string.IsNullOrEmpty(condition.MouldCode)) { sql.AppendLine("AND PA0.MOLDNUMBER=@MouldCode "); parameters.Add(new DataParameter("MouldCode", condition.MouldCode)); } if (!string.IsNullOrEmpty(condition.ProductCode)) { sql.AppendLine("AND PA0.PRODUCTCODE=@ProductCode "); parameters.Add(new DataParameter("ProductCode", condition.ProductCode)); } if (!string.IsNullOrEmpty(condition.CREATEDATESTART)) { sql.AppendLine(" AND PA0.CREATEDATE >= @CREATEDATESTART"); parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "CREATEDATESTART", Value = condition.CREATEDATESTART }); } if (!string.IsNullOrEmpty(condition.CREATEDATEEND)) { sql.AppendLine(" AND PA0.CREATEDATE <= @CREATEDATEEND"); parameters.Add(new DataParameter() { DataType = DbType.String, ParameterName = "CREATEDATEEND", Value = condition.CREATEDATEEND }); } sql.AppendLine(" ORDER BY PA0.CREATEDATE DESC"); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.GetTable(sql.ToString(), parameters.ToArray()); } } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "加工参数标准值设定数据层-获取列表" }); throw; } } public bool HasParameterRecord(string productcode,string parametertable) { try { StringBuilder sql = new StringBuilder(); sql.AppendLine("SELECT * "); sql.AppendLine(" FROM [" + parametertable + "]"); sql.AppendLine(" WHERE [PRODUCTCODE] = @productcode"); List parameters = new List(); parameters.Add(new DataParameter("productcode", productcode)); using (IDataSession session = AppDataFactory.CreateMainSession()) { var record = session.GetTable(sql.ToString(), parameters.ToArray()); return record.Rows.Count > 0; } } catch (Exception ex) { throw ex; } } } }