<?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.nasc.custom.mapper.NascMaterialMapper">

    <resultMap id="materialResult" type="NascMaterial">
        <id     property="id"          column="id"         />
        <result property="optionId"    column="option_id"  />
        <result property="matterId"    column="matter_id"  />
        <result property="conditions"  column="conditions" />
        <result property="dealWith"    column="deal_with"  />
    </resultMap>

    <insert id="insertMaterial" parameterType="NascMaterial" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO nasc_material (option_id, matter_id, conditions, deal_with)
        VALUES (#{optionId}, #{matterId}, #{conditions}, #{dealWith})
    </insert>

    <update id="updateMaterial" parameterType="NascMaterial">
        UPDATE nasc_material
        SET conditions = #{conditions}, deal_with = #{dealWith}
        WHERE id = #{id}
    </update>

    <delete id="deleteMaterialByOptionIds" parameterType="Long">
        DELETE FROM nasc_material
        WHERE option_id IN
        <foreach collection="array" item="optionId" open="(" separator="," close=")">
            #{optionId}
        </foreach>
    </delete>

    <delete id="deleteMaterialByMatterId" parameterType="Long">
        DELETE FROM nasc_material
        WHERE matter_id = #{matterId}
    </delete>

    <select id="getMaterialByMatterId" parameterType="Long" resultMap="materialResult">
        SELECT nm.id, nm.matter_id, nm.conditions, nm.deal_with
        FROM nasc_material nm
        WHERE nm.matter_id = #{matterId}
    </select>

</mapper> 