<?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.ss.vipLevel.mapper.VipLevelMapper">

    <resultMap type="VipLevelVO" id="VipLevelResult" autoMapping="true"/>

    <sql id="selectVipLevelVo">
        select
            svl.id,
            svl.mch_id,
            svl.name,
            svl.discount,
            svl.create_time,
            svl.description,
            if (mch.is_real, mch.real_name, mch.user_name) as mch_name
        from ss_vip_level svl
            left join sm_user mch on mch.user_id = svl.mch_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and svl.id = #{query.id}</if>
        <if test="query.mchId != null "> and svl.mch_id = #{query.mchId}</if>
        <if test="query.name != null  and query.name != ''"> and svl.name like concat('%', #{query.name}, '%')</if>
        <if test="query.description != null  and query.description != ''"> and svl.description like concat('%', #{query.description}, '%')</if>
        <if test="query.mchName != null  and query.mchName != ''">
            and if (mch.is_real, mch.real_name, mch.user_name) like concat('%', #{query.mchName}, '%')
        </if>
        <if test="query.ids != null and query.ids.size() > 0">
            and id in
            <foreach collection="query.ids" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectVipLevelList" parameterType="VipLevelQuery" resultMap="VipLevelResult">
        <include refid="selectVipLevelVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectVipLevelById" parameterType="Long" resultMap="VipLevelResult">
        <include refid="selectVipLevelVo"/>
        where id = #{id}
    </select>

    <insert id="insertVipLevel" parameterType="VipLevel" useGeneratedKeys="true" keyProperty="id">
        insert into ss_vip_level
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mchId != null">mch_id,</if>
            <if test="name != null and name != ''">`name`,</if>
            <if test="discount != null">discount,</if>
            <if test="createTime != null">create_time,</if>
            <if test="description != null">`description`,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mchId != null">#{mchId},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="discount != null">#{discount},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="description != null">#{description},</if>
         </trim>
    </insert>

    <update id="updateVipLevel" parameterType="VipLevel">
        update ss_vip_level
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.mchId != null">mch_id = #{data.mchId},</if>
        <if test="data.name != null and data.name != ''">`name` = #{data.name},</if>
        <if test="data.discount != null">discount = #{data.discount},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.description != null">`description` = #{data.description},</if>
    </sql>

    <delete id="deleteVipLevelById" parameterType="Long">
        delete from ss_vip_level where id = #{id}
    </delete>

    <delete id="deleteVipLevelByIds" parameterType="String">
        delete from ss_vip_level where id in
        <foreach item="id" collection="ids" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
