<?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.zhyc.module.feed.mapper.SgMaterialMapper">

    <resultMap type="SgMaterial" id="SgMaterialResult">
        <result property="materialId" column="material_id"/>
        <result property="materialName" column="material_name"/>
        <result property="isGranular" column="is_granular"/>
    </resultMap>

    <sql id="selectSgMaterialVo">
        select material_id, material_name, is_granular
        from sg_material sg_material_alias
    </sql>

    <select id="selectSgMaterialList" parameterType="SgMaterial" resultMap="SgMaterialResult">
        <include refid="selectSgMaterialVo"/>
        <where>
            <if test="materialId != null  and materialId != ''">and material_id LIKE CONCAT('%',#{materialId},'%')</if>
            <if test="materialName != null  and materialName != ''">and material_name like concat('%', #{materialName},
                '%')
            </if>
            <if test="isGranular != null ">and is_granular = #{isGranular}</if>
            ${params.dataScope}
        </where>
    </select>

    <select id="selectSgMaterialByMaterialId" parameterType="String" resultMap="SgMaterialResult">
        <include refid="selectSgMaterialVo"/>
        where material_id = #{materialId}
    </select>

    <insert id="insertSgMaterial" parameterType="SgMaterial">
        insert into sg_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null and materialId != ''">material_id,</if>
            <if test="materialName != null and materialName != ''">material_name,</if>
            <if test="isGranular != null">is_granular,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null and materialId != ''">#{materialId},</if>
            <if test="materialName != null and materialName != ''">#{materialName},</if>
            <if test="isGranular != null">#{isGranular},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateSgMaterial" parameterType="SgMaterial">
        update sg_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialName != null and materialName != ''">material_name = #{materialName},</if>
            <if test="isGranular != null">is_granular = #{isGranular},</if>
        </trim>
        where material_id = #{materialId}
    </update>

    <delete id="deleteSgMaterialByMaterialId" parameterType="String">
        delete
        from sg_material
        where material_id = #{materialId}
    </delete>

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