============
数据库开发
============

函数
-----------

网上商城主要的功能是用户选择商品添加到购物车，挑选结束后提交订单。商品加入购物车的操作可以抽象成一个函数，通过加入购物车操作可以了解到如何通过KStudio来进行函数的添加。

.. _新建函数-添加购物车:

新建函数-添加购物车
^^^^^^^^^^^^^^^^^^^^^

加入购物车函数的具体信息如下表所示：

+----------+----------------------------------------------------------+
| 函数     | addtocart                                                |
+==========+==========================================================+
| 功能描述 | 商品加入购物车                                           |
+----------+----------------------------------------------------------+
| 操作的表 | 购物车表：每条记录对应了某个用户加入到                   |
|          | 购物车的某一件商品、该商品的数量和是否被选中要购买的标记 |
+----------+----------------------------------------------------------+
| 实现原理 | 往购物车表内                                             |
|          | 增加记录，或修改现有记录内的商品数量，并选中该商品要购买 |
+----------+----------------------------------------------------------+

下面演示该函数的创建过程，首先，在左侧“数据库导航”树上“数据库”->“onlinemarket”->“模式”->“market”->“函数”上右键选择“新建
函数”，如下图所示：

.. figure:: image/KStudio53.png

KStudio出现新建函数的界面，在其“基本属性”面板上填写函数名称“addtocart”，返回值类型为bool，它表示加入购物车是否成功；模式和属主保持默认值，如下图所示：

.. figure:: image/KStudio54.png

依据加入购物车函数的说明，需要如下表所述的三个参数，因此在“参数面板”上增加函数的这三个参数，如下图所示：

+-----------------+----------+----------+-----------------+----------+
| 参数名          | 参数类型 | 参数模式 | 参数说明        | 入参示例 |
+=================+==========+==========+=================+==========+
| user_id         | integer  | IN       | 用户编号，针对  | 1        |
|                 |          |          | 哪个用户进行加  |          |
|                 |          |          | 入购物车的操作  |          |
+-----------------+----------+----------+-----------------+----------+
| product_id      | integer  | IN       | 加入购          | 1        |
|                 |          |          | 物车的商品编号  |          |
+-----------------+----------+----------+-----------------+----------+
| product_quantity| integer  | IN       | 加入购          | 2        |
|                 |          |          | 物车的商品数量  |          |
+-----------------+----------+----------+-----------------+----------+

..

.. figure:: image/KStudio55.png

然后，在“SQL内容”窗口填入该函数的具体SQL语句（见下表）：

.. code::

    AS 
    tempcount integer default 0;
    begin
        select count(1) into tempcount from cart where userid=user_id and proid=product_id;
        if tempcount > 0 then--如果购物车中已经有此商品，则将购物车内此商品的数量进行增加
            update cart set quantity=quantity+product_quantity where userid=user_id and proid=product_id;
        else  -- 购物车中无此商品，那么就新增一条购物车商品数据
            insert into "cart" (userid, proid, quantity, checked, 
        "createtime", "updatetime") values(user_id, product_id, product_quantity, true, now(), now());
        end if;
        return true;
         exception
        when others then
        return false;
    END

.. figure:: image/KStudio56.png

此时，完成了加购物车函数的创建，整体截图如下：

.. figure:: image/KStudio57.png

最后，点击“确定”按钮添加完成，弹出创建成功的对话框如下所示。

.. figure:: image/KStudio58.png

这就是整个函数的创建过程，最后可以使用KStudio的“函数调用”功能验证一下该函数是否可以执行成功。

执行函数
^^^^^^^^^^^^^^^^^^^^^

在左侧“数据库导航”树的“数据库”->“onlinemarket”->“模式”->“market”->“函数”->“addtocart”节点上右键选择“执行函数”：

.. figure:: image/KStudio59.png

在弹出的对话框内填写入参信息，如下图所示：

.. figure:: image/KStudio60.png

然后，点击“确定”按钮进入到函数调用的脚本页面，点击“执行”按钮进行“加入购物车”函数调用操作，如下图所示：

