narsil/Tanshu.Accounts.Repository/Cache.cs

152 lines
5.7 KiB
C#

using System;
using System.Collections.Generic;
using NHibernate;
using Tanshu.Accounts.Entities;
using Tanshu.Accounts.Entities.Auth;
namespace Tanshu.Accounts.Repository
{
public class Cache
{
private static IList<ProductGroup> _productGroups = null;
private static Dictionary<Guid, IList<Modifier>> _modifiers = new Dictionary<Guid, IList<Modifier>>();
private static string _machine = Environment.MachineName;
private static string _location = null;
private static Dictionary<int, PrintLocation> _locations = new Dictionary<int, PrintLocation>();
private static IList<Role> _roles = null;
private static bool _log = false;
public static string Location
{
get
{
if (string.IsNullOrEmpty(_location))
{
using (var bi = new MachineLocationBI())
{
var loc = bi.Get(x => x.Machine == _machine);
if (loc != null)
_location = loc.Location;
}
}
return _location;
}
}
public static PrintLocation BasePrinter
{
get
{
if (string.IsNullOrEmpty(Location))
throw new ArgumentException("No location for Machine");
if (!_locations.ContainsKey(Location.GetHashCode()))
{
using (var bi = new PrintLocationBI())
{
var loc = bi.Get(x => x.Location == Location && x.ProductGroup == null);
_locations.Add(Location.GetHashCode(), loc);
}
}
return _locations[Location.GetHashCode()];
}
}
public static PrintLocation KotPrinter(Guid productGroupID)
{
if (string.IsNullOrEmpty(Location))
throw new ArgumentException("No location for Machine");
if (!_locations.ContainsKey(Location.GetHashCode() ^ productGroupID.GetHashCode()))
{
using (var bi = new PrintLocationBI())
{
var loc = bi.Get(x => x.Location == Location && x.ProductGroup.ProductGroupID == productGroupID) ??
bi.Get(x => x.Location == Location && x.ProductGroup == null);
_locations.Add(Location.GetHashCode() ^ productGroupID.GetHashCode(), loc);
}
}
return _locations[Location.GetHashCode() ^ productGroupID.GetHashCode()];
}
public static IList<ProductGroup> ProductGroups()
{
if (_productGroups == null)
{
using (var bi = new ProductGroupBI())
{
var list = bi.SaleList();
foreach (var item in list)
{
NHibernateUtil.Initialize(item.Products);
}
_productGroups = list;
}
foreach (var item in _productGroups)
{
for (var i = item.Products.Count - 1; i >= 0; i--)
{
if (item.Products[i].HasHappyHour)
{
var product = item.Products[i];
product.HasHappyHour = false;
item.Products.Insert(i + 1, new Product() {
ProductID = product.ProductID,
Name = product.FullName,
Units = product.Units,
ProductGroup = product.ProductGroup,
Vat = product.Vat,
ServiceTax = product.ServiceTax,
ServiceCharge = product.ServiceCharge,
IsScTaxable = product.IsScTaxable,
Price = product.Price,
HasHappyHour = true,
IsActive = product.IsActive,
IsNotAvailable = product.IsNotAvailable,
SortOrder = product.SortOrder,
Quantity = product.Quantity
});
}
}
}
}
return _productGroups;
}
public static IList<Modifier> ProductGroupModifiers(Guid productGroupID)
{
if (!_modifiers.ContainsKey(productGroupID))
{
using (var bi = new ProductGroupModifierBI())
{
var list = bi.List(productGroupID);
_modifiers.Add(productGroupID, list);
}
}
return _modifiers[productGroupID];
}
public static IList<Role> UserRoles(Guid userID)
{
if (_roles == null)
{
using (var bi = new UserBI())
{
_roles = bi.Roles(userID);
}
}
return _roles;
}
public static void ClearRoles()
{
_roles = null;
}
public static void Invalidate()
{
_productGroups = null;
_machine = Environment.MachineName;
_location = null;
_locations = new Dictionary<int, PrintLocation>();
_modifiers = new Dictionary<Guid, IList<Modifier>>();
}
public static bool Log
{
get { return _log; }
set { _log = value; }
}
}
}