机房重构-充值

流程图。捋一下大致思路

 

代码

U层,

 //实例化创建BLL层的工厂
        FactoryBLL factBll = new FactoryBLL();         
        private void BtnOK_Click(object sender, EventArgs e)
        {
            //调用充值的业务接口,和创建BLL层的工厂实例化BLL层的具体类’
            ReChargeIBLL ReChargeIBLL = (ReChargeIBLL)factBll.CreateUser("ReChargeBLL");
            //调用充值的方法
            string StrReChar = ReChargeIBLL.InReCharge(txtcardno.Text, txtaddmoney.Text, this); 
            MessageBox.Show(StrReChar);//显示返回信息
        }

B层

 public class ReChargeBLL : ReChargeIBLL
    {
        FactoryDAL fact = new FactoryDAL();

        /// <summary>
        /// 充值方法
        /// </summary>
        /// <param name="Cardno"></param>
        /// <param name="Addmoney"></param>
        /// <param name="form"></param>
        /// <returns>是否充值成功</returns>
        public string InReCharge(string Cardno, string Addmoney, Form form)
        {
            //用于返回信息
            string strMsger = "";
            //接受判空返回值,判断文本框是否为空。
            string isNull = IsNull.isNull(form);
            //接受是不是数字的判断返回值
            bool isNumber = IsNull.IsNumber(Cardno);
            bool isCash = IsNull.IsNumber(Addmoney);

            //判断文本框内容是否是数字
            string GroupIsNumber = IsNull.GroupIsNumber(form);

            if (isNull == "")//没有返回值说明不为空
            {
                // 判断输入的卡号是否为数字
                if (isNumber != true)//不是数字
                {
                    strMsger = "卡号请输入数字";
                }
                else//是数字
                {
                    #region 获取卡号信息
                    //调用充值接口和创建DAL层的工厂,实例化DAL层具体的类
                    ReChargeIDAL RecharIDAL = (ReChargeIDAL)fact.CreateUser("ReChargeDAL");
                    //转换为卡号实体
                    StudentModel studentCardno = new StudentModel();
                    //给卡号赋值
                    studentCardno.cardno =Convert.ToInt32( Cardno);
                    //获取卡号信息
                    DataTable CardnoInfoTable = RecharIDAL.SelectReCharge(studentCardno);
                    #endregion

                    #region 充值
                    //判断卡号是否存在
                    if (CardnoInfoTable.Rows.Count != 0)//不等于零,说明卡号存在
                    {
                        //判断卡号使用状态
                        if (CardnoInfoTable.Rows[0][6].ToString().Trim() == "已激活")//已激活状态
                        {
                            //获取基础表信息
                            DataTable Baist = RecharIDAL.SelectBast();
                            #region 判断充值金额
                            if (isCash != true)//充值金额不是数字
                            {
                                strMsger = "充值金额请输入数字!";
                            }
                            else//充值金额是数字
                            {
                                //判断充值金额是否大于或等于最小充值金额
                                if (double.Parse(Addmoney) >= double.Parse(Baist.Rows[0][5].ToString()))
                                { //插入充值记录
                                    ReChargeModel recahrge = new ReChargeModel();//实例化充值表实体
                                    recahrge.Cardno = Cardno;//充值卡号
                                    recahrge.Addmoney =Convert.ToDouble(Addmoney);//充值金额
                                    //充值的操作员
                                    recahrge.Date = DateTime.Now.ToShortDateString();//充值日期
                                    recahrge.Time = DateTime.Now.ToShortTimeString();//充值时间
                                    recahrge.Status = "未结账";//结账状态
                                    //调用插入方法
                                    RecharIDAL.InsertReCharge(recahrge);
                                    //更新当前余额
                                    double NowCash = double.Parse(CardnoInfoTable.Rows[0][4].ToString());//当前余额
                                    //新余额=当前余额+充值余额
                                    double NewCash = NowCash + double.Parse(Addmoney);
                                    //给余额赋值
                                    studentCardno.Studentbalance = NewCash;
                                    //调用更新方法
                                    RecharIDAL.UpdateCardnoCash(studentCardno);

                                    string newcash = NewCash.ToString();

                                    strMsger = ("充值成功!   卡上余额 :  "   + newcash+ "元");
                                }
                                else//小于充值金额
                                {
                                    strMsger = "充值金额不能小于最小充值金额,请从新输入!";
                                }
                            }
                            #endregion

                        }
                        else//未激活状态
                        {
                            strMsger = "卡号未激活,请先激活。";
                        }
                    }
                    else//等于零
                    {
                        strMsger = "卡号不存在,请先注册";
                    }
                    #endregion
                }
            }
            else //有返回值
            {//说明为空
                strMsger = isNull;
            }
            return strMsger;
        }

    }

