<?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.IsIsolationPointMapper">

    <resultMap type="IsIsolationPoint" id="IsIsolationPointResult">
        <result property="pointId"    column="point_id"    />
        <result property="pointCode"    column="point_code"    />
        <result property="pointName"    column="point_name"    />
        <result property="pointType"    column="point_type"    />
        <result property="workshopId"    column="workshop_id"    />
        <result property="workareaId"    column="workarea_id"    />
        <result property="powerType"    column="power_type"    />
        <result property="isolationMethod"    column="isolation_method"    />
        <result property="pointIcon"    column="point_icon"    />
        <result property="pointPicture"    column="point_picture"    />
        <result property="delFlag"    column="del_flag"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="lockTypeId"    column="lock_type_id"    />
        <result property="rfidId"    column="rfid_id"    />
        <result property="locksetTypeId"    column="lockset_type_id"    />
        <result property="workstationId"    column="workstation_id"    />
        <result property="lotoId"    column="loto_id"    />
        <result property="remark"    column="remark"    />
        <result property="pointSerialNumber"    column="point_serial_number"    />
    </resultMap>

    <sql id="selectIsIsolationPointVo">
        select p.*, r.rfid as point_nfc from is_isolation_point
                              left join is_rfid_token r on r.rfid_id = p.rfid_id

    </sql>

    <select id="selectIsIsolationPointList" parameterType="IsIsolationPoint" resultMap="IsIsolationPointResult">
        <include refid="selectIsIsolationPointVo"/>
        <where>
            <if test="pointCode != null  and pointCode != ''"> and p.point_code = #{pointCode}</if>
            <if test="pointName != null  and pointName != ''"> and p.point_name like concat('%', #{pointName}, '%')</if>
            <if test="pointType != null  and pointType != ''"> and p.point_type = #{pointType}</if>
            <if test="workshopId != null "> and p.workshop_id = #{workshopId}</if>
            <if test="workareaId != null "> and p.workarea_id = #{workareaId}</if>
            <if test="powerType != null  and powerType != ''"> and p.power_type = #{powerType}</if>
            <if test="isolationMethod != null  and isolationMethod != ''"> and p.isolation_method = #{isolationMethod}</if>
            <if test="pointIcon != null  and pointIcon != ''"> and p.point_icon = #{pointIcon}</if>
            <if test="pointPicture != null  and pointPicture != ''"> and p.point_picture = #{pointPicture}</if>
        </where>
    </select>



    <insert id="insertIsIsolationPoint" parameterType="IsIsolationPoint" useGeneratedKeys="true" keyProperty="pointId">
        insert into is_isolation_point
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="pointCode != null and pointCode != ''">point_code,</if>
            <if test="pointName != null and pointName != ''">point_name,</if>
            <if test="pointType != null">point_type,</if>
            <if test="workshopId != null">workshop_id,</if>
            <if test="workareaId != null">workarea_id,</if>
            <if test="powerType != null">power_type,</if>
            <if test="isolationMethod != null">isolation_method,</if>
            <if test="pointIcon != null">point_icon,</if>
            <if test="pointPicture != null">point_picture,</if>
            <if test="delFlag != null">del_flag,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="lockTypeId != null">lock_type_id,</if>
            <if test="rfidId != null">rfid_id,</if>
            <if test="locksetTypeId != null">lockset_type_id,</if>
            <if test="workstationId != null">workstation_id,</if>
            <if test="lotoId != null">loto_id,</if>
            <if test="switchMapId != null">switch_map_id,</if>
            <if test="remark != null and remark != ''">remark,</if>
            <if test="pointSerialNumber != null and pointSerialNumber != ''">point_serial_number,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="pointCode != null and pointCode != ''">#{pointCode},</if>
            <if test="pointName != null and pointName != ''">#{pointName},</if>
            <if test="pointType != null">#{pointType},</if>
            <if test="workshopId != null">#{workshopId},</if>
            <if test="workareaId != null">#{workareaId},</if>
            <if test="powerType != null">#{powerType},</if>
            <if test="isolationMethod != null">#{isolationMethod},</if>
            <if test="pointIcon != null">#{pointIcon},</if>
            <if test="pointPicture != null">#{pointPicture},</if>
            <if test="delFlag != null">#{delFlag},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="lockTypeId != null">#{lockTypeId},</if>
            <if test="rfidId != null and rfidId != ''">#{rfidId},</if>
            <if test="locksetTypeId != null">#{locksetTypeId},</if>
            <if test="workstationId != null">#{workstationId},</if>
            <if test="lotoId != null">#{lotoId},</if>
            <if test="switchMapId != null">#{switchMapId},</if>
            <if test="remark != null and remark != ''">#{remark},</if>
            <if test="pointSerialNumber != null and pointSerialNumber != ''">#{pointSerialNumber},</if>
         </trim>
    </insert>

    <update id="updateIsIsolationPoint" parameterType="IsIsolationPoint">
        update is_isolation_point
        <trim prefix="SET" suffixOverrides=",">
            <if test="pointCode != null and pointCode != ''">point_code = #{pointCode},</if>
            <if test="pointName != null and pointName != ''">point_name = #{pointName},</if>
            <if test="pointType != null">point_type = #{pointType},</if>
            <if test="workshopId != null">workshop_id = #{workshopId},</if>
            <if test="workareaId != null">workarea_id = #{workareaId},</if>
            <if test="powerType != null">power_type = #{powerType},</if>
            <if test="isolationMethod != null">isolation_method = #{isolationMethod},</if>
            <if test="pointIcon != null">point_icon = #{pointIcon},</if>
            <if test="pointPicture != null">point_picture = #{pointPicture},</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="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="lockTypeId != null">lock_type_id = #{lockTypeId},</if>
            <if test="rfidId != null and rfidId != ''">rfid_id = #{rfidId},</if>
            <if test="locksetTypeId != null">lockset_type_id = #{locksetTypeId},</if>
            <if test="workstationId != null">workstation_id = #{workstationId},</if>
            <if test="lotoId != null">loto_id = #{lotoId},</if>
            <if test="switchMapId != null">switch_map_id = #{switchMapId},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="pointSerialNumber != null and pointSerialNumber != ''">point_serial_number = #{pointSerialNumber},</if>
        </trim>
        where point_id = #{pointId}
    </update>

    <delete id="deleteIsIsolationPointByPointId" parameterType="Long">
        delete from is_isolation_point where point_id = #{pointId}
    </delete>

    <delete id="deleteIsIsolationPointByPointIds" parameterType="String">
        delete from is_isolation_point where point_id in
        <foreach item="pointId" collection="array" open="(" separator="," close=")">
            #{pointId}
        </foreach>
    </delete>

    <select id="getPointDetailList" resultType="com.ktg.iscs.domain.vo.points.PointDetailVO">
        select p.*,
               d.dict_label as powerTypeName,
               dd.dict_label as pointTypeName,
               r.rfid as point_nfc
        from is_isolation_point p
        left join sys_dict_data d on d.dict_type = 'power_type' and p.power_type = d.dict_value
        left join sys_dict_data dd on dd.dict_type = 'point_type' and p.point_type = dd.dict_value
        left join is_rfid_token r on r.rfid_id = p.rfid_id
        <where>
            p.point_id in
            <foreach collection="pointIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>
    <select id="selectIsIsolationPointByPointId" resultType="com.ktg.iscs.domain.vo.points.PointDetailVO">
        SELECT
            p.*,
            wa.workstation_name,
            ls.loto_name,
            l.lock_type_name,
            l.lock_type_code,
            l.lock_type_icon,
            l.lock_type_img,
            r.rfid as point_nfc,
            lsm.switch_map_name
        FROM
            is_isolation_point p
                LEFT JOIN is_workstation wa ON wa.workstation_id = p.workstation_id
                LEFT JOIN is_loto_station ls ON ls.loto_id = p.loto_id
                LEFT JOIN is_loto_switch_map lsm ON lsm.switch_map_id = p.switch_map_id
                LEFT JOIN is_lock_type l ON l.lock_type_id = p.lock_type_id
                LEFT JOIN is_rfid_token r on r.rfid_id = p.rfid_id
        WHERE
            p.point_id = #{pointId}
    </select>
    <select id="getIsIsolationPointPage" resultType="com.ktg.iscs.domain.vo.points.PointPageVO">
        select t.* from (
        SELECT
        max(p.point_id) AS point_id,
        max(p.point_code) AS point_code,
        max(p.point_name) AS point_name,
        max(p.point_type) AS point_type,
        max(p.workshop_id) AS workshop_id,
        max(p.workarea_id) AS workarea_id,
        max(p.power_type) AS power_type,
        max(p.isolation_method) AS isolation_method,
        max(p.point_icon) AS point_icon,
        max(p.point_picture) AS point_picture,
        max(p.del_flag) AS del_flag,
        max(p.create_by) AS create_by,
        max(p.create_time) AS create_time,
        max(p.update_time) AS update_time,
        max(p.update_by) AS update_by,
        max(p.lock_type_id) AS lock_type_id,
        max(p.rfid_id) AS rfid_id,
        max(p.lockset_type_id) AS lockset_type_id,
        max(p.workstation_id) AS workstation_id,
        max(p.loto_id) AS loto_id,
        max(p.remark) AS remark,
        max(p.point_serial_number) AS point_serial_number,
        max(l.lock_type_name) AS lock_type_name,
        max(l.lock_type_code) AS lock_type_code,
        max(l.lock_type_icon) AS lock_type_icon,
        max(l.lock_type_img) AS lock_type_img,
        max(t.lockset_type_img) AS lockset_type_img,
        max(s.loto_name) AS loto_name,
        max(w.workstation_name) AS workstation_name,
        STRING_AGG(ma.machinery_name, ', ') AS machinery_name,
        max(r.rfid) as point_nfc,
        MAX(p.switch_map_id) AS switch_map_id,
        MAX(lsm.switch_map_name) AS switch_map_name
        FROM is_isolation_point p
        LEFT JOIN is_lock_type l ON l.lock_type_id = p.lock_type_id
        LEFT JOIN is_lockset_type t ON t.lockset_type_id = p.lockset_type_id
        LEFT JOIN is_workstation w ON w.workstation_id = p.workstation_id
        LEFT JOIN is_loto_station s ON s.loto_id = p.loto_id
        LEFT JOIN is_loto_switch_map lsm ON lsm.switch_map_id = p.switch_map_id
        LEFT JOIN is_machinery_points m ON m.point_id = p.point_id
        LEFT JOIN is_machinery ma ON ma.machinery_id = m.machinery_id
        LEFT JOIN is_rfid_token r on r.rfid_id = p.rfid_id
        <where>
            <if test="dto.pointCode != null and dto.pointCode.trim != ''">
                and p.point_code like concat('%',#{dto.pointCode},'%')
            </if>
            <if test="dto.pointName != null and dto.pointName.trim != ''">
                and p.point_name like concat('%',#{dto.pointName},'%')
            </if>
            <if test="dto.delFlag != null and dto.delFlag.trim != ''">
                and p.del_flag = #{dto.delFlag}
            </if>
            <if test="dto.pointType != null and dto.pointType.trim != ''">
                and p.point_type = #{dto.pointType}
            </if>
            <if test="dto.powerType != null and dto.powerType.trim != ''">
                and p.power_type = #{dto.powerType}
            </if>
            <if test="dto.startTime != null and dto.startTime.trim != ''">
                and p.create_time &gt;= #{dto.startTime}
            </if>
            <if test="dto.endTime != null and dto.endTime.trim != ''">
                and p.create_time &lt;= #{dto.endTime}
            </if>
            <if test="dto.lockTypeId != null">
                and p.lock_type_id = #{dto.lockTypeId}
            </if>
            <if test="dto.workstationId != null">
                and p.workstation_id = #{dto.workstationId}
            </if>
            <if test="dto.machineryId != null">
                and m.machinery_id = #{dto.machineryId}
            </if>
            <if test="dto.lotoId != null and dto.lotoId != 0">
                and p.loto_id = #{dto.lotoId}
            </if>
            <if test="dto.lotoId != null and dto.lotoId == 0">
                and p.loto_id is null
            </if>
            <if test="dto.switchMapId != null and dto.switchMapId != 0">
                and p.switch_map_id = #{dto.switchMapId}
            </if>
            <if test="dto.switchMapId != null and dto.switchMapId == 0">
                and p.switch_map_id is null
            </if>
        </where>
        group by p.point_id) t
        order by t.point_id desc
    </select>
    <select id="getOneByNfc" resultType="com.ktg.iscs.domain.IsIsolationPoint">
        select p.*,
               r.rfid as point_nfc
        from is_isolation_point p
                 LEFT JOIN is_rfid_token r on r.rfid_id = p.rfid_id
        where r.rfid = #{nfc}
    </select>
    <select id="getIsIsolationPointList" resultType="com.ktg.iscs.domain.IsIsolationPoint">
        SELECT
        p.*,
        r.rfid as point_nfc
        FROM is_isolation_point p
        LEFT JOIN is_rfid_token r on r.rfid_id = p.rfid_id
        <where>
            <if test="workshopId != null">
                and p.workshop_id = #{workshopId}
            </if>
            <if test="workareaId != null">
                and p.workarea_id = #{workareaId}
            </if>
        </where>
        order by point_ id desc
    </select>
</mapper>
