<?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.kmall.admin.dao.GoodsAttributeDao">

    <!-- 可根据自己的需求，是否要使用 -->
    <resultMap type="com.kmall.admin.entity.GoodsAttributeEntity" id="goodsAttributeMap">
        <result property="id" column="id"/>
        <result property="goodsId" column="goods_id"/>
        <result property="merchSn" column="merch_sn"/>
        <result property="attributeId" column="attribute_id"/>
        <result property="value" column="value"/>
    </resultMap>

    <select id="queryObject" resultType="com.kmall.admin.entity.GoodsAttributeEntity">
        select
        id,
        goods_id,
        merch_sn,
        attribute_id,
        value
        from mall_goods_attribute
        where id = #{id}
    </select>

    <select id="queryList" resultType="com.kmall.admin.entity.GoodsAttributeEntity">
        select
        mall_goods_attribute.id,
        mall_goods_attribute.goods_id,
        mall_goods_attribute.merch_sn,
        mall_goods_attribute.attribute_id,
        mall_goods_attribute.value,
        mall_goods.name goods_name,
        mall_attribute.name attribute_name
        from mall_goods_attribute
        LEFT JOIN mall_goods ON mall_goods.id = mall_goods_attribute.goods_id
        LEFT JOIN mall_attribute ON mall_attribute.id = mall_goods_attribute.attribute_id
        WHERE 1=1
        <if test="goodsId != null and goodsId != ''">
            AND mall_goods_attribute.goods_id = #{goodsId}
        </if>
        <if test="attributeId != null and attributeId != ''">
            AND mall_goods_attribute.attribute_id = #{attributeId}
        </if>
        <choose>
            <when test="sidx != null and sidx.trim() != ''">
                order by ${sidx} ${order}
            </when>
            <otherwise>
                order by id desc
            </otherwise>
        </choose>
        <if test="offset != null and limit != null">
            limit #{offset}, #{limit}
        </if>
    </select>

    <select id="queryTotal" resultType="int">
        select count(*) from mall_goods_attribute
        WHERE 1=1
        <if test="goodsId != null and goodsId != ''">
            AND goods_id = #{goodsId}
        </if>
        <if test="attributeId != null and attributeId != ''">
            AND attribute_id = #{attributeId}
        </if>
    </select>

    <insert id="save" parameterType="com.kmall.admin.entity.GoodsAttributeEntity" useGeneratedKeys="true" keyProperty="id">
        insert into mall_goods_attribute(
        `goods_id`,
        `merch_sn`,
        `attribute_id`,
        `value`)
        values(
        #{goodsId},
        #{merchSn},
        #{attributeId},
        #{value})
    </insert>

    <update id="update" parameterType="com.kmall.admin.entity.GoodsAttributeEntity">
        update mall_goods_attribute
        <set>
            <if test="goodsId != null">`goods_id` = #{goodsId},</if>
            <if test="merchSn != null">`merch_sn` = #{merchSn},</if>
            <if test="attributeId != null">`attribute_id` = #{attributeId},</if>
            <if test="value != null">`value` = #{value}</if>
        </set>
        where id = #{id}
    </update>

    <update id="updateByGoodsIdAttributeId" parameterType="com.kmall.admin.entity.GoodsAttributeEntity">
        update mall_goods_attribute
        <set>
            <if test="value != null">`value` = #{value}</if>
        </set>
        where goods_id = #{goodsId} AND attribute_id = #{attributeId}
    </update>

    <delete id="delete">
        delete from mall_goods_attribute where id = #{value}
    </delete>

    <delete id="deleteByGoodsId">
        delete from mall_goods_attribute where goods_id = #{goodsId}
    </delete>

    <delete id="deleteBatch">
        delete from mall_goods_attribute where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

</mapper>