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

    <resultMap type="TmToolMachines" id="TmToolMachinesResult">
        <result property="toolMachinesId" column="tool_machines_id"/>
        <result property="toolMachinesCode" column="tool_machines_code"/>
        <result property="arrangeCode" column="arrange_code"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="processId" column="process_id"/>
        <result property="workorderId" column="workorder_id"/>
        <result property="toolRequestUseId" column="tool_request_use_id"/>
        <result property="itemId" column="item_id"/>
        <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="workunitName" column="workunit_name"/>
        <result property="processName" column="process_name"/>
        <result property="toolRequestUseCode" column="tool_request_use_code"/>
        <result property="workorderName" column="workorder_name"/>
    </resultMap>


    <resultMap type="com.ximai.mes.tm.domain.TmToolMachines" id="TmToolMachinesVo">
        <result property="toolMachinesId" column="tool_machines_id"/>
        <result property="toolMachinesCode" column="tool_machines_code"/>
        <result property="toolRequestUseId" column="tool_request_use_id"/>
        <result property="toolRequestUseCode" column="tool_request_use_code"/>
        <result property="toolRequestUseItemId" column="tool_request_use_item_id"/>
        <result property="toolRequestCode" column="tool_request_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="arrangeCode" column="arrange_code"/>
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>

        <result property="processId" column="process_id"/>
        <result property="processName" column="process_name"/>
        <result property="processCode" column="process_code"/>

        <result property="workorderId" column="workorder_id"/>
        <result property="workorderName" column="workorder_name"/>
        <result property="upDate" column="up_date"/>
        <result property="downDate" column="down_date"/>
        <result property="itemName" column="item_name"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemId" column="item_id"/>

        <result property="toolName" column="tool_name"/>
        <result property="toolCode" column="tool_code"/>

        <result property="warehouse" column="warehouse"/>
        <result property="type" column="type"/>
        <result property="typeName" column="type_name"/>

        <result property="taskId" column="task_id"/>
        <result property="taskCode" column="task_code"/>
        <result property="taskWorkunitId" column="task_workunit_id"/>

    </resultMap>


    <resultMap type="com.ximai.mes.md.vo.MdItemVo" id="MdWorkunitVo2Result">
        <result property="workunitId" column="workunit_id"/>
        <result property="workunitCode" column="workunit_code"/>
        <result property="workunitName" column="workunit_name"/>
        <result property="enableFlag" column="enable_flag"/>
        <result property="remark" column="remark"/>
        <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="workstationId" column="workstation_id"/>
        <result property="taskId" column="task_id"/>
        <result property="taskCode" column="task_code"/>
        <result property="taskName" column="task_name"/>
        <result property="itemId" column="item_id"/>
        <result property="itemCode" column="item_code"/>
        <result property="itemName" column="item_name"/>
        <result property="toolId" column="tool_id"/>
        <result property="toolCode" column="tool_code"/>
        <result property="toolName" column="tool_name"/>
        <result property="type" column="type"/>
        <result property="nextMaintenPeriod" column="next_mainten_period"/>
        <result property="allQuantity" column="all_quantity"/>
    </resultMap>

    <sql id="selectBase">
        SELECT ttm.tool_machines_code,
               ttm.tool_machines_id,
               ttru.tool_request_use_code,
               ttm.task_workunit_id,
               ttm.tool_request_use_item_id,
               ttm.type,
               ttm.warehouse,
               ttm.up_date,
               ttm.down_date,
               ttm.create_by,
               ttm.create_time,
               ttm.update_by,
               ttm.update_time,
               ttm.tool_request_use_id,
               ttm.is_exist_tool,
               im.item_id,
               im.item_name,
               im.item_code,
               im.sap_item_code,
               pt.task_id,
               pt.task_code,
               coalesce(tt.tool_id, ttm.tool_id)     tool_id,
               coalesce(tt.tool_code, ttm.tool_code) tool_code,
               coalesce(tt.tool_name, ttm.tool_name) tool_name,
               t1.workunit_code,
               t1.workunit_name,
               ttr.tool_request_code,
               pt.process_id,
               pt.process_code,
               pt.process_name,
               ttru.tool_request_use_code
        FROM tm_tool_machines ttm
                 LEFT JOIN pro_workorder pw on ttm.workorder_id = pw.workorder_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 = ttm.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 = ttm.task_id
                 LEFT JOIN tm_tool tt ON tt.tool_id = ttrui.tool_id
                 LEFT JOIN md_item im ON im.item_id = ttm.item_id
    </sql>

    <select id="selectTmToolMachinesVoList" resultMap="TmToolMachinesVo">
        <include refid="selectBase"/>
        <where>
            <if test="toolMachinesId != null ">and ttm.tool_machines_id = #{toolMachinesId}</if>
            <if test="workorderCode != null ">and pw.workorder_code = #{workorderCode}</if>
            <if test="toolRequestUseItemId != null ">and ttm.tool_request_use_item_id = #{toolRequestUseItemId}</if>
            <if test="taskWorkunitId != null ">and ttm.task_workunit_id = #{taskWorkunitId}</if>
            <if test="arrangeCode != null ">and ttm.arrange_code = #{arrangeCode}</if>
            <if test="processId != null ">and ttm.process_id = #{processId}</if>
            <if test="workunitId != null ">and ttm.workunit_id = #{workunitId}</if>
            <if test="workunitCode != null ">and t1.workunit_code = #{workunitCode}</if>
            <if test="workunitName != null ">and t1.workunit_name = #{workunitName}</if>
            <if test="toolCode != null ">and tt.tool_code = #{toolCode}</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 ttm.type IN
                <foreach item="item" collection="typeList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="warehouseStr != null  and warehouseStr != ''">and ttm.warehouse IN
                <foreach item="item" collection="warehouseList" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="upDate != null ">and (ttm.up_date > #{upDateMin} and ttm.up_date &lt; #{upDateMax})</if>
            <if test="downDate != null ">and (ttm.down_date > #{downDateMin} and ttm.down_date &lt; #{downDateMax})</if>
        </where>
    </select>


    <sql id="selectTmToolMachinesVo">
        select ttm.tool_machines_id,
               ttm.tool_machines_code,
               ttm.arrange_code,
               ttm.workunit_id,
               ttm.item_id,
               ttm.process_id,
               ttm.workorder_id,
               ttm.tool_request_use_id,
               ttm.is_exist_tool,
               ttm.create_by,
               ttm.create_time,
               ttm.update_by,
               ttm.update_time,
               pp.process_name,
               mw.workunit_name,
               ttru.tool_request_use_code,
               pw.workorder_name
        from tm_tool_machines ttm
                 left join tm_tool_request_use ttru on ttm.tool_request_use_id = ttru.tool_request_use_id
                 left join pro_process pp on ttm.process_id = pp.process_id
            --                  left join pro_work_order_process pwop on pwop.workorder_process_id = ttru.process_id
--                  left join pro_route_process prp on pwop.record_id = prp.record_id
                 left join md_workunit mw on ttm.workunit_id = mw.workunit_id
                 left join pro_workorder pw on ttm.workorder_id = pw.workorder_id
    </sql>


    <select id="selectTmToolMachinesList" parameterType="TmToolMachines" resultMap="TmToolMachinesResult">
        <include refid="selectTmToolMachinesVo"/>
        <where>
            <if test="toolMachinesCode != null  and toolMachinesCode != ''">and ttm.tool_machines_code =
                #{toolMachinesCode}
            </if>
            <if test="arrangeCode != null  and arrangeCode != ''">and ttm.arrange_code = #{arrangeCode}</if>
            <if test="workunitId != null ">and ttm.workunit_id = #{workunitId}</if>
            <if test="processId != null ">and ttm.process_id = #{processId}</if>
            <if test="workorderId != null ">and ttm.workorder_id = #{workorderId}</if>
            <if test="toolRequestUseId != null ">and ttm.tool_request_use_id = #{toolRequestUseId}</if>
        </where>
    </select>

    <select id="selectTmToolMachinesByToolMachinesId" parameterType="Long" resultMap="TmToolMachinesResult">
        <include refid="selectTmToolMachinesVo"/>
        where ttm.tool_machines_id = #{toolMachinesId}

    </select>


    <select id="selectOneByObj" resultMap="TmToolMachinesVo">
        <include refid="selectBase"/>
        <where>
            <if test="toolMachinesId != null ">and ttm.tool_machines_id = #{toolMachinesId}</if>
            <if test="toolMachinesids != null  and toolMachinesids != ''">and ttm.tool_machines_id IN
                <foreach item="item" collection="toolMachinesids" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>


    <select id="selectOneBySn" resultMap="TmToolMachinesResult">
        SELECT
        ttm.*
        FROM
        tm_tool_machines ttm
        LEFT JOIN tm_tool_request_use_item ttrui ON ttm.tool_request_use_item_id = ttrui.tool_request_use_item_id
        LEFT JOIN tm_tool tt ON ttrui.tool_id = tt.tool_id
        <where>
            <if test="toolCode != null ">and tt.tool_code = #{toolCode}</if>
            <if test="toolMachinesCode != null ">and ttm.tool_machines_code = #{toolMachinesCode}</if>
            <if test="toolMachinesId != null ">and ttm.tool_machines_id = #{toolMachinesId}</if>
        </where>
    </select>


    <insert id="insertTmToolMachines" parameterType="TmToolMachines" useGeneratedKeys="true"
            keyProperty="toolMachinesId">
        insert into tm_tool_machines
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolMachinesCode != null">tool_machines_code,</if>
            <if test="arrangeCode != null">arrange_code,</if>
            <if test="taskId != null">task_id,</if>
            <if test="taskCode != null">task_code,</if>
            <if test="itemId != null">item_id,</if>
            <if test="toolId != null">tool_id,</if>
            <if test="toolCode != null">tool_code,</if>
            <if test="toolName != null">tool_name,</if>
            <if test="workunitId != null">workunit_id,</if>
            <if test="workunitCode != null">workunit_code,</if>
            <if test="workunitName != null">workunit_name,</if>
            <if test="taskWorkunitId != null">task_workunit_id,</if>
            <if test="processId != null">process_id,</if>
            <if test="workorderId != null">workorder_id,</if>
            <if test="toolRequestUseId != null">tool_request_use_id,</if>
            <if test="toolRequestUseItemId != null">tool_request_use_item_id,</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>
            <if test="type != null">type,</if>
            <if test="warehouse != null">warehouse,</if>
            <if test="upDate != null">up_date,</if>
            <if test="downDate != null">down_date,</if>
            <if test="isExistTool != null">is_exist_tool,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="toolMachinesCode != null">#{toolMachinesCode},</if>
            <if test="arrangeCode != null">#{arrangeCode},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="taskCode != null">#{taskCode},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="toolId != null">#{toolId},</if>
            <if test="toolCode != null">#{toolCode},</if>
            <if test="toolName != null">#{toolName},</if>

            <if test="workunitId != null">#{workunitId},</if>
            <if test="workunitCode != null">#{workunitCode},</if>
            <if test="workunitName != null">#{workunitName},</if>
            <if test="taskWorkunitId != null">#{taskWorkunitId},</if>
            <if test="processId != null">#{processId},</if>
            <if test="workorderId != null">#{workorderId},</if>
            <if test="toolRequestUseId != null">#{toolRequestUseId},</if>
            <if test="toolRequestUseItemId != null">#{toolRequestUseItemId},</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>
            <if test="type != null">#{type},</if>
            <if test="warehouse != null">#{warehouse},</if>
            <if test="upDate != null">#{upDate},</if>
            <if test="downDate != null">#{downDate},</if>
            <if test="isExistTool != null">#{isExistTool},</if>
        </trim>
    </insert>

    <update id="updateTmToolMachines" parameterType="TmToolMachines">
        update tm_tool_machines
        <trim prefix="SET" suffixOverrides=",">
            <if test="toolMachinesCode != null">tool_machines_code = #{toolMachinesCode},</if>
            <if test="arrangeCode != null">arrange_code = #{arrangeCode},</if>
            <if test="taskId != null">task_id=#{taskId},</if>
            <if test="taskCode != null">task_code= #{taskCode},</if>

            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="toolId != null">tool_id = #{toolId},</if>
            <if test="toolCode != null">tool_code = #{toolCode},</if>
            <if test="toolName != null">tool_name = #{toolName},</if>
            <if test="workunitId != null">workunit_id = #{workunitId},</if>
            <if test="workunitCode != null">workunit_code = #{workunitCode},</if>
            <if test="workunitName != null">workunit_name = #{workunitName},</if>
            <if test="taskWorkunitId != null">task_workunit_id = #{taskWorkunitId},</if>
            <if test="processId != null">process_id = #{processId},</if>
            <if test="workorderId != null">workorder_id = #{workorderId},</if>
            <if test="toolRequestUseId != null">tool_request_use_id = #{toolRequestUseId},</if>
            <if test="toolRequestUseItemId != null">tool_request_use_item_id = #{toolRequestUseItemId},</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="type != null">`type` = #{type},</if>
            <if test="warehouse != null">warehouse = #{warehouse},</if>
            <if test="upDate != null">up_date = #{upDate},</if>
            <if test="downDate != null">down_date = #{downDate},</if>
            <if test="isExistTool != null">is_exist_tool=#{isExistTool},</if>
        </trim>
        where tool_machines_id = #{toolMachinesId}
    </update>

    <delete id="deleteTmToolMachinesByToolMachinesId" parameterType="Long">
        delete
        from tm_tool_machines
        where tool_machines_id = #{toolMachinesId}
    </delete>

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


    <select id="selectUpTmToolRequestList" resultMap="MdWorkunitVo2Result">
        SELECT
        ttm.tool_machines_id,
        ttm.workunit_id,
        ttm.workunit_name,
        ttm.workunit_code,
        pt.task_id,
        pt.task_code,
        pt.task_name,
        tt.tool_id,
        tt.tool_code,
        tt.tool_name,
        mi.item_id,
        mi.item_code,
        mi.item_name,
        mi.sap_item_code,
        ttm.type,
        tt.life_time next_mainten_period,
        tt.quantity all_quantity
        FROM
        tm_tool_machines ttm
        LEFT JOIN tm_tool_request_use ttrs ON ttm.tool_request_use_id = ttrs.tool_request_use_id
        LEFT JOIN tm_tool_request_use_item ttrui ON ttrui.tool_request_use_item_id = ttm.tool_request_use_item_id
        LEFT JOIN tm_tool_request ttr ON ttrs.tool_request_id = ttr.tool_request_id
        LEFT JOIN pro_task pt ON pt.task_id = ttm.task_id
        LEFT JOIN pro_task_workunit ptw ON pt.task_id = ptw.task_id
        LEFT JOIN tm_tool tt ON ttrui.tool_id = tt.tool_id
        LEFT JOIN md_item mi ON tt.item_id = mi.item_id
        <where>
            and mi.sap_item_code is not null
            and ptw.task_workunit_id is not null
            <if test="workunitCode != null  and workunitCode != ''">and ttm.workunit_code = #{workunitCode}</if>
            <if test="workunitName != null  and workunitName != ''">and ttm.workunit_name like concat('%',
                #{workunitName}, '%')
            </if>
            <if test="type != null  and type != ''">and ttm.type = #{type}
            </if>
        </where>
    </select>


</mapper>