.. figure:: image/KStudio61.png

函数执行后打印输出为“t”如下图所示，表示加入购物车成功：

.. figure:: image/KStudio62.png

执行结果查看
~~~~~~~~~~~~~~~~~~

“函数执行”功能演示完毕，现在查看cart表的记录是否有添加，有则证明添加至购物车操作成功。在“数据库”->“onlinemarket”->“模式”->“market”->“表”->
“cart”节点上右键选择“查询数据”

.. figure:: image/KStudio63.png

打开后界面如下，购物车内新增了一条记录：

.. figure:: image/KStudio64.png

至此，创建函数、函数执行的功能已经演示完毕，其他函数也可以仿照上述步骤完成创建或执行。

函数调试
^^^^^^^^^^^^^^^^^^^^^

针对上面刚刚创建的addtocart函数进行调试，首先准备数据，上一步已经将商品编号为1的商品加入到用户编号为1的用户购物车内，此记录保持住，代表某个用户已经加入某个商品
到购物车了，如果再次执行加入购物车操作，则应跳转到增加购物车中此商品数量的分支上去执行。

以上购物车数据准备完成，下面进行“添加购物车addtocart”函数的调试步骤

在“数据库”->“onlinemarket”->模式->“market”->“函数”->“addtocart”上右键选择“调试”选项

.. figure:: image/KStudio65.png

输入参数，和前面操作的购物车记录吻合，仍然是用户1添加n件商品id为1的商品到购物车（这里设置的n件为3件），输入参数如下图所示：

.. figure:: image/KStudio66.png

点击“确定”进入到调试页面，整体截图如下：

.. figure:: image/KStudio67.png

点击工具栏中的“开始调试”\ |image68|\ 按钮，进行函数的调试，点击后界面发生如下变化

.. figure:: image/KStudio69.png

执行工具栏中的“单步跳过” |image70|\ 按钮执行到下一步

.. figure:: image/KStudio71.png

继续单击“单步跳过”按钮进行参数变量的赋值

.. figure:: image/KStudio72.png

继续单击“单步跳过”按钮执行到returnVar:=
market.addtocart(user_id,product_id,product_quantity);

.. figure:: image/KStudio73.png

在这里单击“单步跳入”按钮\ |image74|\ 跳入到addtocart函数的代码执行界面

.. figure:: image/KStudio75.png

单击“单步跳过”，右上角的“变量”面板查看tempcount的值，因为购物车中之前添加了一条记录，所以此值应该为1

.. figure:: image/KStudio76.png

此时tempcount的值大于0，则应该执行update操作更新购物车中的商品数量，单击“单步跳过”查看运行到哪一步，如下图所示运行到update命令执行了更新操作。

.. figure:: image/KStudio77.png

继续“单步跳过”一直到此函数运行完成。运行结束后，到购物车表中查看商品数量是否有增加，如下图所示商品数量增加为5了：

.. figure:: image/KStudio78.png

其他函数的信息
^^^^^^^^^^^^^^^^^^^^^

在商品详情展示的时候，需要显示商品的当月销售量

======== ===================================
函数     monthsale
======== ===================================
功能描述 商品的当月销量
操作的表 订单详情表
实现原理 统计某个商品的从月初1号开始的销售量
======== ===================================

创建过程参考上一章节，内容填写如下：

“基础属性”面板填写名称（monthsale）、返回值类型（integer类型，代表返回当月的销量值）、模式和属主保持默认

.. figure:: image/KStudio79.png

函数参数代表指定的商品id，目的就是为了获取此指定商品的月销量：

.. figure:: image/KStudio80.png

====== ======== ======== ====================== ========
参数名 参数类型 参数模式 说明                   入参示例
====== ======== ======== ====================== ========
pro_id integer  IN       针对哪个商品获取月销量 1
====== ======== ======== ====================== ========

..

函数SQL内容：

