<?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.moral.api.mapper.DeviceMapper"> 
 | 
  
 | 
    <!-- 通用查询映射结果 --> 
 | 
    <resultMap id="BaseResultMap" type="com.moral.api.entity.Device"> 
 | 
        <id column="id" property="id"/> 
 | 
        <result column="name" property="name"/> 
 | 
        <result column="mac" property="mac"/> 
 | 
        <result column="address" property="address"/> 
 | 
        <result column="longitude" property="longitude"/> 
 | 
        <result column="latitude" property="latitude"/> 
 | 
        <result column="state" property="state"/> 
 | 
        <result column="operate_ids" property="operateIds"/> 
 | 
        <result column="monitor_point_id" property="monitorPointId"/> 
 | 
        <result column="organization_id" property="organizationId"/> 
 | 
        <result column="guid" property="guid"/> 
 | 
        <result column="device_version_id" property="deviceVersionId"/> 
 | 
        <result column="profession" property="profession"/> 
 | 
        <result column="tech" property="tech"/> 
 | 
        <result column="detector" property="detector"/> 
 | 
        <result column="purchaser" property="purchaser"/> 
 | 
        <result column="create_time" property="createTime"/> 
 | 
        <result column="update_time" property="updateTime"/> 
 | 
        <result column="install_time" property="installTime"/> 
 | 
        <result column="is_delete" property="isDelete"/> 
 | 
        <result column="extend" property="extend"/> 
 | 
        <result column="town_code" property="townCode"/> 
 | 
        <result column="dev_num" property="devNum"/> 
 | 
    </resultMap> 
 | 
  
 | 
    <!--监测因子趋势图数据--> 
 | 
    <select id="getTrendChartData" resultType="java.util.Map"> 
 | 
        SELECT mac, 
 | 
        DATE_FORMAT(`time`,#{dateFormat}) AS `time`, 
 | 
        `value`->'$.${sensorCode}' AS '${sensorCode}' 
 | 
        FROM history_${timeUnits} 
 | 
        WHERE mac IN 
 | 
            <foreach collection="macs" item="mac" index="index" open="(" close=")" separator=","> 
 | 
                #{mac} 
 | 
            </foreach> 
 | 
        AND `time` <![CDATA[>=]]> #{start} 
 | 
        AND `time` <![CDATA[<]]> #{end} 
 | 
        ORDER BY `time` 
 | 
    </select> 
 | 
    <select id="deviceList" resultType="java.util.Map"> 
 | 
        SELECT 
 | 
            t.latitude as latitude, 
 | 
            t.longitude as longitude, 
 | 
            t.mac as mac, 
 | 
            t.state,(hf.value ->> #{type} )+0 as ${sensorCode} 
 | 
        FROM 
 | 
            device t 
 | 
        inner JOIN monitor_point p on p.id = t.monitor_point_id and p.is_delete = 0 
 | 
                and p.organization_id in ( 
 | 
                    SELECT id FROM ( 
 | 
                    SELECT id, parent_id, name FROM organization  WHERE id = #{organizationId} 
 | 
                    UNION ALL 
 | 
                    SELECT i.id, i.parent_id, i.name 
 | 
                    FROM organization  i 
 | 
                    INNER JOIN ( 
 | 
                    SELECT * FROM ( 
 | 
                    SELECT id, parent_id, name FROM organization  WHERE id = #{organizationId} 
 | 
                    UNION ALL 
 | 
                    SELECT id, parent_id, name FROM organization WHERE parent_id = #{organizationId} 
 | 
                    ) t1 
 | 
                    ) t2 ON i.parent_id = t2.id 
 | 
                    where i.is_delete = 0 
 | 
                    ) t3 
 | 
                    ) 
 | 
        <if test="region != null"> 
 | 
            and p.${region} = #{regionCode} 
 | 
        </if> 
 | 
        <if test="endHourlyTime !=null and endHourlyTime != ''"> 
 | 
            left join history_hourly_${times} hf on hf.time =#{endHourlyTime}  and t.mac = hf.mac 
 | 
        </if> 
 | 
        <if test="endHourlyTime ==null"> 
 | 
            left join history_five_minutely_${times} hf on hf.time = (select max(time) from history_five_minutely_${times} LIMIT 1 
 | 
            ) and t.mac = hf.mac 
 | 
        </if> 
 | 
  
 | 
  
 | 
        WHERE 
 | 
            t.state <![CDATA[ <> ]]> '0'  and t.is_delete = 0 
 | 
        order by t.id desc 
 | 
    </select> 
 | 
  
 | 
    <select id="deviceIdList" resultType="java.lang.Integer"> 
 | 
        SELECT 
 | 
            t.id 
 | 
        FROM 
 | 
            device t 
 | 
                INNER JOIN monitor_point p ON p.id = t.monitor_point_id 
 | 
                AND p.is_delete = 0 
 | 
                AND p.organization_id IN ( 
 | 
                    SELECT 
 | 
                        id 
 | 
                    FROM 
 | 
                        ( 
 | 
                            SELECT 
 | 
                                id, 
 | 
                                parent_id, 
 | 
                                NAME 
 | 
                            FROM 
 | 
                                organization 
 | 
                            WHERE 
 | 
                                id = #{organizationId} UNION ALL 
 | 
                            SELECT 
 | 
                                i.id, 
 | 
                                i.parent_id, 
 | 
                                i.NAME 
 | 
                            FROM 
 | 
                                organization i 
 | 
                                    INNER JOIN ( 
 | 
                                    SELECT 
 | 
                                        * 
 | 
                                    FROM 
 | 
                                        ( 
 | 
                                            SELECT 
 | 
                                                id, 
 | 
                                                parent_id, 
 | 
                                                NAME 
 | 
                                            FROM 
 | 
                                                organization 
 | 
                                            WHERE 
 | 
                                                id = #{organizationId} UNION ALL 
 | 
                                            SELECT 
 | 
                                                id, 
 | 
                                                parent_id, 
 | 
                                                NAME 
 | 
                                            FROM 
 | 
                                                organization 
 | 
                                            WHERE 
 | 
                                                parent_id = #{organizationId} 
 | 
                                        ) t1 
 | 
                                ) t2 ON i.parent_id = t2.id 
 | 
                            WHERE 
 | 
                                i.is_delete = 0 
 | 
                        ) t3 
 | 
                ) 
 | 
        WHERE 
 | 
            t.is_delete = 0 
 | 
    </select> 
 | 
  
 | 
    <select id="deviceOrgIdList" resultType="java.lang.Integer"> 
 | 
        SELECT 
 | 
            id 
 | 
        FROM 
 | 
            ( 
 | 
                SELECT 
 | 
                    id, 
 | 
                    parent_id, 
 | 
                    NAME 
 | 
                FROM 
 | 
                    organization 
 | 
                WHERE 
 | 
                    id = #{organizationId} UNION ALL 
 | 
                SELECT 
 | 
                    i.id, 
 | 
                    i.parent_id, 
 | 
                    i.NAME 
 | 
                FROM 
 | 
                    organization i 
 | 
                        INNER JOIN ( 
 | 
                        SELECT 
 | 
                            * 
 | 
                        FROM 
 | 
                            ( 
 | 
                                SELECT 
 | 
                                    id, 
 | 
                                    parent_id, 
 | 
                                    NAME 
 | 
                                FROM 
 | 
                                    organization 
 | 
                                WHERE 
 | 
                                    id = #{organizationId} UNION ALL 
 | 
                                SELECT 
 | 
                                    id, 
 | 
                                    parent_id, 
 | 
                                    NAME 
 | 
                                FROM 
 | 
                                    organization 
 | 
                                WHERE 
 | 
                                    parent_id = #{organizationId} 
 | 
                            ) t1 
 | 
                    ) t2 ON i.parent_id = t2.id 
 | 
                WHERE 
 | 
                    i.is_delete = 0 
 | 
            ) t3 
 | 
    </select> 
 | 
</mapper> 
 |