<?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="cover"                    column="cover"/>
        <result property="simpleGoodsCategoryName"  column="simple_goods_category_name"/>
        <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="isHot"                    column="is_hot"/>
        <result property="hotName"                  column="hot_name"/>
        <result property="oneRate"                  column="one_rate"/>
        <result property="twoRate"                  column="two_rate"/>
        <result property="threeRate"                column="three_rate"/>
        <result property="deptRate"                 column="dept_rate"/>
        <result property="deptMoney"                column="dept_money"/>
        <result property="retainRate"               column="retain_rate"/>
        <result property="retainMoney"              column="retain_money"/>
        <!-- pc扣点部分 start-->
        <result property="pcOneRate"                  column="pc_one_rate"/>
        <result property="pcTwoRate"                  column="pc_two_rate"/>
        <result property="pcThreeRate"                column="pc_three_rate"/>
        <result property="pcDeptRate"                 column="pc_dept_rate"/>
        <result property="pcDeptMoney"                column="pc_dept_money"/>
        <result property="pcRetainRate"               column="pc_retain_rate"/>
        <result property="pcRetainMoney"              column="pc_retain_money"/>
        <!-- pc扣点部分 end-->
        <result property="isSure"                   column="is_sure"/>
        <result property="isCert"                   column="is_cert"/>
        <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, is_hot, cover, hot_name,
               simple_goods_category_name, is_sure, is_cert, one_rate, two_rate, three_rate, dept_rate,
               pc_one_rate, pc_two_rate, pc_three_rate, pc_dept_rate, pc_dept_money, pc_retain_rate, pc_retain_money,
               dept_money, retain_rate, retain_money, 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,
               dgc.is_hot, dgc.cover, dgc.hot_name, dgc.is_sure, dgc.is_cert, dgc.dept_rate, dgc.dept_money,
               dgc.retain_rate, dgc.retain_money, dgc.simple_goods_category_name,
               dgc.pc_one_rate, dgc.pc_two_rate, dgc.pc_three_rate, dgc.pc_dept_rate, dgc.pc_dept_money, dgc.pc_retain_rate, dgc.pc_retain_money,
               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>
            <if test="type != null ">
                and gc.type = #{type}
            </if>
            <if test="deptGoodsCategoryId != null">
                and dgc.dept_goods_category_id = #{deptGoodsCategoryId}
            </if>
        </where>
        ORDER BY gc.level, dgc.category_sort
    </select>

    <update id="updateDeptGoodsCategory" parameterType="com.ghy.goods.domain.DeptGoodsCategory">
        UPDATE dept_goods_category
        <set>
            <if test="isHot != null">is_hot = #{isHot},</if>
            <if test="cover != null and cover != ''">cover = #{cover},</if>
            <if test="hotName != null">hot_name = #{hotName},</if>
            <if test="simpleGoodsCategoryName != null">simple_goods_category_name = #{simpleGoodsCategoryName},</if>
            <if test="isSure != null">is_sure = #{isSure},</if>
            <if test="isCert != null">is_cert = #{isCert},</if>
            <if test="deptRate != null and deptRate != ''">dept_rate = #{deptRate},</if>
            <if test="deptMoney != null and deptMoney != ''">dept_money = #{deptMoney},</if>
            <if test="retainRate != null and retainRate != ''">retain_rate = #{retainRate},</if>
            <if test="retainMoney != null and retainMoney != ''">retain_money = #{retainMoney},</if>

            <if test="pcDeptRate != null and pcDeptRate != ''">pc_dept_rate = #{pcDeptRate},</if>
            <if test="pcDeptMoney != null and pcDeptMoney != ''">pc_dept_money = #{pcDeptMoney},</if>
            <if test="pcRetainRate != null and pcRetainRate != ''">pc_retain_rate = #{pcRetainRate},</if>
            <if test="pcRetainMoney != null and pcRetainMoney != ''">pc_retain_money = #{pcRetainMoney},</if>

            <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="pcOneRate != null and pcOneRate != ''">pc_one_rate = #{pcOneRate},</if>
            <if test="pcTwoRate != null and pcTwoRate != ''">pc_two_rate = #{pcTwoRate},</if>
            <if test="pcThreeRate != null and pcThreeRate != ''">pc_three_rate = #{pcThreeRate},</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="isHot != null and isHot != ''">is_hot,</if>
        <if test="deptRate != null and deptRate != ''">dept_rate,</if>
        <if test="deptMoney != null and deptMoney != ''">dept_money,</if>
        <if test="retainRate != null and retainRate != ''">retain_rate,</if>
        <if test="retainMoney != null and retainMoney != ''">retain_money,</if>

        <if test="pcDeptRate != null and pcDeptRate != ''">pc_dept_rate,</if>
        <if test="pcDeptMoney != null and pcDeptMoney != ''">pc_dept_money,</if>
        <if test="pcRetainRate != null and pcRetainRate != ''">pc_retain_rate,</if>
        <if test="pcRetainMoney != null and pcRetainMoney != ''">pc_retain_money,</if>

        <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="pcOneRate != null and pcOneRate != ''">pc_one_rate,</if>
        <if test="pcTwoRate != null and pcTwoRate != ''">pc_two_rate,</if>
        <if test="pcThreeRate != null and pcThreeRate != ''">pc_three_rate,</if>

        <if test="createBy != null and createBy != ''">create_by,</if>
        create_time
        )values(
        <if test="isHot != null and isHot != ''">#{isHot},</if>
        <if test="deptRate != null and deptRate != ''">#{deptRate},</if>
        <if test="deptMoney != null and deptMoney != ''">#{deptMoney},</if>
        <if test="retainRate != null and retainRate != ''">#{retainRate},</if>
        <if test="retainMoney != null and retainMoney != ''">#{retainMoney},</if>

        <if test="pcDeptRate != null and pcDeptRate != ''">#{pcDeptRate},</if>
        <if test="pcDeptMoney != null and pcDeptMoney != ''">#{pcDeptMoney},</if>
        <if test="pcRetainRate != null and pcRetainRate != ''">#{pcRetainRate},</if>
        <if test="pcRetainMoney != null and pcRetainMoney != ''">#{pcRetainMoney},</if>

        <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="pcOneRate != null and pcOneRate != ''">#{pcOneRate},</if>
        <if test="pcTwoRate != null and pcTwoRate != ''">#{pcTwoRate},</if>
        <if test="pcThreeRate != null and pcThreeRate != ''">#{pcThreeRate},</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>
            <if test="level != null">
                AND gc.level = #{level}
            </if>
            <if test="isHot != null">
                AND dgc.is_hot = #{isHot}
            </if>
            <if test="type != null">
                AND gc.type = #{type}
            </if>
        </where>
        ORDER BY dgc.category_sort
    </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="selectJoin"/>
        WHERE gc.goods_category_id = #{goodsCategoryId} LIMIT 1
    </select>

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