机房重构之上下机

     上下机是机房的一个大难点,第一次弄就用了不少时间,但是没有加入设计模式,师傅说让我加进去,好吧,不说别的,整呗。
     上下机中上机相对简单,我没用设计模式,下机相对难一些,就用了策略模式与职责链模式相结合。 话不多说,先介绍。

U层

  if (cardno.Text == "")      
            {
                MessageBox.Show("请输入卡号", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);  
            }      
            else
            {
                Entity.Line line2 = new Entity.Line();   //给line的实体层赋值
                line2.cardno = cardno.Text.Trim();
                line2.state="正在上机";
                line2.offdate = DateTime.Now.ToString();
                Facade.lineFacade loginfacade = new Facade.lineFacade();//数据传到外观层
                DataTable flag = loginfacade.selectline(line2);
                line2.ondate = flag.Rows[0][1].ToString();
                if (flag.Rows.Count==0)
                {
                    MessageBox.Show("此卡号未上机,请重新输入", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    cardno.Text = "";
                }
                else
                {  


                    Entity.card card = new Entity.card(); //获取金额类型
                    card.cardno=cardno.Text;
                    Facade.lineFacade loginfacade1 = new Facade.lineFacade();
                    DataTable flag1 = loginfacade1.selectline1(card);
                    card.type = flag1.Rows[0][4].ToString();  //将表中类型读取出来
                    double oldcash=Convert.ToDouble(flag1.Rows[0][2].ToString());//将表中金额读取出来(后面计算用)

                    Entity.BasicData basic = new Entity.BasicData();//储存数据到line表
                    Facade.lineFacade loginfacade2 = new Facade.lineFacade();
                    DataTable flag2=loginfacade2.selectline2(basic);
                    basic.rate = flag2.Rows[0][0].ToString();       //读取两种类型用户的每小时消费金额
                    basic.viprate = flag2.Rows[0][1].ToString();

                    Facade.OperateMoneyFacade facade = new Facade.OperateMoneyFacade();//实例化外观层的职责链模式
                    Entity.Line line = new Entity.Line();
                    facade.Money(line, basic, card);  // 转到外观层
                    line.cardno = cardno.Text;
                    line.offdate = DateTime.Now.ToString();
                    line.state = "正在上机";
                    line.ontime = line.ontime;   //实体中已经在B层算好数据传到实体层,因此可以直接用
                    line.oncash=line.oncash;
                    Facade.lineFacade loginfacade3 = new Facade.lineFacade();
                    DataTable flag3 = loginfacade3.selectline3(line);
                    MessageBox.Show("下机成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);


                    Entity.card user4 = new Entity.card();//更新card表余额
                    user4.cardno = cardno.Text;
                    user4.cash = Convert.ToString(oldcash -Convert.ToDouble(line.oncash));//金额计算
                    Facade.lineFacade loginfacade4 = new Facade.lineFacade();
                    DataTable flag4 = loginfacade4.selectline4(user4);

                    Entity.Line user5 = new Entity.Line();//更新line表状态
                    user5.cardno = cardno.Text;
                    user5.state = "已下机";
                    Facade.lineFacade loginfacade5 = new Facade.lineFacade();
                    DataTable flag5 = loginfacade5.selectline5(user5);
                    #endregion
                    cardno.Text= "";
                 }
              }

外观层

 public class OperateMoneyFacade
    {
        public void Money(Entity.Line line, Entity.BasicData basic, Entity.card card)
        {
            //实例化mOneBll和mTwoBll
            loginBLL.OperateMoneyManager mOneBll = new loginBLL.OperateMoneyOne(); 
            loginBLL.OperateMoneyManager mTwoBll = new loginBLL.OperateMoneyTwo();
            //设置职责链上家与下家
            mOneBll.SetSuccessor(mTwoBll);
            //提交请求到B层
            mOneBll.GetMoney(line, basic, card);

        }
    }

B层

//下机处理策略模式
 public abstract  class bllCashSuper  
 {
    public abstract string acceptCash(Entity.Line line,      Entity.BasicData basic);                                       
 }
public class bllFixedUser : bllCashSuper
 {
    public override string acceptCash(Entity.Line line, Entity.BasicData basic)
     {
         //计算一般用户收费金额
         TimeSpan ontime1 = Convert.ToDateTime(line.offdate) - Convert.ToDateTime(line.ondate);//计算上机时间
         double ontime2 = Convert.ToDouble(ontime1.TotalHours);//转化时间
         line.ontime = Convert.ToString(ontime2 + 1);
         line.oncash = Convert.ToString(Convert.ToInt32(line.ontime) * Convert.ToInt32(basic.rate));
         return Convert.ToString(line.oncash);
     }
 }
  public class bllTempUser : bllCashSuper
 {
     public override string acceptCash(Entity.Line line, Entity.BasicData basic)
     {
         //计算固定用户收费金额
         TimeSpan ontime1 = Convert.ToDateTime(line.offdate) - Convert.ToDateTime(line.ondate);//计算上机时间
         double ontime2 = Convert.ToDouble(ontime1.TotalHours);//转化时间
         int time = Convert.ToInt32(ontime2 + 1);
         line.ontime = Convert.ToString(time);
         line.oncash = Convert.ToString(Convert.ToInt32(line.ontime) * Convert.ToInt32(basic.viprate));
         return Convert.ToString(line.oncash);
     }
 } 

 //下机处理职责链模式
      public abstract class OperateMoneyManager
        {

          protected OperateMoneyManager successor;

            //设置继承者
          public void SetSuccessor(OperateMoneyManager successor)
            {

                this.successor = successor;
            }

            //处理请求的抽象方法
          public  bllFixedUser bllfixeduser = new bllFixedUser();
          public bllTempUser blltempuser = new bllTempUser();

            public abstract void GetMoney(Entity.Line line,Entity.BasicData basic,Entity.card card);

        }
      public class OperateMoneyOne : OperateMoneyManager
        {
          public override void GetMoney(Entity.Line line, Entity.BasicData basic, Entity.card card)
            {
                //当上机时间小于准备时间时,不收费
                if (Convert.ToDouble(line.ontime) <= Convert.ToDouble(basic.leastTime))
                {
                    line.ontime = Convert.ToString(0);

                    line.oncash = Convert.ToString(0);
                }
                else
                {
                    successor.GetMoney(line, basic,card); //转到下一级
                }
            }

        }
      public class OperateMoneyTwo : OperateMoneyManager
        {
            public override void GetMoney(Entity.Line line, Entity.BasicData basic,Entity.card card)
            {
                if (Convert.ToDouble(line.ontime) >(Convert.ToDouble(basic.leastTime)/60))
                {
                    if (card.type == "一般用户")
                    {
                        bllfixeduser.acceptCash(line,basic);   //调用策略模式

                    }
                    else
                    {
                        blltempuser.acceptCash(line, basic);
                    }
                }
                else
                {
                    successor.GetMoney(line, basic,card); //(如果没有下一级可不写)
                }
            }
        }
  }

     其他层与原先的一样,就不再介绍。我写的这个原理就是U层–外观层–B层(设计模式处理)–返回到U层(已经传值到实体层)–七层(在走一次七层将数据保存到表中)。不管是简单复杂反正是弄出来了,这期间请教了很多人,看了很多博客,写了很多代码,调了很多bug,着实不易!所以就想写一个清楚的版本给其他需要的同学借鉴。

    希望这篇博客能对你有帮助!

你可能感兴趣的:(C/S学习)