<?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.fs.company.mapper.CompanyMaterialMapper">
    
    <resultMap type="CompanyMaterial" id="CompanyMaterialResult">
        <result property="id"    column="id"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialType"    column="material_type"    />
        <result property="unit"    column="unit"    />
        <result property="stock"    column="stock"    />
        <result property="companyId"    column="company_id"    />
        <result property="tags"    column="tags"    />
        <result property="images"    column="images"    />
        <result property="createTime"    column="create_time"    />
        <result property="createUserId"    column="create_user_id"    />
        <result property="updateTime"    column="update_time"    />
        <result property="updateUserId"    column="update_user_id"    />
        <result property="isDel"    column="is_del"    />
    </resultMap>

    <sql id="selectCompanyMaterialVo">
        select id, material_name, material_type, unit, stock, company_id, tags, images, create_time, create_user_id, update_time, update_user_id, is_del from company_material
    </sql>

    <select id="selectCompanyMaterialList" parameterType="CompanyMaterial" resultMap="CompanyMaterialResult">
        <include refid="selectCompanyMaterialVo"/>
        <where>  
            <if test="materialName != null  and materialName != ''"> and material_name like concat('%', #{materialName}, '%')</if>
            <if test="materialType != null  and materialType != ''"> and material_type = #{materialType}</if>
            <if test="unit != null  and unit != ''"> and unit = #{unit}</if>
            <if test="stock != null "> and stock = #{stock}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="tags != null  and tags != ''"> and tags = #{tags}</if>
            <if test="images != null  and images != ''"> and images = #{images}</if>
            <if test="createUserId != null "> and create_user_id = #{createUserId}</if>
            <if test="updateUserId != null "> and update_user_id = #{updateUserId}</if>
            <if test="isDel != null "> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectCompanyMaterialById" parameterType="Long" resultMap="CompanyMaterialResult">
        <include refid="selectCompanyMaterialVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertCompanyMaterial" parameterType="CompanyMaterial" useGeneratedKeys="true" keyProperty="id">
        insert into company_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialName != null">material_name,</if>
            <if test="materialType != null">material_type,</if>
            <if test="unit != null">unit,</if>
            <if test="stock != null">stock,</if>
            <if test="companyId != null">company_id,</if>
            <if test="tags != null">tags,</if>
            <if test="images != null">images,</if>
            <if test="createTime != null">create_time,</if>
            <if test="createUserId != null">create_user_id,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="updateUserId != null">update_user_id,</if>
            <if test="isDel != null">is_del,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialName != null">#{materialName},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="unit != null">#{unit},</if>
            <if test="stock != null">#{stock},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="tags != null">#{tags},</if>
            <if test="images != null">#{images},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="createUserId != null">#{createUserId},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="updateUserId != null">#{updateUserId},</if>
            <if test="isDel != null">#{isDel},</if>
         </trim>
    </insert>

    <update id="updateCompanyMaterial" parameterType="CompanyMaterial">
        update company_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="stock != null">stock = #{stock},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="tags != null">tags = #{tags},</if>
            <if test="images != null">images = #{images},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="createUserId != null">create_user_id = #{createUserId},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="updateUserId != null">update_user_id = #{updateUserId},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteCompanyMaterialById" parameterType="Long">
        delete from company_material where id = #{id}
    </delete>

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