narsil/Tanshu.Accounts.SqlDAO/BusinessLayer/ProductGroupModifierBI.cs
2011-02-01 02:03:22 +05:30

98 lines
4.1 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
using Tanshu.Accounts.Contracts;
using System.Data.SqlClient;
using Tanshu.Data.DAO;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.SqlDAO;
using NHibernate.Criterion;
using NHibernate;
namespace Tanshu.Accounts.Repository
{
public static class ProductGroupModifierBI
{
public static void Insert(ProductGroupModifier productGroupModifier)
{
using (var session = SessionManager.Session)
{
session.Save(productGroupModifier);
}
}
public static void Update(ProductGroupModifier productGroupModifier)
{
using (var session = SessionManager.Session)
{
session.Update(productGroupModifier);
}
}
public static void Delete(int productGroupModifierID)
{
using (var session = SessionManager.Session)
{
session.Delete(new ProductGroupModifier() { ProductGroupModifierID = productGroupModifierID });
}
}
public static IList<ProductGroupModifier> GetProductGroupModifiers()
{
using (var session = SessionManager.Session)
{
return session.CreateCriteria<ProductGroupModifier>()
.List<ProductGroupModifier>();
}
}
public static IList<Modifier> GetProductGroupModifiers(int productGroupID)
{
using (var session = SessionManager.Session)
{
var list = (from pgm in session.QueryOver<ProductGroupModifier>()
where pgm.ProductGroup == null || pgm.ProductGroup.ProductGroupID == productGroupID
select pgm.Modifier).Fetch(x => x.Modifier).Eager.List<Modifier>();
//NHibernateUtil.Initialize(list);
foreach (var item in list)
NHibernateUtil.Initialize(item);
return list;
}
}
public static bool HasCompulsoryModifier(int productGroupID)
{
using (var session = SessionManager.Session)
{
var l = from gf in session.QueryOver<ProductGroupModifier>()
where gf.ShowAutomatically == true && (gf.ProductGroup == null || gf.ProductGroup.ProductGroupID == productGroupID)
select gf;
int c = l.RowCount();
return c > 0;
// var a = session.CreateQuery(@"
//select count(pgm)
//from ProductGroupModifier as pgm
//where pgm.ShowAutomatically = true
//and (ProductGroupID is null or ProductGroupID = :ProductGroupID")
// .SetInt32("ProductGroupID", productGroupID)
// .UniqueResult<int>();
// return a > 0;
//var count1 = session.CreateCriteria<ProductGroupModifier>()
// .Add(Restrictions.Eq("ShowAutomatically", true))
// .Add(Restrictions.IsNull("ProductGroupID"))
// .SetProjection(Projections.Count("ProductGroupModifierID")).UniqueResult();
//return (int)count1 > 0;
////var count = session.CreateSQLQuery (@"
////SELECT COUNT(*) FROM Entities_ProductGroupModifiers WHERE
////var counts = session.QueryOver<ProductGroupModifier>()
//// .Where(c => c.ShowAutomatically == true)
//// .Select(Projections.Count("ProductGroupModifierID"));
//var count = session.CreateCriteria<ProductGroupModifier>()
// .Add(Restrictions.Eq("ShowAutomatically", true))
// .Add(Expression.Conjunction()
// .Add(Restrictions.Eq("ProductGroupID", productGroupID))
// .Add(Restrictions.IsNull("ProductGroupID")))
// .SetProjection(Projections.Count("ProductGroupModifierID")).UniqueResult();
//return (int)count > 0;
}
}
}
}