<?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.ShopGiftMapper">

    <!-- 关联查询sql -->
    <sql id="selectSql">
        SELECT a.*,b.name as goodsName, b.price as faceValue, b.image as goodsImage,c.nickname, u.nickname as operatorUserName
        FROM shop_gift a
        LEFT JOIN shop_goods b ON a.goods_id = b.goods_id
        LEFT JOIN gxwebsoft_core.sys_user c ON a.user_id = c.user_id
        LEFT JOIN gxwebsoft_core.sys_user u ON a.operator_user_id = u.user_id
        <where>
            <if test="param.id != null">
                AND a.id = #{param.id}
            </if>
            <if test="param.name != null">
                AND a.name LIKE CONCAT('%', #{param.name}, '%')
            </if>
            <if test="param.code != null">
                AND a.code LIKE CONCAT('%', #{param.code}, '%')
            </if>
            <if test="param.goodsId != null">
                AND a.goods_id = #{param.goodsId}
            </if>
            <if test="param.takeTime != null">
                AND a.take_time LIKE CONCAT('%', #{param.takeTime}, '%')
            </if>
            <if test="param.operatorUserId != null">
                AND a.operator_user_id = #{param.operatorUserId}
            </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.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.code = #{param.keywords}
                    OR a.name LIKE CONCAT('%', #{param.keywords}, '%')
                )
              </if>
        </where>
    </sql>

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

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

</mapper>
