Ejemplo n.º 1
0
        protected void gvBookBorrow_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string BookBarCode = gvBookBorrow.DataKeys[e.NewSelectedIndex].Value.ToString();                                                                                  //获取选中图书的条形码
                                                                                                                                                                              //定义查询语句,查询指定图书条形码的图书信息
            string        sql = "select * from tb_bookInfo as a inner join tb_bookType as b on a.bookType=b.bookTypeID where a.bookBarCode='" + BookBarCode + "'";
            SqlDataReader sdr = DataOperate.GetRow(sql);                                                                                                                      //获取图书信息

            sdr.Read();                                                                                                                                                       //读取一条记录
            if (Convert.ToInt32(sdr["stock"]) > 0)                                                                                                                            //判断图书是否还有库存
            {
                float Money = Convert.ToSingle(money.Text);                                                                                                                   //读者余额
                float Hire  = Convert.ToSingle(sdr["hire"].ToString());                                                                                                       //读书租金
                if (Money > Hire)                                                                                                                                             //读者余额大于该读书租金
                {
                    string sqlBookBorrow = "select count(*) from tb_bookBorrow where bookBarcode='" + bookBarCode.Text + "' and readerBarCode='" + readerBarCode.Text + "' "; //定义查询语句,查询该读者是否借阅此书
                    if (DataOperate.SeleSQL(sqlBookBorrow) == 0)                                                                                                              //调用公共类中seleSQL方法,指定数据不存在(读者当前未借此书)
                    {
                        int      BorrowDay      = Convert.ToInt32(sdr["borrowDay"]);                                                                                          //获取借阅天数
                        string   BorrowTime     = DateTime.Now.Date.ToShortDateString();                                                                                      //获取借阅日期
                        string   ReaderBarCode  = readerBarCode.Text;                                                                                                         //获取读者编号
                        string   BorrowOperator = Session["userName"].ToString();                                                                                             //获取借阅经办人
                        string   ReturnTime     = DateTime.Now.Date.AddDays(BorrowDay).ToShortDateString();                                                                   //获取应还日期
                        string[] sqlT           = new string[3];                                                                                                              //设置SQL语句数组
                                                                                                                                                                              //定义SQL语句,将图书借阅信息插入到图书借阅表中
                        sqlT[0] = "insert tb_bookBorrow values('" + BookBarCode + "','" + ReaderBarCode + "','" + BorrowTime + "','" + BorrowOperator + "','" + ReturnTime + "')";
                        //定义SQL语句,更新图的借阅次数和图书的库存数据
                        sqlT[1] = "update tb_bookInfo set borrowSum=borrowSum+1,stock=stock-1 where bookBarCode='" + BookBarCode + "'";
                        //定义SQL语句,更新读者的可用余额
                        sqlT[2] = "update tb_readerInfo set money=money-" + Hire + " where readerBarCode='" + ReaderBarCode + "'";
                        //调用公共类中的execTransaction方法执行事务
                        if (DataOperate.ExecTransaction(sqlT)) //事务执行成功
                        {
                            bindReaderInfo();                  //重新绑定读者信息
                            gvBookBorrow.DataSource = null;    //将数据源设置为空
                            gvBookBorrow.DataBind();           //重新绑定数据
                            bookBarCode.Text = "";             //将图书条形码文本框清空
                            ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('借阅成功!')</script>");
                        }
                        else
                        {
                            ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('借阅失败!')</script>");
                        }
                    }
                    else
                    {
                        ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('该读者已经借阅此图书,不可以再借阅!')</script>");
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('读者金额不足,不可以借阅图书!')</script>");
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('图书已没有库存,不可以借阅!')</script>");
            }
        }
Ejemplo n.º 2
0
        protected void gvBookReturn_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string   BookBarCode = gvBookReturn.DataKeys[e.NewSelectedIndex].Value.ToString();              //获取选中图书条形码
            DateTime ReturnDate  = Convert.ToDateTime(gvBookReturn.Rows[e.NewSelectedIndex].Cells[4].Text); //获取应还日期
            DateTime TodayDate   = DateTime.Now.Date;                                                       //获取当前日期
            TimeSpan ts          = TodayDate - ReturnDate;                                                  //计算超期天数
            int      daysDate    = ts.Days;
            //计算租金和滞纳金
            string strLagMoney = gvBookReturn.Rows[e.NewSelectedIndex].Cells[7].Text;
            int    LagMoney    = Convert.ToInt32(strLagMoney.Substring(0, strLagMoney.Length));
            string strHire     = gvBookReturn.Rows[e.NewSelectedIndex].Cells[6].Text;
            int    Hire        = Convert.ToInt32(strHire.Substring(0, strHire.Length));
            string hint        = "";

            string[] sqlT;
            int      i = 0;

            if (daysDate > 0)   //存在超期情况
            {
                sqlT = new string[3];
                //更新读者余额
                sqlT[i++] = "update tb_readerInfo set money=money - " + LagMoney * daysDate + " where readerBarCode = '" + readerBarCode.Text + "' ";
                hint      = "您的图书归还期已过" + daysDate + "天,将扣除滞纳金" + LagMoney * daysDate + "元。";
            }
            else  //存在超期情况
            {
                sqlT = new string[3];
                //更新读者余额
                sqlT[i++] = "update tb_readerInfo set money=money + " + Hire + " where readerBarCode = '" + readerBarCode.Text + "' ";
            }
            //定义SQL语句,更新图书库存数
            sqlT[i++] = "update tb_bookInfo set stock=stock+1 where bookBarCode='" + BookBarCode + "'";
            //定义SQL语句,删除借书记录
            sqlT[i] = "delete tb_bookBorrow where bookBarCode='" + BookBarCode + "' and  readerBarCode='" + readerBarCode.Text + "' ";
            //调用公共类中的execTransaction方法执行事务
            if (DataOperate.ExecTransaction(sqlT)) //事务执行成功
            {
                bindReaderInfo();                  //重新绑定读者信息
                gvBookReturn.DataSource = null;    //将数据源设置为空
                gvBookReturn.DataBind();           //重新绑定数据
                bookBarCode.Text = "";             //将图书条形码文本框清空
                //调用自定义方法显示已借阅未归还图书信息
                ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('图书归还成功!" + hint + "')</script>");
            }
            else
            {
                ClientScript.RegisterStartupScript(Page.GetType(), "", "<script>alert('图书归还失败!')</script>");
            }
        }