<?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.timeBill.mapper.TimeBillMapper">

    <resultMap type="TimeBillVO" id="TimeBillResult" autoMapping="true"/>

    <sql id="selectTimeBillVo">
        select
            stb.bill_id,
            stb.bill_no,
            stb.user_id,
            stb.device_id,
            stb.status,
            stb.amount,
            stb.device_start_time,
            stb.device_start_unit,
            stb.device_start_price,
            stb.device_over_time,
            stb.device_over_unit,
            stb.device_over_price,
            stb.start_time,
            stb.end_time,
            stb.mch_id,
            stb.arrival_amount,
            stb.service_amount,
            stb.pay_id,
            stb.create_time,
            stb.deleted,
            stb.device_mac,
            stb.device_no
        from ss_time_bill stb
    </sql>

    <sql id="searchCondition">
        <if test="query.billId != null "> and stb.bill_id = #{query.billId}</if>
        <if test="query.billNo != null  and query.billNo != ''"> and stb.bill_no = #{query.billNo}</if>
        <if test="query.userId != null "> and stb.user_id = #{query.userId}</if>
        <if test="query.deviceId != null "> and stb.device_id = #{query.deviceId}</if>
        <if test="query.status != null  and query.status != ''"> and stb.status = #{query.status}</if>
        <if test="query.mchId != null "> and stb.mch_id = #{query.mchId}</if>
        <if test="query.payId != null "> and stb.pay_id = #{query.payId}</if>
        <if test="query.deleted == null "> and stb.deleted = false</if>
        <if test="query.deleted != null "> and stb.deleted = #{query.deleted}</if>
        <if test="query.deviceMac != null  and query.deviceMac != ''"> and device_mac = #{query.deviceMac}</if>
        <if test="query.deviceNo != null  and query.deviceNo != ''"> and device_no = #{query.deviceNo}</if>
        <if test="query.statusList != null  and query.statusList.size() > 0">
            and stb.status in
            <foreach collection="query.statusList" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
    </sql>

    <select id="selectTimeBillList" parameterType="TimeBillQuery" resultMap="TimeBillResult">
        <include refid="selectTimeBillVo"/>
        <where>
            <include refid="searchCondition"/>
        </where>
    </select>

    <select id="selectTimeBillByBillId" parameterType="Long" resultMap="TimeBillResult">
        <include refid="selectTimeBillVo"/>
        where stb.bill_id = #{billId}
    </select>

    <insert id="insertTimeBill" parameterType="TimeBill">
        insert into ss_time_bill
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="billId != null">bill_id,</if>
            <if test="billNo != null and billNo != ''">bill_no,</if>
            <if test="userId != null">user_id,</if>
            <if test="deviceId != null">device_id,</if>
            <if test="status != null and status != ''">`status`,</if>
            <if test="amount != null">amount,</if>
            <if test="deviceStartTime != null">device_start_time,</if>
            <if test="deviceStartUnit != null and deviceStartUnit != ''">device_start_unit,</if>
            <if test="deviceStartPrice != null">device_start_price,</if>
            <if test="deviceOverTime != null">device_over_time,</if>
            <if test="deviceOverUnit != null and deviceOverUnit != ''">device_over_unit,</if>
            <if test="deviceOverPrice != null">device_over_price,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="mchId != null">mch_id,</if>
            <if test="arrivalAmount != null">arrival_amount,</if>
            <if test="serviceAmount != null">service_amount,</if>
            <if test="payId != null">pay_id,</if>
            <if test="createTime != null">create_time,</if>
            <if test="deleted != null">deleted,</if>
            <if test="deviceMac != null and deviceMac != ''">device_mac,</if>
            <if test="deviceNo != null and deviceNo != ''">device_no,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="billId != null">#{billId},</if>
            <if test="billNo != null and billNo != ''">#{billNo},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deviceId != null">#{deviceId},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="amount != null">#{amount},</if>
            <if test="deviceStartTime != null">#{deviceStartTime},</if>
            <if test="deviceStartUnit != null and deviceStartUnit != ''">#{deviceStartUnit},</if>
            <if test="deviceStartPrice != null">#{deviceStartPrice},</if>
            <if test="deviceOverTime != null">#{deviceOverTime},</if>
            <if test="deviceOverUnit != null and deviceOverUnit != ''">#{deviceOverUnit},</if>
            <if test="deviceOverPrice != null">#{deviceOverPrice},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="mchId != null">#{mchId},</if>
            <if test="arrivalAmount != null">#{arrivalAmount},</if>
            <if test="serviceAmount != null">#{serviceAmount},</if>
            <if test="payId != null">#{payId},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="deleted != null">#{deleted},</if>
            <if test="deviceMac != null and deviceMac != ''">#{deviceMac},</if>
            <if test="deviceNo != null and deviceNo != ''">#{deviceNo},</if>
         </trim>
    </insert>

    <update id="updateTimeBill" parameterType="TimeBill">
        update ss_time_bill
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        where bill_id = #{billId}
    </update>

    <sql id="updateColumns">
        <if test="data.billNo != null and data.billNo != ''">bill_no = #{data.billNo},</if>
        <if test="data.userId != null">user_id = #{data.userId},</if>
        <if test="data.deviceId != null">device_id = #{data.deviceId},</if>
        <if test="data.status != null and data.status != ''">`status` = #{data.status},</if>
        <if test="data.amount != null">amount = #{data.amount},</if>
        <if test="data.deviceStartTime != null">device_start_time = #{data.deviceStartTime},</if>
        <if test="data.deviceStartUnit != null and data.deviceStartUnit != ''">device_start_unit = #{data.deviceStartUnit},</if>
        <if test="data.deviceStartPrice != null">device_start_price = #{data.deviceStartPrice},</if>
        <if test="data.deviceOverTime != null">device_over_time = #{data.deviceOverTime},</if>
        <if test="data.deviceOverUnit != null and data.deviceOverUnit != ''">device_over_unit = #{data.deviceOverUnit},</if>
        <if test="data.deviceOverPrice != null">device_over_price = #{data.deviceOverPrice},</if>
        <if test="data.startTime != null">start_time = #{data.startTime},</if>
        <if test="data.endTime != null">end_time = #{data.endTime},</if>
        <if test="data.mchId != null">mch_id = #{data.mchId},</if>
        <if test="data.arrivalAmount != null">arrival_amount = #{data.arrivalAmount},</if>
        <if test="data.serviceAmount != null">service_amount = #{data.serviceAmount},</if>
        <if test="data.payId != null">pay_id = #{data.payId},</if>
        <if test="data.createTime != null">create_time = #{data.createTime},</if>
        <if test="data.deleted != null">deleted = #{data.deleted},</if>
        <if test="data.deviceMac != null and data.deviceMac != ''">device_mac = #{data.deviceMac},</if>
        <if test="data.deviceNo != null and data.deviceNo != ''">device_no = #{data.deviceNo},</if>
    </sql>

    <update id="logicDel">
        update ss_time_bill
        set deleted = true
        where bill_id in
        <foreach collection="ids" item="billId" open="(" separator="," close=")">
            #{billId}
        </foreach>
    </update>

    <update id="updateByQuery">
        update ss_time_bill stb
        <trim prefix="SET" suffixOverrides=",">
            <include refid="updateColumns"/>
        </trim>
        <where>
            <include refid="searchCondition"/>
        </where>
    </update>

    <delete id="deleteTimeBillByBillId" parameterType="Long">
        delete from ss_time_bill where bill_id = #{billId}
    </delete>

    <delete id="deleteTimeBillByBillIds" parameterType="String">
        delete from ss_time_bill where bill_id in
        <foreach item="billId" collection="array" open="(" separator="," close=")">
            #{billId}
        </foreach>
    </delete>
</mapper>