.. code::

    AS 
    quantity integer;
    recordcount integer default 0;
    begin
        select count(1) into recordcount from orderitem where createtime>trunc(sysdate,'mm') and proid=pro_id;
        if recordcount > 0 then
        select sum("quantity") into quantity from orderitem where createtime>trunc(sysdate,'mm') group by proid having proid=pro_id limit 1; --trunc()获取当月的第一天
        return quantity;
        else 
        return 0;
        end if;
    END

..

整体截图如下：

.. figure:: image/KStudio81.png

点击“确定”按钮完成添加：

.. figure:: image/KStudio82.png

调用函数，在左侧“数据库导航”树节点“数据库”->“onlinemarket”->模式->“market”->“函数”->“monthsale”上右键选择“执行函数”：

.. figure:: image/KStudio83.png

输入入参为1，表示获取id为1的商品的月销量：

.. figure:: image/KStudio84.png

点击“确定”按钮后，出现脚本窗口，点击执行按钮即可执行此函数，输出商品1的当月销量：

.. figure:: image/KStudio85.png

存储过程
--------------

用户针对购物车中的商品进行下单操作，这里我们将下单抽象出一个存储过程，通过创建新订单的操作可以了解如何通过KStudio来创建存储过程。

.. _新建存储过程-下单:

新建存储过程-下单
^^^^^^^^^^^^^^^^^^^^^

下订单的存储过程具体描述如下。

+----------+----------------------------------------------------------+
| 存储过程 | neworder                                                 |
+==========+==========================================================+
| 功能描述 | 购物车中选中的商品进行下单操作                           |
+----------+----------------------------------------------------------+
| 操作的表 | 商品表：商品库存减少指定的数量                           |
|          |                                                          |
|          | 购物车表：对应选中的商品记录进行清空                     |
|          |                                                          |
|          | 订单表：增加订单记录                                     |
|          |                                                          |
|          | 订单地址表：增加订单地址记录                             |
|          |                                                          |
|          | 订单详情表：增加订单详情记录                             |
+----------+----------------------------------------------------------+
| 实现原理 | 传入用户编号                                             |
|          | 和地址编号，会将用户对应的购物车内选中的商品进行下单操作 |
+----------+----------------------------------------------------------+

下面介绍存储过程的创建过程：

在“数据库导航”树节点“数据库”->“onlinemarket”->“模式”->“market”->“存储过程”上右键选择“新建存储过程”选项

.. figure:: image/KStudio86.png

创建面板上“基础属性”填写名称为“neworder”，模式和属主保持默认值

.. figure:: image/KStudio87.png

“参数”面板增加四个参数，前两个为入参，后两个为出参，入参：user_id（用户编号，针对哪个用户的购物车进行下单操作），address_id（用户下单时选择的地址编号），success作为OUT出参，代表新订单创建是否成功，orderid出参（如果成功创建订单，则返回订单的id），编写参数如下图所示：

.. figure:: image/KStudio88.png

参数：

========== ======== ======== ============ ========
参数名     参数类型 参数模式 参数说明     入参示例
========== ======== ======== ============ ========
user_id    integer  IN       用户编号     1
address_id integer  IN       地址编号     1
success    bool     OUT      下单成功与否 
orderid    integer  OUT      返回订单编号 
========== ======== ======== ============ ========

..

SQL内容：

