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

    <resultMap type="com.ximai.mes.tm.domain.TmToolMachinesRecord" id="TmToolMachinesRecordResult">
        <result property="toolMachinesRecordId" column="tool_machines_record_id"/>
        <result property="toolMachinesId" column="tool_machines_id"/>
        <result property="toolRequestUseItemId" column="tool_request_use_item_id"/>
        <result property="type" column="type"/>
        <result property="upDate" column="up_date"/>
        <result property="downDate" column="down_date"/>
        <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="itemName" column="item_name"/>
        <result property="toolName" column="tool_name"/>
        <result property="toolCode" column="tool_code"/>
        <result property="typeName" column="type_name"/>
        <result property="toolRequestUseId" column="tool_request_use_id"/>
        <result property="warehouse" column="warehouse"/>
        <result property="warehouseDesc" column="warehouse_desc"/>
    </resultMap>


    <resultMap type="com.ximai.mes.tm.domain.TmToolMachinesRecord" id="TmToolMachinesRecordVo">
        <result property="toolMachinesRecordId" column="tool_machines_record_id"/>
        <result property="toolMachinesId" column="tool_machines_id"/>
        <result property="toolMachinesCode" column="tool_machines_code"/>
        <result property="toolRequestUseItemId" column="tool_request_use_item_id"/>
        <result property="toolRequestCode" column="tool_request_code"/>
        <result property="type" column="type"/>
        <result property="upDate" column="up_date"/>
        <result property="downDate" column="down_date"/>
        <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="itemName" column="item_name"/>
        <result property="toolName" column="tool_name"/>
        <result property="toolCode" column="tool_code"/>
        <result property="typeName" column="type_name"/>
        <result property="toolRequestUseId" column="tool_request_use_id"/>
        <result property="toolRequestUseCode" column="tool_request_use_code"/>
        <result property="warehouse" column="warehouse"/>
        <result property="warehouseDesc" column="warehouse_desc"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="taskId" column="task_id"/>
        <result property="taskCode" column="task_code"/>
    </resultMap>


    <sql id="selectTmToolMachinesRecordVo">
        SELECT ttmr.tool_machines_record_id,
               ttm.tool_machines_code,
               ttm.tool_machines_id,
               ttr.tool_request_code,
               ttmr.type,
               ttmr.warehouse,
               ttmr.warehouse_desc,
               ttmr.up_date,
               ttmr.down_date,
               ttmr.create_by,
               ttmr.create_time,
               ttmr.update_by,
               ttmr.update_time,
               im.item_name,
               im.item_code,
               pt.task_id,
               pt.task_code,
               tt.tool_name,
               tt.tool_code,
               t1.workunit_code,
               t1.workunit_name,
               CASE
                   WHEN 1 = type THEN
                       '上机'
                   WHEN 3 = type THEN
                       '待上机'
                   ELSE '下机'
                   END AS type_name,
               ttru.tool_request_use_code
        FROM tm_tool_machines_record ttmr
                 LEFT JOIN tm_tool_machines ttm ON ttm.tool_machines_id = ttmr.tool_machines_id
                 LEFT JOIN md_workunit t1 ON t1.workunit_id = ttm.workunit_id
                 LEFT JOIN tm_tool_request_use_item ttrui
                           ON ttrui.tool_request_use_item_id = ttmr.tool_request_use_item_id
                 LEFT JOIN tm_tool_request_use ttru ON ttru.tool_request_use_id = ttrui.tool_request_use_id
                 LEFT JOIN tm_tool_request ttr ON ttr.tool_request_id = ttru.tool_request_id
                 LEFT JOIN pro_task pt ON pt.task_id = ttr.task_id
                 LEFT JOIN tm_tool tt ON tt.tool_id = ttrui.tool_id
                 LEFT JOIN md_item im ON im.item_id = ttrui.item_id
    </sql>

    <select id="selectTmToolMachinesRecordList" resultMap="TmToolMachinesRecordVo">
        <include refid="selectTmToolMachinesRecordVo"/>
        <where>
            <if test="toolMachinesId != null ">and ttmr.tool_machines_id = #{toolMachinesId}</if>
            <if test="toolRequestUseItemId != null ">and ttmr.tool_request_use_item_id = #{toolRequestUseItemId}</if>
            <if test="workunitId != null ">and ttmr.workunit_id = #{workunitId}</if>
            <if test="workunitName != null ">and ttmr.workunit_name = #{workunitName}</if>
            <if test="taskId != null ">and ttm.task_id = #{taskId}</if>
            <if test="taskCode != null ">and ttm.task_code = #{taskCode}</if>
            <if test="typeStr != null  and typeStr != ''">and ttmr.type IN
                <foreach item="item" collection="typeList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="warehouseStr != null  and warehouseStr != ''">and ttmr.warehouse IN
                <foreach item="item" collection="warehouseList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="upDate != null ">and ttmr.up_date >= #{upDate}</if>
            <if test="downDate != null ">and ttmr.down_date &lt;= #{downDate}</if>
        </where>
    </select>


    <select id="selectTmToolMachinesRecordByToolMachinesRecordId"
            resultMap="TmToolMachinesRecordResult">
        <include refid="selectTmToolMachinesRecordVo"/>
        <where>
            <if test="toolMachinesId != null ">ttmr.tool_machines_record_id = #{toolMachinesRecordId}</if>
            <if test="toolRequestUseItemId != null ">and ttmr.tool_request_use_item_id = #{toolRequestUseItemId}</if>
            <if test="workunitId != null ">and ttmr.workunit_id = #{workunitId}</if>
            <if test="workunitName != null ">and ttmr.workunit_name = #{workunitName}</if>
            <if test="taskId != null ">and ttr.task_id = #{taskId}</if>
            <if test="typeStr != null  and typeStr != ''">and ttmr.type IN
                <foreach item="item" collection="typeList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="warehouseStr != null  and warehouseStr != ''">and ttmr.warehouse IN
                <foreach item="item" collection="warehouseList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="upDate != null ">and ttmr.up_date >= #{upDate}</if>
            <if test="downDate != null ">and ttmr.down_date &lt;= #{downDate}</if>
        </where>
    </select>

    <select id="selectByToolMachinesRecordId" resultMap="TmToolMachinesRecordResult">
        select * from tm_tool_machines_record ttmr
        <where>
            <if test="toolMachinesRecordId != null ">ttmr.tool_machines_record_id = #{toolMachinesRecordId}</if>
        </where>
    </select>

    <select id="selectTmToolMachinesRecordByToolMachinesId" parameterType="Long" resultMap="TmToolMachinesRecordResult">
        <include refid="selectTmToolMachinesRecordVo"/>
        where ttmr.tool_machines_id = #{toolMachinesId}
    </select>

    <insert id="insertTmToolMachinesRecord" parameterType="TmToolMachinesRecord" useGeneratedKeys="true"
            keyProperty="toolMachinesRecordId">
        insert into tm_tool_machines_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolMachinesId != null">tool_machines_id,</if>
            <if test="toolRequestUseItemId != null">tool_request_use_item_id,</if>
            <if test="type != null">type,</if>
            <if test="upDate != null">up_date,</if>
            <if test="downDate != null">down_date,</if>
            <if test="warehouse != null">warehouse,</if>
            <if test="warehouseDesc != null">warehouse_desc,</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="toolMachinesId != null">#{toolMachinesId},</if>
            <if test="toolRequestUseItemId != null">#{toolRequestUseItemId},</if>
            <if test="type != null">#{type},</if>
            <if test="upDate != null">#{upDate},</if>
            <if test="downDate != null">#{downDate},</if>
            <if test="warehouse != null">#{warehouse},</if>
            <if test="warehouseDesc != null">#{warehouseDesc},</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="updateTmToolMachinesRecord" parameterType="TmToolMachinesRecord">
        update tm_tool_machines_record
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolMachinesId != null">tool_machines_id = #{toolMachinesId},</if>
            <if test="toolRequestUseItemId != null">tool_request_use_item_id = #{toolRequestUseItemId},</if>
            <if test="type != null">type = #{type},</if>
            <if test="upDate != null">up_date = #{upDate},</if>
            <if test="downDate != null">down_date = #{downDate},</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>
            <if test="warehouse != null">warehouse = #{warehouse},</if>
            <if test="warehouseDesc != null">warehouse_desc = #{warehouseDesc},</if>
        </trim>
        where tool_machines_record_id = #{toolMachinesRecordId}
    </update>

    <delete id="deleteTmToolMachinesRecordByToolMachinesRecordId" parameterType="Long">
        delete
        from tm_tool_machines_record
        where tool_machines_record_id = #{toolMachinesRecordId}
    </delete>

    <delete id="deleteTmToolMachinesRecordByToolMachinesRecordIds" parameterType="String">
        delete from tm_tool_machines_record where tool_machines_record_id in
        <foreach item="toolMachinesRecordId" collection="array" open="(" separator="," close=")">
            #{toolMachinesRecordId}
        </foreach>
    </delete>
</mapper>
