<?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.xzzn.ems.mapper.EmsTicketMapper">

    <resultMap type="EmsTicket" id="EmsTicketResult">
        <result property="id"    column="id"    />
        <result property="ticketNo"    column="ticket_no"    />
        <result property="userId"    column="user_id"    />
        <result property="title"    column="title"    />
        <result property="content"    column="content"    />
        <result property="images"    column="images"    />
        <result property="status"    column="status"    />
        <result property="expectedCompleteTime"    column="expected_complete_time"    />
        <result property="completeTime"    column="complete_time"    />
        <result property="createTime"    column="create_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="workUserId"    column="work_user_id"    />
        <result property="isDelete"    column="isDelete"    />
    </resultMap>

    <sql id="selectEmsTicketVo">
        select id, ticket_no, user_id, title, content, images, status, expected_complete_time, complete_time, create_time, create_by, update_by, update_time, work_user_id, isDelete from ems_ticket
    </sql>

    <select id="selectEmsTicketList" parameterType="EmsTicket" resultMap="EmsTicketResult">
        <include refid="selectEmsTicketVo"/>
        <where>
            <if test="ticketNo != null  and ticketNo != ''"> and ticket_no = #{ticketNo}</if>
            <if test="userId != null  and userId != ''"> and user_id = #{userId}</if>
            <if test="title != null  and title != ''"> and title = #{title}</if>
            <if test="content != null  and content != ''"> and content = #{content}</if>
            <if test="images != null  and images != ''"> and images = #{images}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="expectedCompleteTime != null "> and expected_complete_time = #{expectedCompleteTime}</if>
            <if test="completeTime != null "> and complete_time = #{completeTime}</if>
            <if test="workUserId != null "> and work_user_id = #{workUserId}</if>
            <if test="isDelete != null "> and isDelete = #{isDelete}</if>
        </where>
    </select>

    <select id="selectEmsTicketById" parameterType="String" resultMap="EmsTicketResult">
        <include refid="selectEmsTicketVo"/>
        where id = #{id}
    </select>

    <insert id="insertEmsTicket" parameterType="EmsTicket" useGeneratedKeys="true" keyProperty="id">
        insert into ems_ticket
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="ticketNo != null and ticketNo != ''">ticket_no,</if>
            <if test="userId != null and userId != ''">user_id,</if>
            <if test="title != null and title != ''">title,</if>
            <if test="content != null and content != ''">content,</if>
            <if test="images != null">images,</if>
            <if test="status != null">status,</if>
            <if test="expectedCompleteTime != null">expected_complete_time,</if>
            <if test="completeTime != null">complete_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="workUserId != null">work_user_id,</if>
            <if test="isDelete != null">isDelete,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="ticketNo != null and ticketNo != ''">#{ticketNo},</if>
            <if test="userId != null and userId != ''">#{userId},</if>
            <if test="title != null and title != ''">#{title},</if>
            <if test="content != null and content != ''">#{content},</if>
            <if test="images != null">#{images},</if>
            <if test="status != null">#{status},</if>
            <if test="expectedCompleteTime != null">#{expectedCompleteTime},</if>
            <if test="completeTime != null">#{completeTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="workUserId != null">#{workUserId},</if>
            <if test="isDelete != null">#{isDelete},</if>
        </trim>
    </insert>

    <update id="updateEmsTicket" parameterType="EmsTicket">
        update ems_ticket
        <trim prefix="SET" suffixOverrides=",">
            <if test="ticketNo != null and ticketNo != ''">ticket_no = #{ticketNo},</if>
            <if test="userId != null and userId != ''">user_id = #{userId},</if>
            <if test="title != null and title != ''">title = #{title},</if>
            <if test="content != null and content != ''">content = #{content},</if>
            <if test="images != null">images = #{images},</if>
            <if test="status != null">status = #{status},</if>
            <if test="expectedCompleteTime != null">expected_complete_time = #{expectedCompleteTime},</if>
            <if test="completeTime != null">complete_time = #{completeTime},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="workUserId != null">work_user_id = #{workUserId},</if>
            <if test="isDelete != null">isDelete = #{isDelete},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteEmsTicketById" parameterType="String">
        delete from ems_ticket where id = #{id}
    </delete>

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

    <update id="dropEmsTicketById" parameterType="String">
        update ems_ticket set isDelete = 0 where id = #{id}
    </update>

    <select id="getAllTicketList" resultType="com.xzzn.ems.domain.vo.TicketListVo">
        SELECT t.ticket_no as ticketNo,
               t.title as title,
               t.content as content,
               t.`status` as status,
               s.user_name as userName,
               s1.user_name as workName,
               t.create_time as createTime,
               t.id,
               t.images,
               t.expected_complete_time as expectedCompleteTime
        FROM ems_ticket t
                 LEFT JOIN sys_user s on t.user_id = s.user_id
                 LEFT JOIN sys_user s1 on t.work_user_id = s1.user_id
        where isDelete = 1
        <if test="statusList != null and statusList.length > 0">
            AND t.status IN
            <foreach collection="statusList" item="status" open="(" separator="," close=")">
                #{status}
            </foreach>
        </if>
    </select>

    <select id="getTicketDetailInfo" resultType="com.xzzn.ems.domain.vo.TicketListVo">
        SELECT t.ticket_no as ticketNo,
               t.title as title,
               t.content as content,
               t.`status` as status,
               s.user_name as userName,
               s1.user_name as workName,
               t.create_time as createTime,
               t.complete_time as completeTime,
               t.id, t.images,
               t.user_id as userId, t.work_user_id as workUserId,
               t.expected_complete_time as expectedCompleteTime
        FROM ems_ticket t
                 LEFT JOIN sys_user s on t.user_id = s.user_id
                 LEFT JOIN sys_user s1 on t.work_user_id = s1.user_id
        where t.isDelete = 1
        and t.id = #{id}
    </select>
</mapper>