narsil/Tanshu.Accounts.Repository/ProductBI.cs
2014-11-20 13:42:20 +05:30

82 lines
2.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Tanshu.Accounts.Contracts;
using Tanshu.Accounts.Entities;
using NHibernate;
namespace Tanshu.Accounts.Repository
{
public class ProductBI : UnitOfWork<Product>
{
public Product Get(string nameAndUnits)
{
return Get(x => x.Name + " (" + x.Units + ")" == nameAndUnits);
}
public new IList<Product> List(Expression<Func<Product, bool>> query)
{
var list = _session.QueryOver<Product>()
.Where(query)
.OrderBy(x => x.ProductGroup).Asc
.OrderBy(x => x.SortOrder).Asc
.ThenBy(x => x.Name).Asc
.List();
foreach (var item in list)
{
NHibernateUtil.Initialize(item.ProductGroup);
NHibernateUtil.Initialize(item.ServiceTax);
NHibernateUtil.Initialize(item.Vat);
}
return list;
}
public new IList<Product> List()
{
var list = _session.QueryOver<Product>()
.OrderBy(x => x.ProductGroup).Asc
.ThenBy(x => x.SortOrder).Asc
.ThenBy(x => x.Name).Asc
.List();
foreach (var item in list)
{
NHibernateUtil.Initialize(item.ProductGroup);
NHibernateUtil.Initialize(item.ServiceTax);
NHibernateUtil.Initialize(item.Vat);
}
return list;
}
public static IList<Product> List(Guid productGroupID)
{
using (var session = SessionManager.Session)
{
return (from product in session.QueryOver<Product>()
where product.ProductGroup.ProductGroupID == productGroupID
select product).List();
}
}
public void UpdateSortOrder(IList<Product> list)
{
int order = 0;
Guid lastGroup = Guid.Empty;
for (int i = 0; i < list.Count; i++)
{
var item = list[i];
if (lastGroup != item.ProductGroup.ProductGroupID)
{
order = 0;
lastGroup = item.ProductGroup.ProductGroupID;
}
else
{
order += 5;
}
var product = _session.Get<Product>(item.ProductID);
product.SortOrder = order;
_session.Update(product);
}
}
}
}