.. code::

    AS 
    declare 
    cart_ids integer[];
    mypro_id integer;
    myquantity integer;
    mystock integer;
    totalpayment decimal(20,2) default 0.00;
    myprice decimal(20,2) default 0.00;
    pro_ids integer[];
    quantitys integer[];

    realname varchar(50);
    telephone varchar(50);
    province varchar(50);
    city varchar(50);
    district varchar(50);
    address text;
    shoppingid_int integer;
    orderid_int integer;
    myproid integer;
    myproname varchar(50);
    myproprice decimal(20,2) default 0.00;
    BEGIN
        /**
         * 1. 获取用户购物车内选择的商品,将相应信息放到数组中
         * 2. 将商品的库存减去购物车内设置的值
         * 3. 将订单表增加一条订单记录
         * 4. 将订单详情表增加购物车内的商品
         */
        
        cart_ids := array(select carid from cart where userid=user_id and checked=true);
        if(array_length(cart_ids,1) > 0) then
        for i	in 1..array_length(cart_ids,1)
        loop 
            select proid,quantity into mypro_id,myquantity from cart where carid=cart_ids[i];
            delete from cart where carid=cart_ids[i];-- 清空购物车
            pro_ids[i] = mypro_id; quantitys[i] = myquantity;
            select stock,price into mystock,myprice from product where proid=mypro_id;
            if mystock-myquantity<0 then
            raise exception '库存量不足';
            else
            update product set stock=stock-myquantity where proid=mypro_id;
            totalpayment := totalpayment+myprice*myquantity;
            end if;
        end loop;
        
        insert into orders(userid,payment,status,createtime,updatetime) values 
        (user_id, totalpayment,2,now(),now()) returning orderid into orderid_int;
        
        /*获取收货地址信息,插入到订单收货地址表中*/
        select realname,telephone,province,city,district,address into realname,telephone,province,city,district,address from address where addressid=address_id limit 1;
        
        -- 增加订单收货地址表信息
        insert into shopping(userid,orderid,receivername,receiverphone,receiverprovince,receivercity,receiverdistrict,receiverstreet,createtime,updatetime) values 
        (user_id,orderid_int,realname,telephone,province,city,district,address,now(),now());
        
        -- orderitem表进行增加
        for i	in 1..array_length(pro_ids,1)
        loop 
            select proid,name,price into myproid,myproname,myproprice from product where proid=pro_ids[i];
            insert into orderitem(orderid,userid,proid,proname,currentunitprice,quantity,totalprice,createtime,updatetime) values 
            (orderid_int,user_id,myproid,myproname,myproprice,quantitys[i],quantitys[i]*myproprice,now(),now());
        end loop;
        success = true;
        orderid = orderid_int;
        else 
        raise notice '购物车为空';
        success = false;
        end if;
        exception 
        when others then 
        success = false;
        
    END

整体截图如下：

.. figure:: image/KStudio89.png

点击右下角的“确定”按钮创建完成存储过程。

.. figure:: image/KStudio90.png

以上关于“添加新订单”的存储过程创建完毕，可以使用“执行存储过程”的方式调用创建的存储过程，调用完毕后可以去相应的订单表、订单详情表中查看数据是否有对应的变动。

执行存储过程
^^^^^^^^^^^^^^^^^^^^^

在左侧“数据库导航”树的“数据库”->“onlinemarket”->“模式”->“market”->“存储过程”->“neworder”节点上右键，选择“执行存储过程”,如下图所示：

.. figure:: image/KStudio91.png

入参填写如下，代表id为1的用户使用id为1的地址进行下单操作：

.. figure:: image/KStudio92.png

在脚本中增加一行 

.. code::

   raise notice '创建订单：%，订单id为：%',success,orderid; 

可以查看订单是否创建成功，以及创建成功后的订单id：

.. figure:: image/KStudio93.png

如果购物车为空，则先执行将商品“加入购物车”的操作，执行\ `“addtocart” <#id3>`__\ 函数。然后再执行此新建订单的操作。

.. _执行结果查看-1:

执行结果查看
~~~~~~~~~~~~~~~~~~

查看执行结果，到orders表、orderitem表中查看记录是否有增加，cart表是否删除了对应的数据，product表的库存量是否有减少，使用“新建查询”功能即可完成，这里不再截图展示。

调试存储过程
^^^^^^^^^^^^^^^^^^^^^

存储过程的调试步骤和函数的调试步骤一致。

其他存储过程
^^^^^^^^^^^^^^^^^^^^^

其他存储过程也是上面的方式去新建即可，下面简述其他的存储过程。

