<?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.xinelu.manage.mapper.goodsAttribute.GoodsAttributeMapper">

    <resultMap type="GoodsAttribute" id="GoodsAttributeResult">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="attributeName" column="attribute_name"/>
        <result property="attributeCode" column="attribute_code"/>
        <result property="attributeRemark" column="attribute_remark"/>
        <result property="sort" column="sort"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectGoodsAttributeVo">
        select id,
               goods_id,
               attribute_name,
               attribute_code,
               attribute_remark,
               sort,
               create_by,
               create_time,
               update_by,
               update_time
        from goods_attribute
    </sql>

    <select id="selectGoodsAttributeList" parameterType="GoodsAttribute" resultMap="GoodsAttributeResult">
        <include refid="selectGoodsAttributeVo"/>
        <where>
            <if test="goodsId != null ">
                and goods_id = #{goodsId}
            </if>
            <if test="attributeName != null  and attributeName != ''">
                and attribute_name like concat('%', #{attributeName}, '%')
            </if>
            <if test="attributeCode != null  and attributeCode != ''">
                and attribute_code = #{attributeCode}
            </if>
            <if test="attributeRemark != null  and attributeRemark != ''">
                and attribute_remark = #{attributeRemark}
            </if>
            <if test="sort != null ">
                and sort = #{sort}
            </if>
        </where>
    </select>

    <select id="selectGoodsAttributeById" parameterType="Long"
            resultMap="GoodsAttributeResult">
        <include refid="selectGoodsAttributeVo"/>
        where id = #{id}
    </select>

    <insert id="insertGoodsAttribute" parameterType="GoodsAttribute" useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_attribute
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,
            </if>
            <if test="attributeName != null">attribute_name,
            </if>
            <if test="attributeCode != null">attribute_code,
            </if>
            <if test="attributeRemark != null">attribute_remark,
            </if>
            <if test="sort != null">sort,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},
            </if>
            <if test="attributeName != null">#{attributeName},
            </if>
            <if test="attributeCode != null">#{attributeCode},
            </if>
            <if test="attributeRemark != null">#{attributeRemark},
            </if>
            <if test="sort != null">#{sort},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateGoodsAttribute" parameterType="GoodsAttribute">
        update goods_attribute
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id =
                #{goodsId},
            </if>
            <if test="attributeName != null">attribute_name =
                #{attributeName},
            </if>
            <if test="attributeCode != null">attribute_code =
                #{attributeCode},
            </if>
            <if test="attributeRemark != null">attribute_remark =
                #{attributeRemark},
            </if>
            <if test="sort != null">sort =
                #{sort},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteGoodsAttributeById" parameterType="Long">
        delete
        from goods_attribute
        where id = #{id}
    </delete>

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


    <insert id="insertGoodsAttributeList" parameterType="java.util.List">
        insert into goods_attribute(
        goods_id,
        attribute_name,
        attribute_code,
        attribute_remark,
        sort,
        create_by,
        create_time,
        update_by,
        update_time
        ) values
        <foreach item="goodDetailsLists" index="index" collection="goodDetailsLists" separator=",">
            (
            #{goodDetailsLists.goodsId},
            #{goodDetailsLists.attributeName},
            #{goodDetailsLists.attributeCode},
            #{goodDetailsLists.attributeRemark},
            #{goodDetailsLists.sort},
            #{goodDetailsLists.createBy},
            #{goodDetailsLists.createTime},
            #{goodDetailsLists.updateBy},
            #{goodDetailsLists.updateTime}
            )
        </foreach>
    </insert>


    <insert id="insertGoodsAttributes" parameterType="com.xinelu.manage.dto.goodsInfo.GoodsAttributeDTO"
            useGeneratedKeys="true"
            keyProperty="goodsAttributeId">
        insert into goods_attribute
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">goods_id,
            </if>
            <if test="attributeName != null">attribute_name,
            </if>
            <if test="attributeCode != null">attribute_code,
            </if>
            <if test="attributeRemark != null">attribute_remark,
            </if>
            <if test="attributeDetailsSort != null">sort,
            </if>
            <if test="createBy != null">create_by,
            </if>
            <if test="createTime != null">create_time,
            </if>
            <if test="updateBy != null">update_by,
            </if>
            <if test="updateTime != null">update_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="goodsId != null">#{goodsId},
            </if>
            <if test="attributeName != null">#{attributeName},
            </if>
            <if test="attributeCode != null">#{attributeCode},
            </if>
            <if test="attributeRemark != null">#{attributeRemark},
            </if>
            <if test="attributeDetailsSort != null">#{attributeDetailsSort},
            </if>
            <if test="createBy != null">#{createBy},
            </if>
            <if test="createTime != null">#{createTime},
            </if>
            <if test="updateBy != null">#{updateBy},
            </if>
            <if test="updateTime != null">#{updateTime},
            </if>
        </trim>
    </insert>

    <update id="updateGoodsAttributes" parameterType="com.xinelu.manage.dto.goodsInfo.GoodsAttributeDTO">
        update goods_attribute
        <trim prefix="SET" suffixOverrides=",">
            <if test="goodsId != null">goods_id =
                #{goodsId},
            </if>
            <if test="attributeName != null">attribute_name =
                #{attributeName},
            </if>
            <if test="attributeCode != null">attribute_code =
                #{attributeCode},
            </if>
            <if test="attributeRemark != null">attribute_remark =
                #{attributeRemark},
            </if>
            <if test="attributeDetailsSort != null">sort =
                #{attributeDetailsSort},
            </if>
            <if test="createBy != null">create_by =
                #{createBy},
            </if>
            <if test="createTime != null">create_time =
                #{createTime},
            </if>
            <if test="updateBy != null">update_by =
                #{updateBy},
            </if>
            <if test="updateTime != null">update_time =
                #{updateTime},
            </if>
        </trim>
        where id = #{goodsAttributeId}
    </update>


    <select id="selectGoodsAttributeByIdList" parameterType="Long" resultType="Long">
        select id
        from goods_attribute
        <where>
            <if test="goodsId != null ">
                and goods_id = #{goodsId}
            </if>
        </where>
    </select>

</mapper>
