<?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.dgtly.goods.mapper.GoodsTypeAttributeMapper">
    
    <resultMap type="GoodsTypeAttribute" id="GoodsTypeAttributeResult">
        <result property="id"    column="id"    />
        <result property="typeId"    column="type_id"    />
        <result property="attributeName"    column="attribute_name"    />
        <result property="sort"    column="sort"    />
    </resultMap>

    <sql id="selectGoodsTypeAttributeVo">
        select id, type_id, attribute_name, sort, create_time, create_by, update_time, update_by, remark from goods_type_attribute
    </sql>

    <select id="selectGoodsTypeAttributeList" parameterType="GoodsTypeAttribute" resultMap="GoodsTypeAttributeResult">
        <include refid="selectGoodsTypeAttributeVo"/>
        <where>  
            <if test="typeId != null "> and type_id = #{typeId}</if>
            <if test="name != null  and name != ''"> and attribute_name like concat('%', #{attributeName}, '%')</if>
            <if test="sort != null "> and sort = #{sort}</if>
        </where>
    </select>
    
    <select id="selectGoodsTypeAttributeById" parameterType="Long" resultMap="GoodsTypeAttributeResult">
        <include refid="selectGoodsTypeAttributeVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertGoodsTypeAttribute" parameterType="GoodsTypeAttribute">
        insert into goods_type_attribute
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null ">id,</if>
            <if test="typeId != null ">type_id,</if>
            <if test="attributeName != null  and attributeName != ''">attribute_name,</if>
            <if test="sort != null ">sort,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null ">#{id},</if>
            <if test="typeId != null ">#{typeId},</if>
            <if test="attributeName != null  and attributeName != ''">#{attribute_name},</if>
            <if test="sort != null ">#{sort},</if>
         </trim>
    </insert>

    <update id="updateGoodsTypeAttribute" parameterType="GoodsTypeAttribute">
        update goods_type_attribute
        <trim prefix="SET" suffixOverrides=",">
            <if test="typeId != null ">type_id = #{typeId},</if>
            <if test="attributeName != null  and attributeName != ''">attribute_name = #{attributeName},</if>
            <if test="sort != null ">sort = #{sort},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGoodsTypeAttributeById" parameterType="Long">
        delete from goods_type_attribute where id = #{id}
    </delete>

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

    <insert id="batchAttribute" parameterType="GoodsTypeAttribute">
        insert into goods_type_attribute (attribute_name,type_id,sort) values
        <foreach item="item" index="index" collection="list" separator=",">
            (#{item.attributeName},#{typeId},#{item.sort})
        </foreach>

    </insert>

</mapper>