using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMAPP.FJC.Entity.Equipment; using QMFrameWork.Data; using QMFrameWork.Log; using System.Data; using QMAPP.DAL; using QMAPP.FJC.Entity.MaximoDataDB; namespace QMAPP.FJC.DAL.Equipment { public class MouldDAL : BaseDAL { #region 获取信息 /// /// 获取信息 /// /// 条件 /// 信息 public Mould Get(Mould model) { try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //获取信息 model = session.Get(model); } return model; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "模具信息数据层-获取信息" }); throw; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(Mould condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); //分页关键字段及排序 page.KeyName = "PID"; page.SortExpression = "MOULD_CODE DESC"; 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 GetAllList(Mould 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) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "操作者数据层-获取模具列表" }); throw; } } public List GetMoulds() { List list = new List(); string sql = null; List parameters = new List(); try { sql = "select * from T_EQP_MOULD"; using (IDataSession session = AppDataFactory.CreateMainSession()) { list = session.GetList(sql, parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = "操作者数据层-获取模具列表" }); throw ex; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(Mould condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.AppendLine(" SELECT M.PID "); sqlBuilder.AppendLine(" ,M.MOULD_NAME "); sqlBuilder.AppendLine(" ,M.MOULD_CODE "); sqlBuilder.AppendLine(" ,M.MoldDataName "); sqlBuilder.AppendLine(" ,M.MOULD_TYPE "); sqlBuilder.AppendLine(" ,M.MATERIAL_CODE "); sqlBuilder.AppendLine(" ,MN.MATERIAL_NAME "); sqlBuilder.AppendLine(" ,M.WORKCENTER_CODE "); sqlBuilder.AppendLine(" ,W.WORKCENTER_NAME "); sqlBuilder.AppendLine(" ,M.MACHINE_PID "); sqlBuilder.AppendLine(" ,MAC.MACHINENAME "); sqlBuilder.AppendLine(" ,M.FACTORY_CODE "); sqlBuilder.AppendLine(" ,F.FACTORY_NAME "); sqlBuilder.AppendLine(" ,M.MOULD_STATE "); sqlBuilder.AppendLine(" ,M.USAGECOUNT "); sqlBuilder.AppendLine(" ,M.MAXIMO_SYNC "); sqlBuilder.AppendLine(" ,M.SYNC_DATE "); sqlBuilder.AppendLine(" ,M.CREATEUSER "); sqlBuilder.AppendLine(" ,M.CREATEDATE "); sqlBuilder.AppendLine(" ,M.UPDATEUSER "); sqlBuilder.AppendLine(" ,M.UPDATEDATE "); sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME "); sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME "); sqlBuilder.AppendLine(" FROM T_EQP_MOULD M "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_MATERIAL MN ON MN.MATERIAL_CODE=M.MATERIAL_CODE "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_WORKCENTER W ON W.WORKCENTER_CODE=M.WORKCENTER_CODE "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON F.FACTORY_CODE=M.FACTORY_CODE "); sqlBuilder.AppendLine(" LEFT JOIN T_BD_MACHINEINFO MAC ON MAC.PID=M.MACHINE_PID "); sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=M.CREATEUSER "); sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=M.UPDATEUSER "); //查询条件 //模具号 if (string.IsNullOrEmpty(condition.MOULD_CODE) == false) { whereBuilder.Append(" AND MOULD_CODE = @MOULD_CODE "); parameters.Add(new DataParameter { ParameterName = "MOULD_CODE", DataType = DbType.String, Value = condition.MOULD_CODE }); } //模具名称 if (string.IsNullOrEmpty(condition.MOULD_NAME) == false) { whereBuilder.Append(" AND MOULD_NAME = @MOULD_NAME "); parameters.Add(new DataParameter { ParameterName = "MOULD_NAME", DataType = DbType.String, Value = condition.MOULD_NAME }); } //模具资料 if (string.IsNullOrEmpty(condition.MoldDataName) == false) { whereBuilder.Append(" AND M.MoldDataName = @MoldDataName "); parameters.Add(new DataParameter { ParameterName = "MoldDataName", DataType = DbType.String, Value = condition.MoldDataName }); } //物料号 if (string.IsNullOrEmpty(condition.MATERIAL_CODE) == false) { whereBuilder.Append(" AND M.MATERIAL_CODE = @MATERIAL_CODE "); parameters.Add(new DataParameter { ParameterName = "MATERIAL_CODE", DataType = DbType.String, Value = condition.MATERIAL_CODE }); } //工作中心编号 if (string.IsNullOrEmpty(condition.WORKCENTER_CODE) == false) { whereBuilder.Append(" AND M.WORKCENTER_CODE = @WORKCENTER_CODE "); parameters.Add(new DataParameter { ParameterName = "WORKCENTER_CODE", DataType = DbType.String, Value = condition.WORKCENTER_CODE }); } //工厂编号 if (string.IsNullOrEmpty(condition.FACTORY_CODE) == false) { whereBuilder.Append(" AND M.FACTORY_CODE = @FACTORY_CODE "); parameters.Add(new DataParameter { ParameterName = "FACTORY_CODE", DataType = DbType.String, Value = condition.FACTORY_CODE }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region 信息是否重复 /// /// 判断模具号是否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsMould(Mould model) { string PID = ""; int count = 0; StringBuilder sqlBuilder = new StringBuilder(); try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sqlBuilder.AppendLine("SELECT COUNT(*) FROM T_EQP_MOULD "); sqlBuilder.AppendLine(" WHERE PID <> @PID AND MOULD_CODE=@MOULD_CODE"); using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), new DataParameter("PID", PID), new DataParameter { ParameterName = "MOULD_CODE", Value = model.MOULD_CODE })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int Insert(Mould model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 更新信息 /// /// 更新信息 /// /// /// 更新行数 public int Update(Mould model) { try { if (BaseSession!=null) { return BaseSession.Update(model); } using (IDataSession session = AppDataFactory.CreateMainSession()) { //更新基本信息 return session.Update(model); } } catch (Exception ex) { throw ex; } } #endregion #region 删除 /// /// 删除信息 /// /// /// 删除个数(model); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 获取模具资料维护列表 /// /// 获取模具资料维护 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetMouldData(MouldData condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetMouldDataSql(condition, ref parameters); #region 排序 //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "UPDATEDATE DESC"; } #endregion using (IDataSession session = AppDataFactory.CreateMainSession()) { // 对应多种数据库 string sqlChange = this.ChangeSqlByDB(sql, session); page = session.GetDataPage(sqlChange, parameters.ToArray(), page); } return page; } catch (Exception ex) { RecordExceptionLog(ex, "模具资料维护-获取列表"); throw ex; } } public MouldData GetMouldCode(MouldData condition) { string sql = null; List parameters = new List(); try { sql = this.GetMouldDataSql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { return session.Get(sql, parameters.ToArray()); } } catch (Exception ex) { RecordExceptionLog(ex, "模具资料维护-获取列表"); throw ex; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetMouldDataSql(MouldData condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { sqlBuilder.AppendLine(@" SELECT PID ,MOULD_ID ,MOULDDATA_CODE ,MOULDDATA_NAME ,MEMO ,CREATEUSER ,CREATEDATE ,UPDATEUSER ,UPDATEDATE FROM T_EQP_MOULDDATA "); if (string.IsNullOrEmpty(condition.MOULD_ID) == false) { whereBuilder.Append(" AND MOULD_ID = @MOULD_ID"); parameters.Add(new DataParameter { ParameterName = "MOULD_ID", DataType = DbType.String, Value = condition.MOULD_ID }); } if (string.IsNullOrEmpty(condition.MOULDDATA_CODE) == false) { whereBuilder.Append(" AND MOULDDATA_CODE = @MOULDDATA_CODE"); parameters.Add(new DataParameter { ParameterName = "MOULDDATA_CODE", DataType = DbType.String, Value = condition.MOULDDATA_CODE }); } //查询条件 if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion #region 插入模具资料维护信息 /// /// 插入模具资料维护信息(单表) /// /// 模具资料维护信息 /// 插入行数 public int InsertMouldData(MouldData model) { int count = 0; try { if (BaseSession != null) { //插入基本信息 count = BaseSession.Insert(model); } else { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } } return count; } catch (Exception ex) { RecordExceptionLog(ex, "插入模具资料维护信息"); throw ex; } } #endregion #region 信息是否重复 /// /// 判断模具资料编码是否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsMouldData(MouldData model) { string PID = ""; int count = 0; StringBuilder sqlBuilder = new StringBuilder(); try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sqlBuilder.AppendLine("SELECT COUNT(*) FROM T_EQP_MOULDDATA "); sqlBuilder.AppendLine(" WHERE PID <> @PID AND MOULDDATA_CODE=@MOULDDATA_CODE "); //sqlBuilder.AppendLine(" OR MOULDDATA_NAME=@MOULDDATA_NAME "); using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), new DataParameter("PID", PID), new DataParameter { ParameterName = "MOULDDATA_CODE", Value = model.MOULDDATA_CODE })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } } /// /// 判断模具资料名称是否存在 /// /// /// true:已存在;fasel:不存在。 public bool ExistsMouldnameData(MouldData model) { string PID = ""; int count = 0; StringBuilder sqlBuilder = new StringBuilder(); try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sqlBuilder.AppendLine("SELECT COUNT(*) FROM T_EQP_MOULDDATA "); sqlBuilder.AppendLine(" WHERE PID <> @PID AND MOULDDATA_NAME=@MOULDDATA_NAME "); using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), new DataParameter("PID", PID), new DataParameter { ParameterName = "MOULDDATA_NAME", Value = model.MOULDDATA_NAME })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } } #endregion #region 删除模具资料维护信息 /// /// 删除信息 /// /// /// 删除个数(model); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 获取导出的数据 /// /// 获取导出的数据 /// /// 查询条件 /// 数据 public DataTable GetExportData(Mould model) { DataTable dt = null; string sql = null; List parameters = new List(); try { //构成查询语句 sql = this.GetQuerySql(model, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { dt = session.GetTable(sql, parameters.ToArray()); dt.TableName = "MouldExp"; } return dt; } catch (Exception ex) { throw ex; } } #endregion public int InsertMouldCounter(MounldCounter model, IDataSession Bsession) { return Bsession.Insert(model); } //获取中间表T_QT_EQUIPMENTINFO的数据; public List GetSelectEquipmentInfo(string equipmentType) { if (string.IsNullOrEmpty(equipmentType)) { return null; } List parameters = new List(); string sql = "SELECT * FROM T_QT_EQUIPMENTINFO WHERE EQUIPMENTTYPE= '" + equipmentType + "'"; using (IDataSession session = AppDataFactory.CreateSession("maindbMaximo")) { return session.GetList(sql, parameters.ToArray()).ToList(); } } //public DataPage GetMouldData(MouldData condition, DataPage page) //{ // throw new NotImplementedException(); //} //public int InsertMouldData(MouldData model) //{ // throw new NotImplementedException(); //} } }