<?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.zg.project.information.mapper.MtdMapper">
    
    <resultMap type="Mtd" id="MtdResult">
        <result property="Id"    column="Id"    />
        <result property="mid"    column="mid"    />
        <result property="desMat"    column="des_mat"    />
        <result property="grp"    column="grp"    />
        <result property="unt"    column="unt"    />
        <result property="uprc"    column="uprc"    />
        <result property="btpe"    column="btpe"    />
        <result property="mtpe"    column="mtpe"    />
        <result property="stpe"    column="stpe"    />
    </resultMap>

    <sql id="selectMtdVo">
        select Id, mid, des_mat, grp, unt, uprc, btpe, mtpe, stpe from mtd
    </sql>

    <select id="selectMtdList" parameterType="Mtd" resultMap="MtdResult">
        <include refid="selectMtdVo"/>
        <where>
            <if test="mid != null and mid != ''"> AND mid LIKE CONCAT('%', #{mid}, '%')</if>
            <if test="desMat != null and desMat != ''"> AND des_mat LIKE CONCAT('%', #{desMat}, '%')</if>
            <if test="grp != null and grp != ''"> AND grp LIKE CONCAT('%', #{grp}, '%')</if>
            <if test="unt != null and unt != ''"> AND unt LIKE CONCAT('%', #{unt}, '%')</if>
            <if test="uprc != null and uprc != ''"> AND uprc LIKE CONCAT('%', #{uprc}, '%')</if>
            <if test="btpe != null and btpe != ''"> AND btpe LIKE CONCAT('%', #{btpe}, '%')</if>
            <if test="mtpe != null and mtpe != ''"> AND mtpe LIKE CONCAT('%', #{mtpe}, '%')</if>
            <if test="stpe != null and stpe != ''"> AND stpe LIKE CONCAT('%', #{stpe}, '%')</if>
        </where>
    </select>
    
    <select id="selectMtdById" parameterType="Long" resultMap="MtdResult">
        <include refid="selectMtdVo"/>
        where Id = #{Id}
    </select>

    <select id="selectWlmsByWlbh" resultType="java.lang.String" parameterType="java.lang.String">
        SELECT des_mat
        FROM mtd
        WHERE mid = #{materialCode}
    </select>

    <select id="selectMtdByMid" resultMap="MtdResult">
        SELECT * FROM mtd WHERE mid = #{mid} LIMIT 1
    </select>

    <insert id="insertMtd" parameterType="Mtd" useGeneratedKeys="true" keyProperty="Id">
        insert into mtd
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mid != null">mid,</if>
            <if test="desMat != null">des_mat,</if>
            <if test="grp != null">grp,</if>
            <if test="unt != null">unt,</if>
            <if test="uprc != null">uprc,</if>
            <if test="btpe != null">btpe,</if>
            <if test="mtpe != null">mtpe,</if>
            <if test="stpe != null">stpe,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mid != null">#{mid},</if>
            <if test="desMat != null">#{desMat},</if>
            <if test="grp != null">#{grp},</if>
            <if test="unt != null">#{unt},</if>
            <if test="uprc != null">#{uprc},</if>
            <if test="btpe != null">#{btpe},</if>
            <if test="mtpe != null">#{mtpe},</if>
            <if test="stpe != null">#{stpe},</if>
         </trim>
    </insert>

    <update id="updateMtd" parameterType="Mtd">
        update mtd
        <trim prefix="SET" suffixOverrides=",">
            <if test="mid != null">mid = #{mid},</if>
            <if test="desMat != null">des_mat = #{desMat},</if>
            <if test="grp != null">grp = #{grp},</if>
            <if test="unt != null">unt = #{unt},</if>
            <if test="uprc != null">uprc = #{uprc},</if>
            <if test="btpe != null">btpe = #{btpe},</if>
            <if test="mtpe != null">mtpe = #{mtpe},</if>
            <if test="stpe != null">stpe = #{stpe},</if>
        </trim>
        where Id = #{Id}
    </update>

    <delete id="deleteMtdById" parameterType="Long">
        delete from mtd where Id = #{Id}
    </delete>

    <delete id="deleteMtdByIds" parameterType="String">
        delete from mtd where Id in 
        <foreach item="Id" collection="array" open="(" separator="," close=")">
            #{Id}
        </foreach>
    </delete>
</mapper>