<?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.B2cGoodCategoryMapper">

    <sql id="includeGoodsCategory">
        gc.id,
        gc.id good_category_id,
        gc.category_name category_name,
        COALESCE(gc.category_description, '') category_description,
        COALESCE(gc.parent_id, '') parent_id,
        COALESCE(gcp.category_name, '') parent_name,
        gc.sort,
        gc.status
    </sql>
    <resultMap id="resultMapGoodsCategory" type="java.util.LinkedHashMap">
        <id property="id" column="id" jdbcType="BIGINT" />
        <result property="good_category_id" column="good_category_id" javaType="java.lang.Long"/>
        <result property="parent_id" column="parent_id"/>
        <result property="category_name" column="category_name"/>
        <result property="category_description" column="category_description"/>
        <result property="sort" column="sort" javaType="java.lang.Integer" />
        <result property="status" column="status" javaType="java.lang.Integer" />
    </resultMap>

    <select id="queryGoodCategoryList" resultMap="resultMapGoodsCategory">
        SELECT
        <include refid="includeGoodsCategory" />
        FROM b2c_goods_category gc
        LEFT JOIN b2c_goods_category gcp ON gc.parent_id = gcp.id
        <where>
            <if test="category_name != null and category_name != ''">
                AND gc.category_name LIKE CONCAT('%', #{category_name}, '%')
            </if>
            <if test="status != null and status != ''">
                AND gc.status = #{status}
            </if>
        </where>
        ORDER BY gc.sort DESC
    </select>

    <select id="queryGoodCategoryDetail" resultMap="resultMapGoodsCategory">
        SELECT
        <include refid="includeGoodsCategory" />
        FROM b2c_goods_category gc
        LEFT JOIN b2c_goods_category gcp ON gc.parent_id = gcp.id
        WHERE gc.id = #{good_category_id}
    </select>

    <insert id="insertGoodCategory" parameterType="com.backendsys.entity.B2c.B2cGoodCategoryDTO"
        useGeneratedKeys="true" keyProperty="good_category_id">
        INSERT INTO b2c_goods_category (category_name
        <if test="parent_id != null and parent_id != ''">, parent_id</if>
        <if test="category_description != null and category_description != ''">, category_description</if>
        <if test="sort != null and sort != ''">, sort</if>
        <if test="status != null and status != ''">, status</if>
        )
        VALUES (#{category_name}
        <if test="parent_id != null and parent_id != ''">, #{parent_id}</if>
        <if test="category_description != null and category_description != ''">, #{category_description}</if>
        <if test="sort != null and sort != ''">, #{sort}</if>
        <if test="status != null and status != ''">, #{status}</if>
        )
    </insert>

    <update id="updateGoodCategory" parameterType="com.backendsys.entity.B2c.B2cGoodCategoryDTO"
        useGeneratedKeys="true" keyProperty="good_category_id">
        UPDATE b2c_goods_category
        SET category_name = #{category_name}
            <if test="category_description != null and category_description != ''">, category_description = #{category_description}</if>
            <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_category_id}
    </update>

    <delete id="deleteGoodCategory">
        DELETE FROM b2c_goods_category WHERE id = #{good_category_id}
    </delete>

</mapper>