<?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.MaterialTypeMapper">
    
    <resultMap type="MaterialType" id="MaterialTypeResult">
        <result property="id"    column="id"    />
        <result property="typeCode"    column="type_code"    />
        <result property="typeName"    column="type_name"    />
        <result property="status"    column="status"    />
        <result property="sort"    column="sort"    />
        <result property="remark"    column="remark"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
    </resultMap>

    <sql id="selectMaterialTypeVo">
        select id, type_code, type_name, status, sort, remark, created_at, updated_at from material_type
    </sql>

    <select id="selectMaterialTypeList" parameterType="MaterialType" resultMap="MaterialTypeResult">
        <include refid="selectMaterialTypeVo"/>
        <where>  
            <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="status != null "> and status = #{status}</if>
            <if test="sort != null "> and sort = #{sort}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>
        </where>
    </select>
    
    <select id="selectMaterialTypeById" parameterType="Long" resultMap="MaterialTypeResult">
        <include refid="selectMaterialTypeVo"/>
        where id = #{id}
    </select>

    <insert id="insertMaterialType" parameterType="MaterialType" useGeneratedKeys="true" keyProperty="id">
        insert into material_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="typeCode != null and typeCode != ''">type_code,</if>
            <if test="typeName != null and typeName != ''">type_name,</if>
            <if test="status != null">status,</if>
            <if test="sort != null">sort,</if>
            <if test="remark != null">remark,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="typeCode != null and typeCode != ''">#{typeCode},</if>
            <if test="typeName != null and typeName != ''">#{typeName},</if>
            <if test="status != null">#{status},</if>
            <if test="sort != null">#{sort},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
         </trim>
    </insert>

    <update id="updateMaterialType" parameterType="MaterialType">
        update material_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeCode != null and typeCode != ''">type_code = #{typeCode},</if>
            <if test="typeName != null and typeName != ''">type_name = #{typeName},</if>
            <if test="status != null">status = #{status},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createdAt != null">created_at = #{createdAt},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMaterialTypeById" parameterType="Long">
        delete from material_type where id = #{id}
    </delete>

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