<?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.hw.mes.mapper.MesBaseMaterialTypeMapper">
    
    <resultMap type="MesBaseMaterialType" id="MesBaseMaterialTypeResult">
        <result property="matrialTypeId"    column="matrial_type_id"    />
        <result property="parentId"    column="parent_id"    />
        <result property="typeCode"    column="type_code"    />
        <result property="typeName"    column="type_name"    />
        <result property="activeFlag"    column="active_flag"    />
        <result property="ancestors"    column="ancestors"    />
        <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"    />
    </resultMap>

    <sql id="selectMesBaseMaterialTypeVo">
        select matrial_type_id, parent_id, type_code, type_name, active_flag, ancestors, remark, create_by, create_time, update_by, update_time from mes_base_material_type
    </sql>

    <select id="selectMesBaseMaterialTypeList" parameterType="MesBaseMaterialType" resultMap="MesBaseMaterialTypeResult">
        <include refid="selectMesBaseMaterialTypeVo"/>
        <where>  
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="typeCode != null  and typeCode != ''"> and type_code = #{typeCode}</if>
            <if test="typeName != null  and typeName != ''"> and type_name like concat('%', #{typeName}, '%')</if>
            <if test="activeFlag != null  and activeFlag != ''"> and active_flag = #{activeFlag}</if>
            <if test="ancestors != null  and ancestors != ''"> and ancestors = #{ancestors}</if>
        </where>
    </select>
    
    <select id="selectMesBaseMaterialTypeByMatrialTypeId" parameterType="Long" resultMap="MesBaseMaterialTypeResult">
        <include refid="selectMesBaseMaterialTypeVo"/>
        where matrial_type_id = #{matrialTypeId}
    </select>
        
    <insert id="insertMesBaseMaterialType" parameterType="MesBaseMaterialType" useGeneratedKeys="true" keyProperty="matrialTypeId">
        insert into mes_base_material_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,</if>
            <if test="typeCode != null and typeCode != ''">type_code,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="activeFlag != null">active_flag,</if>
            <if test="ancestors != null">ancestors,</if>
            <if test="remark != null">remark,</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="parentId != null">#{parentId},</if>
            <if test="typeCode != null and typeCode != ''">#{typeCode},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="activeFlag != null">#{activeFlag},</if>
            <if test="ancestors != null">#{ancestors},</if>
            <if test="remark != null">#{remark},</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="updateMesBaseMaterialType" parameterType="MesBaseMaterialType">
        update mes_base_material_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="typeCode != null and typeCode != ''">type_code = #{typeCode},</if>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="activeFlag != null">active_flag = #{activeFlag},</if>
            <if test="ancestors != null">ancestors = #{ancestors},</if>
            <if test="remark != null">remark = #{remark},</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 matrial_type_id = #{matrialTypeId}
    </update>

    <delete id="deleteMesBaseMaterialTypeByMatrialTypeId" parameterType="Long">
        delete from mes_base_material_type where matrial_type_id = #{matrialTypeId}
    </delete>

    <delete id="deleteMesBaseMaterialTypeByMatrialTypeIds" parameterType="String">
        delete from mes_base_material_type where matrial_type_id in 
        <foreach item="matrialTypeId" collection="array" open="(" separator="," close=")">
            #{matrialTypeId}
        </foreach>
    </delete>
</mapper>