narsil/Tanshu.Accounts.Repository/BusinessLayer/FoodTableBI.cs
2011-03-12 17:12:02 +05:30

126 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Tanshu.Accounts.Entities;
namespace Tanshu.Accounts.Repository
{
public class FoodTableBI : IDisposable
{
private readonly bool _disposeSession;
private readonly ISession _session;
private readonly bool _useTransaction;
public FoodTableBI()
{
_session = SessionManager.Session;
_disposeSession = true;
_useTransaction = false;
}
public FoodTableBI(ISession session)
{
this._session = session;
_disposeSession = false;
_useTransaction = false;
}
public FoodTableBI(ISession session, bool useTransaction)
{
this._session = session;
_disposeSession = false;
this._useTransaction = useTransaction;
}
#region IDisposable Members
public void Dispose()
{
if (_disposeSession)
_session.Dispose();
}
#endregion
public void Insert(FoodTable foodTable)
{
_session.Save(foodTable);
}
public void Update(FoodTable foodTable)
{
_session.Update(foodTable);
}
public void Delete(int foodTableID)
{
_session.Delete(new FoodTable {FoodTableID = foodTableID});
}
public FoodTable Get(int foodTableID)
{
return _session.Get<FoodTable>(foodTableID);
}
public FoodTable GetByName(string name)
{
return _session.CreateCriteria<FoodTable>()
.Add(Restrictions.Eq("Name", name))
.UniqueResult<FoodTable>();
}
public IList<FoodTable> List()
{
return _session.CreateCriteria<FoodTable>().List<FoodTable>();
}
public void UpdateStatus(string name, int voucherID, string status)
{
if (!_useTransaction)
using (var trans = _session.BeginTransaction())
{
var table = _session.CreateCriteria<FoodTable>()
.Add(Restrictions.Eq("Name", name))
.UniqueResult<FoodTable>();
if (table == null)
return;
table.VoucherID = string.IsNullOrEmpty(status) ? 0 : voucherID;
table.Status = status;
_session.Update(table);
trans.Commit();
}
else
{
var table = _session.CreateCriteria<FoodTable>()
.Add(Restrictions.Eq("Name", name))
.UniqueResult<FoodTable>();
table.VoucherID = string.IsNullOrEmpty(status) ? 0 : voucherID;
table.Status = status;
_session.Update(table);
}
}
public int Move(string name, FoodTable foodTable)
{
using (var trans = _session.BeginTransaction())
{
var oldTable = _session.CreateCriteria<FoodTable>()
.Add(Restrictions.Eq("Name", name))
.UniqueResult<FoodTable>();
foodTable.Status = oldTable.Status;
foodTable.VoucherID = oldTable.VoucherID;
oldTable.Status = null;
oldTable.VoucherID = 0;
_session.Merge(foodTable);
_session.Update(oldTable);
var voucher = _session.Get<Voucher>(foodTable.VoucherID);
voucher.TableID = foodTable.Name;
_session.Update(voucher);
trans.Commit();
return voucher.VoucherID;
}
}
}
}