# 统计信息 ## 表结构设计 - lucky_stat_shop 店铺统计信息,按天统计 - lucky_stat_shop_hour 店铺统计信息,按小时统计 ``` sql 数据源: local_shop_xcx30.5g 架构: shop_mallnew 表: lucky_stat_shop -- auto-generated definition create table lucky_stat_shop ( id int(11) unsigned auto_increment primary key, site_id int default 0 not null comment '站点id', year int default 0 not null comment '年', month int default 0 not null comment '月', day int default 0 not null comment '日', day_time int default 0 not null comment '当日时间', order_total decimal(10, 2) default 0.00 not null comment '订单金额', shipping_total decimal(10, 2) default 0.00 not null comment '运费金额', refund_total decimal(10, 2) default 0.00 not null comment '退款金额', order_pay_count int default 0 not null comment '订单总数', goods_pay_count int default 0 not null comment '订单商品总数', shop_money decimal(10, 2) default 0.00 not null comment '店铺金额', platform_money decimal(10, 2) default 0.00 not null comment '平台金额', create_time int default 0 not null comment '创建时间', modify_time int default 0 not null comment '修改时间', collect_shop int default 0 not null comment '店铺收藏量', collect_goods int default 0 not null comment '商品收藏量', visit_count int default 0 not null comment '浏览量', order_count int default 0 not null comment '订单量(总)', goods_count int default 0 not null comment '订单商品量(总)', add_goods_count int default 0 not null comment '添加商品数', member_count int default 0 not null comment '会员统计', order_member_count int default 0 not null comment '下单会员数', order_refund_count int default 0 not null comment '退款订单数', order_refund_grand_count decimal(10, 2) default 0.00 not null comment '退款金额', order_refund_grand_total_money decimal(10, 2) default 0.00 not null comment '累计退款金额', coupon_member_count int default 0 not null comment '领券会员数量', member_level_count int default 0 not null comment '超级会员卡销售量', member_level_total_money decimal(10, 2) default 0.00 not null comment '超级会员卡销售额', member_level_grand_count int default 0 not null comment '累计超级会员卡销售量', member_level_grand_total_money decimal(10, 2) default 0.00 not null comment '累计超级会员卡销售额', member_recharge_count int default 0 not null comment '会员储值总订单量', member_recharge_grand_count decimal(10, 2) default 0.00 not null comment '累计会员储值总量', member_recharge_total_money decimal(10, 2) default 0.00 not null comment '会员充值总额', member_recharge_grand_total_money decimal(10, 2) default 0.00 not null comment '累计会员充值总额', member_recharge_member_count int default 0 not null comment '储值会员数', member_giftcard_count int default 0 not null comment '礼品卡订单总量', member_giftcard_grand_count int default 0 not null comment '累计礼品卡订单总量', member_giftcard_total_money decimal(10, 2) default 0.00 not null comment '礼品卡订单总额', h5_visit_count int default 0 not null comment 'h5访问量', wechat_visit_count int default 0 not null comment 'wechat访问量', weapp_visit_count int default 0 not null comment 'weapp访问量', pc_visit_count int default 0 not null comment 'pc访问量', expected_earnings_total_money decimal(10, 2) default 0.00 not null comment '预计收入', expenditure_total_money decimal(10, 2) default 0.00 not null comment '总支出', earnings_total_money decimal(10, 2) default 0.00 not null comment '总收入', member_withdraw_count int default 0 not null comment '会员提现总量', member_withdraw_total_money decimal(10, 2) default 0.00 not null comment '会员提现总额', coupon_count int default 0 not null comment '领券数', add_coupon_count int default 0 not null comment '新增优惠券', order_pay_money decimal(10, 2) default 0.00 not null comment '订单实际支付', add_fenxiao_member_count int default 0 not null comment '新增分销商', fenxiao_order_total_money decimal(10, 2) default 0.00 not null comment '分销订单总额', fenxiao_order_count int default 0 not null comment '分销订单总数', goods_on_type_count int default 0 not null comment '在架商品数', goods_visited_type_count int default 0 not null comment '被访问商品数(仅详情页浏览数)', goods_order_type_count int default 0 not null comment '动销商品数', goods_exposure_count int default 0 not null comment '商品曝光数', goods_visit_count int default 0 not null comment '商品浏览量', goods_visit_member_count int default 0 not null comment '商品访客数', goods_cart_count int default 0 not null comment '加购件数', goods_order_count decimal(12, 3) default 0.000 not null comment '下单件数', order_create_money decimal(10, 2) default 0.00 not null comment '订单下单总额', order_create_count int default 0 not null comment '订单下单量', balance_deduction decimal(10, 2) unsigned default 0.00 not null comment '余额抵扣总额', cashier_billing_count int default 0 not null comment '开单数量', cashier_billing_money decimal(10, 2) default 0.00 not null comment '开单金额', cashier_buycard_count int default 0 not null comment '办卡数量', cashier_buycard_money decimal(10, 2) default 0.00 not null comment '办卡金额', cashier_recharge_count int default 0 not null comment '收银台充值数量', cashier_recharge_money decimal(10, 2) default 0.00 not null comment '收银台充值金额', cashier_refund_count int default 0 not null comment '收银台退款数量', cashier_refund_money decimal(10, 2) default 0.00 not null comment '收银台退款金额', cashier_order_member_count int default 0 not null comment '收银台下单会员数', cashier_balance_money decimal(10, 2) default 0.00 not null comment '收银台余额消费金额', cashier_online_pay_money decimal(10, 2) default 0.00 not null comment '收银台线上金额', cashier_online_refund_money decimal(10, 2) default 0.00 not null comment '收银台线上退款金额', cashier_balance_deduction decimal(10, 2) default 0.00 not null comment '门店余额总计' ) charset = utf8 row_format = DYNAMIC; create index IDX_ns_stat_shop_day on lucky_stat_shop (day); create index IDX_ns_stat_shop_day_time on lucky_stat_shop (day_time); create index IDX_ns_stat_shop_month on lucky_stat_shop (month); create index IDX_ns_stat_shop_site_id on lucky_stat_shop (site_id); create index IDX_ns_stat_shop_year on lucky_stat_shop (year); 数据源: local_shop_xcx30.5g 架构: shop_mallnew 表: lucky_stat_shop_hour -- auto-generated definition create table lucky_stat_shop_hour ( id int(11) unsigned auto_increment primary key, site_id int default 0 not null comment '站点id', year int default 0 not null comment '年', month int default 0 not null comment '月', day int default 0 not null comment '日', hour int default 0 not null comment '时', day_time int default 0 not null comment '当日时间', order_total decimal(10, 2) default 0.00 not null comment '订单金额', shipping_total decimal(10, 2) default 0.00 not null comment '运费金额', refund_total decimal(10, 2) default 0.00 not null comment '退款金额', order_pay_count int default 0 not null comment '订单总数', goods_pay_count int default 0 not null comment '订单商品总数', shop_money decimal(10, 2) default 0.00 not null comment '店铺金额', platform_money decimal(10, 2) default 0.00 not null comment '平台金额', create_time int default 0 not null comment '创建时间', modify_time int default 0 not null comment '修改时间', collect_shop int default 0 not null comment '店铺收藏量', collect_goods int default 0 not null comment '商品收藏量', visit_count int default 0 not null comment '浏览量', order_count int default 0 not null comment '订单量(总)', goods_count int default 0 not null comment '订单商品量(总)', add_goods_count int default 0 not null comment '添加商品数', member_count int default 0 not null comment '会员统计', order_member_count int default 0 not null comment '下单会员数', order_refund_count int default 0 not null comment '退款订单数', order_refund_grand_count decimal(10, 2) default 0.00 not null comment '退款金额', order_refund_grand_total_money decimal(10, 2) default 0.00 not null comment '累计退款金额', coupon_member_count int default 0 not null comment '领券会员数量', member_level_count int default 0 not null comment '超级会员卡销售量', member_level_total_money decimal(10, 2) default 0.00 not null comment '超级会员卡销售额', member_level_grand_count int default 0 not null comment '累计超级会员卡销售量', member_level_grand_total_money decimal(10, 2) default 0.00 not null comment '累计超级会员卡销售额', member_recharge_count int default 0 not null comment '会员储值总订单量', member_recharge_grand_count decimal(10, 2) default 0.00 not null comment '累计会员储值总量', member_recharge_total_money decimal(10, 2) default 0.00 not null comment '会员充值总额', member_recharge_grand_total_money decimal(10, 2) default 0.00 not null comment '累计会员充值总额', member_recharge_member_count int default 0 not null comment '储值会员数', member_giftcard_count int default 0 not null comment '礼品卡订单总量', member_giftcard_grand_count int default 0 not null comment '累计礼品卡订单总量', member_giftcard_total_money decimal(10, 2) default 0.00 not null comment '礼品卡订单总额', h5_visit_count int default 0 not null comment 'h5访问量', wechat_visit_count int default 0 not null comment 'wechat访问量', weapp_visit_count int default 0 not null comment 'weapp访问量', pc_visit_count int default 0 not null comment 'pc访问量', expected_earnings_total_money decimal(10, 2) default 0.00 not null comment '预计收入', expenditure_total_money decimal(10, 2) default 0.00 not null comment '总支出', earnings_total_money decimal(10, 2) default 0.00 not null comment '总收入', member_withdraw_count int default 0 not null comment '会员提现总量', member_withdraw_total_money decimal(10, 2) default 0.00 not null comment '会员提现总额', coupon_count int default 0 not null comment '领券数', add_coupon_count int default 0 not null comment '新增优惠券', order_pay_money decimal(10, 2) default 0.00 not null comment '订单实际支付', add_fenxiao_member_count int default 0 not null comment '新增分销商', fenxiao_order_total_money decimal(10, 2) default 0.00 not null comment '分销订单总额', fenxiao_order_count int default 0 not null comment '分销订单总数', goods_on_type_count int default 0 not null comment '在架商品数', goods_visited_type_count int default 0 not null comment '被访问商品数(仅详情页浏览数)', goods_order_type_count int default 0 not null comment '动销商品数', goods_exposure_count int default 0 not null comment '商品曝光数', goods_visit_count int default 0 not null comment '商品浏览量', goods_visit_member_count int default 0 not null comment '商品访客数', goods_cart_count int default 0 not null comment '加购件数', goods_order_count decimal(12, 3) default 0.000 not null comment '下单件数', order_create_money decimal(10, 2) default 0.00 not null comment '订单下单总额', order_create_count int default 0 not null comment '订单下单量', balance_deduction decimal(10, 2) default 0.00 not null comment '余额抵扣总额', cashier_billing_count int default 0 not null comment '开单数量', cashier_billing_money decimal(10, 2) default 0.00 not null comment '开单金额', cashier_buycard_count int default 0 not null comment '办卡数量', cashier_buycard_money decimal(10, 2) default 0.00 not null comment '办卡金额', cashier_recharge_count int default 0 not null comment '收银台充值数量', cashier_recharge_money decimal(10, 2) default 0.00 not null comment '收银台充值金额', cashier_refund_count int default 0 not null comment '收银台退款数量', cashier_refund_money decimal(10, 2) default 0.00 not null comment '收银台退款金额', cashier_order_member_count int default 0 not null comment '收银台下单会员数', cashier_balance_money decimal(10, 2) default 0.00 not null comment '收银台余额消费金额', cashier_online_pay_money decimal(10, 2) default 0.00 not null comment '收银台线上金额', cashier_online_refund_money decimal(10, 2) default 0.00 not null comment '收银台线上退款金额', cashier_balance_deduction decimal(10, 2) default 0.00 not null comment '门店余额总计' ) charset = utf8 row_format = DYNAMIC; create index IDX_ns_stat_shop_hour_day on lucky_stat_shop_hour (day); create index IDX_ns_stat_shop_hour_day_time on lucky_stat_shop_hour (day_time); create index IDX_ns_stat_shop_hour_hour on lucky_stat_shop_hour (hour); create index IDX_ns_stat_shop_hour_month on lucky_stat_shop_hour (month); create index IDX_ns_stat_shop_hour_site_id on lucky_stat_shop_hour (site_id); create index IDX_ns_stat_shop_hour_year on lucky_stat_shop_hour (year); ``` ## 数据关联 ### 1. 数据的写入 addShopStat 与添加店铺统计数据有关的函数,addShopStat 在三个文件中,都存在 - File1: `src\app\model\stat\StatShop.php` - File2: `src\app\model\system\Stat.php` 实现细节,该File1文件中的 `addShopStat` 函数,从自己函数 `getStatData` 获得整理后的数据,然后将数据传输给 File2 `src/app/model/stat/StatShop.php` 的 `addShopStat` 函数 ```php // src/app/model/stat/StatShop.php public function addShopStat($data) { $carbon = Carbon::now(); $dir = __UPLOAD__.'/stat/stat_shop/'; if (!is_dir($dir) && !mkdir($dir, 0777, true) && !is_dir($dir)) { return $this->error(sprintf('Directory "%s" was not created', $dir)); } $filename = $dir.$carbon->year.'_'.$carbon->month.'_'.$carbon->day.'_'.$carbon->second.'_'.unique_random().'.json'; $stat_extend = new Stat($filename, 'stat_shop',$data['site_id']); $stat_extend->handleData($data);//写入文件 //增加当天时统计 $this->addShopHourStat($data, $carbon); return $this->success(); } ``` 分析:addShopStat,是将统计记录写入到文件中,为快速存储数据及留痕提供数据支持。 ### 2. 数据的提取 cronShopStat 计划任务统计数据处理 文件:src/app/model/stat/StatShop.php `cronShopStat`,是将统计记录从文件中提取出来,然后写入到数据库中,为快速存储数据及留痕提供数据支持。 ``` php // src\app\model\stat\StatShop.php /** * 从stat_shop目录下读取所有文件,将数据处理后,写入数据表中 * 处理完每个文件后,删除文件 */ public function cronShopStat() { $path = __UPLOAD__.'/stat/stat_shop'; if(!is_dir($path)) return; $result = $this->scanFile($path); if(empty($result)) return; try { $json_array = []; foreach ($result as $key => $val){ $stat_extend = new Stat($path.'/'.$val, 'stat_shop'); $json_array[] = $stat_extend->load(); unlink($path.'/'.$val); // 处理完文件后,删除文件 } $data_array = []; foreach ($json_array as $json_k => $json_v){ $k = $json_v['year'].'_'.$json_v['month'].'_'.$json_v['day']; if (isset($data_array[$k])){ foreach ($data_array[$k] as $data_k => $data_v){ if($data_k != 'site_id' && $data_k != 'year' && $data_k != 'month' && $data_k != 'day' && $data_k != 'day_time'){ if ($json_v[$data_k] > 0) { $data_array[$k][$data_k] += $json_v[$data_k]; } else if ($json_v[$data_k] < 0) { $data_array[$k][$data_k] -= abs($json_v[$data_k]); } } } }else{ $data_array[$k] = $json_v; } } Log::write(time().'stat_shop_'.json_encode($data_array)); $system_stat = new \app\model\system\Stat(); foreach ($data_array as $json_k => $json_v){ $system_stat->addStatShopModel($json_v); } } catch (\Exception $e) { } } ``` 1. 从函数的开头可以看出,该函数用于计划任务。 2. 这个函数将被 `src\app\event\stat\CronStatShop.php` 中的 handle() 方法调用. 3. 事件的绑定在 `src\app\event.php` 文件中定义。 ```php //店铺统计更新(按时) 'CronStatShopHour' => [ 'app\event\stat\CronStatShopHour' ], ``` 说明,需要通过ThinkPHP的Event的手动触发或者自动触发来处理 方式1:手动触发(最常用) ```php // 通过 event()助手函数或 Event门面触发: // 在控制器、模型或任何地方触发事件 event('UserLogin', $user); // 等价于 use think\facade\Event; Event::trigger('UserLogin', $user); ``` 方式2:自动触发(通过事件类) 定义事件类并直接触发: ```php // 定义事件类 namespace app\event; class UserLogin { public function __construct(public $user) {} } // 触发事件 $event = new \app\event\UserLogin($user); event($event); ``` ### 3. 数据的展示调用 ## 数据获取 数据对应的文件:`src\app\model\system\Stat.php` 1. 获取天统计表统计字段:getStatField 2. 获取时统计表统计字段:getStatHourField 3. 获取商品销量排行榜:getGoodsSaleNumRankingList ## 应用场景 ### 优惠券的统计 调用者:`src\addon\coupon\model\CouponStat.php` 被调用者:`src\app\model\system\Stat.php` ### 分销订单的统计 调用者:`src\addon\fenxiao\model\FenxiaoStat.php` 被调用者:`src\app\model\system\Stat.php` ### 虚拟商品的统计 调用者:`src\addon\virtualcard\model\VirtualGoods.php` 被调用者:`src\app\model\system\Stat.php` ### 商品收藏的统计 调用者:`src\app\model\goods\GoodsCollect.php` 被调用者:`src\app\model\system\Stat.php` ### 购物车的统计 调用者:`src\app\model\stat\GoodsCartStat.php` 被调用者:`src\app\model\system\Stat.php`