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

    <resultMap type="IsJobTicketUser" id="IsJobTicketUserResult">
        <result property="recordId"    column="record_id"    />
        <result property="ticketId"    column="ticket_id"    />
        <result property="userId"    column="user_id"    />
        <result property="userName"    column="user_name"    />
        <result property="userType"    column="user_type"    />
        <result property="userRole"    column="user_role"    />
        <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="jobStatus"    column="job_status"    />

    </resultMap>

    <sql id="selectIsJobTicketUserVo">
        select record_id, ticket_id, user_id, user_name, user_type, user_role, del_flag, create_by, create_time, update_time,
               update_by, job_status from is_job_ticket_user
    </sql>

    <select id="selectIsJobTicketUserList" parameterType="IsJobTicketUser" resultMap="IsJobTicketUserResult">
        <include refid="selectIsJobTicketUserVo"/>
        <where>
            <if test="ticketId != null "> and ticket_id = #{ticketId}</if>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="userName != null  and userName != ''"> and user_name like concat('%', #{userName}, '%')</if>
            <if test="userType != null  and userType != ''"> and user_type = #{userType}</if>
            <if test="userRole != null  and userRole != ''"> and user_role = #{userRole}</if>
        </where>
    </select>

    <select id="selectIsJobTicketUserByRecordId" parameterType="Long" resultMap="IsJobTicketUserResult">
        <include refid="selectIsJobTicketUserVo"/>
        where record_id = #{recordId}
    </select>

    <insert id="insertIsJobTicketUser" parameterType="IsJobTicketUser" useGeneratedKeys="true" keyProperty="recordId">
        insert into is_job_ticket_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ticketId != null">ticket_id,</if>
            <if test="userId != null">user_id,</if>
            <if test="userName != null">user_name,</if>
            <if test="userType != null">user_type,</if>
            <if test="userRole != null">user_role,</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="jobStatus != null">job_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ticketId != null">#{ticketId},</if>
            <if test="userId != null">#{userId},</if>
            <if test="userName != null">#{userName},</if>
            <if test="userType != null">#{userType},</if>
            <if test="userRole != null">#{userRole},</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="jobStatus != null">#{jobStatus},</if>
         </trim>
    </insert>

    <update id="updateIsJobTicketUser" parameterType="IsJobTicketUser">
        update is_job_ticket_user
        <trim prefix="SET" suffixOverrides=",">
            <if test="ticketId != null">ticket_id = #{ticketId},</if>
            <if test="userId != null">user_id = #{userId},</if>
            <if test="userName != null">user_name = #{userName},</if>
            <if test="userType != null">user_type = #{userType},</if>
            <if test="userRole != null">user_role = #{userRole},</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="jobStatus != null">job_status = #{jobStatus},</if>
        </trim>
        where record_id = #{recordId}
    </update>

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

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

    <select id="getMyJob" resultType="com.ktg.iscs.domain.IsJobTicketUser">
        SELECT
            u.*
        FROM
            is_job_ticket_user u
                LEFT JOIN is_job_ticket t ON t.ticket_id = u.user_id
        WHERE
            u.user_id = #{userId}
          AND u.user_role = 'jtlocker'
          AND t.ticket_status != '5'
          AND t.ticket_status != '6'
    </select>


</mapper>
