<?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.price,
            svl.time,
            svl.create_time,
            svl.update_time,
            svl.description,
            svl.limit_type,
            svl.limit_count,
            svl.status,
            svl.name,
            svl.original_price,
            svl.limit_total,
            svl.mch_id,
            svl.discount,
            svl.store_id,
            svl.deleted,
            ss.name as store_name,
            if (mch.is_real, mch.real_name, mch.user_name) as mch_name
        from ss_vip_level svl
            left join sm_store ss on svl.store_id = ss.store_id
            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.description != null  and query.description != ''"> and svl.`description` like concat('%',#{query.description},'%')</if>
        <if test="query.limitType != null  and query.limitType != ''"> and svl.limit_type = #{query.limitType}</if>
        <if test="query.limitCount != null "> and svl.limit_count = #{query.limitCount}</if>
        <if test="query.status != null and query.status != '' "> and svl.status = #{query.status}</if>
        <if test="query.name != null  and query.name != ''"> and svl.name like concat('%', #{query.name}, '%')</if>
        <if test="query.mchId != null "> and svl.mch_id = #{query.mchId}</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.storeName != null  and query.storeName != ''"> and ss.name like concat('%', #{query.storeName}, '%')</if>
        <if test="query.discount != null "> and svl.discount = #{query.discount}</if>
        <if test="query.storeId != null "> and svl.store_id = #{query.storeId}</if>
        <if test="query.deleted != null "> and svl.deleted = #{query.deleted}</if>
        <if test="query.deleted == null "> and svl.deleted = false</if>
        ${query.params.dataScope}
    </sql>

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

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

    <insert id="insertVipLevelSku" parameterType="VipLevel" useGeneratedKeys="true" keyProperty="id">
        insert into ss_vip_level
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="price != null">price,</if>
            <if test="time != null">`time`,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="description != null">`description`,</if>
            <if test="limitType != null and limitType != ''">limit_type,</if>
            <if test="limitCount != null">limit_count,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="name != null and name != ''">`name`,</if>
            <if test="originalPrice != null">original_price,</if>
            <if test="limitTotal != null">limit_total,</if>
            <if test="mchId != null">mch_id,</if>
            <if test="discount != null">discount,</if>
            <if test="storeId != null and storeId != ''">store_id,</if>
            <if test="deleted != null">deleted,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="price != null">#{price},</if>
            <if test="time != null">#{time},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="description != null">#{description},</if>
            <if test="limitType != null and limitType != ''">#{limitType},</if>
            <if test="limitCount != null">#{limitCount},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="originalPrice != null">#{originalPrice},</if>
            <if test="limitTotal != null">#{limitTotal},</if>
            <if test="mchId != null">#{mchId},</if>
            <if test="discount != null">#{discount},</if>
            <if test="storeId != null and storeId != ''">#{storeId},</if>
            <if test="deleted != null">#{deleted},</if>
         </trim>
    </insert>

    <update id="updateVipLevelSku" 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.price != null">price = #{data.price},</if>
        <if test="data.time != null">`time` = #{data.time},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.updateTime != null">update_time = #{data.updateTime},</if>
        <if test="data.description != null">`description` = #{data.description},</if>
        <if test="data.limitType != null and data.limitType != ''">limit_type = #{data.limitType},</if>
        <if test="data.limitCount != null">limit_count = #{data.limitCount},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.name != null and data.name != ''">`name` = #{data.name},</if>
        <if test="data.originalPrice != null">original_price = #{data.originalPrice},</if>
        <if test="data.limitTotal != null">limit_total = #{data.limitTotal},</if>
        <if test="data.mchId != null">mch_id = #{data.mchId},</if>
        <if test="data.discount != null">discount = #{data.discount},</if>
        <if test="data.storeId != null and data.storeId != ''">store_id = #{data.storeId},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
    </sql>

    <update id="logicDel" parameterType="String">
        update ss_vip_level
        set deleted = true
        where id in
            <foreach item="id" collection="ids" open="(" separator="," close=")">
                #{id}
            </foreach>
            and deleted = false
    </update>
</mapper>
