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

    <resultMap type="IsJobTicketLock" id="IsJobTicketLockResult">
        <result property="recordId"    column="record_id"    />
        <result property="ticketId"    column="ticket_id"    />
        <result property="lockId"    column="lock_id"    />
        <result property="fromHardwareId"    column="from_hardware_id"    />
        <result property="toHardwareId"    column="to_hardware_id"    />
        <result property="isolationPointId"    column="isolation_point_id"    />
        <result property="lockStatus"    column="lock_status"    />
        <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="remark"    column="remark"    />
    </resultMap>

    <sql id="selectIsJobTicketLockVo">
        select record_id, ticket_id, lock_id, from_hardware_id, to_hardware_id, isolation_point_id, lock_status, del_flag, create_by, create_time, update_time, update_by, remark from is_job_ticket_lock
    </sql>

    <select id="selectIsJobTicketLockList" parameterType="IsJobTicketLock" resultMap="IsJobTicketLockResult">
        <include refid="selectIsJobTicketLockVo"/>
        <where>
            <if test="ticketId != null "> and ticket_id = #{ticketId}</if>
            <if test="lockId != null "> and lock_id = #{lockId}</if>
            <if test="fromHardwareId != null "> and from_hardware_id = #{fromHardwareId}</if>
            <if test="toHardwareId != null "> and to_hardware_id = #{toHardwareId}</if>
            <if test="isolationPointId != null "> and isolation_point_id = #{isolationPointId}</if>
            <if test="lockStatus != null  and lockStatus != ''"> and lock_status = #{lockStatus}</if>
        </where>
    </select>

    <select id="selectIsJobTicketLockByRecordId" parameterType="Long" resultMap="IsJobTicketLockResult">
        <include refid="selectIsJobTicketLockVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertIsJobTicketLock" parameterType="IsJobTicketLock" useGeneratedKeys="true" keyProperty="recordId">
        insert into is_job_ticket_lock
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ticketId != null">ticket_id,</if>
            <if test="lockId != null">lock_id,</if>
            <if test="fromHardwareId != null">from_hardware_id,</if>
            <if test="toHardwareId != null">to_hardware_id,</if>
            <if test="isolationPointId != null">isolation_point_id,</if>
            <if test="lockStatus != null">lock_status,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ticketId != null">#{ticketId},</if>
            <if test="lockId != null">#{lockId},</if>
            <if test="fromHardwareId != null">#{fromHardwareId},</if>
            <if test="toHardwareId != null">#{toHardwareId},</if>
            <if test="isolationPointId != null">#{isolationPointId},</if>
            <if test="lockStatus != null">#{lockStatus},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateIsJobTicketLock" parameterType="IsJobTicketLock">
        update is_job_ticket_lock
        <trim prefix="SET" suffixOverrides=",">
            <if test="ticketId != null">ticket_id = #{ticketId},</if>
            <if test="lockId != null">lock_id = #{lockId},</if>
            <if test="fromHardwareId != null">from_hardware_id = #{fromHardwareId},</if>
            <if test="toHardwareId != null">to_hardware_id = #{toHardwareId},</if>
            <if test="isolationPointId != null">isolation_point_id = #{isolationPointId},</if>
            <if test="lockStatus != null">lock_status = #{lockStatus},</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="remark != null">remark = #{remark},</if>
        </trim>
        where record_id = #{recordId}
    </update>

    <delete id="deleteIsJobTicketLockByRecordId" parameterType="Long">
        delete from is_job_ticket_lock where record_id = #{recordId}
    </delete>

    <delete id="deleteIsJobTicketLockByRecordIds" parameterType="String">
        delete from is_job_ticket_lock where record_id in
        <foreach item="recordId" collection="array" open="(" separator="," close=")">
            #{recordId}
        </foreach>
    </delete>

    <select id="getTicketLockByTicketId" resultType="com.ktg.iscs.domain.vo.hardwareApi.JobTicketLockVO">
        select j.*, l.lock_nfc
        from is_job_ticket_lock j
            left join is_lock l on l.lock_id = j.lock_id
        where j.ticket_id = #{ticketId}
    </select>
    <select id="getLockJobByBfc" resultType="com.ktg.iscs.domain.IsJobTicketLock">
        SELECT tl.*
        FROM is_job_ticket_lock tl
                 LEFT JOIN is_lock l on l.lock_id = tl.lock_id
                 LEFT JOIN is_job_ticket j ON j.ticket_id = tl.ticket_id
        <where>
            j.ticket_status != 5
            AND j.ticket_status != 6
            AND l.lock_nfc = #{nfc}
            <if test="ticketId != null">
                AND tl.ticket_id = #{ticketId}
            </if>
        </where>
        order by tl.record_id desc
        offset 0 rows fetch next 1 rows only
    </select>
    <select id="getLockJob" resultType="com.ktg.iscs.domain.IsJobTicketLock">
        SELECT
        t.*
        FROM
        is_job_ticket_lock t left join is_job_ticket t1 on t1.ticket_id = t.ticket_id
        <where>
            t.lock_status != '5'
            and t1.ticket_status != '5'
            and t1.ticket_status != '6'
            <if test="lockId != null">
                AND t.lock_id = #{lockId}
            </if>
        </where>
    </select>
</mapper>
