<?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.DeviceSimMapper">
    <resultMap id="queryPageMap" type="com.xy.dto.DeviceSimDto$PageVo">
        <result column="sim_iccid" property="simIccid"/>
        <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="sim" javaType="com.xy.dto.DeviceSimDto$Vo">
            <id column="id" property="id"/>
            <result column="type" property="type"/>
            <result column="activate_time" property="activateTime"/>
            <result column="timeout" property="timeout"/>
            <result column="last_renewal_time" property="lastRenewalTime"/>
            <result column="dsim_create_time" property="createTime"/>
            <result column="is_test" property="isTest"/>
            <result column="wait_handle" property="waitHandle"/>
        </association>
    </resultMap>

    <select id="page2" resultMap="queryPageMap">
        select
        di.*,
        ds.net_state,
        dsys.sim_iccid, dsys.sim_iccid as id, dsys.sim_isp as type
        from device_sysinfo dsys
        join device_info di on(dsys.device_id = di.device_id)
        join device_status ds on (di.device_id = ds.device_id)
        where
        dsys.sim_iccid is not null and di.active_state = 1
        and NOT EXISTS (
            SELECT 1
            FROM device_sim dsim
            WHERE dsim.id = dsys.sim_iccid
        )
        <if test="queryPage.simId != null and queryPage.simId != ''">
            and dsim.id = #{queryPage.simId}
        </if>
        <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.batchNo != null and queryPage.batchNo != ''">
            and dsim.batch_no = #{queryPage.batchNo}
        </if>
    </select>

    <select id="pageCount2" resultType="int">
        select count(*)
        from device_sysinfo dsys
        join device_info di on(dsys.device_id = di.device_id)
        join device_status ds on (di.device_id = ds.device_id)
        where
        dsys.sim_iccid is not null and di.active_state = 1
        and NOT EXISTS (
            SELECT 1
            FROM device_sim dsim
            WHERE dsim.id = dsys.sim_iccid
        )
        <if test="queryPage.simId != null and queryPage.simId != ''">
            and dsim.id = #{queryPage.simId}
        </if>
        <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.batchNo != null and queryPage.batchNo != ''">
            and dsim.batch_no = #{queryPage.batchNo}
        </if>
    </select>

    <select id="page" resultMap="queryPageMap">
        SELECT
        di.*,
        ds.net_state,
        dsim.id, dsim.type, dsim.activate_time, dsim.timeout, dsim.last_renewal_time, dsim.create_time as
        dsim_create_time, dsim.is_test, dsim.wait_handle,
        dsys.sim_iccid
        FROM
        device_sim dsim
        left join device_sysinfo dsys on(dsim.id = dsys.sim_iccid)
        left join device_info di on(dsys.device_id = di.device_id)
        left join device_status ds on (di.device_id = ds.device_id)
        where
        1 = 1
        <if test="queryPage.isBind == false">
            AND NOT EXISTS (
            SELECT 1
            FROM device_sysinfo dsys
            WHERE dsim.id = dsys.sim_iccid
            )
        </if>
        <if test="queryPage.isBind == true">
            AND EXISTS (
            SELECT 1
            FROM device_sysinfo dsys
            WHERE dsim.id = dsys.sim_iccid
            )
            <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>
        <if test="queryPage.simId != null and queryPage.simId != ''">
            and dsim.id = #{queryPage.simId}
        </if>
        <if test="queryPage.chargingStatus != null and queryPage.chargingStatus != ''">
            <!-- 即将过期 -->
            <if test="queryPage.chargingStatus == 1">
                and dsim.timeout >= #{queryPage.thisTime} and dsim.timeout &lt;= #{queryPage.theTime}
            </if>
            <!-- 已过期 -->
            <if test="queryPage.chargingStatus == 2">
                and dsim.timeout &lt;= #{queryPage.thisTime}
            </if>
        </if>
        <if test="queryPage.isActivate != null">
            <if test="queryPage.isActivate == true">
                and dsim.activate_time is not null
            </if>
            <if test="queryPage.isActivate == false">
                and dsim.activate_time is null
            </if>
        </if>
        <if test="queryPage.batchNo != null and queryPage.batchNo != ''">
            and dsim.batch_no = #{queryPage.batchNo}
        </if>
        <if test="queryPage.waitHandle != null">
            and dsim.wait_handle = #{queryPage.waitHandle}
        </if>
        order by dsim.timeout asc
    </select>

    <select id="pageCount" resultType="int">
        select count(*)
        FROM
        device_sim dsim
        left join device_sysinfo dsys on(dsim.id = dsys.sim_iccid)
        left join device_info di on(dsys.device_id = di.device_id)
        left join device_status ds on (di.device_id = ds.device_id)
        where
        1 = 1
        <if test="queryPage.isBind == false">
            AND NOT EXISTS (
            SELECT 1
            FROM device_sysinfo dsys
            WHERE dsim.id = dsys.sim_iccid
            )
        </if>
        <if test="queryPage.isBind == true">
            AND EXISTS (
            SELECT 1
            FROM device_sysinfo dsys
            WHERE dsim.id = dsys.sim_iccid
            )
            <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>
        <if test="queryPage.simId != null and queryPage.simId != ''">
            and dsim.id = #{queryPage.simId}
        </if>
        <if test="queryPage.chargingStatus != null and queryPage.chargingStatus != ''">
            <!-- 即将过期 -->
            <if test="queryPage.chargingStatus == 1">
                and dsim.timeout >= #{queryPage.thisTime} and dsim.timeout &lt;= #{queryPage.theTime}
            </if>
            <!-- 已过期 -->
            <if test="queryPage.chargingStatus == 2">
                and dsim.timeout &lt;= #{queryPage.thisTime}
            </if>
        </if>
        <if test="queryPage.isActivate != null">
            <if test="queryPage.isActivate == true">
                and dsim.activate_time is not null
            </if>
            <if test="queryPage.isActivate == false">
                and dsim.activate_time is null
            </if>
        </if>
        <if test="queryPage.batchNo != null and queryPage.batchNo != ''">
            and dsim.batch_no = #{queryPage.batchNo}
        </if>
        <if test="queryPage.waitHandle != null">
            and dsim.wait_handle = #{queryPage.waitHandle}
        </if>
    </select>

    <select id="deviceSimTimeout" resultType="java.lang.Long">
        select merc_id
        from device_sysinfo dsi
        join device_info di on(dsi.device_id = di.device_id)
        join device_sim dsim on(dsi.sim_iccid = dsim.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 = 2 and mmi.work_type =
        #{query.workType}
        )
        <if test="query.begin != null">
            and dsim.timeout >= #{query.begin}
        </if>
        <if test="query.end != null">
            and dsim.timeout &lt;= #{query.end}
        </if>
        GROUP BY di.merc_id
    </select>

    <select id="deviceSimTimeoutCount" resultType="com.xy.dto.DeviceSimDto$DeviceSimTimeoutCountVo">
        select
        (select count(*)
        from device_info di
        join device_sysinfo ds on(di.device_id = ds.device_id)
        join device_sim dsim on(ds.sim_iccid = dsim.id)
        where
        dsim.timeout >= #{query.begin1} and dsim.timeout &lt;= #{query.end1}
        <if test="query.mercId != null">
            and di.merc_id = #{query.mercId}
        </if>
        ) as beTimeoutCout,
        (select count(*)
        from device_info di
        join device_sysinfo ds on(di.device_id = ds.device_id)
        join device_sim dsim on(ds.sim_iccid = dsim.id)
        where
        dsim.timeout &lt;= #{query.end2}
        <if test="query.mercId != null">
            and di.merc_id = #{query.mercId}
        </if>
        ) as timeoutCout,
        (select count(*)
        from device_info di
        join device_sysinfo ds on(di.device_id = ds.device_id)
        join device_sim dsim on(ds.sim_iccid = dsim.id)
        <where>
            <if test="query.mercId != null">
                and di.merc_id = #{query.mercId}
            </if>
        </where>
        ) as totalNum
        from dual
    </select>
</mapper>
