Files
crawler-plugin/backend-java/src/main/resources/mapper/jd/JdProductMapper.xml
T

167 lines
10 KiB
XML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.nanri.aiimage.modules.jd.mapper.JdProductMapper">
<!-- 京东店铺商品(V193,v1.4):SQL 全部落 XML(铁律 1)。
覆盖最新唯一写入口:按唯一键 uk_company_shop_code(company_id,shop_id,product_code)
upsert——同一商品二次采集刷新那条(synced_at 刷新),不同商品全保留,行数不增
(R6/AC-5)。后管只读页的分页/筛选 SQL(Phase 5)落同份 XML。 -->
<!-- 同步链唯一写入口(Phase 2 落 JdResultIngestService):多值批量 upsert,
AppearancePatentCleanItemMapper#upsertBatch 同形。刻意不写 company_id/
product_code 的 UPDATE 段(唯一键三元组自身不随更新漂移);
synced_at 在 UPDATE 段显式刷 CURRENT_TIMESTAMP(INSERT 路径走列默认值,
显式传 null 会覆盖掉默认值,故两段口径分开)。
⚠ upsert-only:回传读出 0 行=什么都不写,不得 delete-then-insert 覆盖式重写
(重投递载荷读不准时会抹掉存量,AppearancePatentCleanItemMapper 2026-11-11 实录)。
⚠ UPDATE 段不写 image_count / main_image_url(P1-1):buildRow 恒 0/恒 null,
写进 UPDATE 会把登记链 refreshProductImageCount 刷的真实计数抹回 0
(后管「图片数」恒 0);image_count 只由登记侧按表实数刷,main_image_url
是 P2 死列(BACKLOG B98-01)不随更新。 -->
<insert id="upsertBatch">
INSERT INTO biz_jd_product
(company_id, user_id, task_id, shop_id, shop_name, product_code, sku_id, item_num, sku_count,
health_score, title, brand, brand_id, category_path, price_min, price_max, stock_total,
sale_status, list_tab, jd_created_at, jd_online_at, jd_updated_at,
main_image_url, detail_url, attr_json, attr_count, image_count, collect_batch_id)
VALUES
<foreach collection="rows" item="row" separator=",">
(#{row.companyId}, #{row.userId}, #{row.taskId}, #{row.shopId}, #{row.shopName},
#{row.productCode}, #{row.skuId}, #{row.itemNum}, #{row.skuCount},
#{row.healthScore}, #{row.title}, #{row.brand}, #{row.brandId},
#{row.categoryPath}, #{row.priceMin}, #{row.priceMax}, #{row.stockTotal},
#{row.saleStatus}, #{row.listTab}, #{row.jdCreatedAt}, #{row.jdOnlineAt}, #{row.jdUpdatedAt},
#{row.mainImageUrl}, #{row.detailUrl}, #{row.attrJson}, #{row.attrCount},
#{row.imageCount}, #{row.collectBatchId})
</foreach>
ON DUPLICATE KEY UPDATE
user_id = VALUES(user_id),
task_id = VALUES(task_id),
shop_name = VALUES(shop_name),
sku_id = VALUES(sku_id),
<!-- 货号不进 UPDATE 段(2026-11-15 用户口径「系统自行生成货号,不用之前的货号」):
货号=行号(reassignItemNums 在落库后刷写,全局唯一、从 1 递增,行不变货号就不变),
重爬命中已有行时不得把系统货号刷回京东原值。 -->
sku_count = VALUES(sku_count),
health_score = VALUES(health_score),
title = VALUES(title),
brand = VALUES(brand),
brand_id = VALUES(brand_id),
category_path = VALUES(category_path),
price_min = VALUES(price_min),
price_max = VALUES(price_max),
stock_total = VALUES(stock_total),
sale_status = VALUES(sale_status),
list_tab = VALUES(list_tab),
jd_created_at = VALUES(jd_created_at),
jd_online_at = VALUES(jd_online_at),
jd_updated_at = VALUES(jd_updated_at),
detail_url = VALUES(detail_url),
attr_json = VALUES(attr_json),
attr_count = VALUES(attr_count),
collect_batch_id = VALUES(collect_batch_id),
synced_at = CURRENT_TIMESTAMP
</insert>
<!-- 货号系统生成(2026-11-15 用户口径「我们系统自行生成货号 排序 不用之前的货号;
从 1 开始递增或者随机数(唯一)」):行号即货号——全局唯一、从 1 递增、天然零竞争,
行不变货号就不变(重爬命中已有行时货号稳定)。只在商品行落库后按本任务刷一次:
新插入行的 item_num 是京东原值(暂存),刷成行号;已等于行号的行不重复写(幂等);
manual_locked=1 的行不动(人工编辑过货号的人工值优先)。京东原货号仍在 attr_json
的「货号」属性行留档(京麦原文随属性区同步),主字段货号一律系统形状。 -->
<update id="reassignItemNums">
UPDATE biz_jd_product
SET item_num = CONVERT(id, CHAR)
WHERE company_id = #{companyId}
AND task_id = #{taskId}
AND manual_locked = 0
AND (item_num IS NULL OR item_num = '' OR item_num != CONVERT(id, CHAR))
</update>
<!-- 覆盖清理候选行(重爬覆盖语义,2026-11-15):本任务成功收尾后,同一公司同一批店铺里
不是本任务写、且所属任务已不在在飞(PENDING/RUNNING 不删——同店铺并发两单先收尾的
单不得抹掉仍在跑的单的行,后收尾的单会在自己的收尾时清)的商品行。
shop_id 取本任务落过的行的 DISTINCT 值(shop_id 缺省回落 shop_name,与 buildRow 同源),
删除本体走 JdProductRowDeleteService#deleteRows(SKU/图片行级联),本查询只出 id。
V199:manual_locked=1 的行(后管人工编辑过)不进清理候选——人工值优先,重爬不得整行删掉。 -->
<select id="selectSupersededProductIds" resultType="long">
SELECT p.id
FROM biz_jd_product p
WHERE p.company_id = #{companyId}
<!-- V199 覆盖清理候选必须排除 manual_locked=1 的行(V199 人工编辑锁) -->
AND p.manual_locked = 0
AND p.shop_id IN (
SELECT DISTINCT shop_id FROM biz_jd_product
WHERE company_id = #{companyId} AND task_id = #{taskId}
)
AND p.task_id != #{taskId}
AND NOT EXISTS (
SELECT 1 FROM biz_file_task t
WHERE t.id = p.task_id AND t.status IN ('PENDING', 'RUNNING')
)
</select>
<!-- 后管只读页(Phase 5,v1.4)过滤面:公司(物化列 eq)、商品编码/标题关键词
(contains LIKE)、类目 eq、分组 eq、未分类(group_id IS NULL,店铺分类弹窗
「未分类」范围)、已分类(group_id IS NOT NULL,页面筛选行「分类状态」档,
2026-11-15 客户口径「查询已分类的跟未分类,分类好的可以不显示」)、
同步起止(datetime 闭区间)。
sale_status 筛选随 2026-11-15 客户口径撤掉;店铺名过滤同日客户口径「店铺不需要」
同窗撤掉(前端筛选框与列下线;DB 列本身与详情/回传链不动)。
全部走 #{} 占位符;offset/limit 必须是服务侧钳制后的值(铁律 15),mapper 不做第二次换算。 -->
<sql id="productConds">
<if test="companyId != null and companyId &gt; 0">AND p.company_id = #{companyId}</if>
<if test="productCode != null and productCode != ''">AND p.product_code LIKE CONCAT('%', #{productCode}, '%')</if>
<if test="titleKeyword != null and titleKeyword != ''">AND p.title LIKE CONCAT('%', #{titleKeyword}, '%')</if>
<if test="category != null and category != ''">AND p.category_path = #{category}</if>
<if test="groupId != null and groupId &gt; 0">AND p.group_id = #{groupId}</if>
<if test="ungrouped != null and ungrouped">AND p.group_id IS NULL</if>
<if test="grouped != null and grouped">AND p.group_id IS NOT NULL</if>
<if test="syncedFrom != null">AND p.synced_at &gt;= #{syncedFrom}</if>
<if test="syncedTo != null">AND p.synced_at &lt;= #{syncedTo}</if>
</sql>
<!-- 只读页总数(与 selectProductPage 同一过滤面)。 -->
<select id="countProducts" resultType="long">
SELECT COUNT(*)
FROM biz_jd_product p
WHERE 1=1 <include refid="productConds"/>
</select>
<!-- 只读页分页查询(synced_at 倒序)。
group_name 不在此联表:读侧按行集 group_id 批量取分组名补齐(一次查询,无逐行查询)。 -->
<select id="selectProductPage" resultType="com.nanri.aiimage.modules.jd.model.entity.JdProductEntity">
SELECT p.id, p.company_id, p.user_id, p.task_id, p.shop_id, p.shop_name, p.product_code, p.sku_id,
p.item_num, p.sku_count, p.health_score,
p.title, p.brand, p.brand_id, p.category_path, p.price_min, p.price_max, p.stock_total,
p.sale_status, p.list_tab, p.jd_created_at, p.jd_online_at, p.jd_updated_at,
p.main_image_url, p.detail_url,
p.attr_json, p.attr_count, p.image_count, p.group_id, p.collect_batch_id, p.synced_at
FROM biz_jd_product p
WHERE 1=1 <include refid="productConds"/>
ORDER BY p.synced_at DESC, p.id DESC
LIMIT #{offset}, #{limit}
</select>
<!-- 类目筛选下拉数据源(2026-11-15 用户口径「京麦的类目也要同步过来后台」):本公司已落库行
的 DISTINCT category_path(=京麦末级类目名,契约 §2.3 E3)+ 行数。
空类目行不进下拉;companyId 为空(超管不选公司=全量)时跨公司去重。 -->
<select id="selectProductCategories" resultType="com.nanri.aiimage.modules.jd.model.vo.JdProductCategoryVo">
SELECT p.category_path AS name, COUNT(*) AS productCount
FROM biz_jd_product p
WHERE p.category_path IS NOT NULL AND p.category_path != ''
<if test="companyId != null">AND p.company_id = #{companyId}</if>
GROUP BY p.category_path
ORDER BY p.category_path
</select>
<!-- 商品上架配额计数(JdPublishQuotaService):按 owner uid 集计 biz_jd_product 行数;
uid 集=管理员本人+名下员工(公司超管=全公司成员),由服务侧保证非空
(空集按 0 处理,不进 SQL)。user_id=任务 owner(V193 物化归属列)。 -->
<select id="countByUserIds" resultType="long">
SELECT COUNT(*)
FROM biz_jd_product
WHERE user_id IN
<foreach collection="userIds" item="uid" open="(" separator="," close=")">#{uid}</foreach>
</select>
</mapper>