<?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.yc.ship.module.resource.dal.mysql.category.CategoryMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->
<!--        mysql 8 特性 共用表表达式 CTE 递归查询-->
    <select id="selectListWithSupplierNum" resultType="com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO">
        WITH recursive temp AS (
        SELECT c.* FROM product_category c
        WHERE c.deleted = 0 AND c.use_status = 1
        <choose>
            <when test="reqVO.parentId != null">
                AND c.parent_id = #{reqVO.parentId}
            </when>
            <otherwise>
                AND c.parent_id = 0
            </otherwise>
        </choose>
        <choose>
            <when test="reqVO.type != null">
                AND c.type = #{reqVO.type}
            </when>
            <otherwise>
                AND c.type = 0
            </otherwise>
        </choose>
        UNION ALL
        SELECT c.* FROM product_category c
        INNER JOIN temp t2 ON t2.id = c.parent_id
        WHERE c.deleted = 0 AND c.use_status = 1
        <choose>
            <when test="reqVO.type != null">
                AND c.type = #{reqVO.type}
            </when>
            <otherwise>
                AND c.type = 0
            </otherwise>
        </choose>
        ) SELECT
        pc.id, pc.type,
        pc.parent_id parentId,
        pc.cate_name cateName,
        pc.use_status useStatus,
        pc.sort_num sortNum,
        pc.cate_code cateCode,
        pc.full_path_name fullPathName,
        pc.parent_path parentPath,
        pc.create_time createTime,
        COUNT(pr.id) resourceNum
        FROM temp pc LEFT JOIN product_resource pr ON pr.category_id = pc.id
        AND pr.is_use = 1 AND pr.deleted = 0 GROUP BY pc.id
    </select>

    <select id="getPlatTenantId" resultType="java.util.Map">
        select tenant_id from ota_distributor where account_tenant_id = #{currentTenantId} limit 1
    </select>

    <select id="getPlatAllTenantId" resultType="java.util.Map">
        select DISTINCT account_tenant_id account_tenant_id from ota_distributor where tenant_id = #{TenantId} and is_use = 1 and deleted=0
    </select>

    <select id="getCategoryByTenantIds" resultType="com.yc.ship.module.resource.dal.dataobject.category.CategoryDO">
        SELECT * FROM product_category WHERE tenant_id IN
        <foreach collection="mapList" index="index" item="item" open="(" separator="," close=")">
            #{item.account_tenant_id}
        </foreach> and deleted = 0 and type = 1
    </select>


    <select id="selectListByCatalogues"
            resultType="com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO">
        SELECT
            t1.id,
            t1.type,
            t1.parent_id parentId,
            t1.cate_name cateName,
            t1.use_status useStatus,
            t1.sort_num sortNum,
            t1.cate_code cateCode,
            t1.full_path_name fullPathName,
            t1.parent_path parentPath,
            t1.create_time createTime
        FROM
            product_category t1
            INNER JOIN product_category_extra t2 ON t1.id = t2.id
        WHERE
            t1.deleted = 0
            AND t2.deleted = 0
            AND t1.use_status = 1
        <if test="businessType == 1">
            AND FIND_IN_SET(#{catalogues}, t2.`columns`)
        </if>
        <if test="businessType == 2">
            AND FIND_IN_SET(#{catalogues}, t2.`type`)
        </if>
        ORDER BY t2.enable_hot_ads DESC
    </select>
    <select id="selectCategoryPage"
            resultType="com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO">
        SELECT
            t1.id,
            t1.type,
            t1.parent_id parentId,
            t1.cate_name cateName,
            t1.use_status useStatus,
            t1.sort_num sortNum,
            t1.cate_code cateCode,
            t1.full_path_name fullPathName,
            t1.parent_path parentPath,
            t1.create_time createTime,
            t2.type typeId
        FROM
            product_category t1
            INNER JOIN product_category_extra t2 ON t1.id = t2.id
        WHERE
            t1.use_status = 1
            AND t1.deleted = 0
        <if test="param.cateName != null">
            AND t1.cate_name LIKE CONCAT('%',#{param.cateName},'%')
        </if>
        <if test="param.cateCode != null">
            AND t1.cate_code LIKE CONCAT('%',#{param.cateCode},'%')
        </if>
        <if test="param.type != null">
            AND t1.type = #{param.type}
        </if>
        <if test="param.parentId != null">
            AND t1.parent_id = #{param.parentId}
        </if>
        <if test="param.typeId != null and param.typeId != '-1'">
            AND t2.type = #{param.typeId}
        </if>
        ORDER BY t2.id DESC
    </select>

    <select id="getListByIds" resultType="com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO">
        WITH recursive temp AS (
            SELECT c.* FROM product_category c WHERE c.deleted = 0 AND c.use_status = 1
            <choose>
                <when test="reqVO.type != null">
                    AND c.type = #{reqVO.type}
                </when>
                <otherwise>
                    AND c.type = 0
                </otherwise>
            </choose>
            <if test="reqVO.ids != null and reqVO.ids.size() > 0">
                AND c.id IN
                <foreach collection="reqVO.ids" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            UNION ALL
            SELECT c.* FROM product_category c
            INNER JOIN temp t2 ON t2.parent_id = c.id WHERE c.deleted = 0 AND c.use_status = 1
            <choose>
                <when test="reqVO.type != null">
                    AND c.type = #{reqVO.type}
                </when>
                <otherwise>
                    AND c.type = 0
                </otherwise>
            </choose>
        ) SELECT
              pc.id, pc.type,
              pc.parent_id parentId,
              pc.cate_name cateName,
              pc.use_status useStatus,
              pc.sort_num sortNum,
              pc.cate_code cateCode,
              pc.full_path_name fullPathName,
              pc.parent_path parentPath,
              pc.create_time createTime
        FROM temp pc group by pc.id
    </select>

    <select id="getAllUpCategoryById" resultType="com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO">
        WITH recursive temp AS (
        SELECT c.* FROM product_category c WHERE c.deleted = 0 AND c.use_status = 1
        <choose>
            <when test="type != null">
                AND c.type = #{type}
            </when>
            <otherwise>
                AND c.type = 0
            </otherwise>
        </choose>
        AND c.id =  #{id}
        UNION ALL
        SELECT c.* FROM product_category c
        INNER JOIN temp t2 ON t2.parent_id = c.id WHERE c.deleted = 0 AND c.use_status = 1
        <choose>
            <when test="type != null">
                AND c.type = #{type}
            </when>
            <otherwise>
                AND c.type = 0
            </otherwise>
        </choose>
        ) SELECT
        pc.id, pc.type,
        pc.parent_id parentId,
        pc.cate_name cateName,
        pc.use_status useStatus,
        pc.sort_num sortNum,
        pc.cate_code cateCode,
        pc.full_path_name fullPathName,
        pc.parent_path parentPath,
        pc.create_time createTime
        FROM temp pc group by pc.id
        ORDER BY create_time
    </select>

    <select id="getAllDownCategoryById" resultType="com.yc.ship.module.resource.controller.admin.category.vo.CategoryRespVO">
        WITH recursive temp AS (
        SELECT c.* FROM product_category c WHERE c.deleted = 0 AND c.use_status = 1
        <choose>
            <when test="type != null">
                AND c.type = #{type}
            </when>
            <otherwise>
                AND c.type = 0
            </otherwise>
        </choose>
        AND c.id =  #{id}
        UNION ALL
        SELECT c.* FROM product_category c
        INNER JOIN temp t2 ON t2.id = c.parent_id WHERE c.deleted = 0 AND c.use_status = 1
        <choose>
            <when test="type != null">
                AND c.type = #{type}
            </when>
            <otherwise>
                AND c.type = 0
            </otherwise>
        </choose>
        ) SELECT
        pc.id, pc.type,
        pc.parent_id parentId,
        pc.cate_name cateName,
        pc.use_status useStatus,
        pc.sort_num sortNum,
        pc.cate_code cateCode,
        pc.full_path_name fullPathName,
        pc.parent_path parentPath,
        pc.create_time createTime
        FROM temp pc group by pc.id
        ORDER BY create_time
    </select>

    <update id="updateProductCategoryId">
        update product_spu set category_id = #{id} where category_id = #{parentId} and deleted = 0
    </update>
</mapper>
