<?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.goodsCategory.GoodsCategoryMapper">

    <resultMap type="GoodsCategory" id="GoodsCategoryResult">
        <result property="id" column="id"/>
        <result property="parentId" column="parent_id"/>
        <result property="goodsCategoryName" column="goods_category_name"/>
        <result property="goodsCategoryCode" column="goods_category_code"/>
        <result property="goodsCategoryPicture" column="goods_category_picture"/>
        <result property="categoryRemark" column="category_remark"/>
        <result property="categoryLevel" column="category_level"/>
        <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="selectGoodsCategoryVo">
        select id,
               parent_id,
               goods_category_name,
               goods_category_code,
               goods_category_picture,
               category_remark,
               category_level,
               sort,
               create_by,
               create_time,
               update_by,
               update_time
        from goods_category
    </sql>

    <select id="selectGoodsCategoryList" parameterType="GoodsCategory" resultMap="GoodsCategoryResult">
        <include refid="selectGoodsCategoryVo"/>
        <where>
            <if test="parentId != null">
                and parent_id =#{parentId}
            </if>
            <if test="goodsCategoryName != null  and goodsCategoryName != ''">
                and goods_category_name like concat('%', #{goodsCategoryName}, '%')
            </if>
            <if test="goodsCategoryCode != null  and goodsCategoryCode != ''">
                and goods_category_code = #{goodsCategoryCode}
            </if>
            <if test="goodsCategoryPicture != null  and goodsCategoryPicture != ''">
                and goods_category_picture = #{goodsCategoryPicture}
            </if>
            <if test="categoryRemark != null  and categoryRemark != ''">
                and category_remark = #{categoryRemark}
            </if>
            <if test="sort != null ">
                and sort = #{sort}
            </if>
        </where>
        order by sort
    </select>

    <select id="selectGoodsCategoryById" parameterType="Long"
            resultMap="GoodsCategoryResult">
        <include refid="selectGoodsCategoryVo"/>
        where id = #{id}
    </select>

    <insert id="insertGoodsCategory" parameterType="GoodsCategory" useGeneratedKeys="true"
            keyProperty="id">
        insert into goods_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="parentId != null">parent_id,
            </if>
            <if test="goodsCategoryName != null">goods_category_name,
            </if>
            <if test="goodsCategoryCode != null">goods_category_code,
            </if>
            <if test="goodsCategoryPicture != null">goods_category_picture,
            </if>
            <if test="categoryRemark != null">category_remark,
            </if>
            <if test="categoryLevel != null">category_level,
            </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="parentId != null">#{parentId},
            </if>
            <if test="goodsCategoryName != null">#{goodsCategoryName},
            </if>
            <if test="goodsCategoryCode != null">#{goodsCategoryCode},
            </if>
            <if test="goodsCategoryPicture != null">#{goodsCategoryPicture},
            </if>
            <if test="categoryRemark != null">#{categoryRemark},
            </if>
            <if test="categoryLevel != null">#{categoryLevel},
            </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="updateGoodsCategory" parameterType="GoodsCategory">
        update goods_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="parentId != null">parent_id =
                #{parentId},
            </if>
            <if test="goodsCategoryName != null">goods_category_name =
                #{goodsCategoryName},
            </if>
            <if test="goodsCategoryCode != null">goods_category_code =
                #{goodsCategoryCode},
            </if>
            <if test="goodsCategoryPicture != null">goods_category_picture =
                #{goodsCategoryPicture},
            </if>
            <if test="categoryRemark != null">category_remark =
                #{categoryRemark},
            </if>
            <if test="categoryLevel != null">category_level =
                #{categoryLevel},
            </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="deleteGoodsCategoryById" parameterType="Long">
        delete
        from goods_category
        where id = #{id}
    </delete>

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

    <select id="selectCategoryPictureByIds" resultType="string">
        select goods_category_picture
        from goods_category where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectCategoryByIds" resultType="com.xinelu.manage.domain.goodsCategory.GoodsCategory">
        SELECT
        id,
        parent_id,
        goods_category_name,
        goods_category_picture,
        category_level
        FROM
        goods_category
        WHERE
        id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
        UNION ALL
        SELECT
        id,
        parent_id,
        goods_category_name,
        goods_category_picture,
        category_level
        FROM
        goods_category
        WHERE
        parent_id IN
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectParentIdById" resultType="com.xinelu.manage.vo.goodsCategory.ParentCategory">
        select gc.id,
               gc.parent_id,
               gc.goods_category_name,
               gc.goods_category_code,
               gc.goods_category_picture,
               gc.category_remark,
               gc.category_level,
               gc.sort,
               (select goods_category_name from goods_category where id = gc.parent_id) parentName
        from goods_category gc
        where gc.id = #{id}
    </select>

    <select id="getGoodsCategoryNameCount" resultType="java.lang.Integer">
        SELECT count(1)
        FROM goods_category
        WHERE goods_category_name = #{goodsCategoryName}
        <if test="id != null">
            and id &lt;&gt; #{id}
        </if>
    </select>
</mapper>
