narsil/Tanshu.Accounts.Repository/ProductGroupBI.cs

74 lines
2.6 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Tanshu.Accounts.Entities;
using NHibernate;
using NHibernate.Criterion;
using NHibernate.Transform;
namespace Tanshu.Accounts.Repository
{
public class ProductGroupBI : UnitOfWork<ProductGroup>
{
public new IList<ProductGroup> List()
{
return _session.QueryOver<ProductGroup>()
.OrderBy(x => x.SortOrder).Asc
.ThenBy(x => x.Name).Asc
.List();
}
public new IList<ProductGroup> List(Expression<Func<ProductGroup, bool>> query)
{
_session.FlushMode = FlushMode.Never;
return _session.QueryOver<ProductGroup>()
.Where(query)
.OrderBy(x => x.SortOrder).Asc
.ThenBy(x => x.Name).Asc
.List();
}
public IList<ProductGroup> SaleList()
{
ProductGroup pgAlias = null;
Product pAlias = null;
ICriterion isActive = Restrictions.Where<Product>(x => x.IsActive);
return _session.QueryOver<ProductGroup>(() => pgAlias)
.Left.JoinAlias(x => x.Products, () => pAlias, isActive)
.Where(x => x.IsActive)
.OrderBy(x => x.SortOrder).Asc
.ThenBy(x => x.Name).Asc
.ThenBy(() => pAlias.SortOrder).Asc
.ThenBy(() => pAlias.Name).Asc
.TransformUsing(Transformers.DistinctRootEntity)
.List();
}
public IList<string> GetProductGroupTypes()
{
const string query = @"select distinct(pg.GroupType) from ProductGroup pg order by pg.GroupType";
var hnq = _session.CreateQuery(query);
return hnq.List<string>();
}
public ProductGroup GetProductGroupOfProduct(Guid productID)
{
using (var session = SessionManager.Session)
{
var item = (from p in session.QueryOver<Product>()
where p.ProductID == productID
select p.ProductGroup).SingleOrDefault<ProductGroup>();
NHibernateUtil.Initialize(item);
return item;
}
}
public void UpdateSortOrder(IList<ProductGroup> list)
{
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
var productGroup = _session.Get<ProductGroup>(item.ProductGroupID);
productGroup.SortOrder = i;
_session.Update(productGroup);
}
}
}
}