+----------+----------------------------------------------------------+
| 存储过程 | categorysales                                            |
+==========+==========================================================+
| 功能描述 | 统计某个类别的商品们在指定时间内的销售额                 |
+----------+----------------------------------------------------------+
| 操作的表 | 订单详情表：统计某个类别商品们的销售额                   |
+----------+----------------------------------------------------------+
| 实现原理 | 传入商品分类id，以及                                     |
|          | 开始结束时间，会将此分类商品在指定时间内的销售额计算出来 |
+----------+----------------------------------------------------------+

..

同上一章节的步骤，关于内容填写如下：

基本属性填写：

.. figure:: image/KStudio94.png

参数，注意totalprices是OUT模式的，代表输出的销售额：

.. figure:: image/KStudio95.png

+-------------+----------+----------+---------------+---------------+
| 参数名称    | 参数类型 | 参数模式 | 说明          | 参数示例      |
+=============+==========+==========+===============+===============+
| categoryid  | integer  | IN       | 分类编号      | 1             |
+-------------+----------+----------+---------------+---------------+
| starttime   | datetime | IN       | 统计金        | 2022-04-20    |
|             |          |          | 额的开始时间  | 00:00:00      |
|             |          |          |               | （根据实际情况|
|             |          |          |               | 填写）        |
+-------------+----------+----------+---------------+---------------+
| endtime     | datetime | IN       | 统计金        | 2022-05-07    |
|             |          |          | 额的结束时间  | 00:00:00      |
|             |          |          |               | （根据实际情况|
|             |          |          |               | 填写）        |
+-------------+----------+----------+---------------+---------------+
| totalprices | decimal  | OUT      | 某类别商品    |               |
|             |          |          | 销售的总金额  |               |
+-------------+----------+----------+---------------+---------------+

..

SQL内容：

.. code::

    AS 
    prices decimal(20,2)[];
    begin
        totalprices := 0.0;
        prices := array(select totalprice from orderitem where 
        proid in (select proid from product where cateid=categoryid)
        and createtime>starttime and createtime<endtime);
        if(array_length(prices, 1) > 0) then
            for i in 1..array_length(prices, 1)
            loop 
                totalprices := totalprices+prices[i];
            end loop;
        end if;
    END

..

以上创建分类销售额的存储过程完毕。

执行此存储过程，在该存储过程上右键“执行存储过程”

存储过程参数如下填写，categoryid为分类id，starttime和endtime根据实际情况填写：

.. figure:: image/KStudio96.png

点击确定，在脚本中编写增加一行

.. code::

   raise notice 'totalprices is:%',totalprices;

打印销售额结果

.. figure:: image/KStudio97.png

打印输出商品分类1在指定时间内的总销售额。

以上存储过程介绍完毕，下面进行视图的创建说明。

视图
------------

为了应用程序的查看方便，可以通过视图对表数据进行封装，下面介绍视图的创建

新建视图-最受用户喜爱的前十名商品
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

在“数据库”->“onlinemarket”->“模式”->“market”->“视图”树节点上右键“新建
视图”,如下图所示

.. figure:: image/KStudio98.png

输入视图的基本属性，名称为likeproducttop10，其他保持默认：

.. figure:: image/KStudio99.png

SQL内容：

.. code::

    SELECT o.proid,
        sum(o.quantity) AS totalquantity
       FROM market.orderitem o
      GROUP BY o.proid
      ORDER BY (sum(o.quantity)) DESC
     LIMIT 10;

..

整体截图如下：

.. figure:: image/KStudio100.png

点击“确定”完成视图的创建

.. figure:: image/KStudio101.png

查询视图
~~~~~~~~~~~~~~

调用创建的视图查看最受欢迎的10件商品，点击工具栏上的“新建查询”，在脚本编辑的面板上输入如下命令：\ **select**
\* **FROM** likeproducttop10;

.. figure:: image/KStudio102.png

新建视图-订单和订单地址表合并查看
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

同上一节的步骤，在“视图”节点上右键“新建 视图”。

基本属性填写如下：

.. figure:: image/KStudio103.png

SQL内容填写如下：

