<?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.gxwebsoft.shop.mapper.GoodsMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,b.image as merchantAvatar
        FROM shop_goods a
        LEFT JOIN shop_merchant b ON a.merchant_id = b.merchant_id
        <where>
            <if test="param.goodsId != null">
                AND a.goods_id = #{param.goodsId}
            </if>
            <if test="param.type != null">
                AND a.type = #{param.type}
            </if>
            <if test="param.code != null">
                AND a.code LIKE CONCAT('%', #{param.code}, '%')
            </if>
            <if test="param.goodsName != null">
                AND a.goods_name LIKE CONCAT('%', #{param.goodsName}, '%')
            </if>
            <if test="param.image != null">
                AND a.image LIKE CONCAT('%', #{param.image}, '%')
            </if>
            <if test="param.content != null">
                AND a.content LIKE CONCAT('%', #{param.content}, '%')
            </if>
            <if test="param.categoryId != null">
                AND a.category_id = #{param.categoryId}
            </if>
            <if test="param.categoryParent != null">
                AND a.category_parent LIKE CONCAT('%', #{param.categoryParent}, '%')
            </if>
            <if test="param.categoryChildren != null">
                AND a.category_children LIKE CONCAT('%', #{param.categoryChildren}, '%')
            </if>
            <if test="param.specs != null">
                AND a.specs = #{param.specs}
            </if>
            <if test="param.position != null">
                AND a.position LIKE CONCAT('%', #{param.position}, '%')
            </if>
            <if test="param.price != null">
                AND a.price = #{param.price}
            </if>
            <if test="param.salePrice != null">
                AND a.sale_price = #{param.salePrice}
            </if>
            <if test="param.deductStockType != null">
                AND a.deduct_stock_type = #{param.deductStockType}
            </if>
            <if test="param.files != null">
                AND a.files LIKE CONCAT('%', #{param.files}, '%')
            </if>
            <if test="param.sales != null">
                AND a.sales = #{param.sales}
            </if>
            <if test="param.stock != null">
                AND a.stock = #{param.stock}
            </if>
            <if test="param.goodsWeight != null">
                AND a.goods_weight = #{param.goodsWeight}
            </if>
            <if test="param.recommend != null">
                AND a.recommend = #{param.recommend}
            </if>
            <if test="param.merchantId != null">
                AND a.merchant_id = #{param.merchantId}
            </if>
            <if test="param.parentId != null">
                AND a.category_id IN (SELECT category_id FROM shop_goods_category WHERE parent_id=#{param.parentId})
            </if>
            <!--            <if test="param.merchantIds != null">-->
            <!--              AND a.category_id IN (SELECT category_id FROM shop_goods_category WHERE parent_id=#{param.parentId})-->
            <!--            </if>-->
            <if test="param.shopIdsByIndustry != null">
                AND a.merchant_id IN
                <foreach collection="param.shopIdsByIndustry" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="param.merchantName != null">
                AND a.merchant_name LIKE CONCAT('%', #{param.merchantName}, '%')
            </if>
            <if test="param.isShow != null">
                AND a.is_show = #{param.isShow}
            </if>
            <if test="param.status != null">
                AND a.status = #{param.status}
            </if>
            <if test="param.comments != null">
                AND a.comments LIKE CONCAT('%', #{param.comments}, '%')
            </if>
            <if test="param.sortNumber != null">
                AND a.sort_number = #{param.sortNumber}
            </if>
            <if test="param.userId != null">
                AND a.user_id = #{param.userId}
            </if>
            <if test="param.categoryName != null">
                AND a.category_name LIKE CONCAT('%', #{param.categoryName}, '%')
            </if>
            <if test="param.deleted != null">
                AND a.deleted = #{param.deleted}
            </if>
            <if test="param.deleted == null">
                AND a.deleted = 0
            </if>
            <if test="param.createTimeStart != null">
                AND a.create_time &gt;= #{param.createTimeStart}
            </if>
            <if test="param.createTimeEnd != null">
                AND a.create_time &lt;= #{param.createTimeEnd}
            </if>
            <if test="param.keywords != null">
                AND (a.comments LIKE CONCAT('%', #{param.keywords}, '%')
                OR a.goods_name LIKE CONCAT('%', #{param.keywords}, '%')
                OR a.parent_name LIKE CONCAT('%', #{param.keywords}, '%')
                OR a.category_name LIKE CONCAT('%', #{param.keywords}, '%')
                OR a.goods_id = #{param.keywords}
                OR a.code = #{param.keywords}
                )
            </if>
        </where>
        <if test="param.orderBy != null">
            ORDER BY ${param.orderBy}
        </if>
        <if test="param.orderBy == null">
            ORDER BY a.sort_number ASC, a.create_time DESC
        </if>
    </sql>

    <!-- 分页查询 -->
    <select id="selectPageRel" resultType="com.gxwebsoft.shop.entity.Goods">
        <include refid="selectSql"></include>
    </select>

    <!-- 查询全部 -->
    <select id="selectListRel" resultType="com.gxwebsoft.shop.entity.Goods">
        <include refid="selectSql"></include>
    </select>

</mapper>
