账户登陆的时候可以查看消费金额和卡内余额,实时刷新数据库中的数据。
窗体
IDAL层
/// <summary>
/// 客户登录
/// </summary>
/// <param name="customerEntity"></param>
/// <returns></returns>
List<CustomerEntity> login(CustomerEntity customerEntity);
/// <summary>
/// 根据卡号查询客户信息
/// </summary>
/// <param name="cardID"></param>
/// <returns></returns>
List<CustomerEntity> getCustomerByCardID(String cardID);
/// <summary>
/// 获取服务器时间
/// </summary>
/// <returns></returns>
string getTime();
/// <summary>
/// 更新客户表金额
/// </summary>
/// <param name="cardID"></param>
/// <param name="c"
/// ></param>
/// <returns></returns>
int updateCustomerCash(string cardID, decimal cCash);
DAL层
根据卡号查询客户
/// <summary>
/// 根据卡号查询客户
/// </summary>
/// <param name="cardID"></param>
/// <returns></returns>
public List<CustomerEntity> getCustomerByCardID(string cardID)
{
string sql = "select * from T_Customer where cardID=@cardID";
SqlParameter[] ps =
{
new SqlParameter("@cardID",cardID)
};
DataTable dt = SqlHelper.GetDataTable(sql, CommandType.Text, ps);
ConvertSQLHelper convert = new ConvertSQLHelper();
List<CustomerEntity> list = convert.ConvertToModel<CustomerEntity>(dt);
return list;
}
客户登陆
/// <summary>
/// 客户登陆
/// </summary>
/// <param name="customerEntity"></param>
/// <returns></returns>
public List<CustomerEntity> login(CustomerEntity customerEntity)
{
string sql = "select * from T_Customer where cardID=@cardID and cPassword=@cPassword and cState='"+Constant.CSTATEUSE+"'";
SqlParameter[] ps =
{
new SqlParameter("@cardID",customerEntity.cardID),
new SqlParameter("@cPassword",customerEntity.cPassword)
};
DataTable dt = SqlHelper.GetDataTable(sql, CommandType.Text, ps);
ConvertSQLHelper convert = new ConvertSQLHelper();
List<CustomerEntity> list = convert.ConvertToModel<CustomerEntity>(dt);
return list;
}
获取时间
/// <summary>
/// 获取百度的时间
/// </summary>
/// <returns></returns>
public string getTime()
{
WebRequest request = null;
WebResponse response = null;
WebHeaderCollection headerCollection = null;
string datetime = string.Empty;
try
{
request = WebRequest.Create("https://www.baidu.com");
request.Timeout = 3000;
request.Credentials = CredentialCache.DefaultCredentials;
response = request.GetResponse();
headerCollection = response.Headers;
foreach (var h in headerCollection.AllKeys)
{
if (h == "Date")
{
datetime = headerCollection[h];
}
}
return datetime;
}
catch (Exception) { return datetime; }
finally
{
if (request != null)
{ request.Abort(); }
if (response != null)
{ response.Close(); }
if (headerCollection != null)
{ headerCollection.Clear(); }
}
}
更新客户表中的金额
//更新客户表中金额的值
public int updateCustomerCash(string cardID,decimal cCash)
{
string sql = "update T_Customer set cCash=@cCash where cardID=@cardID";
SqlParameter[] ps =
{
new SqlParameter("@cCash",cCash),
new SqlParameter("@cardID",cardID)
};
return SqlHelper.ExecuteNonQuery(sql, CommandType.Text, ps);
}
IBLL层
/// <summary>
/// 登录
/// </summary>
/// <param name="customerEntity"></param>
/// <returns></returns>
bool login1(CustomerEntity customerEntity);
/// <summary>
/// 获取网络时间
/// </summary>
/// <returns></returns>
string getTime();
/// <summary>
/// 上机
/// </summary>
/// <param name="cardID"></param>
/// <returns></returns>
Hashtable onLine(string cardID);
/// <summary>
/// 计算消费金额
/// </summary>
/// <param name="rate"></param>
/// <param name="costTime"></param>
/// <returns></returns>
decimal getCostMoney(string rate, string costTime);
/// <summary>
/// 判断余额还足,否则自动结账,强制下机
/// </summary>
/// <param name="cCash"></param>
/// <param name="costMoney"></param>
/// <param name="cardID"></param>
/// <param name="cName"></param>
/// <param name="type"></param>
/// <returns></returns>
bool ForceOffLine(string cCash, string costMoney, string cardID, string cName, string type);
BLL层
登陆
/// <summary>
/// 登录
/// </summary>
/// <param name="customerEntity"></param>
/// <returns></returns>
public bool login1(CustomerEntity customerEntity)
{
if (customerEntity.cardID == "" || customerEntity.cPassword == "")
{
throw new Exception("请补全信息");
}
CustomerIDAL customerIdal = customerFactory.Customer();
List<CustomerEntity> list = customerIdal.login(customerEntity);
if (list.Count > 0)//登录成功
{
return true;
}
else
{
throw new Exception("账号或密码不对");
}
}
获取时间
/// <summary>
/// 获取网络时间
/// </summary>
/// <returns></returns>
public string getTime()
{
CustomerFactory customerFactory = new CustomerFactory();
CustomerIDAL customerIDAL = customerFactory.Customer();
string datetime = customerIDAL.getTime();
return datetime;
}
上机
/// <summary>
/// 上机
/// </summary>
/// <param name="cardID"></param>
/// <returns></returns>
public Hashtable onLine(string cardID)
{
OnLineChain.OnLineChain onLineChain = new OnLineChain.OnLineChain();
Hashtable ht= onLineChain.doHandler(cardID);
//返回结果
return ht;
}
//这里用了上机的职责链模式
这里用到了职责链模式:职责链模式 ——上机
计算消费金额
/// <summary>
/// 计算消费金额
/// </summary>
/// <param name="rate"></param>
/// <param name="costTime"></param>
/// <returns></returns>
public decimal getCostMoney(string rate, string costTime)
{
//5分钟以内不记账,5-60分钟一个消费金额,60-120两个消费金额,依次类推
CustomerIDAL customerIdal = customerFactory.Customer();
CostMoneyChain.CostMoneyChain chain = new CostMoneyChain.CostMoneyChain();
decimal costMoney = chain.getCostMoney(rate, Convert.ToInt32(costTime));
return costMoney;
}
//这里用了消费金额的职责链模式
这里用到了职责链模式:职责链模式——消费金额
判断余额
/// <summary>
/// 判断余额还足,否则自动结账,强制下机
/// </summary>
/// <param name="cCash"></param>
/// <param name="costMoney"></param>
/// <param name="cardID"></param>
/// <param name="cName"></param>
/// <param name="type"></param>
/// <returns></returns>
public bool ForceOffLine(string cCash, string costMoney, string cardID, string cName, string type)
{
decimal dCash = Convert.ToDecimal(cCash);
decimal dCostMoney = Convert.ToDecimal(costMoney);
if (dCash <= dCostMoney)
{
//强制下机
CustomerIDAL customerIdal = customerFactory.Customer();
LineIDAL lineIDAL = lineFactory.line();
lineEntity.cardID = cardID;
lineEntity.lineState = Entity.Constant.FORCEOFFLINE;
string offTime = customerIdal.getTime();
lineEntity.offDateTime = Convert.ToDateTime(offTime);
lineEntity.spentCash = dCostMoney;
int intOffLine = lineIDAL.updateOffLine(lineEntity);
//更新客户表中的余额
int result = customerIdal.updateCustomerCash(cardID, dCash - dCostMoney);
if (intOffLine > 0 && result > 0)
{
return true;
}
else
{
throw new Exception("强制下机失败");
}
}
else
{
//余额尚足,不用强制下机
return false;
}
}
form窗体
登陆即上机
/// <summary>
/// 登陆即上机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmCustomerMain_Load(object sender, EventArgs e)
{
//需要实例化的类
CustomerFactory customerFactory = new CustomerFactory();
CustomerIBLL customerIBLL = customerFactory.customer();
Hashtable hashTable = new Hashtable();
//登录进来就相当于上机了
try
{
hashTable = customerIBLL.onLine(cardID);
string cType = hashTable["cType"].ToString();
string cName = hashTable["cName"].ToString();
string cCash = hashTable["cCash"].ToString();
string rate = hashTable["rate"].ToString();
string onTime = hashTable["datetime"].ToString();
lbCardID.Text = cardID.Trim();
lbCardType.Text = cType;
lbName.Text = cName;
lbOnTime.Text = Convert.ToDateTime(onTime).ToString();
lbBasicData.Text = rate;
lbSurplusMoney.Text = cCash;
//上机时间开始计时
timer1.Start();
}
//出现错误就会直接抛出来,不用停止程序
catch (Exception ex)
{
MessageBox.Show(ex.Message);
this.Close();
FrmIndex.Get().Show();
}
}
获取时间
//累加秒
int second = 0;
//分钟
int min = 0;
/// <summary>
/// 时钟在运行
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
second = second + 1;
min = second / 60;
//每过一分钟,costtime加1
lbCostTime.Text = min.ToString();
}
上机时间变化,消费变化
/// <summary>
/// 上机时间变化,跟随的消费变化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lbCostTime_TextChanged(object sender, EventArgs e)
{
//需要实例化的类
CustomerFactory customerFactory = new CustomerFactory();
CustomerIBLL customerIBLL = customerFactory.customer();
//Hashtable hashTable = new Hashtable();
decimal costMoney = customerIBLL.getCostMoney(lbBasicData.Text, lbCostTime.Text);
lbCostMoney.Text = costMoney.ToString();
}
消费金额变化,判断余额
/// <summary>
/// 每当消费金额一变呢,就要判断余额还够吗,不够的话,就强制下机
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void lbCostMoney_TextChanged(object sender, EventArgs e)
{
//需要实例化的类
CustomerFactory customerFactory = new CustomerFactory();
CustomerIBLL customerIBLL = customerFactory.customer();
Hashtable hashTable = new Hashtable();
//消费金额改变,数据库中的余额也随之改变。
double money = Convert.ToDouble(lbSurplusMoney.Text) - Convert.ToDouble(lbCostMoney.Text);
lbSurplusMoney.Text = money.ToString();
try
{
bool flag = customerIBLL.ForceOffLine(lbSurplusMoney.Text.Trim(), lbCostMoney.Text.Trim(), lbCardID.Text.Trim(), lbName.Text.Trim(), lbCardType.Text.Trim());
if (flag)
{
MessageBox.Show("您的余额不足,已经强制下机");
Application.Exit();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
欢迎点评~