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

    <resultMap type="TmToolMaintenance" id="TmToolMaintenanceResult">
        <result property="toolMaintenanceId"    column="tool_maintenance_id"    />
        <result property="toolId"    column="tool_id"    />
        <result property="maintenanceStatus"    column="maintenance_status"    />
        <result property="itemCode"    column="item_code"    />
        <result property="toolName"    column="tool_name"    />
        <result property="toolCode"    column="tool_code"    />
        <result property="location"    column="location"    />
        <result property="createTime"    column="create_time"    />
        <result property="processPerson"    column="process_person"    />
        <result property="processStatus"    column="process_status"    />
        <result property="processTime"    column="process_time"    />
        <result property="startTime"    column="start_time"    />
        <result property="endTime"    column="end_time"    />
        <result property="lifeTime"    column="life_Time"    />
        <result property="processId"    column="process_id"    />
        <result property="toolMaintenanceCode"    column="tool_maintenance_code"    />


    </resultMap>

    <sql id="selectTmToolMaintenanceVo">
        select
            p.tool_maintenance_id,
            p.tool_maintenance_code,
            p.tool_id,
            p.maintenance_type,
            p.maintenance_status,
            p.item_code,
            p.tool_name,
            p.tool_code,
            p.location,
            p.create_time,
            b.process_id,
            b.process_person,
            b.process_status,
            b.process_time,
            b.start_time,
            b.end_time,
            b.life_time
            from tm_tool_maintenance p
            left join tm_tool_maintenance_process  b on p.tool_maintenance_id = b.tool_maintenance_id
    </sql>

<!--    <select id="selectTmToolMaintenanceList" parameterType="TmToolMaintenance" resultMap="TmToolMaintenanceResult">-->
<!--        <include refid="selectTmToolMaintenanceVo"/>-->
<!--        <where>-->
<!--            <if test="maintenanceStatus != null  and maintenanceStatus != ''"> and p.maintenance_status = #{maintenanceStatus}</if>-->
<!--            <if test="toolMaintenanceCode != null  and toolMaintenanceCode != ''"> and p.tool_maintenance_code = #{toolMaintenanceCode}</if>-->
<!--            <if test="itemCode != null  and itemCode != ''"> and p.item_code = #{itemCode}</if>-->
<!--            <if test="toolId != null  and toolId != ''"> and p.tool_id = #{toolId}</if>-->
<!--            <if test="maintenanceType != null  and maintenanceType != ''"> and p.maintenance_type = #{maintenanceType}</if>-->
<!--            <if test="toolName != null  and toolName != ''"> and p.tool_name like concat('%', #{toolName}, '%')</if>-->
<!--            <if test="toolCode != null  and toolCode != ''"> and p.tool_code = #{toolCode}</if>-->
<!--            <if test="location != null  and location != ''"> and p.location = #{location}</if>-->
<!--        </where>-->
<!--    </select>-->
    <select id="selectTmToolMaintenanceList" parameterType="TmToolMaintenance" resultMap="TmToolMaintenanceResult">
        <include refid="selectTmToolMaintenanceVo"/>
        <where>
            <if test="maintenanceStatus != null  and maintenanceStatus != ''"> and p.maintenance_status = #{maintenanceStatus}</if>
            <if test="toolMaintenanceCode != null  and toolMaintenanceCode != ''"> and p.tool_maintenance_code = #{toolMaintenanceCode}</if>
            <if test="maintenanceType != null  and maintenanceType != ''"> and p.maintenance_type = #{maintenanceType}</if>
            <if test="itemCode != null  and itemCode != ''"> and p.item_code = #{itemCode}</if>
            <if test="toolName != null  and toolName != ''"> and p.tool_name like concat('%', #{toolName}, '%')</if>
            <if test="toolCode != null  and toolCode != ''"> and p.tool_code = #{toolCode}</if>
            <if test="location != null  and location != ''"> and p.location = #{location}</if>
        </where>
    </select>

    <select id="selectTmToolMaintenanceByToolMaintenanceId" parameterType="Long" resultMap="TmToolMaintenanceResult">
        <include refid="selectTmToolMaintenanceVo"/>
        where p.tool_maintenance_id = #{toolMaintenanceId}
    </select>

    <insert id="insertTmToolMaintenance" parameterType="TmToolMaintenance" useGeneratedKeys="true" keyProperty="toolMaintenanceId">
        insert into tm_tool_maintenance
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="toolId != null">tool_id,</if>
            <if test="maintenanceStatus != null">maintenance_status,</if>
            <if test="toolMaintenanceCode != null ">tool_maintenance_code,</if>
            <if test="maintenanceType != null">maintenance_type,</if>
            <if test="itemCode != null">item_code,</if>
            <if test="toolName != null">tool_name,</if>
            <if test="toolCode != null">tool_code,</if>
            <if test="location != null">location,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="toolId != null">#{toolId},</if>
            <if test="maintenanceStatus != null">#{maintenanceStatus},</if>
            <if test="toolMaintenanceCode != null">#{toolMaintenanceCode},</if>
            <if test="maintenanceType != null">#{maintenanceType},</if>
            <if test="itemCode != null">#{itemCode},</if>
            <if test="toolName != null">#{toolName},</if>
            <if test="toolCode != null">#{toolCode},</if>
            <if test="location != null">#{location},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
    </insert>

    <update id="updateTmToolMaintenance" parameterType="TmToolMaintenance">
        update tm_tool_maintenance
        <trim prefix="SET" suffixOverrides=",">
            <if test="maintenanceStatus != null">maintenance_status = #{maintenanceStatus},</if>
            <if test="toolMaintenanceCode != null">tool_maintenance_code=#{toolMaintenanceCode},</if>
            <if test="itemCode != null">item_code = #{itemCode},</if>
            <if test="toolId != null">tool_id = #{toolId},</if>
            <if test="maintenanceType != null">maintenance_type=#{maintenanceType},</if>
            <if test="toolName != null">tool_name = #{toolName},</if>
            <if test="toolCode != null">tool_code = #{toolCode},</if>
            <if test="location != null">location = #{location},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
        </trim>
        where tool_maintenance_id = #{toolMaintenanceId}
    </update>

    <delete id="deleteTmToolMaintenanceByToolMaintenanceId" parameterType="Long">
        delete from tm_tool_maintenance where tool_maintenance_id = #{toolMaintenanceId}
    </delete>

    <delete id="deleteTmToolMaintenanceByToolMaintenanceIds" parameterType="String">
        delete from tm_tool_maintenance where tool_maintenance_id in
        <foreach item="toolMaintenanceId" collection="array" open="(" separator="," close=")">
            #{toolMaintenanceId}
        </foreach>
    </delete>
</mapper>
