public PagedTableData Query(Type type, IEnumerable <QuickQueryProperty> properties, QueryInfo queryInfo, NameValueCollection addtionalCondtions, int?ApplyTypeID) { ISession session = NHibernateHelper.GetCurrentSession(); ICriterion criterion = GetCriterion(properties); string searchName = null; //所搜索的单位名称 string[] allKeys = addtionalCondtions.AllKeys; //所有的参数 if (criterion != null) { string keyValue = null; //只获取单位更名或者单位更名加延续的参数 ApplyTypeID if (System.Array.IndexOf(allKeys, "ApplyTypeID") == -1) { if (System.Array.IndexOf(allKeys, "applytypeid") > -1) { keyValue = addtionalCondtions["applytypeid"]; } } else { keyValue = addtionalCondtions["ApplyTypeID"]; } if (keyValue.IsNotNullAndEmpty() && (keyValue == "13" || keyValue == "16")) { searchName = criterion.ToString().Substring(10); criterion = null; //单位更名或者单位更名加延续 清除所搜索的单位名称 } } var projections = properties .Select(p => Projections.Property(p.Name)) .ToArray(); ICriteria criteria = session.CreateCriteria(type).AddIf(criterion, criterion != null); //criteria.Add(Restrictions.Gt("ID", 0)); string sql = "{0}"; foreach (var key in allKeys) { if (key == "ApplyTypeID" || key == "applytypeid") { if (searchName.IsNotNullAndEmpty()) { sql = (sql == "{0}") ? string.Format(sql, "CompanyID in (select CompanyID from Engineers where ApplyTypeID='" + addtionalCondtions[key] + "' and Engineerid in (select EngineerId from Reg_changes where NewCompanyName like '" + searchName + "' or OldCompanyName like '" + searchName + "')){0}") : string.Format(sql, "and CompanyID in (select CompanyID from Engineers where ApplyTypeID='" + addtionalCondtions[key] + "' and Engineerid in (select EngineerId from Reg_changes where NewCompanyName like '" + searchName + "' or OldCompanyName like '" + searchName + "')){0}"); } else { string keyValue = addtionalCondtions["ApplyTypeID"]; if (keyValue.IsNotNullAndEmpty() && (keyValue == "4" || keyValue == "17")) { sql = (sql == "{0}") ? string.Format(sql, "CompanyID in (select NewCompanyID from reg_changes where ApplyTypeID='" + addtionalCondtions[key] + "'){0}") : string.Format(sql, "and CompanyID in (select NewCompanyID from reg_changes where ApplyTypeID='" + addtionalCondtions[key] + "'){0}"); } else { sql = (sql == "{0}") ? string.Format(sql, "CompanyID in (select CompanyID from Engineers where ApplyTypeID='" + addtionalCondtions[key] + "'){0}") : string.Format(sql, "and CompanyID in (select CompanyID from Engineers where ApplyTypeID='" + addtionalCondtions[key] + "'){0}"); } } } if (key == "CheckStateID" || key == "checkstateid") { sql = (sql == "{0}") ? string.Format(sql, "CheckStateID='" + addtionalCondtions[key] + "'{0}") : string.Format(sql, "and CheckStateID='" + addtionalCondtions[key] + "'{0}"); } if (key == "CheckStateIDE2" || key == "checkstateide2") { sql = (sql == "{0}") ? string.Format(sql, "CheckStateIDE2='" + addtionalCondtions[key] + "'{0}") : string.Format(sql, "and CheckStateIDE2='" + addtionalCondtions[key] + "'{0}"); } if (key == "CheckStateID2" || key == "checkstateid2") { sql = (sql == "{0}") ? string.Format(sql, "CheckStateIDE2='" + addtionalCondtions[key] + "'{0}") : string.Format(sql, "and CheckStateIDE2='" + addtionalCondtions[key] + "'{0}"); } if (key == "CheckStateID3" || key == "checkstateid3") { sql = (sql == "{0}") ? string.Format(sql, "CheckStateIDE2='" + addtionalCondtions[key] + "'{0}") : string.Format(sql, "and CheckStateIDE2='" + addtionalCondtions[key] + "'{0}"); } if (key == "areaid" || key == "AreaID") { sql = (sql == "{0}") ? string.Format(sql, " AreaID='" + addtionalCondtions[key] + "'{0}") : string.Format(sql, " and AreaID='" + addtionalCondtions[key] + "'{0}"); } if (key == "pointid" || key == "PointId") { sql = (sql == "{0}") ? string.Format(sql, " TrainPointID='" + addtionalCondtions[key] + "'{0}") : string.Format(sql, " and TrainPointID='" + addtionalCondtions[key] + "'{0}"); } } //object checkState = null; //var checkStateType = type.GetProperty("CheckStateIDE2").PropertyType; //checkState = Enum.Parse(checkStateType, addtionalCondtions["CheckStateIDE2"]); //criteria.AddIf(Restrictions.Eq("CheckStateIDE2", GetStates(type, "CheckStateIDE2", addtionalCondtions["CheckStateIDE2"])), allKeys.Contains("CheckStateIDE2")); sql = string.Format(sql, ""); if (sql != "") { criteria.Add(new SQLCriterion(new NHibernate.SqlCommand.SqlString(sql), new object[] { }, new NHibernate.Type.IType[] { })); } ICriteria countCriteria = criteria.SetProjection(Projections.Count(properties.Select(p => p.Name).FirstOrDefault())); int count = (int)countCriteria.UniqueResult(); Pager pager = new Pager(queryInfo.Page, queryInfo.PageSize, count, queryInfo.OrderBy, queryInfo.IsDesc); ICriteria dataCriteria = criteria .SetProjection(projections) .SetFirstResult(pager.FristDataPos - 1) .SetMaxResults(pager.TakeCount); IList <object[]> list = criteria.List <object[]>(); DataTable table = new DataTable(type.Name); foreach (QuickQueryProperty property in properties) { table.Columns.Add(property.Text, property.Type); } foreach (var row in list) { table.Rows.Add(row); } return(new PagedTableData( pager, table, properties.Select(p => p.Format), properties.Select(p => p.IsBusinessIdProperty), properties.Select(p => p.IsTextProperty) )); }
public object GetByDatabaseID(Type type, object id) { ISession session = NHibernateHelper.GetCurrentSession(); return(session.Get(type, id)); }
protected virtual ISession GetSession() { return(NHibernateHelper.GetCurrentSession()); }