<?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.feeRule.mapper.FeeRuleMapper">

    <resultMap type="FeeRuleVO" id="EFeeRuleResult" autoMapping="true" />

    <sql id="selectEFeeRuleVo">
        select
            r.rule_id,
            r.`mode`,
            r.hours,
            r.price,
            r.`explain`,
            r.merchant_id,
            r.create_by,
            r.create_time,
            r.min_hours,
            r.deleted,
            r.deposit,
            r.refund_duration,
            m.user_name merchantName,
            m.real_name as realName
        from ss_fee_rule r
        left join ss_user m on m.user_id = r.merchant_id
    </sql>

    <sql id="searchCondition">
        <if test="query.mode != null  and query.mode != ''"> and r.mode = #{query.mode}</if>
        <if test="query.hours != null "> and r.hours = #{query.hours}</if>
        <if test="query.price != null "> and r.price = #{query.price}</if>
        <if test="query.explain != null  and query.explain != ''"> and r.`explain` like concat('%', #{query.explain}, '%')</if>
        <if test="query.merchantId != null "> and r.merchant_id = #{query.merchantId}</if>
        <if test="query.deleted == null">and r.deleted = false</if>
        <if test="query.deleted != null">and r.deleted = #{deleted}</if>
        <if test="query.ruleIds != null and query.ruleIds.size() > 0">
            and r.rule_id in
            <foreach collection="query.ruleIds" open="(" close=")" separator="," item="item">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectEFeeRuleList" parameterType="FeeRuleQuery" resultMap="EFeeRuleResult">
        <include refid="selectEFeeRuleVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectEFeeRuleByRuleId" parameterType="Long" resultMap="EFeeRuleResult">
        <include refid="selectEFeeRuleVo"/>
        where rule_id = #{ruleId} and deleted = false
    </select>

<!--    <select id="selectEFeeRuleListByRoomId" parameterType="Long" resultMap="EFeeRuleResult">-->
<!--        <include refid="selectEFeeRuleVo"/>-->
<!--        <where>-->
<!--            <include refid="searchCondition"/>-->
<!--        </where>-->
<!--    </select>-->

    <select id="verifyHourlyUnique" resultType="java.lang.Integer">
        select count(1) from ss_fee_rule where mode = '1' and r.deleted = false
    </select>

    <insert id="insertEFeeRule" parameterType="FeeRule" useGeneratedKeys="true" keyProperty="ruleId">
        insert into ss_fee_rule
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mode != null">mode,</if>
            <if test="hours != null">hours,</if>
            <if test="price != null">price,</if>
            <if test="explain != null">`explain`,</if>
            <if test="merchantId != null">merchant_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="minHours != null">min_hours,</if>
            <if test="deposit != null">deposit,</if>
            <if test="refundDuration != null">refund_duration,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mode != null">#{mode},</if>
            <if test="hours != null">#{hours},</if>
            <if test="price != null">#{price},</if>
            <if test="explain != null">#{explain},</if>
            <if test="merchantId != null">#{merchantId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="minHours != null">#{minHours},</if>
            <if test="deposit != null">#{deposit},</if>
            <if test="refundDuration != null">#{refundDuration},</if>
         </trim>
    </insert>

    <update id="updateEFeeRule" parameterType="FeeRule">
        update ss_fee_rule
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where rule_id = #{data.ruleId}
    </update>

    <sql id="updateColumns">
        <if test="data.mode != null">mode = #{data.mode},</if>
        <if test="data.hours != null">hours = #{data.hours},</if>
        <if test="data.price != null">price = #{data.price},</if>
        <if test="data.explain != null">`explain` = #{data.explain},</if>
        <if test="data.merchantId != null">merchant_id = #{data.merchantId},</if>
        <if test="data.createBy != null">create_by = #{data.createBy},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.minHours != null">min_hours = #{data.minHours},</if>
        <if test="data.deposit != null">deposit = #{data.deposit},</if>
        <if test="data.refundDuration != null">refund_duration = #{data.refundDuration},</if>
    </sql>

    <update id="deleteEFeeRuleByRuleId" parameterType="Long">
        update ss_fee_rule
        set deleted = 1
        where rule_id = #{ruleId}
    </update>

    <update id="deleteEFeeRuleByRuleIds" parameterType="List">
        update ss_fee_rule
        set deleted = 1
        where rule_id in
        <foreach item="ruleId" collection="array" open="(" separator="," close=")">
            #{ruleId}
        </foreach>
    </update>
</mapper>
