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

    <resultMap type="com.ximai.mes.tm.domain.TmToolRequest" id="TmToolRequestResult">
        <result property="toolRequestId" column="tool_request_id"/>
        <result property="toolRequestCode" column="tool_request_code"/>
        <result property="processId" column="process_id"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="taskWorkunitId" column="task_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"/>
    </resultMap>

    <sql id="selectTmToolRequestVo">
        select ttr.tool_request_id,
               ttr.tool_request_code,
               ttr.process_id,
               ttr.workunit_id,
               ttr.task_workunit_id,
               ttr.estimated_life_time,
               ttr.request_date,
               ttr.start_time,
               ttr.end_time,
               ttr.request_num,
               ttr.arrange_code,
               ttr.create_by,
               ttr.create_time,
               ttr.update_by,
               ttr.update_time,
               ttr.arrange_code,
               pp.process_name,
               mw.workunit_name
        from tm_tool_request ttr
                 left join pro_process pp on pp.process_id = ttr.process_id
                 left join pro_work_order_process pwop on pwop.workorder_process_id = ttr.process_id
--                  left join pro_route_process prp on pwop.record_id = prp.record_id
                 left join md_workunit mw on ttr.workunit_id = mw.workunit_id
    </sql>

    <select id="selectTmToolRequestList" resultMap="TmToolRequestResult">
        <include refid="selectTmToolRequestVo"/>
        <where>
            <if test="toolRequestCode != null  and toolRequestCode != ''">and ttr.tool_request_code =
                #{toolRequestCode}
            </if>
            <if test="processId != null ">and ttr.process_id = #{processId}</if>
            <if test="workunitId != null ">and ttr.workunit_id = #{workunitId}</if>
            <if test="workunitName != null  and workunitName != ''">and mw.workunit_name like concat('%',
                #{workunitName}, '%')
            </if>
            <if test="requestDate != null ">and ttr.request_date = #{requestDate}</if>
            <if test="startTime != null ">and ttr.start_time = #{startTime}</if>
            <if test="endTime != null ">and ttr.end_time = #{endTime}</if>
            <if test="requestNum != null ">and ttr.request_num = #{requestNum}</if>
            <if test="arrangeCode != null  and arrangeCode != ''">and ttr.arrange_code = #{arrangeCode}</if>
        </where>
        order by create_time
    </select>

    <select id="selectTmToolRequestByToolRequestId" parameterType="Long" resultMap="TmToolRequestResult">
        <include refid="selectTmToolRequestVo"/>
        where ttr.tool_request_id = #{toolRequestId}
    </select>

    <insert id="insertTmToolRequest" parameterType="TmToolRequest" useGeneratedKeys="true" keyProperty="toolRequestId">
        insert into tm_tool_request
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolRequestCode != null">tool_request_code,</if>
            <if test="processId != null">process_id,</if>
            <if test="estimatedLifeTime != null">estimated_life_time,</if>
            <if test="workunitId != null">workunit_id,</if>
            <if test="taskWorkunitId != null">task_workunit_id,</if>
            <if test="taskId != null">task_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="toolRequestCode != null">#{toolRequestCode},</if>
            <if test="processId != null">#{processId},</if>
            <if test="estimatedLifeTime != null">#{estimatedLifeTime},</if>
            <if test="workunitId != null">#{workunitId},</if>
            <if test="taskWorkunitId != null">#{taskWorkunitId},</if>
            <if test="taskId != null">#{taskId},</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="updateTmToolRequest" parameterType="TmToolRequest">
        update tm_tool_request
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolRequestCode != null">tool_request_code = #{toolRequestCode},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="workunitId != null">workunit_id = #{workunitId},</if>
            <if test="taskWorkunitId != null">task_workunit_id = #{taskWorkunitId},</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="estimatedLifeTime != null">estimated_life_time=#{estimatedLifeTime},</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_id = #{toolRequestId}
    </update>

    <delete id="deleteTmToolRequestByToolRequestId" parameterType="Long">
        delete
        from tm_tool_request
        where tool_request_id = #{toolRequestId}
    </delete>

    <delete id="deleteTmToolRequestByToolRequestIds" parameterType="String">
        delete from tm_tool_request where tool_request_id in
        <foreach item="toolRequestId" collection="array" open="(" separator="," close=")">
            #{toolRequestId}
        </foreach>
    </delete>
</mapper>
