using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Data; using QMFrameWork.Log; using QMFrameWork.Data; using QMFrameWork.Data.Attributes; using QMFrameWork.Common.Util; using QMAPP.Entity; using QMAPP.DAL.Sys; using QMAPP.Entity.Sys; namespace QMAPP.DAL { /// /// 数据层基类 /// 作 者:李炳海 /// 编写日期:2015年01月07日 /// public class BaseDAL { /// /// 数据会话 /// public IDataSession BaseSession = null; #region 逻辑删除 /// /// 逻辑删除 /// /// 实体类型 /// 实体 /// 删除行数 public int LogicDelete(T info) where T : new() { int count = 0; string tableName = ""; string sql = ""; string where = ""; string UPDATEUSR = ""; T oldInfo = default(T); DataChangeManageDAL markManager = new DataChangeManageDAL(); List parameters = new List(); try { //获取表名 Type type = typeof(T); object[] attrsClassAtt = type.GetCustomAttributes(typeof(DBTableAttribute), true); DBTableAttribute tableAtt = (DBTableAttribute)attrsClassAtt[0]; tableName = tableAtt.TableName; //获取主键信息 Dictionary pkColumns = new DataQueryHelper().GetPkColumns(info); UPDATEUSR = BindHelper.GetPropertyValue(info, "UPDATE_USER") == null ? "" : BindHelper.GetPropertyValue(info, "UPDATEUSER").ToString(); //获取原实体 oldInfo = (T)(info as BaseEntity).Clone(); oldInfo = this.BaseSession.Get(info); //逻辑删除 sql = string.Format("UPDATE {0} SET FLAG_DEL=1,UPDATE_DATE=GETDATE(),UPDATE_USER=@UPDATE_USER WHERE ", tableName, UPDATEUSR); parameters.Add(new DataParameter("UPDATE_USER", UPDATEUSR)); foreach (string key in pkColumns.Keys) { where += " AND " + key + " = @" + key; parameters.Add(new DataParameter(key, pkColumns[key])); } sql += where.Substring(4); sql = this.ChangeSqlByDB(sql, this.BaseSession); count = this.BaseSession.ExecuteSql(sql, parameters.ToArray()); //记录痕迹 markManager.Session = this.BaseSession; markManager.RecordDataChangeMark(DataOprType.Delete, UPDATEUSR, oldInfo, info); return count; } catch (Exception ex) { throw ex; } } #endregion #region 变更痕迹保留 /// /// 新增痕迹保留 /// /// 实体类型 /// 实体信息 /// 影响行数 public int InsertWithMark(T info) where T : new() { int count = 0; DataChangeManageDAL markManager = new DataChangeManageDAL(); try { //获取操作人 object oprUserValue = QMFrameWork.Common.Util.BindHelper.GetPropertyValue(info, "CREATEUSER"); string oprUser = oprUserValue == null ? "" : oprUserValue.ToString(); //插入记录 count=this.BaseSession.Insert(info); //记录痕迹 markManager.Session = this.BaseSession; markManager.RecordDataChangeMark(DataOprType.Insert,oprUser, default(T), info); return count; } catch (Exception ex) { throw ex; } } /// /// 更新痕迹保留 /// /// 实体类型 /// 实体信息 /// 影响行数 public int UpdateWithMark(T info) where T : new() { int count = 0; T oldInfo = default(T); DataChangeManageDAL markManager = new DataChangeManageDAL(); try { //获取原实体 oldInfo = (T)(info as BaseEntity).Clone(); oldInfo = this.BaseSession.Get(oldInfo); //更新 count = this.BaseSession.Update(info); //获取操作人 object oprUserValue = QMFrameWork.Common.Util.BindHelper.GetPropertyValue(info, "UPDATEUSER"); string oprUser = oprUserValue == null ? "" : oprUserValue.ToString(); //记录痕迹 markManager.Session = this.BaseSession; markManager.RecordDataChangeMark(DataOprType.Update,oprUser, oldInfo, info); return count; } catch (Exception ex) { throw ex; } } /// /// 删除痕迹保留 /// /// 实体类型 /// 实体信息 /// 影响行数 public int DeleteWithMark(T info) where T : new() { int count = 0; List parameters = new List(); T oldInfo = default(T); DataChangeManageDAL markManager = new DataChangeManageDAL(); try { //获取原实体 oldInfo = (T)(info as BaseEntity).Clone(); oldInfo = this.BaseSession.Get(info); //删除 count = this.BaseSession.Delete(info); //获取操作人 object oprUserValue = QMFrameWork.Common.Util.BindHelper.GetPropertyValue(info, "UPDATEUSER"); string oprUser = oprUserValue == null ? "" : oprUserValue.ToString(); //记录痕迹 markManager.Session = this.BaseSession; markManager.RecordDataChangeMark(DataOprType.Delete,oprUser, oldInfo, info); return count; } catch (Exception ex) { throw ex; } } #endregion #region Sql转换 /// /// Sql转换 /// /// 转换前Sql /// session /// 转换后sql public string ChangeSqlByDB(string sql, IDataSession session) { return sql.Replace("@", session.DbHelper.GetParameterPrefix()); } #endregion #region 获取数据库时间 /// /// 获取数据库时间 /// /// 数据库时间 public DateTime GetDbNowTime() { DateTime nowTime; string sql = null; try { sql = "SELECT GETDATE()"; using (IDataSession session = AppDataFactory.CreateMainSession()) { nowTime = (DateTime)session.ExecuteSqlScalar(sql); } return nowTime; } catch (Exception ex) { throw ex; } } #endregion #region 获取权限过滤语句 /// /// 获取权限过滤语句 /// /// 实体类型 /// 表名(别名) /// 关联字段 /// 用户主键 /// 过滤语句 public string GetFilterByPowerSql(string tblName, string indexName, string userID) { return this.GetFilterByPowerSql(tblName, indexName, new LoginInfo { UserID = userID }); } /// /// 获取权限过滤语句 /// /// 实体类型 /// 表名(别名) /// 关联字段 /// 登录信息 /// 过滤语句 public string GetFilterByPowerSql(string tblName, string indexName, LoginInfo login) { string where = ""; //判断是否为管理员 if (!string.IsNullOrEmpty(login.IsAdmin) && login.IsAdmin.ToLower() == "true") { return where; } //权限过滤语句 switch ((typeof(T)).Name) { case "Corp": where = string.Format("SELECT CORPID FROM T_BD_EMPLOYEECORP WHERE USERID = '{0}' AND CORPID=" + tblName + "." + indexName, login.UserID); break; case "Line": where = string.Format("SELECT LINEID FROM T_BD_EMPLOYEELINE WHERE USERID = '{0}' AND LINEID=" + tblName + "." + indexName, login.UserID); break; case "MatSort": where = string.Format("SELECT MATSORTID FROM T_BD_EMPLOYEEMATSORT WHERE USERID = '{0}' AND MATSORTID=" + tblName + "." + indexName, login.UserID); break; } if (where != "") { where = " AND EXISTS(" + where + ")"; } return where; } #endregion #region SQL查询特殊字符的处理 /// /// SQL查询特殊字符的处理 /// /// sql语句 /// 转换后的sql public string EscapeValue(string sql) { //sql = sql.Replace("", ""); // ADO.Net已经做了,不要自己做 sql = sql.Replace("[", "[[]"); // 这句话一定要在下面两个语句之前,否则作为转义符的方括号会被当作数据被再次处理 sql = sql.Replace("_", "[_]"); sql = sql.Replace("%", "[%]"); return sql; } #endregion #region 输出异常日志 /// /// 输出异常日志 /// /// 异常 /// 自定义异常信息 public void RecordExceptionLog(Exception ex,string info) { LogManager.LogHelper.Error(new LogInfo() { ErrorInfo = ex, Tag = ex.StackTrace, Info = info }); } #endregion } }