<?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.fzm.mall.server.admin.goods_center.mapper.SpuMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.fzm.mall.server.admin.goods_center.model.Spu">
        <id column="id" property="id"/>
        <result column="goods_id" property="goodsId"/>
        <result column="merchant_id" property="merchantId"/>
        <result column="type" property="type"/>
        <result column="name" property="name"/>
        <result column="desc" property="desc"/>
        <result column="prop" property="prop"/>
        <result column="detail" property="detail"/>
        <result column="thumb" property="thumb"/>
        <result column="video" property="video"/>
        <result column="default_price" property="defaultPrice"/>
        <result column="video_poster" property="videoPoster"/>
        <result column="img_list" property="imgList"/>
        <result column="status" property="status"/>
        <result column="bp_num" property="bpNum"/>
        <result column="return_reason" property="returnReason"/>
        <result column="note" property="note"/>
        <result column="traceability_number" property="traceabilityNumber"/>
        <result column="commodity_pass" property="commodityPass"/>
        <result column="delay_delivery" property="delayDelivery"/>
        <result column="nft_file" property="nftFile"/>
        <result column="hash" property="hash"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <select id="page" resultType="com.fzm.mall.server.admin.goods_center.entity.vo.GoodsSpuVO">
        SELECT
        G.goods_id,G.name,G.thumb,G.status,G.max_price,G.default_price,
        M.enterprise_name,M.apply_name AS linkName,M.apply_phone AS linkPhone,G.check_fail_reason,G.type,
        G.create_time,G.commodity_pass
        FROM
        goods_spu G
        LEFT JOIN
        merchant M
        ON
        G.merchant_id = M.merchant_id
        <where>
            G.status != 0
            <if test='merchantId!="e3ac4b4a4e65460dbd9f1ec6cbd7a900"'>
                AND
                G.merchant_id = #{merchantId}
            </if>
            <if test="goodsId!=null and goodsId!=''">
                AND
                G.goods_id = #{goodsId}
            </if>
            <if test="goodsName!=null and goodsName!=''">
                <bind name="goodsName" value="'%' + goodsName + '%'"/>
                AND
                G.name LIKE #{goodsName}
            </if>
            <if test="status!=null">
                AND
                G.status = #{status}
            </if>
            <if test="addTimeHead!=null">
                AND
                G.create_time &gt;= #{addTimeHead}
            </if>
            <if test="addTimeTail!=null">
                AND
                G.create_time &lt;= #{addTimeTail}
            </if>
            <if test="goodsType!=null">
                AND
                G.type = #{goodsType}
            </if>
        </where>
        ORDER BY
        G.create_time
        DESC
    </select>

    <select id="queryByCategory" resultType="com.fzm.mall.server.admin.goods_center.entity.vo.GoodsListVO">
        SELECT
        A.goods_id,A.name,A.description
        FROM
        goods_spu A
        LEFT JOIN
        goods_category_spu B
        ON
        A.goods_id = B.goods_id
        <where>
            A.merchant_id = #{merchantId}
            <if test="categoryIds!=null and categoryIds.size()>0">
                AND
                B.category_id
                IN
                <foreach item="item" index="index" collection="categoryIds"
                         open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="goods!=null and goods!=''">
                AND
                (
                A.name = #{goods}
                OR
                A.goods_id = #{goods}

                )
            </if>
        </where>
        ORDER BY
        A.create_time
        DESC
    </select>

    <select id="queryByNameOrId" resultType="com.fzm.mall.server.admin.goods_center.entity.vo.GoodsListVO">
        select p.goods_id, p.name, p.description, k.sku_id
        from goods_spu p
        left join goods_sku k on p.goods_id = k.goods_id
        <where>
            p.merchant_id = #{merchantId} and p.status = #{spuStatusOnSale}
            <if test="goods != null and goods != ''">
                and (
                p.name like CONCAT('%', #{goods},'%')
                or
                p.goods_id like CONCAT('%', #{goods},'%')
                )
            </if>
        </where>
    </select>
</mapper>
