narsil/Tanshu.Accounts.Repository/BusinessLayer/FoodTableBI.cs

139 lines
4.2 KiB
C#

using System;
using System.Collections.Generic;
using NHibernate;
using NHibernate.Criterion;
using Tanshu.Accounts.Entities;
using System.Linq;
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 Get(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;
}
}
public void UpdateStatus(Voucher voucher)
{
string status;
if (!voucher.Printed)
status = "running";
else if (voucher.Settlements.Count(x => x.Settled == SettleOption.Unsettled) != 0 && voucher.Void == false)
status = "printed";
else
status = null;
UpdateStatus(voucher.TableID, voucher.VoucherID, status);
}
}
}