<?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.xy.mapper.DeviceChargingMapper">
    <resultMap id="queryPageMap" type="com.xy.dto.DeviceChargingDto$PageVo">
        <association property="deviceInfo" javaType="com.xy.dto.DeviceInfoDto$Vo">
            <id column="device_id" property="deviceId"/>
            <result column="device_name" property="deviceName"/>
            <result column="lon" property="lon"/>
            <result column="lat" property="lat"/>
            <result column="device_type" property="deviceType"/>
            <result column="merc_device_code" property="mercDeviceCode"/>
            <result column="merc_id" property="mercId"/>
            <result column="merc_code" property="mercCode"/>
            <result column="place_id" property="placeId"/>
            <result column="place_line_id" property="placeLineId"/>
            <result column="district_id" property="districtId"/>
            <result column="active_state" property="activeState"/>
            <result column="active_time" property="activeTime"/>
            <result column="busy_state" property="busyState"/>
            <result column="show_status" property="showStatus"/>
            <result column="freeze_status" property="freezeStatus"/>
            <result column="device_img" property="deviceImg"/>
            <result column="update_time" property="updateTime"/>
            <result column="merc_name" property="mercName"/>
            <result column="create_time" property="createTime"/>
            <association property="deviceStatus" javaType="com.xy.dto.DeviceStatusDto$Vo">
                <result column="net_state" property="netState"/>
            </association>
        </association>
        <association property="deviceCharging" javaType="com.xy.dto.DeviceChargingDto$Vo">
            <result column="charging_sum_money" property="chargingSumMoney"/>
            <result column="timeout" property="timeout"/>
            <result column="last_charging_time" property="lastChargingTime"/>
            <result column="charging_create_time" property="createTime"/>
            <result column="last_charging_money" property="lastChargingMoney"/>
            <result column="last_charging_pay_type" property="lastChargingPayType"/>
        </association>
    </resultMap>

    <select id="page" resultMap="queryPageMap">
        select di.*,
        ds.net_state,
        dc.last_charging_money,
        dc.last_charging_pay_type,
        dc.charging_sum_money, dc.timeout, dc.last_charging_time, dc.create_time as charging_create_time
        from device_info di
        join device_status ds on (di.device_id = ds.device_id)
        join device_charging dc on(di.device_id = dc.device_id)
        where
        1 = 1
        <if test="queryPage.mercId != null">
            and di.merc_id = #{queryPage.mercId}
        </if>
        <if test="queryPage.deviceId != null">
            and di.device_id = #{queryPage.deviceId}
        </if>
        <if test="queryPage.freezeStatus != null">
            and di.freeze_status = #{queryPage.freezeStatus}
        </if>

        <if test="queryPage.chargingStatus != null and queryPage.chargingStatus != ''">
            <!-- 即将过期 -->
            <if test="queryPage.chargingStatus == 1">
                and dc.timeout >= #{queryPage.thisTime} and dc.timeout &lt;= #{queryPage.theTime}
            </if>
            <!-- 已过期 -->
            <if test="queryPage.chargingStatus == 2">
                and dc.timeout &lt;= #{queryPage.thisTime}
            </if>
        </if>
        order by dc.timeout asc
    </select>
</mapper>