.. code::

    SELECT o.orderid,
        o.userid,
        o.payment,
        o.status,
        o.paymenttime,
        o.sendtime,
        o.endtime,
        o.closetime,
        o.createtime,
        o.updatetime,
        s.shoppingid,
        s.receivername,
        s.receiverphone,
        s.receiverprovince,
        s.receivercity,
        s.receiverdistrict,
        s.receiverstreet
       FROM (market.orders o
         LEFT JOIN market.shopping s ON ((o.orderid = s.orderid)));


.. _查询视图-1:

查询视图
~~~~~~~~~~~~~~

点击“确定”创建视图，查询编写的orderandshopping视图，就在“新建查询”的面板上执行：select
\* FROM orderandshopping ;

.. figure:: image/KStudio104.png

触发器
------------

这里通过添加日志记录的方式来设置触发器，用户会有登录登出、下单、付款等操作，在进行这些操作的时候需要将这些操作记录下来，由此可以通过触发器功能来实现，下面以新建订单就会添加日志记录到日志表中来了解使用KStudio创建触发器。

新建触发器-新建订单触发添加日志记录
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能描述：orders表在新插入数据之后会触发写日志记录的操作，在左侧导航树节点“数据库”->“onlinemarket”->“模式”->“market”->“触发器”上右键“新建触发器”

.. figure:: image/KStudio105.png

填写基本属性：触发器名称：newordertrigger，基于表：orders，触发时机：after，触发类型：insert，类型：row，状态为enable。如下图所示：

.. figure:: image/KStudio106.png

SQL内容：

.. figure:: image/KStudio107.png

.. code::

    AS
    BEGIN
        insert into loginfo(loglevel, logtype, logdetail, logtime) values (1,'neworder','add new order into orders',now());
    END;

..

整体截图如下：

.. figure:: image/KStudio108.png

点击“确定”按钮完成触发器的生成。

.. figure:: image/KStudio109.png

执行触发器
^^^^^^^^^^^^^^^

检查此触发器的方式，第一步，添加商品到购物车，在“数据库”->“onlinemarket”->“模式”->“market”->“函数”->“addtocart”上右键执行此函数，入参填写（user_id:1，product_id:1，product_quantity:2）,具体步骤参见 :ref:`新建函数-添加购物车` 章节；第二步，在“数据库”->“onlinemarket”->“模式”->“market”->“存储过程”->“neworder”上右键调用新建订单的存储过程,入参填写（user_id:1,address_id:1），具体步骤参见 :ref:`新建存储过程-下单` 章节。在创建订单的时候会对订单表orders进行插入数据的操作，由此触发日志的写入。

创建新订单完成后，去查看对应的loginfo表是否有记录被触发增加，loginfo表上右键->查询数据：

.. figure:: image/KStudio110.png

.. figure:: image/KStudio111.png

可以看到loginfo表会增加对应的记录，在新建订单的时候触发了日志的写入。

程序包
---------------------------

以一个统计功能的程序包为例，说明KStudio对程序包功能的使用方法。

新建程序包-统计功能程序包
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

这里将要创建一个基于统计功能的程序包，统计用户的购买力（在指定时间内某个用户消费的金额），统计分类购买力（某个分类商品在指定时间内的销售额）。下面进行程序包的编写，在“数据库”->“onlinemarket”->“模式”->“market”->“程序包”节点上右键选择“新建
程序包”

.. figure:: image/KStudio112.png

基本属性填写如下，名称为statistic，模式和执行身份保持默认即可

.. figure:: image/KStudio113.png

包声明Package Header内容为：

.. code::

    AS
    function userPaymentPower(user_id integer, starttime datetime, endtime datetime) returns numeric; 
    function categoryPayment(categoryid integer, starttime datetime, endtime datetime) returns numeric;
    end

包主体Package Body填写内容如下：

