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

128 lines
3.9 KiB
C#
Raw Normal View History

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;
using NHibernate.Criterion;
namespace Tanshu.Accounts.Repository
{
public class FoodTableBI : IDisposable
{
ISession session;
bool disposeSession;
bool useTransaction;
public FoodTableBI()
{
this.session = SessionManager.Session;
disposeSession = true;
this.useTransaction = false;
}
public FoodTableBI(ISession session)
{
this.session = session;
disposeSession = false;
this.useTransaction = false;
}
public FoodTableBI(ISession session, bool useTransaction)
{
this.session = session;
disposeSession = false;
this.useTransaction = useTransaction;
}
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;
if (status == null || status == string.Empty)
table.VoucherID = 0;
else
table.VoucherID = voucherID;
table.Status = status;
session.Update(table);
trans.Commit();
}
else
{
var table = session.CreateCriteria<FoodTable>()
.Add(Restrictions.Eq("Name", name))
.UniqueResult<FoodTable>();
if (status == null || status == string.Empty)
table.VoucherID = 0;
else
table.VoucherID = voucherID;
table.Status = status;
session.Update(table);
}
}
public void Dispose()
{
if (disposeSession)
session.Dispose();
}
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;
}
}
}
}