-- 歪马送酒看板: 页级汇总表 + 两个汇总视图 -- 采集脚本(歪马送酒_shark请求.py / 歪马送酒_搜索.py)入库时写 waima_page_summary, 看板只读它, 不再解析 rawdata -- 本文件先在库上执行一次, 再跑采集脚本. 可重复执行(表用 if not exists, 视图用 or replace) -- 页级汇总: 一行一页, 只存数出来的数字, 不含 rawdata create table if not exists waima_page_summary ( id int not null auto_increment comment '自增id', source varchar(16) not null comment 'coupon=券商品页, search=搜索页', batch varchar(128) not null comment '任务批次', store_id varchar(128) not null comment '店铺id', store_name varchar(128) not null comment '店铺名', biz_id varchar(255) not null comment '券是couponConfigId, 搜索是keyword', page int not null comment '第几页', product_count int not null default 0 comment '本页商品件数', has_more int not null default 0 comment '本页 hasMore/hasNextPage', total_num int default null comment '搜索命中总数, 券为null', spu_ids text comment '本页商品spuId, 逗号拼接, 看板拿去跨页去重', snapshot_time timestamp not null default current_timestamp comment '页入库时间', created_at timestamp not null default current_timestamp comment '创建时间', updated_at timestamp not null default current_timestamp on update current_timestamp comment '更新时间', primary key (id), unique key uk_page (source, batch, store_id, biz_id, page), key idx_time (snapshot_time), key idx_batch_store (batch, store_id) ) engine=InnoDB default charset=utf8mb4 comment='页级汇总, 看板读这张表不用解析rawdata'; -- 如果之前已经建过 waima_page_summary, 补这一列(只执行一次, 已建表时用): alter table waima_page_summary add column spu_ids text comment '本页商品spuId, 逗号拼接, 看板拿去跨页去重' -- 券汇总: 一张券一行, 页数/件数/状态都在这里算好 create or replace view waima_coupon_summary as select i.id as item_id, i.shop_id as store_id, i.couponConfigId as coupon_id, i.couponName as coupon_name, i.couponTypeDoc as type_doc, i.couponCategoryType as category, i.couponPrice as price, i.limitPriceText as limit_text, i.couponConditionText as condition_text, i.useDoc as use_doc, i.couponUseRules as use_rules, i.tabId, i.is_ok, i.current_page, i.created_at, b.batch, b.store_name, coalesce(p.page_count, 0) as page_count, coalesce(p.product_count, 0) as product_count, p.last_page, p.last_has_more, p.last_time, case when i.is_ok = 1 or p.last_has_more = 0 then case when coalesce(p.product_count, 0) = 0 then 'empty' else 'done' end when i.current_page > 0 or coalesce(p.page_count, 0) > 0 then 'running' else 'pending' end as status from waima_coupon_list_item i left join ( select i2.id as item_id, l.batch, l.store_name from waima_coupon_list_item i2 join waima_coupon_list l on l.id = ( select l2.id from waima_coupon_list l2 where l2.store_id = i2.shop_id and l2.snapshot_time <= i2.created_at order by l2.snapshot_time desc, l2.id desc limit 1 ) ) b on b.item_id = i.id left join ( select batch, store_id, biz_id, page_count, product_count, last_page, last_time, last_has_more from ( select batch, store_id, biz_id, has_more as last_has_more, page as last_page, snapshot_time as last_time, count(*) over w as page_count, sum(product_count) over w as product_count, row_number() over (partition by batch, store_id, biz_id order by page desc) as rn from waima_page_summary where source = 'coupon' window w as (partition by batch, store_id, biz_id) ) t where rn = 1 ) p on p.batch = b.batch and p.store_id = i.shop_id and p.biz_id = i.couponConfigId; -- 关键词汇总: 一个关键词一行, 完成率按任务表 current_page 算 create or replace view waima_keyword_summary as select k.id as task_id, k.batch, k.store_id, k.store_name, k.keyword, k.page as plan_page, k.current_page, k.totalNum as total_num, k.created_at, k.updated_at, coalesce(p.page_count, 0) as saved_pages, coalesce(p.product_count, 0) as product_count, p.last_time, case when k.current_page >= k.page then 'done' when k.current_page > 0 then 'running' else 'pending' end as status from waima_keyword_input k left join ( select batch, store_id, biz_id, count(*) as page_count, sum(product_count) as product_count, max(snapshot_time) as last_time from waima_page_summary where source = 'search' group by batch, store_id, biz_id ) p on p.batch = k.batch and p.store_id = k.store_id and p.biz_id = k.keyword;