<?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.kmall.api.dao.ApiGoodsMapper">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.api.entity.GoodsVo" id="goodsMap">
        <result property="id" column="id"/>
        <result property="category_id" column="category_id"/>
        <result property="goods_sn" column="goods_sn"/>
        <result property="name" column="name"/>
        <result property="brand_id" column="brand_id"/>
        <result property="goods_number" column="goods_number"/>
        <result property="keywords" column="keywords"/>
        <result property="goods_brief" column="goods_brief"/>
        <result property="goods_desc" column="goods_desc"/>
        <result property="is_on_sale" column="is_on_sale"/>
        <result property="add_time" column="add_time"/>
        <result property="sort_order" column="sort_order"/>
        <result property="is_delete" column="is_delete"/>
        <result property="attribute_category" column="attribute_category"/>
        <result property="counter_price" column="counter_price"/>
        <result property="extra_price" column="extra_price"/>
        <result property="is_new" column="is_new"/>
        <result property="goods_unit" column="goods_unit"/>
        <result property="primary_pic_url" column="primary_pic_url"/>
        <result property="list_pic_url" column="list_pic_url"/>
        <result property="retail_price" column="retail_price"/>
        <result property="market_price" column="market_price"/>
        <result property="sell_volume" column="sell_volume"/>
        <result property="primary_product_id" column="primary_product_id"/>
        <result property="unit_price" column="unit_price"/>
        <result property="promotion_desc" column="promotion_desc"/>
        <result property="promotion_tag" column="promotion_tag"/>
        <result property="app_exclusive_price" column="app_exclusive_price"/>
        <result property="is_app_exclusive" column="is_app_exclusive"/>
        <result property="is_limited" column="is_limited"/>
        <result property="is_hot" column="is_hot"/>
        <result property="product_id" column="product_id"/>
        <result property="sku" column="sku"/>
        <result property="goodsBizType" column="goods_biz_type"/>
        <result column="creater_sn" property="createrSn" jdbcType="VARCHAR" />
        <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />
        <result column="moder_sn" property="moderSn" jdbcType="VARCHAR" />
        <result column="mod_time" property="modTime" jdbcType="TIMESTAMP" />
        <result column="tstm" property="tstm" jdbcType="TIMESTAMP" />
        <result property="goodsRate" column="goods_rate"/>
        <result property="stockNum" column="stock_num"/>
    </resultMap>

    <sql id="Base_Column_List" >
        id, category_id, goods_sn, sku, goods_biz_type, name, brand_id, goods_number, keywords,
        goods_brief, is_on_sale, add_time, sort_order, is_delete, attribute_category, counter_price,
        extra_price, is_new, goods_unit, primary_pic_url, list_pic_url, retail_price, sell_volume,
        primary_product_id, unit_price, promotion_desc, promotion_tag, app_exclusive_price,
        is_app_exclusive, is_limited, is_hot, market_price, creater_sn, create_time, moder_sn,
        mod_time, tstm,goods_desc,goods_rate
    </sql>

    <select id="queryObject" resultMap="goodsMap">
		select *
        from mall_goods
		where id = #{value}
	</select>

    <select id="queryObjectByStoreId" resultMap="goodsMap">
        select
        a.id,
        a.category_id,
        a.goods_sn,
        a.name,
        a.brand_id,
        a.goods_number,
        a.keywords,
        a.goods_brief,
        a.is_on_sale,
        a.add_time,
        a.sort_order,
        a.is_delete,
        a.attribute_category,
        a.counter_price,
        a.extra_price,
        a.is_new,
        a.goods_unit,
        a.primary_pic_url,
        a.list_pic_url,
        psr1.retail_price,
        a.sell_volume,
        a.primary_product_id,
        a.unit_price,
        a.promotion_desc,
        a.promotion_tag,
        a.app_exclusive_price,
        a.is_app_exclusive,
        a.is_limited,
        a.is_hot,
        psr1.market_price,
        psr1.stock_num,
        a.goods_desc,a.goods_biz_type,a.goods_rate
        from mall_goods a
        LEFT JOIN mall_product_store_rela psr1 ON a.id = psr1.goods_id
        where a.id = #{id} and psr1.store_id = #{storeId}
    </select>

    <select id="queryList" resultMap="goodsMap">
        select
        <if test="fields != null and fields != ''">
            ${fields}
        </if>
        <if test="fields == null or fields == ''">
            a.*
        </if>
        ,psr1.stock_num
        from mall_goods a
        LEFT JOIN mall_product_store_rela psr1 ON a.id = psr1.goods_id
        LEFT JOIN mall_product b ON b.id = psr1.product_id
        where 1 = 1 and a.is_delete != 1 and psr1.stock_num > 0 and a.is_on_sale = 1
        <if test="is_new != null and is_new != ''">
            and a.is_new = #{is_new}
        </if>
        <if test="is_hot != null and is_hot != ''">
            and a.is_hot = #{is_hot}
        </if>
        <if test="brand_id != null and brand_id != ''">
            and a.brand_id = #{brand_id}
        </if>
        <if test="category_id != null and category_id != ''">
            and a.category_id = #{category_id}
        </if>
        <if test="keyword != null">
            and a.name like concat('%',#{keyword},'%')
        </if>
        <if test="is_delete != null">
            and a.is_delete = #{is_delete}
        </if>
        <if test="goodsBizType != null">
            and a.goods_biz_type = #{goodsBizType}
        </if>
        <if test="notGoodsBizType == true">
            and a.goods_biz_type in ('00','11')
        </if>
        <if test="categoryIds != null">
            and a.category_id in
            <foreach item="item" collection="categoryIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="category_parent_id != null and category_parent_id != null">
            and FIND_IN_SET(a.category_id, getCategoryChildLst(#{category_parent_id}))
        </if>
        <if test="goods_ids != null">
            and a.id in
            <foreach item="item" collection="goods_ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="store_id != null and store_id != ''">
            and psr1.store_id = #{store_id}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryHotGoodsList" resultMap="goodsMap">
        select
        <if test="fields != null and fields != ''">
            ${fields}
        </if>
        <if test="fields == null or fields == ''">
            a.*,b.id as product_id
        </if>
        from mall_goods a
        LEFT JOIN mall_product_store_rela psr1 ON a.id = psr1.goods_id
        LEFT JOIN mall_product b ON b.id = psr1.product_id
        where 1 = 1 and b.id is not null and psr1.stock_num > 0 and a.is_delete != 1 and psr1.retail_price is not null and a.is_on_sale = 1
        <if test="is_new != null and is_new != ''">
            and a.is_new = #{is_new}
        </if>
        <if test="is_hot != null and is_hot != ''">
            and a.is_hot = #{is_hot}
        </if>
        <if test="brand_id != null and brand_id != ''">
            and a.brand_id = #{brand_id}
        </if>
        <if test="category_id != null and category_id != ''">
            and a.category_id = #{category_id}
        </if>
        <if test="keyword != null">
            and a.keywords like concat('%',#{keyword},'%')
        </if>
        <if test="categoryIds != null">
            and a.category_id in
            <foreach item="item" collection="categoryIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="category_parent_id != null and category_parent_id != null">
            and FIND_IN_SET(a.category_id, getCategoryChildLst(#{category_parent_id}))
        </if>
        <if test="goods_ids != null">
            and a.id in
            <foreach item="item" collection="goods_ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="store_id != null and store_id != ''">
            and psr1.store_id = #{store_id}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryCatalogProductList" resultMap="goodsMap">
        select a.id, a.name, a.list_pic_url, psr1.market_price, psr1.retail_price, a.goods_brief, b.id AS product_id,psr1.stock_num
        <if test="is_group != null and is_group == true">
           ,gg.id as group_id
        </if>
        FROM
        mall_goods a
        LEFT JOIN mall_product_store_rela psr1 ON a.id = psr1.goods_id
        LEFT JOIN mall_product b ON b.id = psr1.product_id
        <if test="is_group != null and is_group == true">
            left join mall_goods_group gg on gg.goods_id = a.id
        </if>
        WHERE
        1 = 1 AND psr1.stock_num > 0 and b.id > 0 and a.is_delete != 1 and psr1.retail_price is not null and a.is_on_sale = 1
        <if test="is_new != null and is_new != ''">
            and a.is_new = #{is_new}
        </if>
        <if test="is_hot != null and is_hot != ''">
            and a.is_hot = #{is_hot}
        </if>
        <if test="brand_id != null and brand_id != ''">
            and a.brand_id = #{brand_id}
        </if>
        <if test="name != null and name != ''">
            and a.'name' like concat('%',#{keyword},'%')
        </if>
        <if test="category_id != null and category_id != ''">
            and a.category_id = #{category_id}
        </if>
        <if test="keyword != null">
            and a.keywords like concat('%',#{keyword},'%')
        </if>
        <if test="is_group != null and is_group == true">
            and gg.id is not null
        </if>
        <if test="categoryIds != null">
            and a.category_id in
            <foreach item="item" collection="categoryIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="category_parent_id != null and category_parent_id != null">
            and FIND_IN_SET(a.category_id, getCategoryChildLst(#{category_parent_id}))
        </if>
        <if test="goods_ids != null">
            and a.id in
            <foreach item="item" collection="goods_ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="store_id != null and store_id != ''">
            and psr1.store_id = #{store_id}
        </if>

        <if test="goodsBizType != null and goodsBizType != ''">
            and a.goods_biz_type = #{goodsBizType}
        </if>

        group by a.id, a.name, a.list_pic_url, psr1.market_price, psr1.retail_price, a.goods_brief, b.id
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by a.id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*)
        from mall_goods a
        left join mall_product_store_rela s on a.id = s.goods_id
        where 1 = 1 and a.is_on_sale = 1 AND s.stock_num > 0
        <if test="is_new != null and is_new != ''">
            and a.is_new = #{is_new}
        </if>
        <if test="is_hot != null and is_hot != ''">
            and a.is_hot = #{is_hot}
        </if>
        <if test="brand_id != null and brand_id != ''">
            and a.brand_id = #{brand_id}
        </if>
        <if test="category_id != null and category_id != ''">
            and a.category_id = #{category_id}
        </if>
        <if test="keyword != null">
            and a.name like concat('%',#{keyword},'%')
        </if>
        <if test="is_delete != null">
            and a.is_delete = #{is_delete}
        </if>
        <if test="categoryIds != null">
            and a.category_id in
            <foreach item="item" collection="categoryIds" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="category_parent_id != null and category_parent_id != null">
            and FIND_IN_SET(a.category_id, getCategoryChildLst(#{category_parent_id}))
        </if>
        <if test="goods_ids != null">
            and a.id in
            <foreach item="item" collection="goods_ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="store_id != null and store_id != ''">
            and s.store_id = #{store_id}
        </if>
        <if test="goodsBizType != null and goodsBizType != ''">
            and a.goods_biz_type = #{goodsBizType}
        </if>
    </select>
</mapper>