D层

 public class ReChargeDAL : ReChargeIDAL
    {
        //实例化一个SQHelp层,用于连接数据库进行查询。
        SQLHelper SQLHelper = new SQLHelper();

        /// <summary> 
        /// 向充值表插入信息
        /// </summary>
        /// <param name="reChargeModel">充值实体</param>
        /// <returns>返回受影响行数</returns>
        public int InsertReCharge(ReChargeModel reChargeModel)
        {
            //定义参数
            SqlParameter[] sqlParams =
             {
                new SqlParameter("@cardno",reChargeModel.Cardno ),
                new SqlParameter("@addmoney",reChargeModel.Addmoney ),
                new SqlParameter("@date",DateTime.Now.ToShortDateString()),
                new SqlParameter("@time",DateTime.Now.ToShortTimeString()),               
                new SqlParameter("@status","未结账"),
                new SqlParameter("@OpertionID","TZK")

             };
            string sql= @"insert into ReCharge_Info (cardno,addmoney,date,time,status,opertionID)values(@cardno,@addmoney,@date,@time,@status,@OpertionID)";
            int Relust = SQLHelper.ExecuteNonQuery(sql, sqlParams, CommandType.Text);
            return Relust;   //返回受影响行数

        }

        /// <summary>
        /// 查询基础表
        /// </summary>
        /// <returns>整个基础表信息</returns>
        public DataTable SelectBast()
        {
            //定义SQL语句
            string sql = @"select * from BasicData_Info";
            //接受返回值
            DataTable SelectBast = SQLHelper.ExecuteQuery(sql, CommandType.Text);
            return SelectBast;//返回结果
        }

       

        /// <summary>
        /// 查询卡号是否存在
        /// </summary>
        /// <param name="StudentCardno"></param>
        /// <returns>整个卡号信息表</returns>
        public DataTable SelectReCharge(StudentModel StudentCardno)
        {
            //定义参数
            SqlParameter[] sqlams = { new SqlParameter("@StudentCardno", StudentCardno.cardno) };
            //定义SQL语句
            string sql = @"select * from Student_Info where cardno=@StudentCardno";
            //执行SQL语句
            DataTable RegistraTable = SQLHelper.ExecuteQuery(sql, sqlams, CommandType.Text);
            return RegistraTable;//返回查询结果
        }

        /// <summary>
        /// 更新卡号余额
        /// </summary>
        /// <param name="StudentCash">卡号余额</param>
        /// <returns>受影响行数</returns>
        public int UpdateCardnoCash(StudentModel StudentCash)
        {
            //定义参数
            SqlParameter[] sqlams = {
                new SqlParameter("@Cardno", StudentCash.cardno),
                new SqlParameter("@Cash", StudentCash.Studentbalance)};
            //定义SQL语句
            string sql = @"update Student_Info set cash=@Cash where cardno=@Cardno";
            //执行SQL语句
            int Relust = SQLHelper.ExecuteNonQuery(sql, sqlams, CommandType.Text);
            return Relust;//返回受影响行数
        }
    }

工厂

 public class FactoryBLL
    {
        //获取配置文件,要实例化的程序集名称。
        string StrDB = System.Configuration.ConfigurationManager.AppSettings["BL"];
        /// <summary>
        ///应用反射获得BLL层操作 。
        ///缺点:数据类型是死的,不同数据类型的工厂调用需要在调用处强制转换。
        /// </summary>
        /// <param name="CreatClassName">要实例化的类</param>
        /// <returns>BLL层类</returns>
        public object CreateUser(string CreatClassName)
        {
            //具体要实例化的类
            string ClassName = StrDB + "." + CreatClassName;
            //利用反射返回要实例化的具体类
            return (object)Assembly.Load(StrDB).CreateInstance(ClassName);
        }
    }
 /// <summary>
    /// 创建数据库层查询功能
    /// </summary>
    public class FactoryDAL
    { //获取配置文件,要实例化的程序集名称。
        string StrDB = System.Configuration.ConfigurationManager.AppSettings["DB"];
        /// <summary>
        ///应用反射获得DAL层操作,来返回D层的登录查询类。
        ///缺点:数据类型是死的,不同数据类型的工厂调用需要在调用处强制转换。
        /// </summary>
        /// <param name="CreatClassName">要实例化的类</param>
        /// <returns>d层具体查询</returns>
        public object CreateUser(string CreatClassName)
        {
            //具体要实例化的类
            string ClassName = StrDB + "." + CreatClassName;
            //利用反射返回要实例化的具体类
            return (object)Assembly.Load(StrDB).CreateInstance(ClassName);
        }
    }

实体

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Entity
{
    public class ReChargeModel
    {
        /// <summary>
        /// 卡号
        /// </summary>
        public string Cardno { set; get; }

        /// <summary>
        /// 密码
        /// </summary>
        public string PWd { set; get; }

        /// <summary>
        /// 充值金额
        /// </summary>
        public double Addmoney { set; get; }

        /// <summary>
        /// 充值日期
        /// </summary>
        public string Date { set; get; }

        /// <summary>
        /// 充值时间
        /// </summary>
        public string Time { set; get; }


        /// <summary>
        /// 是否结账
        /// </summary>
        public string Status { set; get; }

        /// <summary>
        /// 操作员
        /// </summary>
        public string OpertionID { set; get; }
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_43472073/article/details/108235850