<?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.resource.ResourceMapper">

    <!--
        一般情况下，尽可能使用 Mapper 进行 CRUD 增删改查即可。
        无法满足的场景，例如说多表关联查询，才使用 XML 编写 SQL。
        代码生成器暂时只生成 Mapper XML 文件本身，更多推荐 MybatisX 快速开发插件来生成查询。
        文档可见：https://www.iocoder.cn/MyBatis/x-plugins/
     -->

    <select id="queryPage" resultType="com.yc.ship.module.resource.dal.dataobject.resource.ResourceDO">
        SELECT
            pr.id,ps.is_system_direct accessType,price,pr.is_use,pr.name,pr.source_type,pr.start_time,pr.end_time,
            pr.third_resource_id,pr.category_id,pc.cate_name as category_name,
            pr.supplier_id,ps.name as supplier_name,pr.create_time , pr.update_time
        from product_resource pr
        LEFT JOIN product_supplier ps on pr.supplier_id = ps.id
        LEFT JOIN product_category pc on pc.id = pr.category_id
        where 1=1
        AND pr.deleted = 0
        AND ps.deleted = 0
        AND pc.deleted = 0
        <if test="param.name != null and param.name !=''">
            <bind name="bindName" value="'%'+param.name+'%'"/>
            and pr.name like #{bindName}
        </if>
        <if test="param.sourceType != null ">
            and FIND_IN_SET(pr.source_type,#{param.sourceType})
        </if>
        <if test="param.accessType != null ">
            and FIND_IN_SET(pr.access_type,#{param.accessType})
        </if>
        <if test="param.isUse != null">
            and FIND_IN_SET(pr.is_use,#{param.isUse})
        </if>
        <if test="param.categoryId != null and param.categoryId !=''  and param.categoryId !=0 ">
            and pr.category_id =#{param.categoryId}
        </if>
        <if test="param.supplierId != null and param.supplierId !=''">
            and pr.supplier_id =#{param.supplierId}
        </if>
        order by pr.create_time desc , pr.update_time desc

    </select>

    <select id="getByProductBaseId" resultType="com.yc.ship.module.resource.dal.dataobject.resource.ResourceDO">
        SELECT pr.*, ps.`name` supplierName, ps.is_system_direct accessType FROM product_resource pr
            INNER JOIN product_group pg ON pg.first_resource_id = pr.id
            INNER JOIN product_base pb ON pb.id = pg.product_base_id
            INNER JOIN product_supplier ps ON ps.id = pr.supplier_id
        WHERE pr.is_use = 1 and pr.deleted = 0 and pb.id = #{id} GROUP BY pr.id
    </select>

    <select id="getListWithoutExcludeId" resultType="com.yc.ship.module.resource.dal.dataobject.resource.ResourceDO">
        SELECT pr.*, ps.`name` supplierName, ps.is_system_direct accessType FROM product_resource pr
            INNER JOIN product_base pb ON pb.resource_category_id = pr.category_id
        INNER JOIN product_supplier ps ON ps.id = pr.supplier_id

        WHERE pr.is_use = 1
            AND pr.deleted = 0
            AND pb.id = #{productBaseId}
        and (pb.fixed_resource_id = pr.id or pb.fixed_resource_id is null)
        <if test="excludeId != null">
            AND pr.id != #{excludeId}
        </if>
        ORDER BY case WHEN pr.id = pb.first_resource_id then 1 ELSE 0 end desc,pr.price, IFNUll(pr.stock, 99999) DESC
    </select>

    <select id="getByResourceId" resultType="com.yc.ship.module.resource.dal.dataobject.resource.ResourceDO">
        SELECT pr.*, ps.`name` supplierName, ps.is_system_direct accessType FROM product_resource pr
         INNER JOIN product_supplier ps ON ps.id = pr.supplier_id
        WHERE pr.is_use = 1 and pr.deleted = 0 and pr.id = #{id} 
    </select>

    <select id="getResourceByCategoryId" resultType="com.yc.ship.module.resource.dal.dataobject.resource.ResourceDO">
        SELECT * FROM product_resource
        WHERE deleted = 0 AND category_id IN (
            WITH recursive temp AS (
                SELECT c.* FROM product_category c WHERE c.id = #{categoryId}
                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
            ) SELECT pc.id
            FROM temp pc )

    </select>
</mapper>