using System; using System.Collections.Generic; using System.Data; using Easy4net.DBUtility; using Easy4net.Context; namespace Easy4net.Common { /// /// 参数映射集合类 /// public class ParamMap : Map { /// /// 是否分页 /// private bool isPage; /// /// 页面偏移量 /// private int pageOffset; /// /// 每页限制记录数 /// private int pageLimit; /// /// 排序字段 /// private string orderFields; /// /// 是否递减排序 /// private bool isDesc = true; /// /// 数据库参数集合 /// private List m_ParamList = new List(); /// /// 创建一个参数映射集合 /// private ParamMap() { } /// /// 排序的字段 /// public string OrderFields { get { return orderFields; } set { orderFields = value; } } /// /// 是否递减排序 /// public bool IsDesc { get { return isDesc; } set { isDesc = value; } } /// /// 创建一个参数映射集合 /// /// public static ParamMap NewMap() { ParamMap paramMap = new ParamMap(); return paramMap; } /// /// 是否进行分页 /// public bool IsPage { get { return isPage; } } /// /// 每页的偏移量 /// public int PageOffset { get { if (this.ContainsKey("pageIndex") && this.ContainsKey("pageSize")) { int pageIndex = this.getInt("pageIndex"); int pageSize = this.getInt("pageSize"); if (pageIndex <= 0) pageIndex = 1; if (pageSize <= 0) pageSize = 1; return (pageIndex - 1) * pageSize; } return 0; } } /// /// 每页的记录条数限制 /// public int PageLimit { get { if (this.ContainsKey("pageSize")) { return this.getInt("pageSize"); } return 0; } } /// /// 根据键值获取整型数据 /// /// /// public int getInt(string key) { var value = this[key]; return Convert.ToInt32(value); } /// /// 根据键值获取字符串数据 /// /// /// public String getString(string key) { var value = this[key]; return Convert.ToString(value); } /// /// 根据键值获取Double型数据 /// /// /// public Double toDouble(string key) { var value = this[key]; return Convert.ToDouble(value); } /// /// 根据键值获取Long型数据 /// /// /// public Int64 toLong(string key) { var value = this[key]; return Convert.ToInt64(value); } /// /// 根据键值获取Decimal型数据 /// /// /// public Decimal toDecimal(string key) { var value = this[key]; return Convert.ToDecimal(value); } /// /// 根据键值获取DateTime型数据 /// /// /// public DateTime toDateTime(string key) { var value = this[key]; return Convert.ToDateTime(value); } /// /// 设置排序字段及排序方式 /// /// 排序字段 /// 是否为递减排序 public void setOrderFields(string orderFields, bool isDesc) { this.orderFields = orderFields; this.isDesc = isDesc; } /// /// 此方法已过时,请使用 setPageParamters方法分页 /// /// [Obsolete("此方法已过时,请使用 setPageParamters方法分页")] private void setPageIndex(int pageIndex) { this["pageIndex"] = pageIndex; setPages(); } /// /// 此方法已过时,请使用 setPageParamters方法分页 /// /// [Obsolete("此方法已过时,请使用 setPageParamters方法分页")] private void setPageSize(int pageSize) { this["pageSize"] = pageSize; setPages(); } /// /// 分页参数设置 /// /// 第几页,从0开始 /// 每页最多显示几条数据 public void setPageParamters(int page, int limit) { this["pageIndex"] = page; this["pageSize"] = limit; setPages(); } /// /// 设置分页参数 /// private void setPages() { Session session = SessionThreadLocal.Get(); if (this.ContainsKey("pageIndex") && this.ContainsKey("pageSize")) { this.isPage = true; if (session.DbFactory.DbType == DatabaseType.MYSQL || session.DbFactory.DbType == DatabaseType.SQLITE) { this["offset"] = this.PageOffset; this["limit"] = this.PageLimit; this.Remove("pageIndex"); this.Remove("pageSize"); this.Add("offset", this.getInt("offset")); this.Add("limit", this.getInt("limit")); } if (session.DbFactory.DbType == DatabaseType.SQLSERVER) { int pageIndex = this.getInt("pageIndex"); int pageSize = this.getInt("pageSize"); if (pageIndex <= 0) pageIndex = 1; if (pageSize <= 0) pageSize = 1; this["pageStart"] = (pageIndex - 1) * pageSize + 1; this["pageEnd"] = pageIndex * pageSize; this.Remove("pageIndex"); this.Remove("pageSize"); this.Add("pageStart", this.getInt("pageStart")); this.Add("pageEnd", this.getInt("pageEnd")); } if (session.DbFactory.DbType == DatabaseType.ACCESS) { int pageIndex = this.getInt("pageIndex"); int pageSize = this.getInt("pageSize"); this["page_offset"] = pageIndex * pageSize; this["page_limit"] = pageSize; this.Remove("pageIndex"); this.Remove("pageSize"); } } } /// /// 添加键值对映射 /// /// /// public override void Add(object key, object value) { base.Put(key, value); Session session = SessionThreadLocal.Get(); IDbDataParameter param = session.DbFactory.CreateDbParameter(key.ToString(), value); m_ParamList.Add(param); } /// /// 添加键值对映射 /// /// /// public new void Put(object key, object value) { this.Add(key, value); } /// /// 添加键值对映射 /// /// /// public new void setParameter(string key, object value) { this.Add(key, value); } /// /// 输出参数集合 /// /// public IDbDataParameter[] toDbParameters() { int i = 0; IDbDataParameter[] paramArr = new IDbDataParameter[m_ParamList.Count]; foreach (IDbDataParameter dbParameter in m_ParamList) { paramArr[i] = dbParameter; i++; } return paramArr; } } }