<?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.ruoyi.web.modules.category.mapper.BizGoodsCategoryMapper">

    <resultMap id="BizGoodsCategoryResult" type="com.ruoyi.web.modules.category.domain.BizGoodsCategory">
        <id property="categoryId" column="category_id"/>
        <result property="shopId" column="shop_id"/>
        <result property="parentId" column="parent_id"/>
        <result property="categoryLevel" column="category_level"/>
        <result property="categoryName" column="category_name"/>
        <result property="categoryPic" column="category_pic"/>
        <result property="sortNo" column="sort_no"/>
        <result property="showFlag" column="show_flag"/>
        <result property="hotFlag" column="hot_flag"/>
        <result property="delFlag" column="del_flag"/>
        <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="parentName" column="parent_name"/>
    </resultMap>

    <sql id="selectVo">
        select c.category_id, c.shop_id, c.parent_id, c.category_level, c.category_name, c.category_pic,
               c.sort_no, c.show_flag, c.hot_flag, c.del_flag, c.create_by, c.create_time, c.update_by, c.update_time,
               p.category_name as parent_name
        from biz_goods_category c
        left join biz_goods_category p on c.parent_id = p.category_id and p.del_flag = '0'
            and ((c.shop_id is null and p.shop_id is null) or (c.shop_id = p.shop_id))
    </sql>

    <sql id="shopIdCondition">
        <choose>
            <when test="shopId != null">
                and shop_id = #{shopId}
            </when>
            <otherwise>
                and shop_id is null
            </otherwise>
        </choose>
    </sql>

    <sql id="cShopIdCondition">
        <choose>
            <when test="shopId != null">
                and c.shop_id = #{shopId}
            </when>
            <otherwise>
                and c.shop_id is null
            </otherwise>
        </choose>
    </sql>

    <select id="selectById" resultMap="BizGoodsCategoryResult">
        <include refid="selectVo"/>
        where c.category_id = #{categoryId} and c.del_flag = '0'
        <include refid="cShopIdCondition"/>
    </select>

    <select id="selectByIdIgnoreShop" resultMap="BizGoodsCategoryResult">
        select category_id, shop_id, parent_id, category_level, category_name, category_pic,
               sort_no, show_flag, hot_flag, del_flag
        from biz_goods_category
        where category_id = #{categoryId} and del_flag = '0'
    </select>

    <select id="selectList" resultMap="BizGoodsCategoryResult">
        <include refid="selectVo"/>
        where c.del_flag = '0'
        <include refid="cShopIdCondition"/>
        <if test="keyword != null and keyword != ''">
            and c.category_name like concat('%', #{keyword}, '%')
        </if>
        <if test="categoryLevel != null and categoryLevel != ''">
            and c.category_level = #{categoryLevel}
        </if>
        order by
            ifnull(case when c.category_level = '1' then c.sort_no else p.sort_no end, 0) asc,
            case when c.category_level = '1' then c.category_id else c.parent_id end asc,
            c.category_level asc,
            ifnull(c.sort_no, 0) asc,
            c.create_time asc
    </select>

    <select id="countByName" resultType="int">
        select count(1) from biz_goods_category
        where parent_id = #{parentId} and category_name = #{categoryName}
          and del_flag = '0'
        <include refid="shopIdCondition"/>
        <if test="excludeId != null">
            and category_id != #{excludeId}
        </if>
    </select>

    <insert id="insert" useGeneratedKeys="true" keyProperty="categoryId">
        insert into biz_goods_category (
            shop_id, parent_id, category_level, category_name, category_pic,
            sort_no, show_flag, hot_flag, del_flag, create_by, create_time
        ) values (
            #{shopId}, #{parentId}, #{categoryLevel}, #{categoryName}, #{categoryPic},
            #{sortNo}, #{showFlag}, #{hotFlag}, '0', #{createBy}, sysdate()
        )
    </insert>

    <update id="update">
        update biz_goods_category
        <set>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="categoryLevel != null">category_level = #{categoryLevel},</if>
            <if test="categoryName != null">category_name = #{categoryName},</if>
            <if test="categoryPic != null">category_pic = #{categoryPic},</if>
            <if test="sortNo != null">sort_no = #{sortNo},</if>
            <if test="showFlag != null">show_flag = #{showFlag},</if>
            <if test="hotFlag != null">hot_flag = #{hotFlag},</if>
            update_by = #{updateBy},
            update_time = sysdate()
        </set>
        where category_id = #{categoryId} and del_flag = '0'
        <include refid="shopIdCondition"/>
    </update>

    <update id="logicDeleteByIds">
        update biz_goods_category
        set del_flag = '2', update_by = #{updateBy}, update_time = sysdate()
        where del_flag = '0'
        <include refid="shopIdCondition"/>
          and category_id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectChildIds" resultType="long">
        select category_id from biz_goods_category
        where parent_id = #{parentId} and del_flag = '0'
        <include refid="shopIdCondition"/>
    </select>

</mapper>
