<?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.zc.business.mapper.DcDeviceMapper">

    <resultMap type="DcDevice" id="DcDevice">

        <result property="id" column="id"/>
        <result property="iotDeviceId" column="iot_device_id"/>
        <result property="groupId" column="group_id"/>
        <result property="productId" column="product_id"/>
        <result property="stakeMark" column="stake_mark"/>
        <result property="direction" column="direction"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceCode" column="device_code"/>
        <result property="deviceType" column="device_type"/>
        <result property="installationDate" column="installation_Date"/>
        <result property="productionDate" column="production_date"/>
        <result property="durableYears" column="durable_years"/>
        <result property="installationSite" column="installation_site"/>
        <result property="useState" column="use_state"/>
        <result property="deviceState" column="device_state"/>
        <result property="otherConfig" column="other_config"/>
        <result property="remark" column="remark"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
        <result property="facilitiesType" column="facilities_type"/>
        <result property="deviceIp" column="device_ip"/>
        <result property="stakeMarkRange" column="stake_mark_range"/>
        <result property="networkSegment" column="network_segment"/>
        <result property="deviceImg" column="device_img"/>
        <result property="longitude"    column="longitude"/>
        <result property="latitude"    column="latitude"/>
        <result property="childType"    column="child_type"/>
        <result property="manufacturer"    column="manufacturer"/>
    </resultMap>

    <resultMap id="dcStakeMarkResult" type="dcStakeMark">
        <result property="longitude"    column="longitude"/>
        <result property="latitude"    column="latitude"/>

    </resultMap>

    <sql id="selectDcDeviceVo">
        select id,
               iot_device_id,
               group_id,
               product_id,
               stake_mark,
               direction,
               device_name,
               device_code,
               device_type,
               installation_Date,
               production_date,
               durable_years,
               installation_site,
               use_state,
               device_state,
               other_config,
               remark,
               create_time,
               update_time,
               facilities_type,
               device_ip,
               stake_mark_range,
               network_segment,
               device_img,
               child_type
        from dc_device
    </sql>


    <select id="selectDcDeviceList" parameterType="dcDevice" resultMap="DcDevice">
        select t1.id, t1.iot_device_id, t1.group_id, t1.product_id, t1.stake_mark, t1.direction, t1.device_name, t1.device_code, t1.device_type,
        t1.installation_Date,t1.production_date,t1.durable_years,t1.installation_site,t1.use_state,t1.device_state,t1.other_config,t1.remark,
        t1.create_time,t1.update_time,t1.facilities_type,t1.device_ip,t1.stake_mark_range,t1.network_segment,t1.device_img,t1.child_type,
        t1.facilities_type,t2.longitude,t2.latitude,t3.manufacturer
        from dc_device t1
        left join dc_stake_mark t2 on t1.stake_mark = t2.stake_mark and t1.direction = t2.direction
        left join dc_product t3 on t3.id = t1.product_id
        <where>
            <if test="id != null  and id != ''">and t1.id = #{id}</if>
            <if test="iotDeviceId != null  and iotDeviceId != ''">and t1.iot_device_id = #{iotDeviceId}</if>
            <if test="groupId != null  and groupId != ''">and t1.group_id = #{groupId}</if>
            <if test="productId != null  and productId != ''">and t1.product_id = #{productId}</if>
            <if test="stakeMark != null  and stakeMark != ''">and t1.stake_mark = #{stakeMark}</if>
            <if test="direction != null  and direction != ''">and t1.direction = #{direction}</if>
            <if test="deviceName != null  and deviceName != ''">and t1.device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceCode != null  and deviceCode != ''">and t1.device_code = #{deviceCode}</if>
            <if test="deviceType != null  and deviceType != ''">and t1.device_type = #{deviceType}</if>
            <if test="installationDate != null  and installationDate != ''">and t1.installation_Date = #{installationDate}</if>
            <if test="productionDate != null  and productionDate != ''">and t1.production_date = #{productionDate}</if>
            <if test="durableYears != null  and durableYears != ''">and t1.durable_years = #{durableYears}</if>
            <if test="installationSite != null  and installationSite != ''">and t1.installation_site = #{installationSite}</if>
            <if test="useState != null">and t1.use_state = #{useState}</if>
            <if test="deviceState != null  and deviceState != ''">and t1.device_state = #{deviceState}</if>
            <if test="otherConfig != null  and otherConfig != ''">and json_extract(t1.other_config,'$.childType') = #{otherConfig}</if>
            <if test="remark != null  and remark != ''">and t1.remark = #{remark}</if>
            <if test="createTime != null  and createTime != ''">and t1.create_time = #{createTime}</if>
            <if test="updateTime != null  and updateTime != ''">and t1.update_time = #{updateTime}</if>
            <if test="facilitiesType != null  and facilitiesType != ''">and t1.facilities_type = #{facilitiesType}</if>
            <if test="deviceIp != null  and deviceIp != ''">and t1.device_ip = #{deviceIp}</if>
            <if test="stakeMarkRange != null  and stakeMarkRange != ''">and t1.stake_mark_range = #{stakeMarkRange}</if>
            <if test="networkSegment != null  and networkSegment != ''">and t1.network_segment = #{networkSegment}</if>
            <if test="deviceImg != null  and deviceImg != ''">and t1.device_img = #{deviceImg}</if>
            <if test="childType != null  and childType != ''">and t1.child_type = #{childType}</if>
        </where>

    </select>

    <select id="numberOfDevicesByType" parameterType="dcDevice" resultMap="DcDevice">
        SELECT use_state, COALESCE(child_type, device_type) AS device_type, COUNT(*) AS sumAll
        FROM dc_device
        <where>
            <if test="useState!=null">
                use_state = #{useState}
            </if>
        </where>
        GROUP BY device_type, child_type;
    </select>

    <select id="selectNearCamPile" resultMap="DcDevice">
        select t1.id, t1.iot_device_id, t1.group_id, t1.product_id, t1.stake_mark, t1.direction, t1.device_name,
            t1.device_code, t1.device_type,t1.installation_Date,t1.production_date,t1.durable_years,t1.installation_site,
            t1.use_state,t1.device_state,t1.other_config,t1.remark,t1.create_time,t1.update_time,t1.facilities_type,
            t1.device_ip,t1.stake_mark_range,t1.network_segment,t1.device_img,t1.child_type,t1.facilities_type,t2.longitude,t2.latitude
        from dc_device t1
        left join dc_stake_mark t2 on t1.stake_mark = t2.stake_mark and t1.direction = t2.direction
        where  !ISNULL(t1.iot_device_id) and t1.device_type = '1' and t1.direction = #{direction}
            and t2.mileage >= #{startMileage} and t2.mileage &lt;= #{endMileage}
    </select>
    <select id="selectNearBoard" resultType="com.zc.business.domain.DcDevice">
         select t1.id, t1.iot_device_id, t1.group_id, t1.product_id, t1.stake_mark, t1.direction, t1.device_name,
            t1.device_code, t1.device_type,t1.installation_Date,t1.production_date,t1.durable_years,t1.installation_site,
            t1.use_state,t1.device_state,t1.other_config,t1.remark,t1.create_time,t1.update_time,t1.facilities_type,
            t1.device_ip,t1.stake_mark_range,t1.network_segment,t1.device_img,t1.child_type,t1.facilities_type,t2.longitude,t2.latitude
        from dc_device t1
        left join dc_stake_mark t2 on t1.stake_mark = t2.stake_mark and t1.direction = t2.direction
        where t1.device_type = '2' and t1.direction = #{direction}
            and t2.mileage >= #{startMileage} and t2.mileage &lt;= #{endMileage}
    </select>
    <select id="countTheNumberOfEligibleItems" resultType="java.util.Map">

        SELECT dc_road_section.id,
               COUNT(dc_device.stake_mark) as number
        FROM
            dc_road_section
            JOIN dc_stake_mark
        ON dc_road_section.id = dc_stake_mark.section_id
            JOIN dc_device ON dc_stake_mark.stake_mark = dc_device.stake_mark
        GROUP BY
            dc_road_section.id;
    </select>
    <select id="selectDeviceNameList" resultType="java.util.HashMap">
        select iot_device_id deviceId, device_type deviceType,device_name deviceName,device_state deviceState from dc_device
        <where>
            <if test="deviceState != null  and deviceState != ''">and device_state = #{deviceState}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceType != null  and deviceType != ''">and device_type = #{deviceType} and iot_device_id is not null</if>
            <if test="deviceType == null  || deviceType == ''">
            and (device_type=3||device_type=13||device_type=15||device_type=16) and iot_device_id is not null
            </if>
        </where>
    </select>
    <select id="selectDeviceParameterProperties" resultType="java.util.HashMap">
        select id, device_type deviceType,
               property_id propertyId,property_name propertyName,
               name from dc_device_attribute
               where device_type=#{deviceType}
    </select>

    <select id="selectDeviceNumber" resultType="int">
        SELECT
                (SELECT COUNT(id) FROM dc_device) +
                (SELECT COUNT(id) FROM dc_facility) AS total_count;
    </select>

    <select id="selectDeviceOfRoad" resultType="java.util.HashMap">
        select d.id deviceId, r.id roadId,use_state useStata FROM dc_device d
        JOIN dc_stake_mark s ON s.stake_mark = d.stake_mark AND s.direction = d.direction
        JOIN dc_road_section r ON r.id = s.section_id
    </select>

</mapper>