<?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.soss.system.mapper.MaterialMapper">
    
    <resultMap type="Material" id="MaterialResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="remarks"    column="remarks"    />
        <result property="unit"    column="unit"    />
        <result property="state"    column="state"    />
        <result property="createdAt"    column="created_at"    />
        <result property="updatedAt"    column="updated_at"    />
        <result property="code"    column="code"    />
    </resultMap>

    <sql id="selectMaterialVo">
        select id, name, remarks, unit, state, created_at, updated_at, code from material
    </sql>

    <select id="selectMaterialList" parameterType="Material" resultMap="MaterialResult">
        <include refid="selectMaterialVo"/>
        <where>  
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="state != null  and state != ''"> and state = #{state}</if>
            <if test="createdAt != null "> and created_at = #{createdAt}</if>
            <if test="updatedAt != null "> and updated_at = #{updatedAt}</if>
            <if test="code != null  and code != ''"> and code = #{code}</if>
        </where>
    </select>
    
    <select id="selectMaterialById" parameterType="String" resultMap="MaterialResult">
        <include refid="selectMaterialVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertMaterial" parameterType="Material" useGeneratedKeys="true" keyProperty="id">
        insert into material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">name,</if>
            <if test="remarks != null">remarks,</if>
            <if test="unit != null and unit != ''">unit,</if>
            <if test="state != null and state != ''">state,</if>
            <if test="createdAt != null">created_at,</if>
            <if test="updatedAt != null">updated_at,</if>
            <if test="code != null">code,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null and name != ''">#{name},</if>
            <if test="remarks != null">#{remarks},</if>
            <if test="unit != null and unit != ''">#{unit},</if>
            <if test="state != null and state != ''">#{state},</if>
            <if test="createdAt != null">#{createdAt},</if>
            <if test="updatedAt != null">#{updatedAt},</if>
            <if test="code != null">#{code},</if>
         </trim>
    </insert>

    <update id="updateMaterial" parameterType="Material">
        update material
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null and name != ''">name = #{name},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
            <if test="unit != null and unit != ''">unit = #{unit},</if>
            <if test="state != null and state != ''">state = #{state},</if>
            <if test="updatedAt != null">updated_at = #{updatedAt},</if>
            <if test="code != null">code = #{code},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMaterialById" parameterType="String">
        delete from material where id = #{id}
    </delete>

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