<?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.ghy.goods.mapper.GoodsCategoryMapper">

    <resultMap id="GoodsCategoryResult" type="com.ghy.goods.domain.GoodsCategory">
        <result property="goodsCategoryId"     column="goods_category_id"     />
        <result property="goodsCategoryCode"   column="goods_category_code"     />
        <result property="goodsCategoryName"   column="goods_category_name"     />
        <result property="parentCategoryId"    column="parent_category_id"     />
        <result property="level"               column="level"     />
        <result property="type"                column="type"     />
        <result property="createBy"            column="create_by"     />
        <result property="createTime"          column="create_time"   />
        <result property="updateBy"            column="update_by"     />
        <result property="updateTime"          column="update_time"   />
        <result property="remark"              column="remark"        />
    </resultMap>

    <sql id="selectGoodsCategory">
        SELECT goods_category_id, goods_category_code, goods_category_name, parent_category_id,
               level, type, create_by, create_time, remark
        FROM goods_category
    </sql>

    <select id="selectGoodsCategoryList" parameterType="com.ghy.goods.domain.GoodsCategory" resultMap="GoodsCategoryResult">
        <include refid="selectGoodsCategory" />
        <where>
            <if test="goodsCategoryCode != null and goodsCategoryCode != ''">
                AND goods_category_code like concat('%', #{goodsCategoryCode}, '%')
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="GoodsCategoryResult">
        <include refid="selectGoodsCategory"/>
        WHERE goods_category_id = #{goodsCategoryId}
    </select>

    <update id="updateGoodsCategory" parameterType="com.ghy.goods.domain.GoodsCategory">
        UPDATE goods_category
        <set>
            <if test="goodsCategoryCode != null and goodsCategoryCode != ''">goods_category_code = #{goodsCategoryCode},</if>
            <if test="goodsCategoryName != null and goodsCategoryName != ''">goods_category_name = #{goodsCategoryName},</if>
            <if test="parentCategoryId != null and parentCategoryId != ''">parent_category_id = #{parentCategoryId},</if>
            <if test="level != null and level != ''">level = #{level},</if>
            <if test="type != null and type != ''">type = #{type},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        WHERE goods_category_id = #{goodsCategoryId}
    </update>

    <delete id="deleteGoodsCategoryByIds" parameterType="Long">
        DELETE FROM goods_category WHERE goods_category_id IN
        <foreach collection="array" item="goodsCategoryId" open="(" separator="," close=")">
            #{goodsCategoryId}
        </foreach>
    </delete>

    <insert id="insertCategoryGoods" parameterType="com.ghy.goods.domain.GoodsCategory" useGeneratedKeys="true"
            keyProperty="goodsCategoryId">
        insert into goods_category(
        <if test="goodsCategoryId != null and goodsCategoryId != 0">goods_category_id,</if>
        <if test="goodsCategoryCode != null and goodsCategoryCode != ''">goods_category_code,</if>
        <if test="goodsCategoryName != null and goodsCategoryName != ''">goods_category_name,</if>
        <if test="parentCategoryId != null and parentCategoryId != ''">parent_category_id,</if>
        <if test="level != null and level != ''">level,</if>
        <if test="type != null and type != ''">type,</if>
        <if test="remark != null and remark != ''">remark,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="goodsCategoryId != null and goodsCategoryId != 0">#{goodsCategoryId},</if>
        <if test="goodsCategoryCode != null and goodsCategoryCode != ''">#{goodsCategoryCode},</if>
        <if test="goodsCategoryName != null and goodsCategoryName != ''">#{goodsCategoryName},</if>
        <if test="parentCategoryId != null and parentCategoryId != ''">#{parentCategoryId},</if>
        <if test="level != null and level != ''">#{level},</if>
        <if test="type != null and type != ''">#{type},</if>
        <if test="remark != null and remark != ''">#{remark},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <select id="selectByGoodsCategoryName" parameterType="long" resultMap="GoodsCategoryResult">
        <include refid="selectGoodsCategory"/>
        WHERE goods_category_name = #{goodsCategoryName} LIMIT 1
    </select>

    <select id="selectByGoodsCategoryCode" parameterType="long" resultMap="GoodsCategoryResult">
        <include refid="selectGoodsCategory"/>
        WHERE goods_category_code = #{goodsCategoryCode} LIMIT 1
    </select>
</mapper>