.. code::
    
    AS
    function userPaymentPower(user_id integer, starttime datetime, endtime datetime) returns numeric as
    userpaymenttotal numeric ;
    userpaymentnum integer := 0;
    begin 
        select count(1) into userpaymentnum from orderitem where userid=user_id and createtime>starttime and createtime<endtime;
        if userpaymentnum>0 then
            select sum(totalprice) into userpaymenttotal from orderitem 
            where createtime>starttime and createtime<endtime
            group by userid having userid=user_id limit 1;
            return userpaymenttotal;
        else 
            return 0;
        end if;
        end;
    function categoryPayment(categoryid integer, starttime datetime, endtime datetime) returns numeric as 
    totalpay numeric;
    prices numeric(20,2)[];
    begin 
        totalpay := 0;
        prices := array(select totalprice from orderitem where 
        proid in (select proid from product where cateid=categoryid)
        and createtime>starttime and createtime<endtime);
        if(array_length(prices, 1) > 0) then 
            for i in 1..array_length(prices, 1)
            loop 
                totalpay := totalpay+prices[i];
            end loop;
         else 
            totalpay := 0;
         end if;
         return totalpay;
    end ;
    end statistic


整体截图如下：

.. figure:: image/KStudio114.png

点击右下角的“确定”按钮，程序包即创建完成。

.. figure:: image/KStudio115.png

调试程序包
^^^^^^^^^^^^^^^

在刚创建的程序包上，“数据库”->“onlinemarket”->“模式”->“market”->“程序包”->“statistic”上右键选择“调试”。

.. figure:: image/KStudio116.png

在“内部函数名”下拉菜单中选择调试“categorypayment(int4,datetime,datetime)”函数，同时填写输入参数,categoryid填写为1，starttime和endtime根据实际情况填写，如下图所示：

.. figure:: image/KStudio117.png

点击“确定”按钮进入到调试页面，如下图所示的界面：

.. figure:: image/KStudio118.png

点击“开始调试”按钮\ |image68|\ 进入到程序包的调试，开始调试后页面显示如下：

.. figure:: image/KStudio119.png

点击顶部工具栏的“单步跳过”
|image70|\ 按钮，则下方的ANONYMOUS_BLOCK代码块的执行往下行走

.. figure:: image/KStudio120.png

在returnVar:=
market.statistic.categorypayment(categoryid,starttime,endtime);行的前方行号上双击，即可为其添加断点，如下图所示：

.. figure:: image/KStudio121.png

添加完断点之后，点击顶部工具栏的“继续”\ |image122|\ 按钮可将代码执行到断点处，如下所示：

.. figure:: image/KStudio123.png

点击“单步跳入”
|image74|\ 按钮可跳入到程序包子函数categorypayment的函数体内，如下图所示：

.. figure:: image/KStudio124.png

右上角的“变量”面板显示变量的值变化，单击“单步跳过”
|image70|\ 按钮往下执行

.. figure:: image/KStudio125.png

可以看到totalpay这个变量的值被赋予了0，继续“单步跳过”执行，可以看到prices的值已经获取到了，下面就是将这些值相加赋予给totalpay。

.. figure:: image/KStudio126.png

点击“单步跳过”

.. figure:: image/KStudio127.png

.. figure:: image/KStudio128.png

一直执行到return totalpay；这行上，查看变量的变化

.. figure:: image/KStudio129.png

继续“单步跳入”执行显示如下图：

.. figure:: image/KStudio130.png

再单击“单步跳过”，会打印出程序包子函数执行的结果，如下图所示：

.. figure:: image/KStudio131.png

再单击“单步跳过”

.. figure:: image/KStudio132.png

再单击“单步跳过”，即可结束整个程序包的执行

.. figure:: image/KStudio133.png


.. |image68| image:: image/KStudio68.png
   :width: 0.23958in
   :height: 0.22917in
.. |image70| image:: image/KStudio70.png
   :width: 0.23958in
   :height: 0.22917in
.. |image74| image:: image/KStudio74.png
   :width: 0.23958in
   :height: 0.22917in
.. |image122| image:: image/KStudio122.png
   :width: 0.23958in
   :height: 0.22917in
