<iftest="query.belong != null"> and (`goods_sku`.`belong` = #{query.belong} or goods_sku.special_flag=1) </if>
<iftest="query.selectSpecialFlag != null and query.selectSpecialFlag == 1">
and `goods_sku`.`special_flag` = 1
and `goods_sku`.id = goods_spu.special_sku_id
</if>
<iftest="query.selectSpecialFlag != null and query.selectSpecialFlag == 0">
and `goods_sku`.`special_flag` = 0
</if>
<iftest="query.orderFlag != null"> and `goods_sku`.`order_flag` = #{query.orderFlag}</if>
<iftest="query.keyword != null and query.keyword != '' ">
and (
`goods_spu`.`name` like concat('%', #{query.keyword},'%') or
`goods_spu`.`spu_code` like concat('%', #{query.keyword},'%') or
`goods_sku`.`sku_code` like concat('%', #{query.keyword},'%') or
`goods_sku`.`sku_name` like concat('%', #{query.keyword},'%')
)
</if>
</sql>
<sqlid="spuWherePart">
and `goods_spu`.`del_flag` = 0
<iftest="query.id != null"> and `goods_spu`.`id` = #{query.id}</if>
<iftest="query.spuCode != null and query.spuCode != ''"> and `goods_spu`.`spu_code` like concat('%', #{query.spuCode},'%')</if>
<iftest="query.name != null and query.name != ''"> and `goods_spu`.`name` like concat('%', #{query.name},'%') </if>
<iftest="query.categoryFirst != null"> and `goods_spu`.`category_first` = #{query.categoryFirst}</if>
<iftest="query.categorySecond != null"> and `goods_spu`.`category_second` = #{query.categorySecond}</if>
<iftest="query.categoryThird != null"> and `goods_spu`.`category_third` = #{query.categoryThird}</if>
<iftest="query.cateFirstLabel != null and query.cateFirstLabel != ''"> and `cate1`.`name` like concat('%',#{query.cateFirstLabel},'%') </if>
<iftest="query.cateSecondLabel != null and query.cateSecondLabel != ''"> and `cate2`.`name` like concat('%',#{query.cateSecondLabel},'%') </if>
<iftest="query.cateThirdLabel != null and query.cateThirdLabel != ''"> and `cate3`.`name` like concat('%',#{query.cateThirdLabel},'%') </if>
<iftest="query.categoryLast != null"> and `goods_spu`.`category_last` = #{query.categoryLast}</if>
<iftest="query.shelf != null and query.shelf != ''"> and `goods_spu`.`shelf` = #{query.shelf}</if>
<iftest="query.sort != null"> and `goods_spu`.`sort` = #{query.sort}</if>
<iftest="query.saleNum != null"> and `goods_spu`.`sale_num` = #{query.saleNum}</if>
<iftest="query.tasteValue != null and query.tasteValue != ''"> and `goods_spu`.`taste_value` like concat('%',#{query.tasteValue},'%') </if>
<iftest="query.createTime != null"> and `goods_spu`.`create_time` = #{query.createTime}</if>
<iftest="query.updateTime != null"> and `goods_spu`.`update_time` = #{query.updateTime}</if>
<iftest="query.createBy != null and query.createBy != ''"> and `goods_spu`.`create_by` = #{query.createBy}</if>
<iftest="query.updateBy != null and query.updateBy != ''"> and `goods_spu`.`update_by` = #{query.updateBy}</if>
<iftest="query.tenantId != null and query.tenantId != ''"> and `goods_spu`.`tenant_id` = #{query.tenantId}</if>
<iftest="query.delFlag != null and query.delFlag != ''"> and `goods_spu`.`del_flag` = #{query.delFlag}</if>
<!--<if test="query.selectSpecialFlag != null and query.selectSpecialFlag == 1">
and `goods_spu`.`special_sku_id` is not null and `goods_spu`.`special_sku_id` != '0'
</if>
<iftest="query.selectSpecialFlag != null and query.selectSpecialFlag == 0">
and (`goods_spu`.`special_sku_id` is null or `goods_spu`.`special_sku_id` = '0')
</if>-->
<iftest="query.searchValue != null and query.searchValue != ''"> and (`goods_spu`.`name` like concat('%',#{query.searchValue},'%') or `goods_spu`.`goods_spu` like concat('%',#{query.searchValue},'%')) </if>
<iftest="query.selectIds!=null and query.selectIds.size > 0">
<iftest="query.categorySecond != null"> and `goods_spu`.`category_second` = #{query.categorySecond}</if>
SELECT
<iftest="query.categoryThird != null"> and `goods_spu`.`category_third` = #{query.categoryThird}</if>
`cate1`.`name` AS `cate_first_label`,
<iftest="query.cateFirstLabel != null and query.cateFirstLabel != ''"> and `cate1`.`name` like concat('%',#{query.cateFirstLabel},'%') </if>
`cate2`.`name` AS `cate_second_label`,
<iftest="query.cateSecondLabel != null and query.cateSecondLabel != ''"> and `cate2`.`name` like concat('%',#{query.cateSecondLabel},'%') </if>
`cate3`.`name` AS `cate_third_label`,
<iftest="query.cateThirdLabel != null and query.cateThirdLabel != ''"> and `cate3`.`name` like concat('%',#{query.cateThirdLabel},'%') </if>
<includerefid="goodsSpuSql"/>,
<iftest="query.categoryLast != null"> and `goods_spu`.`category_last` = #{query.categoryLast}</if>
<includerefid="goodsSkuSql"/>
<iftest="query.shelf != null and query.shelf != ''"> and `goods_spu`.`shelf` = #{query.shelf}</if>
FROM (
<iftest="query.sort != null"> and `goods_spu`.`sort` = #{query.sort}</if>
(SELECT sku.*,ROW_NUMBER() OVER(PARTITION BY sku.spu_id ORDER BY sku.belong_sort) AS rn FROM goods_sku sku WHERE sku.order_flag = 1 AND sku.special_flag = 0)
<iftest="query.saleNum != null"> and `goods_spu`.`sale_num` = #{query.saleNum}</if>
UNION ALL
<iftest="query.belong != null"> and `goods_spu`.`belong` = #{query.belong}</if>
SELECT sku.*,1 as rn FROM goods_sku sku WHERE sku.order_flag = 1 AND sku.special_flag = 1
<iftest="query.tasteValue != null and query.tasteValue != ''"> and `goods_spu`.`taste_value` like concat('%',#{query.tasteValue},'%') </if>
) AS goods_sku
<iftest="query.createTime != null"> and `goods_spu`.`create_time` = #{query.createTime}</if>
left join goods_spu on goods_sku.spu_id = goods_spu.id
<iftest="query.updateTime != null"> and `goods_spu`.`update_time` = #{query.updateTime}</if>
LEFT JOIN `goods_category` `cate1` ON `cate1`.`id` = `goods_spu`.`category_first`
<iftest="query.createBy != null and query.createBy != ''"> and `goods_spu`.`create_by` = #{query.createBy}</if>
LEFT JOIN `goods_category` `cate2` ON `cate2`.`id` = `goods_spu`.`category_second`
<iftest="query.updateBy != null and query.updateBy != ''"> and `goods_spu`.`update_by` = #{query.updateBy}</if>
LEFT JOIN `goods_category` `cate3` ON `cate3`.`id` = `goods_spu`.`category_third`
<iftest="query.tenantId != null and query.tenantId != ''"> and `goods_spu`.`tenant_id` = #{query.tenantId}</if>
<where>
<iftest="query.delFlag != null and query.delFlag != ''"> and `goods_spu`.`del_flag` = #{query.delFlag}</if>
and rn = 1
<iftest="query.orderFlag != null"> and `goods_spu`.`order_flag` = #{query.orderFlag}</if>
<includerefid="spuWherePart"></include>
<iftest="query.selectSpecialFlag != null and query.selectSpecialFlag == 1">
<includerefid="skuWherePart"></include>
and `goods_spu`.`special_sku_id` is not null and `goods_spu`.`special_sku_id` != '0'
</where>
</if>
limit #{pageInfo.startIndex},#{pageInfo.size}
<iftest="query.selectSpecialFlag != null and query.selectSpecialFlag == 0">
</select>
and (`goods_spu`.`special_sku_id` is null or `goods_spu`.`special_sku_id` = '0')
<iftest="query.searchValue != null and query.searchValue != ''"> and (`goods_spu`.`name` like concat('%',#{query.searchValue},'%') or `goods_spu`.`goods_spu` like concat('%',#{query.searchValue},'%')) </if>
SELECT
<iftest="query.selectIds!=null and query.selectIds.size > 0">
(SELECT sku.*,ROW_NUMBER() OVER(PARTITION BY sku.spu_id ORDER BY sku.belong_sort) AS rn FROM goods_sku sku WHERE sku.order_flag = 1 AND sku.special_flag = 0)
#{selectId}
UNION ALL
</foreach>
SELECT sku.*,1 as rn FROM goods_sku sku WHERE sku.order_flag = 1 AND sku.special_flag = 1
</if>
) AS goods_sku
<iftest="query.selectCateIds!=null and query.selectCateIds.size > 0">
left join goods_spu on goods_sku.spu_id = goods_spu.id