<?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.ruoyi.project.business.mapper.TbSalesDiscountMapper">

    <resultMap type="TbSalesDiscount" id="TbSalesDiscountResult">
        <result property="id" column="id"/>
        <result property="roleId" column="role_id"/>
        <result property="minDiscount" column="min_discount"/>
        <result property="maxDiscount" column="max_discount"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
    </resultMap>
    <resultMap type="com.ruoyi.project.business.domain.vo.salesDiscount.SalesDiscountRes" id="TbSalesDiscountResultRes">
        <result property="id" column="id"/>
        <result property="roleId" column="role_id"/>
        <result property="minDiscount" column="min_discount"/>
        <result property="maxDiscount" column="max_discount"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="remark" column="remark"/>
        <result property="roleName" column="role_name"/>
        <result property="createUserNick" column="nick_name"/>
    </resultMap>

    <sql id="selectTbSalesDiscountVo">
        select t1.id,
               t1.role_id,
               t1.min_discount,
               t1.max_discount,
               t1.del_flag,
               t1.create_time,
               t1.create_by,
               t1.update_time,
               t1.update_by,
               t1.remark,
               t2.role_name,
               t3.nick_name
        from tb_sales_discount t1
        left join sys_role t2 on t1.role_id = t2.role_id
        left join sys_user t3 on t1.create_by = t3.user_id
    </sql>

    <select id="selectTbSalesDiscountList" parameterType="com.ruoyi.project.business.domain.vo.salesDiscount.SalesDiscountReq" resultMap="TbSalesDiscountResultRes">
        <include refid="selectTbSalesDiscountVo"/>
        <where>
            and t1.del_flag = 0
            <if test="roleId != null ">and role_id = #{roleId}</if>
            <if test="roleIds != null and roleIds.size() > 0">
                and t1.role_id in
                <foreach collection="roleIds" item="roleId" open="(" close=")" separator=",">
                    #{roleId}
                </foreach>
            </if>
            <if test="minDiscount != null ">and min_discount = #{minDiscount}</if>
            <if test="maxDiscount != null ">and max_discount = #{maxDiscount}</if>
            <if test="createUserNick != null and createUserNick != ''">and t3.nick_name like concat('%',#{createUserNick},'%')</if>
            <if test="beginTime != null and endTime != null">and t1.create_time between #{beginTime} and #{endTime}</if>
            <if test="remark != null and remark != ''">and t1.remark like concat('%', #{remark}, '%')</if>
        </where>
        order by t1.id desc
    </select>

</mapper>