using System; using System.Collections.Generic; using System.Linq; using System.Text; using QMFrameWork.Data; using QMAPP.MD.Entity; using QMFrameWork.Log; using System.Data; using QMAPP.DAL; namespace QMAPP.MD.DAL { /// /// 模块名称:班次 /// 作 者:郭兆福 /// 编写日期:2017年05月17日 /// public class ShiftDAL:BaseDAL { #region 获取信息 /// /// 获取信息 /// /// 条件 /// *信息 public Shift__ Get(Shift__ model) { try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //获取信息 model = session.Get(model); } return model; } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层-获取信息"); throw ex; } } /// /// 根据条件得到班次名称 /// /// 条件 /// 班次名称 public string GetShiftName(Shift__ condition) { string shiftName = ""; string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { // 对应多种数据库 string sqlChange = this.ChangeSqlByDB(sql, session); Shift__ model = session.Get(sqlChange, parameters.ToArray()); if (null != model) { shiftName = model.SHIFT_NAME; } } return shiftName; } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层 - 获取班次名称"); throw ex; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public DataPage GetList(Shift__ condition, DataPage page) { string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); //分页关键字段及排序 page.KeyName = "PID"; if (string.IsNullOrEmpty(page.SortExpression)) { page.SortExpression = "UPDATEDATE DESC"; } 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; } } #endregion #region 获取查询语句 /// /// 获取查询语句 /// /// 查询条件 /// 参数 /// 查询语句 private string GetQuerySql(Shift__ condition, ref List parameters) { StringBuilder sqlBuilder = new StringBuilder(); StringBuilder whereBuilder = new StringBuilder(); try { sqlBuilder.AppendLine(" SELECT M.PID "); sqlBuilder.AppendLine(" ,M.FACTORY_CODE "); sqlBuilder.AppendLine(" ,F.FACTORY_NAME "); sqlBuilder.AppendLine(" ,M.SHIFT_NAME "); sqlBuilder.AppendLine(" ,M.SHIFT_CODE "); sqlBuilder.AppendLine(" ,M.WORK_START_TIME "); sqlBuilder.AppendLine(" ,M.WORK_END_TIME "); sqlBuilder.AppendLine(" ,M.REMARK "); sqlBuilder.AppendLine(" ,M.CREATEUSER "); sqlBuilder.AppendLine(" ,M.CREATEDATE "); sqlBuilder.AppendLine(" ,M.UPDATEUSER "); sqlBuilder.AppendLine(" ,M.UPDATEDATE "); sqlBuilder.AppendLine(" ,M.FLGDEL "); sqlBuilder.AppendLine(" FROM T_QT_SHIFT M "); sqlBuilder.AppendLine(" LEFT JOIN T_MD_FACTORY F ON M.FACTORY_CODE=F.FACTORY_CODE "); //查询条件 whereBuilder.Append(" AND M.FLGDEL = '0'"); //班次编号 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 (string.IsNullOrEmpty(condition.SHIFT_CODE) == false) { whereBuilder.Append(" AND M.SHIFT_CODE = @SHIFT_CODE"); parameters.Add(new DataParameter { ParameterName = "SHIFT_CODE", DataType = DbType.String, Value = condition.SHIFT_CODE }); } //班次名称 if (string.IsNullOrEmpty(condition.SHIFT_NAME) == false) { whereBuilder.Append(" AND M.SHIFT_NAME LIKE @SHIFT_NAME"); parameters.Add(new DataParameter { ParameterName = "SHIFT_NAME", DataType = DbType.String, Value = EscapeValue(condition.SHIFT_NAME)+"%" }); } if (whereBuilder.Length > 0) { sqlBuilder.Append(" WHERE " + whereBuilder.ToString().Substring(4)); } //sqlBuilder.AppendLine(" order by M.SHIFT_CODE "); return sqlBuilder.ToString(); } catch (Exception ex) { throw ex; } } #endregion /// /// 获取全部用户 /// /// 获取条件 /// 用户信息列表 public List GetAllShift(Shift__ condition) { string sql = null; List list; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { //sql = "SELECT * FROM T_QT_SHIFT ORDER BY SHIFT_CODE "; list = session.GetList(sql, parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { throw; } } #region 信息是否重复 /// /// 信息是否重复 /// /// /// true:已存在;fasel:不存在。 public bool ExistsShift(Shift__ model) { string PID = ""; int count = 0; string sql = null; try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sql = "SELECT COUNT(*) FROM T_QT_SHIFT WHERE PID <> @PID AND SHIFT_CODE=@SHIFT_CODE AND FACTORY_CODE=@FACTORY_CODE"; using (IDataSession session = AppDataFactory.CreateMainSession()) { // 对应多种数据库 string sqlChange = this.ChangeSqlByDB(sql, session); count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, new DataParameter("PID", PID), new DataParameter("SHIFT_CODE", model.SHIFT_CODE), new DataParameter("FACTORY_CODE", model.FACTORY_CODE))); } return count > 0; } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层-信息是否重复表"); throw ex; } } #endregion #region 插入信息 /// /// 插入信息(单表) /// /// 班次信息 /// 插入行数 public int Insert(Shift__ model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //插入基本信息 count = session.Insert(model); } return count; } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层-插入信息"); throw ex; } } #endregion #region 更新信息 /// /// 更新信息 /// /// 班次信息 /// 更新行数 public int Update(Shift__ model) { int count = 0; try { using (IDataSession session = AppDataFactory.CreateMainSession()) { //更新基本信息 count = session.Update(model); } return count; } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层-更新信息"); throw ex; } } #endregion #region 删除 /// /// 删除信息 /// /// 班次信息(ID) /// 删除个数 public int Delete(Shift__ 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) { RecordExceptionLog(ex, "班次信息数据层-删除信息"); throw ex; } } #endregion #region 校验时间 /// /// 校验时间 /// /// /// true:已存在;fasel:不存在。 public bool ExistsShiftTime(Shift__ model) { string PID = ""; int count = 0; string sql = null; try { if (string.IsNullOrEmpty(model.PID) == false) { PID = model.PID; } sql = "SELECT COUNT(*) FROM T_QT_SHIFT WHERE PID <> @PID AND SHIFT_CODE=@SHIFT_CODE"; using (IDataSession session = AppDataFactory.CreateMainSession()) { // 对应多种数据库 string sqlChange = this.ChangeSqlByDB(sql, session); count = Convert.ToInt32(session.ExecuteSqlScalar(sqlChange, new DataParameter("PID", PID), new DataParameter { ParameterName = "SHIFT_CODE", Value = model.SHIFT_CODE })); } if (count > 0) { return true; } else { return false; } } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层-校验时间"); throw ex; } } #endregion #region 获取列表 /// /// 获取列表 /// /// 条件 /// 数据页 /// 数据页 public List GetList(Shift__ condition) { string sql = null; List parameters = new List(); List psList = new List(); try { sql = this.GetQuerySql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { // 对应多种数据库 string sqlChange = this.ChangeSqlByDB(sql, session); psList = session.GetList(sqlChange, parameters.ToArray()).ToList(); } return psList; } catch (Exception ex) { RecordExceptionLog(ex, "班次信息数据层-获取列表"); throw ex; } } #endregion #region 物料信息下拉 /// /// /// /// /// public List GetShiftList(Shift__ condition) { List list = new List(); string sql = null; List parameters = new List(); try { sql = this.GetQuerySql(condition, ref parameters); using (IDataSession session = AppDataFactory.CreateMainSession()) { string sqlChange = ChangeSqlByDB(sql, session); list = session.GetList(sqlChange, parameters.ToArray()).ToList(); } return list; } catch (Exception ex) { RecordExceptionLog(ex, "物料信息-列表"); throw ex; } } #endregion } }