<?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.backendsys.mapper.B2c.B2cGoodBrandMapper">

    <sql id="includeGoodsBrand">
        gb.id id,
        gb.id good_brand_id,
        gb.brand_name,
        COALESCE(gb.parent_id, '') parent_id,
        COALESCE(gbp.brand_name, '') parent_name,
        gb.sort,
        gb.status
    </sql>
    <resultMap id="resultMapGoodsBrand" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="good_brand_id" column="good_brand_id" javaType="java.lang.Long"/>
        <result property="parent_id" column="parent_id"/>
        <result property="brand_name" column="brand_name" />
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
    </resultMap>

    <select id="queryGoodBrandList" resultMap="resultMapGoodsBrand">
        SELECT
        <include refid="includeGoodsBrand" />
        FROM b2c_goods_brand gb
        LEFT JOIN b2c_goods_brand gbp ON gb.parent_id = gbp.id
        <where>
            <if test="brand_name != null and brand_name != ''">
                AND gb.brand_name LIKE CONCAT('%', #{brand_name}, '%')
            </if>
            <if test="status != null and status != ''">
                AND gb.status = #{status}
            </if>
        </where>
        ORDER BY gb.sort DESC
    </select>

    <select id="queryGoodBrandDetail" resultMap="resultMapGoodsBrand">
        SELECT
        <include refid="includeGoodsBrand" />
        FROM b2c_goods_brand gb
        LEFT JOIN b2c_goods_brand gbp ON gb.parent_id = gbp.id
        WHERE gb.id = #{good_brand_id}
    </select>

    <insert id="insertGoodBrand" parameterType="com.backendsys.entity.B2c.B2cGoodBrandDTO"
        useGeneratedKeys="true" keyProperty="good_brand_id">
        INSERT INTO b2c_goods_brand (brand_name
        <if test="parent_id != null and parent_id != ''">, parent_id</if>
        <if test="sort != null and sort != ''">, sort</if>
        <if test="status != null and status != ''">, status</if>
        )
        VALUES (#{brand_name}
        <if test="parent_id != null and parent_id != ''">, #{parent_id}</if>
        <if test="sort != null and sort != ''">, #{sort}</if>
        <if test="status != null and status != ''">, #{status}</if>
        )
    </insert>

    <update id="updateGoodBrand" parameterType="com.backendsys.entity.B2c.B2cGoodBrandDTO"
        useGeneratedKeys="true" keyProperty="good_brand_id">
        UPDATE b2c_goods_brand
        SET brand_name = #{brand_name}
            <if test="parent_id != null and parent_id != ''">, parent_id = #{parent_id}</if>
            <if test="sort != null and sort != ''">, sort = #{sort}</if>
            <if test="status != null and status != ''">, status = #{status}</if>
        WHERE id = #{good_brand_id}
    </update>

    <delete id="deleteGoodBrand">
        DELETE FROM b2c_goods_brand WHERE id = #{good_brand_id}
    </delete>

</mapper>