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

    <resultMap id="DeptGoodsCategoryResult" type="com.ghy.goods.domain.DeptGoodsCategory">
        <result property="deptGoodsCategoryId"      column="dept_goods_category_id"/>
        <result property="deptId"                   column="dept_id"/>
        <result property="goodsCategoryId"          column="goods_category_id"/>
        <result property="goodsCategoryName"        column="goods_category_name"/>
        <result property="parentCategoryId"         column="parent_category_id"     />
        <result property="level"                    column="level"     />
        <result property="status"                   column="status"     />
        <result property="type"                     column="type"     />
        <result property="categorySort"             column="category_sort"/>
        <result property="oneRate"                  column="one_rate"/>
        <result property="twoRate"                  column="two_rate"/>
        <result property="threeRate"                column="three_rate"/>
        <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="selectDeptGoodsCategory">
        SELECT dept_goods_category_id, dept_id, goods_category_id, category_sort,
               one_rate, two_rate, three_rate, create_by, create_time, remark
        FROM dept_goods_category
    </sql>

    <sql id="selectJoin">
        SELECT dgc.dept_goods_category_id, dgc.dept_id, dgc.goods_category_id, dgc.category_sort,
               dgc.one_rate, dgc.two_rate, dgc.three_rate, dgc.create_by, dgc.create_time, dgc.remark,
               gc.goods_category_name, gc.level, gc.parent_category_id, gc.type, gc.status
        FROM dept_goods_category dgc LEFT JOIN goods_category gc ON dgc.goods_category_id = gc.goods_category_id
    </sql>

    <select id="appList" parameterType="com.ghy.goods.domain.DeptGoodsCategory" resultMap="DeptGoodsCategoryResult">
        <include refid="selectJoin"/>
        <where>
            dgc.dept_id = #{deptId}
            <if test="goodsCategoryId == null ">
                and gc.parent_category_id is null
            </if>
            <if test="goodsCategoryId != null ">
                and gc.parent_category_id = #{goodsCategoryId}
            </if>
        </where>

    </select>

    <update id="updateDeptGoodsCategory" parameterType="com.ghy.goods.domain.DeptGoodsCategory">
        UPDATE dept_goods_category
        <set>
            <if test="categorySort != null">category_sort = #{categorySort},</if>
            <if test="oneRate != null and oneRate != ''">one_rate = #{oneRate},</if>
            <if test="twoRate != null and twoRate != ''">two_rate = #{twoRate},</if>
            <if test="threeRate != null and threeRate != ''">three_rate = #{threeRate},</if>
            <if test="remark != null and remark != ''">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        WHERE dept_goods_category_id = #{deptGoodsCategoryId}
    </update>

    <delete id="deleteDeptGoodsCategoryByIds" parameterType="Long">
        DELETE FROM dept_goods_category WHERE dept_goods_category_id IN
        <foreach collection="ids" item="goodsCategoryId" open="(" separator="," close=")">
            #{goodsCategoryId}
        </foreach>
    </delete>

    <delete id="deleteById">
        DELETE FROM dept_goods_category WHERE dept_goods_category_id = #{id}
    </delete>

    <insert id="insertDeptGoodsCategory" parameterType="com.ghy.goods.domain.DeptGoodsCategory" useGeneratedKeys="true"
    keyProperty="goodsCategoryId">
        insert into dept_goods_category(
        <if test="deptId != null and deptId != ''">dept_id,</if>
        <if test="goodsCategoryId != null">goods_category_id,</if>
        <if test="categorySort != null">category_sort,</if>
        <if test="oneRate != null and oneRate != ''">one_rate,</if>
        <if test="twoRate != null and twoRate != ''">two_rate,</if>
        <if test="threeRate != null and threeRate != ''">three_rate,</if>
        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="deptId != null and deptId != ''">#{deptId},</if>
        <if test="goodsCategoryId != null">#{goodsCategoryId},</if>
        <if test="categorySort != null">#{categorySort},</if>
        <if test="oneRate != null and oneRate != ''">#{oneRate},</if>
        <if test="twoRate != null and twoRate != ''">#{twoRate},</if>
        <if test="threeRate != null and threeRate != ''">#{threeRate},</if>
        <if test="createBy != null and createBy != ''">#{createBy},</if>
        sysdate()
        )
    </insert>

    <select id="selectList" parameterType="com.ghy.goods.domain.DeptGoodsCategory" resultMap="DeptGoodsCategoryResult">
        <include refid="selectJoin"/>
        <where>
            <if test="deptId != null">
                AND dgc.dept_id = #{deptId}
            </if>
            <if test="goodsCategoryName != null and goodsCategoryName != ''">
                AND gc.goods_category_name LIKE concat('%', #{goodsCategoryName}, '%')
            </if>
            <if test="status != null and status != ''">
                AND gc.status = #{status}
            </if>
        </where>
    </select>

    <select id="selectById" parameterType="long" resultMap="DeptGoodsCategoryResult">
        <include refid="selectJoin"/>
        WHERE dgc.dept_goods_category_id = #{deptGoodsCategoryId}
    </select>

    <select id="selectOneByGoodsCategoryId" resultMap="DeptGoodsCategoryResult">
        <include refid="selectDeptGoodsCategory"/>
        WHERE goods_category_id = #{goodsCategoryId} LIMIT 1
    </select>

    <select id="selectByDeptId" resultMap="DeptGoodsCategoryResult">
        <include refid="selectDeptGoodsCategory"/>
        WHERE dept_id = #{deptId}
    </select>
</mapper>
