<?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.ktg.iscs.mapper.IsMapPointMapper">

    <select id="getIsMapPointPage" resultType="com.ktg.iscs.domain.IsMapPoint">
        SELECT
            p.*,
            m.name as map_name,
            CASE
                WHEN p.map_type = '1' THEN
                    w.workstation_name
                WHEN p.map_type = '2' or p.map_type = '3' THEN
                    ip.point_name
                WHEN p.map_type = '4' THEN
                    mo.motor_name
                END AS entity_name
        FROM
            is_map_point p
                LEFT JOIN is_map m ON m.id = p.map_id
                LEFT JOIN is_workstation w ON w.workstation_id = p.entity_id
                LEFT JOIN is_isolation_point ip ON ip.point_id = p.entity_id
                LEFT JOIN is_motor mo ON mo.motor_id = p.entity_id
        <where>
            <if test="isMapPoint.mapId != null">
            and p.map_id = #{isMapPoint.mapId}
            </if>
            <if test="isMapPoint.mapName != null  and isMapPoint.mapName != ''">
                and m.name like concat('%',#{isMapPoint.mapName},'%')
            </if>
        </where>
        order by p.id desc
    </select>
    <select id="getIsMapPointList" resultType="com.ktg.iscs.domain.IsMapPoint">
        SELECT
        p.*,
        m.name as map_name,

        CASE
        WHEN p.map_type = '1' THEN
        w.workstation_name
        WHEN p.map_type = '2' or p.map_type = '3' THEN
        ip.point_name
        END AS entity_name,

        CASE
        WHEN p.map_type = '2' or p.map_type = '3' THEN
        ip.point_icon
        END AS point_icon,

        CASE
        WHEN p.map_type = '2' or p.map_type = '3' THEN
        ip.point_picture
        END AS point_picture,

        CASE
        WHEN p.map_type = '2' or p.map_type = '3' THEN
        r.rfid
        END AS point_nfc,

        CASE
        WHEN p.map_type = '2' or p.map_type = '3' THEN
        ip.point_serial_number
        END AS point_serial_number

        FROM
        is_map_point p
        LEFT JOIN is_map m ON m.id = p.map_id
        LEFT JOIN is_workstation w ON w.workstation_id = p.entity_id
        LEFT JOIN is_isolation_point ip ON ip.point_id = p.entity_id
        left join is_rfid_token r on r.rfid_id = ip.rfid_id
        <where>
            <if test="isMapPoint.mapId != null">
                and p.map_id = #{isMapPoint.mapId}
            </if>
        </where>
        order by p.id desc
    </select>
</mapper>
