using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMAPP.FJC.Entity.Basic; using QMFrameWork.Data; using QMFrameWork.Log; using System.Data; namespace QMAPP.FJC.DAL.Basic { public class FactoryDAL { #region 获取信息 /// /// 获取信息 /// /// 条件 /// 信息 public Factory Get(Factory 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(Factory condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); //分页关键字段及排序 page.KeyName = "PID"; page.SortExpression = "FACTORY_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(Factory 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; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(Factory condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { //构成查询语句 sqlBuilder.AppendLine(" SELECT P.PID "); sqlBuilder.AppendLine(" ,P.FACTORY_CODE "); sqlBuilder.AppendLine(" ,P.FACTORY_NAME "); sqlBuilder.AppendLine(" ,P.FACTORY_SHORT "); sqlBuilder.AppendLine(" ,P.REMARK "); sqlBuilder.AppendLine(" ,P.CORP_PID "); sqlBuilder.AppendLine(" ,R.CORP_CODE "); sqlBuilder.AppendLine(" ,R.CORP_NAME "); sqlBuilder.AppendLine(" ,P.FLAGDEL "); sqlBuilder.AppendLine(" ,P.CREATEUSR "); sqlBuilder.AppendLine(" ,P.CREATEDATE "); sqlBuilder.AppendLine(" ,P.UPDATEUSR "); sqlBuilder.AppendLine(" ,P.UPDATEDATE "); sqlBuilder.AppendLine(" ,C.USERNAME AS CREATEUSERNAME "); sqlBuilder.AppendLine(" ,U.USERNAME AS UPDATEUSERNAME "); sqlBuilder.AppendLine(" FROM T_BD_FACTORY P "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_CORP R ON P.CORP_PID=R.PID "); sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER C ON C.USERID=P.CREATEUSR "); sqlBuilder.AppendLine(" LEFT JOIN T_QM_USER U ON U.USERID=P.UPDATEUSR "); whereBuilder.Append(" AND P.FLAGDEL='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.FACTORY_NAME) == false) { whereBuilder.Append(" AND FACTORY_NAME = @FACTORY_NAME "); parameters.Add(new DataParameter { ParameterName = "FACTORY_NAME", DataType = DbType.String, Value = condition.FACTORY_NAME }); } //公司 if (string.IsNullOrEmpty(condition.CORP_PID) == false) { whereBuilder.Append(" AND CORP_PID = @CORP_PID "); parameters.Add(new DataParameter { ParameterName = "CORP_PID", DataType = DbType.String, Value = condition.CORP_PID }); } 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 ExistsPlant(Factory 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_BD_FACTORY "); sqlBuilder.AppendLine(" WHERE PID <> @PID AND FACTORY_CODE=@FACTORY_CODE"); using (IDataSession session = AppDataFactory.CreateMainSession()) { count = Convert.ToInt32(session.ExecuteSqlScalar(sqlBuilder.ToString(), new DataParameter("PID", PID), new DataParameter { ParameterName = "FACTORY_CODE", Value = model.FACTORY_CODE })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { throw ex; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 信息 /// 插入行数 public int Insert(Factory 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(Factory model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //更新基本信息 count = session.Update(model); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 删除 /// /// 删除信息 /// /// /// 删除个数 //提交事务 public int Delete(Factory model) { StringBuilder sqlBuilder = new StringBuilder(); List parameters = new List(); int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //删除基本信息 sqlBuilder.Append("UPDATE T_BD_FACTORY "); sqlBuilder.Append("SET FLAGDEL = '1'"); sqlBuilder.Append("WHERE PID = @PID"); parameters.Add(new DataParameter { ParameterName = "PID", DataType = DbType.String, Value = model.PID }); count = session.ExecuteSql(sqlBuilder.ToString(), parameters.ToArray()); } return count; } catch (Exception ex) { throw ex; } } #endregion #region 获取工厂列表(下拉列表数据源) /// /// 获取工厂列表(下拉列表数据源) /// /// 条件 /// 全部集合 public List GetFactoryList(Factory 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; } } #endregion } }