<?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.bst.suit.mapper.SuitMapper">

    <resultMap type="SuitVO" id="SuitResult" autoMapping="true">
        <result property="startRule" column="start_rule" typeHandler="com.ruoyi.bst.suit.mapper.typehanlder.SuitStartFeeRuleJsonTypeHandler"/>
        <result property="intervalRule" column="interval_rule" typeHandler="com.ruoyi.bst.suit.mapper.typehanlder.SuitIntervalFeeRuleJsonListTypeHandler"/>
    </resultMap>

    <sql id="selectSuitVo">
        select
            bs.id,
            bs.user_id,
            bs.name,
            bs.status,
            bs.create_time,
            bs.free_ride_time,
            bs.rental_unit,
            bs.riding_rule,
            bs.start_rule,
            bs.interval_rule,
            bs.instructions,
            bs.deleted,
            bs.order_num,
            bs.deposit_amount,
            bs.type,
            bs.seconds,
            su.nick_name as user_name
        <include refid="searchTables"/>
    </sql>

    <sql id="searchTables">
        from bst_suit bs
            left join sys_user su on su.user_id = bs.user_id
    </sql>

    <sql id="searchCondition">
        <if test="query.id != null "> and bs.id = #{query.id}</if>
        <if test="query.userId != null "> and bs.user_id = #{query.userId}</if>
        <if test="query.name != null  and query.name != ''"> and bs.name like concat('%', #{query.name}, '%')</if>
        <if test="query.status != null  and query.status != ''"> and bs.status = #{query.status}</if>
        <if test="query.rentalUnit != null  and query.rentalUnit != ''"> and bs.rental_unit = #{query.rentalUnit}</if>
        <if test="query.ridingRule != null  and query.ridingRule != ''"> and bs.riding_rule = #{query.ridingRule}</if>
        <if test="query.deleted != null "> and bs.deleted = #{query.deleted}</if>
        <if test="query.deleted == null "> and bs.deleted = false</if>
        <if test="query.userName != null and query.userName != ''"> and su.nick_name like concat('%', #{query.userName}, '%')</if>
        <if test="query.type != null  and query.type != ''"> and bs.type = #{query.type}</if>
        <if test="query.ids != null and query.ids.size() > 0"> 
            and bs.id in
            <foreach item="item" collection="query.ids" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="query.modelId != null">
            and bs.id in (
                select bms.suit_id from bst_model_suit bms where bms.model_id = #{query.modelId}
            )
        </if>
        ${@com.ruoyi.framework.util.DataScopeUtil@dataScope(
            null,
            "bs.user_id",
            null,
            null,
            query.scope
        )}
        ${query.params.dataScope}
    </sql>

    <select id="selectSuitList" parameterType="SuitQuery" resultMap="SuitResult">
        <include refid="selectSuitVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectSuitById" parameterType="Long" resultMap="SuitResult">
        <include refid="selectSuitVo"/>
        where bs.id = #{id}
    </select>

    <insert id="insertSuit" parameterType="Suit" useGeneratedKeys="true" keyProperty="id">
        insert into bst_suit
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="name != null and name != ''">name,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="createTime != null">create_time,</if>
            <if test="freeRideTime != null">free_ride_time,</if>
            <if test="rentalUnit != null and rentalUnit != ''">rental_unit,</if>
            <if test="ridingRule != null and ridingRule != ''">riding_rule,</if>
            <if test="startRule != null">start_rule,</if>
            <if test="intervalRule != null">interval_rule,</if>
            <if test="instructions != null">instructions,</if>
            <if test="deleted != null">deleted,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="depositAmount != null">deposit_amount,</if>
            <if test="seconds != null">seconds,</if>
            <if test="type != null and type != ''">type,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="name != null and name != ''">#{name},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="freeRideTime != null">#{freeRideTime},</if>
            <if test="rentalUnit != null and rentalUnit != ''">#{rentalUnit},</if>
            <if test="ridingRule != null and ridingRule != ''">#{ridingRule},</if>
            <if test="startRule != null">#{startRule, typeHandler=com.ruoyi.bst.suit.mapper.typehanlder.SuitStartFeeRuleJsonTypeHandler},</if>
            <if test="intervalRule != null">#{intervalRule, typeHandler=com.ruoyi.bst.suit.mapper.typehanlder.SuitIntervalFeeRuleJsonListTypeHandler},</if>
            <if test="instructions != null">#{instructions},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="depositAmount != null">#{depositAmount},</if>
            <if test="seconds != null">#{seconds},</if>
            <if test="type != null and type != ''">#{type},</if>
         </trim>
    </insert>

    <update id="updateSuit" parameterType="Suit">
        update bst_suit
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where id = #{data.id}
    </update>

    <sql id="updateColumns">
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.name != null and data.name != ''">name = #{data.name},</if>
        <if test="data.status != null and data.status != ''">status = #{data.status},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.freeRideTime != null">free_ride_time = #{data.freeRideTime},</if>
        <if test="data.rentalUnit != null and data.rentalUnit != ''">rental_unit = #{data.rentalUnit},</if>
        <if test="data.ridingRule != null and data.ridingRule != ''">riding_rule = #{data.ridingRule},</if>
        <if test="data.startRule != null">start_rule = #{data.startRule, typeHandler=com.ruoyi.bst.suit.mapper.typehanlder.SuitStartFeeRuleJsonTypeHandler},</if>
        <if test="data.intervalRule != null">interval_rule = #{data.intervalRule, typeHandler=com.ruoyi.bst.suit.mapper.typehanlder.SuitIntervalFeeRuleJsonListTypeHandler},</if>
        <if test="data.instructions != null">instructions = #{data.instructions},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
        <if test="data.orderNum != null">order_num = #{data.orderNum},</if>
        <if test="data.depositAmount != null">deposit_amount = #{data.depositAmount},</if>
        <if test="data.type != null and data.type != ''">type = #{data.type},</if>
        <if test="data.seconds != null">seconds = #{data.seconds},</if>
    </sql>

    <delete id="deleteSuitById" parameterType="Long">
        delete from bst_suit where id = #{id}
    </delete>

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

    <!-- selectIdList --> 

    <select id="selectIdList" resultType="Long"> 
        select bs.id
        <include refid="searchTables"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>
    
</mapper>
