<?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.DeviceInfoMapper">
    <resultMap id="queryPageMap" type="com.xy.mapper.entity.DeviceInfoQueryPage">
        <id column="device_id" property="deviceId"/>
        <result column="algorithm_id" property="algorithmId"/>
        <result column="device_name" property="deviceName"/>
        <result column="m" property="m"/>
        <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"/>
        <result column="place_name" property="placeName"/>
        <result column="place_line_name" property="placeLineName"/>
        <result column="district_name" property="districtName"/>
        <result column="aisle_num" property="aisleNum"/>

        <association property="deviceSysinfo" javaType="com.xy.dto.DeviceSysinfoDto$Vo">
            <result column="sysinfo_device_sn" property="deviceSn"/>
            <result column="android_start_app_version" property="androidStartAppVersion"/>
            <result column="android_version" property="androidVersion"/>
            <result column="android_root_state" property="androidRootState"/>
            <result column="android_model" property="androidModel"/>
            <result column="sys_launcher" property="sysLauncher"/>
            <result column="sys_timezone" property="sysTimezone"/>
            <result column="sys_datetimeauto" property="sysDatetimeauto"/>
            <result column="sys_timezhoneauto" property="sysTimezhoneauto"/>
            <result column="app_upm_version" property="appUpmVersion"/>
            <result column="app_downm_version" property="appDownmVersion"/>
            <result column="sim_iccid" property="simIccid"/>
            <result column="sim_isp" property="simIsp"/>
            <result column="sim_company" property="simCompany"/>
            <result column="disk_space" property="diskSpace"/>
            <result column="memery_space" property="memerySpace"/>
            <result column="is_have_temp" property="isHaveTemp"/>
            <result column="is_have_light_main" property="isHaveLightMain"/>
            <result column="is_have_light_second" property="isHaveLightSecond"/>
            <result column="is_have_freeze" property="isHaveFreeze"/>
            <result column="is_have_heat" property="isHaveHeat"/>
            <result column="device_model" property="deviceModel"/>
            <result column="screen_type" property="screenType"/>
            <result column="is_have_alipay_npad" property="isHaveAlipayNpad"/>
            <result column="nfc_sn" property="nfcSn"/>
        </association>
        <association property="deviceStatus" javaType="com.xy.dto.DeviceStatusDto$Vo">
            <result column="sys_start_time" property="sysStartTime"/>
            <result column="sys_cur_time" property="sysCurTime"/>
            <result column="sys_power" property="sysPower"/>
            <result column="wifi_open" property="wifiOpen"/>
            <result column="blooth_open" property="bloothOpen"/>
            <result column="net_state" property="netState"/>
            <result column="net_type" property="netType"/>
            <result column="net_dbm" property="netDbm"/>
            <result column="door_state_l" property="doorStateL"/>
            <result column="door_state_r" property="doorStateR"/>
            <result column="lock_state_l" property="lockStateL"/>
            <result column="lock_state_r" property="lockStateR"/>
            <result column="temp_value" property="tempValue"/>
            <result column="temp_state" property="tempState"/>
            <result column="voice_volume" property="voiceVolume"/>
            <result column="light_main" property="lightMain"/>
            <result column="light_second" property="lightSecond"/>
            <result column="status_disk_space" property="diskSpace"/>
            <result column="disk_remain_space" property="diskRemainSpace"/>
            <result column="memery_remain_space" property="memeryRemainSpace"/>
            <result column="camera_l_main_state" property="cameraLMainState"/>
            <result column="camera_l_second_state" property="cameraLSecondState"/>
            <result column="device_state_l" property="deviceStateL"/>
            <result column="device_state_r" property="deviceStateR"/>
            <result column="status_update_time" property="updateTime"/>
            <result column="stock" property="stock"/>
            <result column="after_fill_stock" property="afterFillStock"/>
            <result column="stock_status" property="stockStatus"/>
            <result column="is_use_battery" property="isUseBattery"/>
            <result column="jg1_lock_state_l" property="jg1LockStateL"/>
            <result column="jg1_lock_state_r" property="jg1LockStateR"/>
        </association>
        <association property="deviceRegister" javaType="com.xy.dto.DeviceRegisterDto$Vo">
            <result column="register_num" property="registerNum"/>
            <result column="dr_device_sn" property="deviceSn"/>
            <result column="device_type" property="deviceType"/>
            <result column="device_pc" property="devicePc"/>
            <result column="asset_no" property="assetNo"/>
            <result column="dr_create_time" property="createTime"/>
            <result column="create_user" property="createUser"/>
        </association>
    </resultMap>

    <select id="listByAdminName" resultType="com.xy.dto.DeviceInfoDto$ListByAdminName">
        SELECT d.device_id, d.device_name, p.admin_name, d.device_type,d.algorithm_id
        FROM device_info d
        left join merc_place p on p.id = d.place_id
        <where>
            and d.merc_id = #{dto.mercId}
            and d.active_state = 1
            <if test="dto.deviceIdList != null and dto.deviceIdList.size > 0">
                and device_id in
                <foreach collection="dto.deviceIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.searchKey != null and dto.searchKey != ''">
                and CONCAT(IFNULL(d.device_name, ''), IFNULL(p.admin_name, '')) like CONCAT('%', #{dto.searchKey}, '%')
            </if>
            <if test="dto.algorithmId != null">
                and d.algorithm_id = #{dto.algorithmId}
            </if>
        </where>
    </select>

    <select id="pageByAdminName" resultType="com.xy.dto.DeviceInfoDto$ListByAdminName">
        SELECT d.device_id, d.device_name, p.admin_name,p.admin_id, d.device_type,p.region_name,p.place_name,m.name as
        merc_name,m.merc_code as merc_code
        FROM device_info d
        left join merc_place p on p.id = d.place_id
        left join merc m on m.id = d.merc_id
        <where>
            and d.merc_id = #{dto.mercId}
            and d.active_state = 1
            <if test="dto.deviceIdList != null and dto.deviceIdList.size > 0">
                and device_id in
                <foreach collection="dto.deviceIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dto.searchKey != null and dto.searchKey != ''">
                and CONCAT(IFNULL(d.device_name, ''), IFNULL(p.admin_name, '')) like CONCAT('%', #{dto.searchKey}, '%')
            </if>
            <if test="dto.deviceType != null">
                and d.device_type = #{dto.deviceType}
            </if>
            <if test="dto.placeId != null">
                and d.place_id = #{dto.placeId}
            </if>
            <if test="dto.deviceId != null">
                and d.device_id = #{dto.deviceId}
            </if>
            <if test="dto.regionName != null and dto.regionName != ''">
                and p.region_name = #{dto.regionName}
            </if>
        </where>
        order by p.admin_name asc,d.device_id desc
    </select>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="queryPageMap">
        select info.*,
        dr.register_num,
        dr.device_sn as dr_device_sn,
        dr.device_pc,
        dr.asset_no,
        dr.create_time as
        dr_create_time,
        dr.create_user,
        sysinfo.device_sn as sysinfo_device_sn,
        sysinfo.android_start_app_version,
        sysinfo.android_version,
        sysinfo.android_root_state,
        sysinfo.android_model,
        sysinfo.sys_launcher,
        sysinfo.sys_timezone,
        sysinfo.sys_datetimeauto,
        sysinfo.sys_timezhoneauto,
        sysinfo.app_upm_version,
        sysinfo.app_downm_version,
        sysinfo.sim_iccid,
        sysinfo.sim_isp,
        sysinfo.sim_company,
        sysinfo.disk_space,
        sysinfo.memery_space,
        sysinfo.create_time as sysinfo_create_time,
        sysinfo.is_have_temp,
        sysinfo.is_have_light_main,
        sysinfo.is_have_light_second,
        sysinfo.is_have_freeze,
        sysinfo.is_have_heat,
        sysinfo.device_model,
        sysinfo.screen_type,
        sysinfo.is_have_alipay_npad,
        sysinfo.nfc_sn,
        status.sys_start_time,
        status.sys_cur_time,
        status.sys_power,
        status.wifi_open,
        status.blooth_open,
        status.net_state,
        status.net_type,
        status.net_dbm,
        status.door_state_l,
        status.door_state_r,
        status.lock_state_l,
        status.lock_state_r,
        status.temp_value,
        status.temp_state,
        status.voice_volume,
        status.light_main,
        status.light_second,
        status.disk_space as status_disk_space,
        status.disk_remain_space,
        status.memery_remain_space,
        status.camera_l_main_state,
        status.camera_l_second_state,
        status.camera_r_main_state,
        status.camera_r_second_state,
        status.device_state_l,
        status.device_state_r,
        status.update_time as status_update_time,
        status.stock,
        status.after_fill_stock,
        status.stock_status,
        status.is_use_battery,
        status.jg1_lock_state_l,
        status.jg1_lock_state_r,
        mp.place_name,
        ml.line_name as place_line_name,
        sr.name as district_name
        <if test="queryPage.lon != null and queryPage.lon != ''">
            ,
            ROUND(
            6378.138 * 2 * ASIN(
            SQRT(
            POW(
            SIN(
            (
            #{queryPage.lat} * PI() / 180 - info.lat * PI() / 180
            ) / 2
            ),
            2
            ) +
            COS(#{queryPage.lat} * PI() / 180) * COS(info.lat * PI() / 180) * POW(
            SIN(
            (
            #{queryPage.lon} * PI() / 180 - info.lon * PI() / 180
            ) / 2
            ),
            2
            )
            )
            ) * 1000
            ) AS m
        </if>
        from device_info info
        join device_sysinfo sysinfo on (info.device_id = sysinfo.device_id)
        join device_status status on (info.device_id = status.device_id)
        join device_register dr on (info.device_id = dr.device_id)
        left join merc mr on (info.merc_id = mr.id)
        left join merc_place mp on (info.place_id = mp.id)
        left join merc_line ml on (info.place_line_id = ml.id)
        left join sys_region sr on (info.district_id = sr.id)
        where 1 = 1

        <if test="queryPage.isBindMerc != null">
            <choose>
                <when test="queryPage.isBindMerc == true">
                    and info.merc_id is not null
                </when>
                <otherwise>
                    and (info.merc_id is null or info.merc_id = '')
                </otherwise>
            </choose>
        </if>
        <if test="queryPage.isHaveNfc != null">
            and sysinfo.is_have_alipay_npad= #{queryPage.isHaveNfc}
        </if>
        <if test="queryPage.keyword != null and queryPage.keyword != ''">
            and CONCAT(IFNULL(info.device_name, ''), IFNULL(info.device_id, '')) LIKE
            CONCAT('%',#{queryPage.keyword},'%')
        </if>
        <if test="queryPage.showStatus != null">
            and info.show_status = #{queryPage.showStatus}
        </if>
        <if test="queryPage.deviceIdName != null and queryPage.deviceIdName != ''">
            and (
            LOCATE(#{queryPage.deviceIdName}, info.device_id) > 0 or
            LOCATE(#{queryPage.deviceIdName}, info.device_name)
            > 0
            )
        </if>
        <if test="queryPage.deviceId != null">
            and LOCATE(#{queryPage.deviceId}, info.device_id) > 0
        </if>
        <if test="queryPage.deviceIdList != null">
            and info.device_id IN
            <foreach collection="queryPage.deviceIdList" item="deviceId" open="(" close=")" separator=",">
                #{deviceId}
            </foreach>
        </if>

        <if test="queryPage.deviceName != null and queryPage.deviceName != ''">
            and LOCATE(#{queryPage.deviceName}, info.device_name) > 0
        </if>
        <if test="queryPage.no != null and queryPage.no != ''">
            and (
            LOCATE(#{queryPage.no}, info.merc_device_code) > 0 or
            LOCATE(#{queryPage.no}, sysinfo.device_sn) > 0 or
            LOCATE(#{queryPage.no}, sysinfo.sim_iccid) > 0
            )
        </if>
        <if test="queryPage.activeState != null">
            and info.active_state = #{queryPage.activeState}
        </if>
        <if test="queryPage.netState != null">
            and status.net_state = #{queryPage.netState}
        </if>

        <if test="queryPage.stockStatus != null">
            and status.stock_status = #{queryPage.stockStatus}
        </if>


        <if test="queryPage.busyState != null">
            and info.busy_state = #{queryPage.busyState}
        </if>
        <if test="queryPage.deviceType != null">
            and info.device_type = #{queryPage.deviceType}
        </if>
        <if test="queryPage.mercDeviceCode != null and queryPage.mercDeviceCode != ''">
            and LOCATE(#{queryPage.mercDeviceCode}, info.merc_device_code) > 0
        </if>
        <if test="queryPage.mercCode != null and queryPage.mercCode != ''">
            and info.merc_code = #{queryPage.mercCode}
        </if>
        <if test="queryPage.mercId != null">
            and info.merc_id = #{queryPage.mercId}
        </if>
        <if test="queryPage.merc != null and queryPage.merc != ''">
            and (
            info.merc_id = #{queryPage.merc} or info.merc_code like concat(#{queryPage.merc}, '%') or
            LOCATE(#{queryPage.merc}, mr.name) > 0
            )
        </if>
        <if test="queryPage.placeId != null">
            and info.place_id = #{queryPage.placeId}
        </if>
        <if test="queryPage.placeLineId != null">
            and info.place_line_id = #{queryPage.placeLineId}
        </if>
        <if test="queryPage.districtId != null">
            and info.district_id = #{queryPage.districtId}
        </if>
        <if test="queryPage.activeState != null">
            and info.active_state = #{queryPage.activeState}
        </if>
        <if test="queryPage.busyState != null">
            and info.busy_state = #{queryPage.busyState}
        </if>
        <if test="queryPage.appUpmVersion != null and queryPage.appUpmVersion != ''">
            and sysinfo.app_upm_version = #{queryPage.appUpmVersion}
        </if>
        <if test="queryPage.appDownmVersion != null and queryPage.appDownmVersion != ''">
            and sysinfo.app_downm_version = #{queryPage.appDownmVersion}
        </if>
        <if test="queryPage.beginActiveTime != null and queryPage.beginActiveTime != ''">
            and info.active_time >= #{queryPage.beginActiveTime}
        </if>
        <if test="queryPage.endActiveTime != null and queryPage.endActiveTime != ''">
            and info.active_time &lt;= #{queryPage.endActiveTime}
        </if>
        <if test="queryPage.lon != null and queryPage.lon != ''">
            <if test="queryPage.nearby != null">
                HAVING `m` &lt;= #{queryPage.nearby}
            </if>
        </if>
        <choose>
            <when test="queryPage.lon != null and queryPage.lon != ''">
                order by `m` asc
            </when>
            <otherwise>
                order by info.create_time desc
            </otherwise>
        </choose>
        <if test="queryPage.limitNum != null">
            limit #{queryPage.limitNum }
        </if>
    </select>


    <!-- 分页查询统计 -->
    <select id="pageCountForPc" resultType="com.xy.dto.DeviceInfoDto$PageCount">
        SELECT
        COUNT(*) AS total,
        SUM(CASE WHEN status.net_state = 'online' THEN 1 ELSE 0 END) AS onlineNum,
        SUM(CASE WHEN status.net_state = 'offline' THEN 1 ELSE 0 END) AS offLineNum,
        SUM(CASE WHEN info.active_state = 1 THEN 1 ELSE 0 END) AS activeNum,
        SUM(CASE WHEN info.active_state = 0 THEN 1 ELSE 0 END) AS noActiveNum
        FROM device_info info
        JOIN device_status status ON info.device_id = status.device_id
        join device_sysinfo sysinfo on info.device_id = sysinfo.device_id

        <where>
            <if test="queryPage.isBindMerc != null">
                <choose>
                    <when test="queryPage.isBindMerc == true">
                        and info.merc_id is not null
                    </when>
                    <otherwise>
                        and (info.merc_id is null or info.merc_id = '')
                    </otherwise>
                </choose>
            </if>
            <if test="queryPage.isHaveNfc != null">
                and sysinfo.is_have_alipay_npad= #{queryPage.isHaveNfc}
            </if>
            <if test="queryPage.keyword != null and queryPage.keyword != ''">
                and CONCAT(IFNULL(info.device_name, ''), IFNULL(info.device_id, '')) LIKE
                CONCAT('%',#{queryPage.keyword},'%')
            </if>
            <if test="queryPage.showStatus != null">
                and info.show_status = #{queryPage.showStatus}
            </if>
            <if test="queryPage.deviceIdName != null and queryPage.deviceIdName != ''">
                and (
                LOCATE(#{queryPage.deviceIdName}, info.device_id) > 0 or
                LOCATE(#{queryPage.deviceIdName}, info.device_name)
                > 0
                )
            </if>
            <if test="queryPage.deviceId != null">
                and LOCATE(#{queryPage.deviceId}, info.device_id) > 0
            </if>
            <if test="queryPage.deviceIdList != null">
                and info.device_id IN
                <foreach collection="queryPage.deviceIdList" item="deviceId" open="(" close=")" separator=",">
                    #{deviceId}
                </foreach>
            </if>

            <if test="queryPage.deviceName != null and queryPage.deviceName != ''">
                and LOCATE(#{queryPage.deviceName}, info.device_name) > 0
            </if>
            <if test="queryPage.no != null and queryPage.no != ''">
                and (
                LOCATE(#{queryPage.no}, info.merc_device_code) > 0 or
                LOCATE(#{queryPage.no}, sysinfo.device_sn) > 0 or
                LOCATE(#{queryPage.no}, sysinfo.sim_iccid) > 0
                )
            </if>
            <if test="queryPage.activeState != null">
                and info.active_state = #{queryPage.activeState}
            </if>
            <if test="queryPage.netState != null">
                and status.net_state = #{queryPage.netState}
            </if>

            <if test="queryPage.stockStatus != null">
                and status.stock_status = #{queryPage.stockStatus}
            </if>


            <if test="queryPage.busyState != null">
                and info.busy_state = #{queryPage.busyState}
            </if>
            <if test="queryPage.deviceType != null">
                and info.device_type = #{queryPage.deviceType}
            </if>
            <if test="queryPage.mercDeviceCode != null and queryPage.mercDeviceCode != ''">
                and LOCATE(#{queryPage.mercDeviceCode}, info.merc_device_code) > 0
            </if>
            <if test="queryPage.mercCode != null and queryPage.mercCode != ''">
                and info.merc_code = #{queryPage.mercCode}
            </if>
            <if test="queryPage.mercId != null">
                and info.merc_id = #{queryPage.mercId}
            </if>
            <if test="queryPage.merc != null and queryPage.merc != ''">
                and (
                info.merc_id = #{queryPage.merc} or info.merc_code like concat(#{queryPage.merc}, '%') or
                LOCATE(#{queryPage.merc}, mr.name) > 0
                )
            </if>
            <if test="queryPage.placeId != null">
                and info.place_id = #{queryPage.placeId}
            </if>
            <if test="queryPage.placeLineId != null">
                and info.place_line_id = #{queryPage.placeLineId}
            </if>
            <if test="queryPage.districtId != null">
                and info.district_id = #{queryPage.districtId}
            </if>
            <if test="queryPage.activeState != null">
                and info.active_state = #{queryPage.activeState}
            </if>
            <if test="queryPage.busyState != null">
                and info.busy_state = #{queryPage.busyState}
            </if>
            <if test="queryPage.appUpmVersion != null and queryPage.appUpmVersion != ''">
                and sysinfo.app_upm_version = #{queryPage.appUpmVersion}
            </if>
            <if test="queryPage.appDownmVersion != null and queryPage.appDownmVersion != ''">
                and sysinfo.app_downm_version = #{queryPage.appDownmVersion}
            </if>
            <if test="queryPage.beginActiveTime != null and queryPage.beginActiveTime != ''">
                and info.active_time >= #{queryPage.beginActiveTime}
            </if>
            <if test="queryPage.endActiveTime != null and queryPage.endActiveTime != ''">
                and info.active_time &lt;= #{queryPage.endActiveTime}
            </if>
        </where>
    </select>


    <!--  小程序首页设备列表线路分组  -->
    <select id="merHomeCountList" resultType="com.xy.dto.DeviceInfoDto$MercHomeCountVO">
        select count(*) deviceNum, IFNULL(district_id, -1) districtId
        from device_info

        <where>
            <if test="query.activeState != null and query.activeState != ''">
                and active_state = #{query.activeState}
            </if>

            <if test="query.busyStatus != null">
                and busy_state = #{query.busyStatus}
            </if>

            <if test="query.deviceType != null">
                and device_type = #{query.deviceType}
            </if>

            <if test="query.mercId != null">
                and merc_id = #{query.mercId}
            </if>
            <if test="query.deviceName != null and query.deviceName != ''">
                and device_name like concat('%', #{query.deviceName}, '%')
            </if>
            <if test="query.deviceIdList != null and query.deviceIdList.size > 0">
                and device_id in
                <foreach collection="query.deviceIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        group by district_id
    </select>
    <select id="netStateCount" resultType="com.xy.dto.DeviceInfoDto$NetStateCount">
        SELECT d.merc_id,net_state, count(*) as count
        FROM device_info d
        LEFT JOIN device_status s ON s.device_id = d.device_id
        <where>
            d.active_state = '1'
            <if test="mercIdList != null and mercIdList.size() > 0">
                AND d.merc_id IN
                <foreach collection="mercIdList" item="mercId" index="index"
                         open="(" close=")" separator=",">
                    #{mercId}
                </foreach>
            </if>
        </where>
        GROUP BY d.merc_id,s.net_state
    </select>
    <select id="placeDeviceNum" resultType="com.xy.dto.DeviceInfoDto$PlaceDeviceNumVo">
        SELECT place_id,count(*) as num
        FROM device_info
        <where>
            <if test="dto.placeId != null">
                AND place_id = #{dto.placeId}
            </if>
            <if test="dto.placeIdList != null and dto.placeIdList.size() > 0">
                AND place_id IN
                <foreach collection="dto.placeIdList" item="placeId" index="index" open="(" close=")" separator=",">
                    #{placeId}
                </foreach>
            </if>
        </where>
        GROUP BY place_id
    </select>
</mapper>
