<?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="BaseResultMap" type="com.xy.entity.DeviceCharging">
        <id column="device_id" property="deviceId"/>
        <result column="device_type" property="deviceType"/>
        <result column="charging_money" property="chargingMoney"/>
        <result column="charging_sum_money" property="chargingSumMoney"/>
        <result column="charging_type" property="chargingType"/>
        <result column="status" property="status"/>
        <result column="timeout" property="timeout"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 管理费过期 -->
    <select id="deviceChargingTimeout" resultType="java.lang.Long">
        select di.merc_id
        from device_charging dc
        join device_info di on(dc.device_id = di.device_id)
        where
        NOT EXISTS (
            SELECT 1
            FROM merc_msg_info mmi
            WHERE di.merc_id = mmi.merc_id and mmi.create_time >= #{query.mmiTime} and mmi.type = 1 and mmi.work_type = #{query.workType}
        )
        <if test="query.begin != null">
            and dc.timeout >= #{query.begin}
        </if>
        <if test="query.end != null">
            and dc.timeout &lt;= #{query.end}
        </if>
        GROUP BY di.merc_id
    </select>

    <select id="deviceChargingTimeoutCount" resultType="com.xy.dto.DeviceChargingDto$DeviceChargingTimeoutCountVo">
        select
        (select count(*)
        from device_info di
        join device_charging dc on(di.device_id = dc.device_id)
        where
        di.merc_id = #{query.mercId} and dc.timeout >= #{query.begin1} and dc.timeout &lt;= #{query.end1}) as beTimeoutCout,
        (select count(*)
        from device_info di
        join device_charging dc on(di.device_id = dc.device_id)
        where
        di.merc_id = #{query.mercId} and dc.timeout &lt;= #{query.end2}) as timeoutCout
        from dual
    </select>
</mapper>
