<?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace= "com.qs.serve.modules.goods.mapper.GoodsSpuMapper" >
<resultMap id= "goodsSpuMap" type= "com.qs.serve.modules.goods.entity.GoodsSpu" >
<result property= "id" column= "id" />
<result property= "spuCode" column= "spu_code" />
<result property= "name" column= "name" />
<result property= "categoryFirst" column= "category_first" />
<result property= "categorySecond" column= "category_second" />
<result property= "categoryThird" column= "category_third" />
<result property= "categoryLast" column= "category_last" />
<result property= "picUrls" column= "pic_urls" typeHandler= "com.qs.serve.common.framework.mybatis.handler.meta.SplitStringTypeHandler" />
<result property= "shelf" column= "shelf" />
<result property= "sort" column= "sort" />
<result property= "saleNum" column= "sale_num" />
<result property= "tasteValue" column= "taste_value" />
<result property= "createTime" column= "create_time" />
<result property= "updateTime" column= "update_time" />
<result property= "createBy" column= "create_by" />
<result property= "updateBy" column= "update_by" />
<result property= "tenantId" column= "tenant_id" />
<result property= "delFlag" column= "del_flag" />
<result property= "costFlag" column= "cost_flag" />
<result property= "cateFirstLabel" column= "cate_first_label" />
<result property= "cateSecondLabel" column= "cate_second_label" />
<result property= "cateThirdLabel" column= "cate_third_label" />
<result property= "belong" column= "belong" />
</resultMap>
<sql id= "goodsSkuSql" >
goods_sku.volume,
goods_sku.weight,
goods_sku.sales_price as sku_price,
goods_sku.sku_name as sku_name,
goods_sku.id as sku_id,
goods_sku.sku_code as sku_code,
goods_sku.special_flag as sku_special_flag
</sql>
<sql id= "goodsSpuSql" >
goods_spu.`id`,
goods_spu.`spu_code`,
goods_spu.`name`,
goods_spu.`category_first`,
goods_spu.`category_second`,
goods_spu.`category_third`,
goods_spu.`category_last`,
goods_spu.`pic_urls`,
goods_spu.`shelf`,
goods_spu.`sort`,
goods_spu.`sale_num`,
goods_spu.`spec_type`,
goods_spu.`taste_value`,
goods_spu.`create_time`,
goods_spu.`update_time`,
goods_spu.`create_by`,
goods_spu.`update_by`,
goods_spu.`tenant_id`,
goods_spu.`cost_flag`,
goods_spu.`order_flag`,
goods_spu.`del_flag`,
goods_spu.`special_sku_id`,
goods_spu.`sku_num_val`,
goods_spu.`belong`
</sql>
<sql id= "skuWherePart" >
<if test= "query.belong != null" > and (`goods_sku`.`belong` = #{query.belong} or goods_sku.special_flag=1) </if>
<if test= "query.selectSpecialFlag != null and query.selectSpecialFlag != -1" >
and `goods_sku`.`special_flag` = #{query.selectSpecialFlag}
</if>
<if test= "query.orderFlag != null " >
and (`goods_sku`.`order_flag` = #{query.orderFlag} or `goods_sku`.`special_flag` = 1)
</if>
<if test= "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>
<sql id= "spuWherePart" >
and `goods_spu`.`del_flag` = 0
<if test= "query.id != null" > and `goods_spu`.`id` = #{query.id}</if>
<if test= "query.spuCode != null and query.spuCode != ''" > and `goods_spu`.`spu_code` like concat('%', #{query.spuCode},'%')</if>
<if test= "query.name != null and query.name != ''" > and `goods_spu`.`name` like concat('%', #{query.name},'%') </if>
<if test= "query.categoryFirst != null" > and `goods_spu`.`category_first` = #{query.categoryFirst}</if>
<if test= "query.categorySecond != null" > and `goods_spu`.`category_second` = #{query.categorySecond}</if>
<if test= "query.categoryThird != null" > and `goods_spu`.`category_third` = #{query.categoryThird}</if>
<if test= "query.cateFirstLabel != null and query.cateFirstLabel != ''" > and `cate1`.`name` like concat('%',#{query.cateFirstLabel},'%') </if>
<if test= "query.cateSecondLabel != null and query.cateSecondLabel != ''" > and `cate2`.`name` like concat('%',#{query.cateSecondLabel},'%') </if>
<if test= "query.cateThirdLabel != null and query.cateThirdLabel != ''" > and `cate3`.`name` like concat('%',#{query.cateThirdLabel},'%') </if>
<if test= "query.categoryLast != null" > and `goods_spu`.`category_last` = #{query.categoryLast}</if>
<if test= "query.shelf != null" > and `goods_spu`.`shelf` = #{query.shelf}</if>
<if test= "query.costFlag != null" > and `goods_spu`.`cost_flag` = #{query.costFlag}</if>
<if test= "query.orderFlag != null" > and `goods_spu`.`order_flag` = #{query.orderFlag}</if>
<if test= "query.sort != null" > and `goods_spu`.`sort` = #{query.sort}</if>
<if test= "query.saleNum != null" > and `goods_spu`.`sale_num` = #{query.saleNum}</if>
<if test= "query.tasteValue != null and query.tasteValue != ''" > and `goods_spu`.`taste_value` like concat('%',#{query.tasteValue},'%') </if>
<if test= "query.createTime != null" > and `goods_spu`.`create_time` = #{query.createTime}</if>
<if test= "query.updateTime != null" > and `goods_spu`.`update_time` = #{query.updateTime}</if>
<if test= "query.createBy != null and query.createBy != ''" > and `goods_spu`.`create_by` = #{query.createBy}</if>
<if test= "query.updateBy != null and query.updateBy != ''" > and `goods_spu`.`update_by` = #{query.updateBy}</if>
<if test= "query.tenantId != null and query.tenantId != ''" > and `goods_spu`.`tenant_id` = #{query.tenantId}</if>
<if test= "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>
<if test= "query.selectSpecialFlag != null and query.selectSpecialFlag == 0" >
and (`goods_spu`.`special_sku_id` is null or `goods_spu`.`special_sku_id` = '0')
</if> -->
<if test= "query.searchValue != null and query.searchValue != ''" > and (`goods_spu`.`name` like concat('%',#{query.searchValue},'%') or `goods_spu`.`goods_spu` like concat('%',#{query.searchValue},'%')) </if>
<if test= "query.selectIds!=null and query.selectIds.size > 0" >
and `goods_spu`.`id` in
<foreach collection= "query.selectIds" item = "selectId" index= "i" open= "(" close= ")" separator= "," >
#{selectId}
</foreach>
</if>
<if test= "query.selectCateIds!=null and query.selectCateIds.size > 0" >
and (`goods_spu`.`category_first` in
<foreach collection= "query.selectCateIds" item= "selectId" index= "i" open= "(" close= ")" separator= "," >
#{selectId}
</foreach>
or `goods_spu`.`category_second` in
<foreach collection= "query.selectCateIds" item= "selectId" index= "i" open= "(" close= ")" separator= "," >
#{selectId}
</foreach>
or `goods_spu`.`category_third` in
<foreach collection= "query.selectCateIds" item= "selectId" index= "i" open= "(" close= ")" separator= "," >
#{selectId}
</foreach> )
</if>
</sql>
<select id= "selectSpuList" parameterType= "com.qs.serve.modules.goods.entity.GoodsSpu" resultMap= "goodsSpuMap" >
SELECT
`cate1`.`name` AS `cate_first_label`,
`cate2`.`name` AS `cate_second_label`,
`cate3`.`name` AS `cate_third_label`,
<include refid= "goodsSpuSql" />
FROM `goods_spu` `goods_spu`
LEFT JOIN `goods_category` `cate1` ON `cate1`.`id` = `goods_spu`.`category_first`
LEFT JOIN `goods_category` `cate2` ON `cate2`.`id` = `goods_spu`.`category_second`
LEFT JOIN `goods_category` `cate3` ON `cate3`.`id` = `goods_spu`.`category_third`
<where >
<include refid= "spuWherePart" > </include>
</where>
</select>
<select id= "selectSkuJoinSpuList" resultType= "com.qs.serve.modules.goods.entity.GoodsSpu" >
SELECT
`cate1`.`name` AS `cate_first_label`,
`cate2`.`name` AS `cate_second_label`,
`cate3`.`name` AS `cate_third_label`,
<include refid= "goodsSpuSql" /> ,
<include refid= "goodsSkuSql" />
FROM (
(
SELECT sku.*,ROW_NUMBER() OVER(PARTITION BY sku.spu_id ORDER BY sku.belong_sort) AS rn
FROM goods_sku sku
left join goods_spu spu on sku.spu_id = spu.id
WHERE sku.order_flag = 1 AND sku.special_flag = 0
<if test= "query.keyword != null and query.keyword != '' " >
and (
`sku`.`sku_code` like concat('%', #{query.keyword},'%') or
`sku`.`sku_name` like concat('%', #{query.keyword},'%') or
`spu`.`name` like concat('%', #{query.keyword},'%') or
`spu`.`spu_code` like concat('%', #{query.keyword},'%')
)
</if>
)
UNION ALL
SELECT sku.*,1 as rn FROM goods_sku sku
left join goods_spu spu on sku.spu_id = spu.id
WHERE sku.special_flag = 1
<if test= "query.keyword != null and query.keyword != '' " >
and (
`sku`.`sku_code` like concat('%', #{query.keyword},'%') or
`sku`.`sku_name` like concat('%', #{query.keyword},'%') or
`spu`.`name` like concat('%', #{query.keyword},'%') or
`spu`.`spu_code` like concat('%', #{query.keyword},'%')
)
</if>
) AS goods_sku
left join goods_spu on goods_sku.spu_id = goods_spu.id
LEFT JOIN `goods_category` `cate1` ON `cate1`.`id` = `goods_spu`.`category_first`
LEFT JOIN `goods_category` `cate2` ON `cate2`.`id` = `goods_spu`.`category_second`
LEFT JOIN `goods_category` `cate3` ON `cate3`.`id` = `goods_spu`.`category_third`
<where >
and rn = 1
<include refid= "spuWherePart" > </include>
<include refid= "skuWherePart" > </include>
</where>
limit #{pageInfo.startIndex},#{pageInfo.size}
</select>
<select id= "selectCountSkuJoinSpuList" resultType= "java.lang.Long" >
SELECT
count(1)
FROM (
(
SELECT sku.*,ROW_NUMBER() OVER(PARTITION BY sku.spu_id ORDER BY sku.belong_sort) AS rn
FROM goods_sku sku
left join goods_spu spu on sku.spu_id = spu.id
WHERE sku.order_flag = 1 AND sku.special_flag = 0
<if test= "query.keyword != null and query.keyword != '' " >
and (
`sku`.`sku_code` like concat('%', #{query.keyword},'%') or
`sku`.`sku_name` like concat('%', #{query.keyword},'%') or
`spu`.`name` like concat('%', #{query.keyword},'%') or
`spu`.`spu_code` like concat('%', #{query.keyword},'%')
)
</if>
)
UNION ALL
SELECT sku.*,1 as rn FROM goods_sku sku
left join goods_spu spu on sku.spu_id = spu.id
WHERE sku.special_flag = 1
<if test= "query.keyword != null and query.keyword != '' " >
and (
`sku`.`sku_code` like concat('%', #{query.keyword},'%') or
`sku`.`sku_name` like concat('%', #{query.keyword},'%') or
`spu`.`name` like concat('%', #{query.keyword},'%') or
`spu`.`spu_code` like concat('%', #{query.keyword},'%')
)
</if>
) AS goods_sku
left join goods_spu on goods_sku.spu_id = goods_spu.id
<where >
and rn = 1
<include refid= "spuWherePart" > </include>
<include refid= "skuWherePart" > </include>
</where>
</select>
<select id= "getSpuSkuNumInfo" resultType= "com.qs.serve.modules.goods.entity.dto.GoodSpuSkuNum" >
select
tmp1.count_order,
tmp2.count_spec,
tmp3.count_sku
from goods_spu spu
left join ( select count(0) as count_order,sku.spu_id from goods_sku sku where sku.spu_id = #{spuId} and sku.del_flag=0 and sku.order_flag = 1 ) tmp1
on tmp1.spu_id = spu.id
left join ( select count(0) as count_spec,sku.spu_id from goods_sku sku where sku.spu_id = #{spuId} and sku.del_flag=0 and sku.special_flag = 1 ) tmp2
on tmp2.spu_id = spu.id
left join ( select count(0) as count_sku,sku.spu_id from goods_sku sku where sku.spu_id = #{spuId} and sku.del_flag=0 and sku.order_flag = 0 and sku.special_flag = 0) tmp3
on tmp3.spu_id = spu.id
where spu.id = #{spuId}
</select>
<select id= "selectLastChangeSpuId" resultType= "java.lang.Long" >
select goods_sku.spu_id from goods_sku where DATE(goods_sku.update_time) > CURDATE() - INTERVAL 7 DAY
UNION
select goods_spu.id from goods_spu where DATE(goods_spu.update_time) > CURDATE() - INTERVAL 7 DAY
</select>
</mapper>