<?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.inspect.metadata.prodevmnt.mapper.BasedataDeviceMapper">

    <resultMap type="BasedataDevice" id="BasedataDeviceResult">
        <result property="deviceId" column="device_id"/>
        <result property="deviceCode" column="device_code"/>
        <result property="deviceName" column="device_name"/>
        <result property="manufacturer" column="manufacturer"/>
        <result property="parentId" column="parent_id"/>
        <result property="ancestors" column="ancestors"/>
        <result property="areaId" column="area_id"/>
        <result property="relationAreaId" column="relation_area_id"/>
        <result property="areaName" column="area_name"/>
        <result property="deviceTypeId" column="device_type_id"/>
        <result property="deviceTypeName" column="device_type_name"/>
        <result property="powerMainEquFlag" column="power_main_equ_flag"/>
        <result property="deviceModel" column="device_model"/>
        <result property="phyAssetId" column="phy_asset_id"/>
        <result property="stationNo" column="station_no"/>
        <result property="monitorIndexNo" column="monitor_index_no"/>
        <result property="phaseNum" column="phase_num"/>
        <result property="phase" column="phase"/>
        <result property="status" column="status"/>
        <result property="positionX" column="position_x"/>
        <result property="positionY" column="position_y"/>
        <result property="positionZ" column="position_z"/>
        <result property="deviceLength" column="device_length"/>
        <result property="deviceWidth" column="device_width"/>
        <result property="deviceHeight" column="device_height"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="installPosition" column="install_position"/>
        <result property="orderNum" column="order_num"/>
        <result property="oldDeviceId" column="old_device_id"/>
        <result property="oldParentId" column="old_parent_id"/>
        <result property="oldAreaId" column="old_area_id"/>
        <result property="oldAreaName" column="old_area_name"/>
        <result property="patrolPointId" column="patrol_point_id"/>
        <result property="patrolPointName" column="patrol_point_name"/>
    </resultMap>

    <sql id="selectBasedataDeviceVo">
        select device_id,
               device_code,
               device_name,
               manufacturer,
               parent_id,
               ancestors,
               area_id,
               area_name,
               device_type_id,
               device_type_name,
               power_main_equ_flag,
               device_model,
               phy_asset_id,
               station_no,
               monitor_index_no,
               phase_num,
               phase,
               status,
               position_x,
               position_y,
               position_z,
               device_length,
               device_width,
               device_height,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               order_num,
               old_device_id,
               old_parent_id,
               old_area_id,
               old_area_name
        from basedata_device
    </sql>

    <select id="selectBasedataDeviceList" parameterType="BasedataDevice" resultMap="BasedataDeviceResult">
        <include refid="selectBasedataDeviceVo"/>
        <where>
            <if test="deviceId != null  and deviceId != ''">and device_id = #{deviceId}</if>
            <if test="deviceCode != null  and deviceCode != ''">and device_code = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="manufacturer != null  and manufacturer != ''">and manufacturer = #{manufacturer}</if>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="areaId != null  and areaId != ''">and area_id = #{areaId}</if>
            <if test="areaName != null  and areaName != ''">and area_name like concat('%', #{areaName}, '%')</if>
            <if test="deviceTypeId != null  and deviceTypeId != ''">and device_type_id = #{deviceTypeId}</if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and device_type_name like concat('%',
                #{deviceTypeName}, '%')
            </if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
            <if test="deviceModel != null  and deviceModel != ''">and device_model = #{deviceModel}</if>
            <if test="phyAssetId != null  and phyAssetId != ''">and phy_asset_id = #{phyAssetId}</if>
            <if test="stationNo != null  and stationNo != ''">and station_no = #{stationNo}</if>
            <if test="monitorIndexNo != null  and monitorIndexNo != ''">and monitor_index_no = #{monitorIndexNo}</if>
            <if test="phaseNum != null  and phaseNum != ''">and phase_num = #{phaseNum}</if>
            <if test="phase != null  and phase != ''">and phase = #{phase}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="positionX != null  and positionX != ''">and position_x = #{positionX}</if>
            <if test="positionY != null  and positionY != ''">and position_y = #{positionY}</if>
            <if test="positionZ != null  and positionZ != ''">and position_z = #{positionZ}</if>
            <if test="deviceLength != null  and deviceLength != ''">and device_length = #{deviceLength}</if>
            <if test="deviceWidth != null  and deviceWidth != ''">and device_width = #{deviceWidth}</if>
            <if test="deviceHeight != null  and deviceHeight != ''">and device_height = #{deviceHeight}</if>
            <if test="installPosition != null  and installPosition != ''">and install_position = #{installPosition}</if>
            <if test="orderNum != null  and orderNum != ''">and order_num = #{orderNum}</if>
            <if test="oldDeviceId != null  and oldDeviceId != ''">and old_device_id = #{oldDeviceId}</if>
            <if test="deviceIdAll != null  and deviceIdAll != ''">
                and device_id in
                <foreach item="algId" collection="deviceIdListAll" open="(" separator="," close=")">
                    #{algId}
                </foreach>

            </if>
            <!-- 区域数组查询 -->
            <if test="params != null">
                <if test="params.areaIdList != null">
                    and area_id in
                    <foreach item="item" collection="params.areaIdList" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>

    <select id="selectBasedataDeviceRelationList" parameterType="BasedataDevice" resultMap="BasedataDeviceResult">
        select device_id,
        device_code,
        device_name,
        manufacturer,
        parent_id,
        ancestors,
        area_id,
        relation_area_id,
        area_name,
        device_type_id,
        device_type_name,
        power_main_equ_flag,
        device_model,
        phy_asset_id,
        station_no,
        monitor_index_no,
        phase_num,
        phase,
        status,
        position_x,
        position_y,
        position_z,
        device_length,
        device_width,
        device_height,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        order_num,
        old_device_id,
        old_parent_id,
        old_area_id,
        old_area_name
        from basedata_device
        <where>
            <if test="deviceId != null  and deviceId != ''">and device_id = #{deviceId}</if>
            <if test="deviceCode != null  and deviceCode != ''">and device_code = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="manufacturer != null  and manufacturer != ''">and manufacturer = #{manufacturer}</if>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="areaId != null  and areaId != ''">and area_id = #{areaId}</if>
            <if test="areaName != null  and areaName != ''">and area_name like concat('%', #{areaName}, '%')</if>
            <if test="deviceTypeId != null  and deviceTypeId != ''">and device_type_id = #{deviceTypeId}</if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and device_type_name like concat('%',
                #{deviceTypeName}, '%')
            </if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
            <if test="deviceModel != null  and deviceModel != ''">and device_model = #{deviceModel}</if>
            <if test="phyAssetId != null  and phyAssetId != ''">and phy_asset_id = #{phyAssetId}</if>
            <if test="stationNo != null  and stationNo != ''">and station_no = #{stationNo}</if>
            <if test="monitorIndexNo != null  and monitorIndexNo != ''">and monitor_index_no = #{monitorIndexNo}</if>
            <if test="phaseNum != null  and phaseNum != ''">and phase_num = #{phaseNum}</if>
            <if test="phase != null  and phase != ''">and phase = #{phase}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="positionX != null  and positionX != ''">and position_x = #{positionX}</if>
            <if test="positionY != null  and positionY != ''">and position_y = #{positionY}</if>
            <if test="positionZ != null  and positionZ != ''">and position_z = #{positionZ}</if>
            <if test="deviceLength != null  and deviceLength != ''">and device_length = #{deviceLength}</if>
            <if test="deviceWidth != null  and deviceWidth != ''">and device_width = #{deviceWidth}</if>
            <if test="deviceHeight != null  and deviceHeight != ''">and device_height = #{deviceHeight}</if>
            <if test="installPosition != null  and installPosition != ''">and install_position = #{installPosition}</if>
            <if test="orderNum != null  and orderNum != ''">and order_num = #{orderNum}</if>
            <if test="oldDeviceId != null  and oldDeviceId != ''">and old_device_id = #{oldDeviceId}</if>
            <if test="deviceIdAll != null  and deviceIdAll != ''">
                and device_id in
                <foreach item="algId" collection="deviceIdListAll" open="(" separator="," close=")">
                    #{algId}
                </foreach>

            </if>
            <!-- 区域数组查询 -->
            <if test="params != null">
                <if test="params.areaIdList != null">
                    and relation_area_id in
                    <foreach item="item" collection="params.areaIdList" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by order_num
    </select>

    <select id="selectBasedataDeviceByConditionList" parameterType="BasedataDevice" resultMap="BasedataDeviceResult">
        select device_id,
        device_code,
        device_name,
        manufacturer,
        parent_id,
        ancestors,
        area_id,
        area_name,
        device_type_id,
        device_type_name,
        power_main_equ_flag,
        device_model,
        phy_asset_id,
        station_no,
        monitor_index_no,
        phase_num,
        phase,
        status,
        position_x,
        position_y,
        position_z,
        device_length,
        device_width,
        device_height,
        del_flag,
        create_by,
        create_time,
        update_by,
        update_time,
        order_num,
        old_device_id,
        old_parent_id,
        old_area_id,
        old_area_name
        from basedata_device
        where
            device_id = #{deviceId} or parent_id = #{parentId}
    </select>

    <select id="selectBasedataDeviceListFilterByMeter" parameterType="BasedataDevice" resultMap="BasedataDeviceResult">
        select a.*,b.patrol_point_id as patrol_point_id,b.patrol_point_name as patrol_point_name
        from basedata_device a
--         left join basedata_patrolpoint b on (a.device_id=b.device_id or a.device_id=b.main_device_id) and b.alg_subtype_ids like concat('%', '52', '%')
        left join basedata_patrolpoint b on (a.device_id=b.device_id  and b.alg_subtype_ids like concat('%', '52', '%'))
        <where>
            <if test="deviceId != null  and deviceId != ''">and a.device_id = #{deviceId}</if>
            <if test="deviceCode != null  and deviceCode != ''">and a.device_code = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''">and a.device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="manufacturer != null  and manufacturer != ''">and a.manufacturer = #{manufacturer}</if>
            <if test="parentId != null ">and a.parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and a.ancestors = #{ancestors}</if>
            <if test="areaId != null  and areaId != ''">and a.area_id = #{areaId}</if>
            <if test="areaName != null  and areaName != ''">and a.area_name like concat('%', #{areaName}, '%')</if>
            <if test="deviceTypeId != null  and deviceTypeId != ''">and a.device_type_id = #{deviceTypeId}</if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and a.device_type_name like concat('%',
                #{deviceTypeName}, '%')
            </if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and a.power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
            <if test="deviceModel != null  and deviceModel != ''">and a.device_model = #{deviceModel}</if>
            <if test="phyAssetId != null  and phyAssetId != ''">and a.phy_asset_id = #{phyAssetId}</if>
            <if test="stationNo != null  and stationNo != ''">and a.station_no = #{stationNo}</if>
            <if test="monitorIndexNo != null  and monitorIndexNo != ''">and a.monitor_index_no = #{monitorIndexNo}</if>
            <if test="phaseNum != null  and phaseNum != ''">and a.phase_num = #{phaseNum}</if>
            <if test="phase != null  and phase != ''">and a.phase = #{phase}</if>
            <if test="status != null  and status != ''">and a.status = #{status}</if>
            <if test="positionX != null  and positionX != ''">and a.position_x = #{positionX}</if>
            <if test="positionY != null  and positionY != ''">and a.position_y = #{positionY}</if>
            <if test="positionZ != null  and positionZ != ''">and a.position_z = #{positionZ}</if>
            <if test="deviceLength != null  and deviceLength != ''">and a.device_length = #{deviceLength}</if>
            <if test="deviceWidth != null  and deviceWidth != ''">and a.device_width = #{deviceWidth}</if>
            <if test="deviceHeight != null  and deviceHeight != ''">and a.device_height = #{deviceHeight}</if>
            <if test="installPosition != null  and installPosition != ''">and a.install_position = #{installPosition}</if>
            <if test="orderNum != null  and orderNum != ''">and a.order_num = #{orderNum}</if>
            <if test="oldDeviceId != null  and oldDeviceId != ''">and a.old_device_id = #{oldDeviceId}</if>
            <if test="deviceIdAll != null  and deviceIdAll != ''">
                and a.device_id in
                <foreach item="algId" collection="deviceIdListAll" open="(" separator="," close=")">
                    #{algId}
                </foreach>

            </if>
            <!-- 区域数组查询 -->
            <if test="params != null">
                <if test="params.areaIdList != null">
                    and a.area_id in
                    <foreach item="item" collection="params.areaIdList" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>

    <select id="selectBasedataDeviceListWithPreset" parameterType="BasedataDevice" resultMap="BasedataDeviceResult">
        select a.*,b.patrol_point_id as patrol_point_id,b.patrol_point_name as patrol_point_name
        from basedata_device a
        left join basedata_patrolpoint b on a.device_id=b.device_id
        <where>
            <if test="deviceId != null  and deviceId != ''">and a.device_id = #{deviceId}</if>
            <if test="deviceCode != null  and deviceCode != ''">and a.device_code = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''">and a.device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="manufacturer != null  and manufacturer != ''">and a.manufacturer = #{manufacturer}</if>
            <if test="parentId != null ">and a.parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and a.ancestors = #{ancestors}</if>
            <if test="areaId != null  and areaId != ''">and a.area_id = #{areaId}</if>
            <if test="areaName != null  and areaName != ''">and a.area_name like concat('%', #{areaName}, '%')</if>
            <if test="deviceTypeId != null  and deviceTypeId != ''">and a.device_type_id = #{deviceTypeId}</if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and a.device_type_name like concat('%',
                #{deviceTypeName}, '%')
            </if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and a.power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
            <if test="deviceModel != null  and deviceModel != ''">and a.device_model = #{deviceModel}</if>
            <if test="phyAssetId != null  and phyAssetId != ''">and a.phy_asset_id = #{phyAssetId}</if>
            <if test="stationNo != null  and stationNo != ''">and a.station_no = #{stationNo}</if>
            <if test="monitorIndexNo != null  and monitorIndexNo != ''">and a.monitor_index_no = #{monitorIndexNo}</if>
            <if test="phaseNum != null  and phaseNum != ''">and a.phase_num = #{phaseNum}</if>
            <if test="phase != null  and phase != ''">and a.phase = #{phase}</if>
            <if test="status != null  and status != ''">and a.status = #{status}</if>
            <if test="positionX != null  and positionX != ''">and a.position_x = #{positionX}</if>
            <if test="positionY != null  and positionY != ''">and a.position_y = #{positionY}</if>
            <if test="positionZ != null  and positionZ != ''">and a.position_z = #{positionZ}</if>
            <if test="deviceLength != null  and deviceLength != ''">and a.device_length = #{deviceLength}</if>
            <if test="deviceWidth != null  and deviceWidth != ''">and a.device_width = #{deviceWidth}</if>
            <if test="deviceHeight != null  and deviceHeight != ''">and a.device_height = #{deviceHeight}</if>
            <if test="installPosition != null  and installPosition != ''">and a.install_position = #{installPosition}</if>
            <if test="orderNum != null  and orderNum != ''">and a.order_num = #{orderNum}</if>
            <if test="oldDeviceId != null  and oldDeviceId != ''">and a.old_device_id = #{oldDeviceId}</if>
            <if test="deviceIdAll != null  and deviceIdAll != ''">
                and a.device_id in
                <foreach item="algId" collection="deviceIdListAll" open="(" separator="," close=")">
                    #{algId}
                </foreach>

            </if>
            <!-- 区域数组查询 -->
            <if test="params != null">
                <if test="params.areaIdList != null">
                    and a.area_id in
                    <foreach item="item" collection="params.areaIdList" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>

    <resultMap type="TreeModule" id="TreeModuleResult">
        <result property="id" column="id"/>
        <result property="label" column="label"/>
        <result property="type" column="type"/>
        <result property="parentId" column="parent_id"/>
        <result property="areaId" column="area_id"/>
    </resultMap>
    <select id="selectAreaAndDeviceTree" parameterType="BasedataDevice" resultMap="TreeModuleResult">
        select
        device_id as id,
        device_name as label,
        <if test="deviceIdAll!=null and deviceIdAll =='0'.toString">
            "main" as type,
            area_id as area_id,
        </if>
        <if test="deviceIdAll!=null and deviceIdAll =='1'.toString">
            "dev" as type,
        </if>
        parent_id as parent_id


        from basedata_device
        <where>
            <if test="deviceId != null  and deviceId != ''">and device_id = #{deviceId}</if>
            <if test="deviceCode != null  and deviceCode != ''">and device_code = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
            <if test="manufacturer != null  and manufacturer != ''">and manufacturer = #{manufacturer}</if>
            <if test="parentId != null ">and parent_id = #{parentId}</if>
            <if test="ancestors != null  and ancestors != ''">and ancestors = #{ancestors}</if>
            <if test="areaId != null  and areaId != ''">and area_id = #{areaId}</if>
            <if test="areaName != null  and areaName != ''">and area_name like concat('%', #{areaName}, '%')</if>
            <if test="deviceTypeId != null  and deviceTypeId != ''">and device_type_id = #{deviceTypeId}</if>
            <if test="deviceTypeName != null  and deviceTypeName != ''">and device_type_name like concat('%',
                #{deviceTypeName}, '%')
            </if>
            <if test="powerMainEquFlag != null  and powerMainEquFlag != ''">and power_main_equ_flag =
                #{powerMainEquFlag}
            </if>
            <if test="deviceModel != null  and deviceModel != ''">and device_model = #{deviceModel}</if>
            <if test="phyAssetId != null  and phyAssetId != ''">and phy_asset_id = #{phyAssetId}</if>
            <if test="stationNo != null  and stationNo != ''">and station_no = #{stationNo}</if>
            <if test="monitorIndexNo != null  and monitorIndexNo != ''">and monitor_index_no = #{monitorIndexNo}</if>
            <if test="phaseNum != null  and phaseNum != ''">and phase_num = #{phaseNum}</if>
            <if test="phase != null  and phase != ''">and phase = #{phase}</if>
            <if test="status != null  and status != ''">and status = #{status}</if>
            <if test="positionX != null  and positionX != ''">and position_x = #{positionX}</if>
            <if test="positionY != null  and positionY != ''">and position_y = #{positionY}</if>
            <if test="positionZ != null  and positionZ != ''">and position_z = #{positionZ}</if>
            <if test="deviceLength != null  and deviceLength != ''">and device_length = #{deviceLength}</if>
            <if test="deviceWidth != null  and deviceWidth != ''">and device_width = #{deviceWidth}</if>
            <if test="deviceHeight != null  and deviceHeight != ''">and device_height = #{deviceHeight}</if>
            <if test="installPosition != null  and installPosition != ''">and install_position = #{installPosition}</if>
            <if test="orderNum != null  and orderNum != ''">and order_num = #{orderNum}</if>
        </where>
    </select>
    <select id="selectPointList" resultMap="TreeModuleResult">
        select patrol_point_id   as id,
               patrol_point_name as label,
               "point"          as type,
               device_id        as parent_id

        from basedata_patrolpoint
    </select>
    <select id="selectBasedataTreeByEqBookCode" parameterType="BasedataDevice" resultMap="BasedataDeviceResult">
        select a.* from basedata_device a
        inner join basedata_patrolpoint b on a.device_id=b.main_device_id and a.power_main_equ_flag='Yes'
        INNER JOIN basedata_patrolpoint_preset c on c.patrol_point_id=b.patrol_point_id
        INNER JOIN basedata_eqpbook d on c.eqp_book_id=d.eqp_book_id
        <where>
            <if test="eqBookCode != null  and eqBookCode != ''">and d.patrol_device_code = #{eqBookCode}</if>
            <if test="deviceIdAll != null  and deviceIdAll != ''">
                and a.device_id in
                <foreach item="algId" collection="deviceIdListAll" open="(" separator="," close=")">
                    #{algId}
                </foreach>

            </if>
        </where>
    </select>

    <select id="selectBasedataDeviceByDeviceId" parameterType="Long" resultMap="BasedataDeviceResult">
        <include refid="selectBasedataDeviceVo"/>
        where device_id = #{deviceId}
    </select>

    <select id="selectBasedataDeviceRelationByDeviceId" parameterType="Long" resultMap="BasedataDeviceResult">
        select device_id,
               device_code,
               device_name,
               manufacturer,
               parent_id,
               ancestors,
               area_id,
               relation_area_id,
               area_name,
               device_type_id,
               device_type_name,
               power_main_equ_flag,
               device_model,
               phy_asset_id,
               station_no,
               monitor_index_no,
               phase_num,
               phase,
               status,
               position_x,
               position_y,
               position_z,
               device_length,
               device_width,
               device_height,
               del_flag,
               create_by,
               create_time,
               update_by,
               update_time,
               order_num,
                   old_device_id,
               old_parent_id,
               old_area_id,
               old_area_name
        from basedata_device
        where device_id = #{deviceId}
    </select>

    <insert id="insertBasedataDevice" parameterType="BasedataDevice" useGeneratedKeys="true" keyProperty="deviceId">
        insert into basedata_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="manufacturer != null">manufacturer,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="areaId != null">area_id,</if>
            <if test="areaId != null">relation_area_id,</if>
            <if test="areaName != null">area_name,</if>
            <if test="deviceTypeId != null">device_type_id,</if>
            <if test="deviceTypeName != null">device_type_name,</if>
            <if test="powerMainEquFlag != null">power_main_equ_flag,</if>
            <if test="deviceModel != null">device_model,</if>
            <if test="phyAssetId != null">phy_asset_id,</if>
            <if test="stationNo != null">station_no,</if>
            <if test="monitorIndexNo != null">monitor_index_no,</if>
            <if test="phaseNum != null">phase_num,</if>
            <if test="phase != null">phase,</if>
            <if test="status != null">status,</if>
            <if test="positionX != null">position_x,</if>
            <if test="positionY != null">position_y,</if>
            <if test="positionZ != null">position_z,</if>
            <if test="deviceLength != null">device_length,</if>
            <if test="deviceWidth != null">device_width,</if>
            <if test="deviceHeight != null">device_height,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="installPosition != null">install_position,</if>
            <if test="orderNum != null">order_num,</if>
            <if test="oldDeviceId != null">old_device_id,</if>
            <if test="oldParentId != null">old_parent_id,</if>
            <if test="oldAreaId != null">old_area_id,</if>
            <if test="oldAreaName != null">old_area_name,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="manufacturer != null">#{manufacturer},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="areaName != null">#{areaName},</if>
            <if test="deviceTypeId != null">#{deviceTypeId},</if>
            <if test="deviceTypeName != null">#{deviceTypeName},</if>
            <if test="powerMainEquFlag != null">#{powerMainEquFlag},</if>
            <if test="deviceModel != null">#{deviceModel},</if>
            <if test="phyAssetId != null">#{phyAssetId},</if>
            <if test="stationNo != null">#{stationNo},</if>
            <if test="monitorIndexNo != null">#{monitorIndexNo},</if>
            <if test="phaseNum != null">#{phaseNum},</if>
            <if test="phase != null">#{phase},</if>
            <if test="status != null">#{status},</if>
            <if test="positionX != null">#{positionX},</if>
            <if test="positionY != null">#{positionY},</if>
            <if test="positionZ != null">#{positionZ},</if>
            <if test="deviceLength != null">#{deviceLength},</if>
            <if test="deviceWidth != null">#{deviceWidth},</if>
            <if test="deviceHeight != null">#{deviceHeight},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="installPosition != null">#{installPosition},</if>
            <if test="orderNum != null">#{orderNum},</if>
            <if test="oldDeviceId != null">#{oldDeviceId},</if>
            <if test="oldParentId != null">#{oldParentId},</if>
            <if test="oldAreaId != null">#{oldAreaId},</if>
            <if test="oldAreaName != null">#{oldAreaName},</if>
        </trim>
    </insert>

    <update id="updateBasedataDevice" parameterType="BasedataDevice">
        update basedata_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="manufacturer != null">manufacturer = #{manufacturer},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="relationAreaId != null">relation_area_id = #{relationAreaId},</if>
            <if test="areaName != null">area_name = #{areaName},</if>
            <if test="deviceTypeId != null">device_type_id = #{deviceTypeId},</if>
            <if test="deviceTypeName != null">device_type_name = #{deviceTypeName},</if>
            <if test="powerMainEquFlag != null">power_main_equ_flag = #{powerMainEquFlag},</if>
            <if test="deviceModel != null">device_model = #{deviceModel},</if>
            <if test="phyAssetId != null">phy_asset_id = #{phyAssetId},</if>
            <if test="stationNo != null">station_no = #{stationNo},</if>
            <if test="monitorIndexNo != null">monitor_index_no = #{monitorIndexNo},</if>
            <if test="phaseNum != null">phase_num = #{phaseNum},</if>
            <if test="phase != null">phase = #{phase},</if>
            <if test="status != null">status = #{status},</if>
            <if test="positionX != null">position_x = #{positionX},</if>
            <if test="positionY != null">position_y = #{positionY},</if>
            <if test="positionZ != null">position_z = #{positionZ},</if>
            <if test="deviceLength != null">device_length = #{deviceLength},</if>
            <if test="deviceWidth != null">device_width = #{deviceWidth},</if>
            <if test="deviceHeight != null">device_height = #{deviceHeight},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="installPosition != null">install_position = #{installPosition},</if>
            <if test="orderNum != null">order_num = #{orderNum},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <update id="updateBasedataDeviceRelation">
        update basedata_device set relation_area_id = #{toAreaId} where device_id = #{deviceId} or parent_id = #{deviceId}
    </update>


    <delete id="deleteBasedataDeviceByDeviceId" parameterType="Long">
        delete
        from basedata_device
        where device_id = #{deviceId}
    </delete>

    <delete id="deleteBasedataDeviceByDeviceIds" parameterType="Long">
        delete from basedata_device where device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>

    <delete id="deleteBySendCode">
        delete from basedata_device where old_device_id like concat(#{sendCode},'%')
    </delete>

    <select id="selectBasedataDeviceByPatrolPointId" parameterType="String" resultMap="BasedataDeviceResult">
        SELECT b.*
        FROM basedata_patrolpoint a,
             basedata_device b
        WHERE 1 = 1
          AND a.main_device_id = b.device_id
          AND a.patrol_point_id = #{patrolpointId}
    </select>

    <select id="selectBasedataDeviceMainEquBatchByAreas" parameterType="java.util.List"
            resultMap="BasedataDeviceResult">
        <include refid="selectBasedataDeviceVo"/>
        where power_main_equ_flag = 'Yes' and area_id in
        <foreach collection="areas" item="item" index="index"
                 separator="," open="(" close=")">
            #{item.areaId}
        </foreach>
    </select>

    <select id="getDeviceAllDeviceCode" parameterType="java.lang.String" resultType="java.lang.String">
        SELECT DISTINCT(b.patrol_device_code)
        FROM basedata_eqpbook b
        WHERE b.patrol_device_code != ''
    </select>

    <select id="checkDeviceNameUnique" resultMap="BasedataDeviceResult">
        <include refid="selectBasedataDeviceVo"/>
        <where>
            device_name = #{deviceName}
            <if test="deviceId != null">and device_id != #{deviceId}</if>
        </where>
    </select>

    <select id="checkDeviceCodeUnique" resultMap="BasedataDeviceResult">
        <include refid="selectBasedataDeviceVo"/>
        <where>device_code=#{deviceCode}
            <if test="deviceId != null">and device_id != #{deviceId}</if>
            limit 1
        </where>
    </select>

    <select id="checkDeviceNameIsExist" parameterType="String" resultType="java.util.List">
        SELECT
        b.device_name
        FROM
        basedata_device b
        <where>
            <if test="deviceId != null  and deviceId != ''">and device_id != #{deviceId}</if>
            <if test="deviceName != null  and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')
            </if>
        </where>
    </select>

    <select id="selectPatrolPointList" parameterType="Long" resultMap="BasedataDeviceResult">
        SELECT
        *
        FROM
        basedata_device a
        WHERE
        a.device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </select>
</mapper>
