<?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.ximai.mes.tm.mapper.TmToolRequestUseMapper">

    <resultMap type="TmToolRequestUse" id="TmToolRequestUseResult">
        <result property="toolRequestUseId"    column="tool_request_use_id"    />
        <result property="toolRequestUseCode"    column="tool_request_use_code"    />
        <result property="toolRequestId"    column="tool_request_id"    />
        <result property="processId"    column="process_id"    />
        <result property="workunitId"    column="workunit_id"    />
        <result property="requestDate"    column="request_date"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="requestNum"    column="request_num"    />
        <result property="arrangeCode"    column="arrange_code"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="processName"    column="process_name"    />
        <result property="workunitName"    column="workunit_name"    />
        <result property="toolRequestCode"    column="tool_request_code"    />
    </resultMap>

    <sql id="selectTmToolRequestUseVo">
        select
               ttru.tool_request_use_id,
               ttru.tool_request_use_code,
               ttru.tool_request_id,
               ttru.process_id,
               ttru.workunit_id,
               ttru.request_date,
               ttru.start_time,
               ttru.end_time,
               ttru.request_num,
               ttru.arrange_code,
               ttru.create_by,
               ttru.create_time,
               ttru.update_by,
               ttru.update_time,
               pp.process_name,
               mw.workunit_name,
               mw.workunit_code,
               ttr.tool_request_code
        from tm_tool_request_use ttru
              left join tm_tool_request ttr on ttru.tool_request_id = ttr.tool_request_id
              left join pro_process pp on ttru.process_id = pp.process_id
              left join md_workunit mw on ttru.workunit_id = mw.workunit_id
    </sql>

    <select id="selectTmToolRequestUseList" parameterType="TmToolRequestUse" resultMap="TmToolRequestUseResult">
        <include refid="selectTmToolRequestUseVo"/>
        <where>
            <if test="toolRequestUseCode != null  and toolRequestUseCode != ''"> and ttru.tool_request_use_code = #{toolRequestUseCode}</if>
            <if test="toolRequestId != null "> and ttru.tool_request_id = #{toolRequestId}</if>
            <if test="toolRequestUseId != null "> and ttru.tool_request_use_id = #{toolRequestUseId}</if>
            <if test="processId != null "> and ttru.process_id = #{processId}</if>
            <if test="workunitId != null "> and ttru.workunit_id = #{workunitId}</if>
            <if test="taskWorkunitId != null "> and ttr.task_workunit_id = #{taskWorkunitId}</if>
            <if test="requestDate != null "> and ttru.request_date = #{requestDate}</if>
            <if test="startTime != null "> and ttru.start_time = #{startTime}</if>
            <if test="endTime != null "> and ttru.end_time = #{endTime}</if>
            <if test="requestNum != null "> and ttru.request_num = #{requestNum}</if>
            <if test="arrangeCode != null  and arrangeCode != ''"> and ttru.arrange_code = #{arrangeCode}</if>
            <if test="workunitName != null  and workunitName != ''"> and mw.workunit_name like concat('%', #{workunitName}, '%')</if>
        </where>
    </select>

    <select id="selectTmToolRequestUseByToolRequestUseId" parameterType="Long" resultMap="TmToolRequestUseResult">
        <include refid="selectTmToolRequestUseVo"/>
        where ttru.tool_request_use_id = #{toolRequestUseId}
    </select>

    <insert id="insertTmToolRequestUse" parameterType="TmToolRequestUse" useGeneratedKeys="true" keyProperty="toolRequestUseId">
        insert into tm_tool_request_use
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolRequestUseCode != null">tool_request_use_code,</if>
            <if test="toolRequestId != null">tool_request_id,</if>
            <if test="processId != null">process_id,</if>
            <if test="workunitId != null">workunit_id,</if>
            <if test="requestDate != null">request_date,</if>
            <if test="startTime != null">start_time,</if>
            <if test="endTime != null">end_time,</if>
            <if test="requestNum != null">request_num,</if>
            <if test="arrangeCode != null">arrange_code,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="toolRequestUseCode != null">#{toolRequestUseCode},</if>
            <if test="toolRequestId != null">#{toolRequestId},</if>
            <if test="processId != null">#{processId},</if>
            <if test="workunitId != null">#{workunitId},</if>
            <if test="requestDate != null">#{requestDate},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="endTime != null">#{endTime},</if>
            <if test="requestNum != null">#{requestNum},</if>

            <if test="arrangeCode != null">#{arrangeCode},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateTmToolRequestUse" parameterType="TmToolRequestUse">
        update tm_tool_request_use
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolRequestUseCode != null">tool_request_use_code = #{toolRequestUseCode},</if>
            <if test="toolRequestId != null">tool_request_id = #{toolRequestId},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="workunitId != null">workunit_id = #{workunitId},</if>
            <if test="requestDate != null">request_date = #{requestDate},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="endTime != null">end_time = #{endTime},</if>
            <if test="requestNum != null">request_num = #{requestNum},</if>
            <if test="arrangeCode != null">arrange_code = #{arrangeCode},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where tool_request_use_id = #{toolRequestUseId}
    </update>

    <delete id="deleteTmToolRequestUseByToolRequestUseId" parameterType="Long">
        delete from tm_tool_request_use where tool_request_use_id = #{toolRequestUseId}
    </delete>

    <delete id="deleteTmToolRequestUseByToolRequestUseIds" parameterType="String">
        delete from tm_tool_request_use where tool_request_use_id in
        <foreach item="toolRequestUseId" collection="array" open="(" separator="," close=")">
            #{toolRequestUseId}
        </foreach>
    </delete>
</mapper>
