<?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.product.dal.mysql.productBase.ProductBaseMapper">

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

    <select id="queryProductBasePage" resultType="com.yc.ship.module.product.controller.admin.productBase.vo.ProductBaseRespVO">
        SELECT
            pb.id,
            pb.`name`,
            pb.short_name shortName,
            pb.project_id projectId,
            pb.resource_category_id resourceCategoryId,
            pb.first_resource_id firstResourceId,
            pb.fixed_resource_id fixedResourceId,
            pb.can_repeat_buy canRepeatBuy,
            pb.check_type checkType,
            pb.check_times checkTimes,
            pb.is_refund isRefund,
            pb.is_auto_check isAutoCheck,
            pb.is_buy_check isBuyCheck,
            pb.start_point startPoint,
            pb.end_point endPoint,
            pb.route_id routeId,
            pb.stock,
            pb.use_status useStatus,
            pb.is_only isOnly,
            pb.price_only priceOnly,
            pb.create_time createTime,
            pc.cate_name resourceName,
            pp.`name` projectName
        FROM
            product_base pb
                INNER JOIN product_category pc ON pc.id = pb.resource_category_id
                INNER JOIN product_project pp ON pp.id = pb.project_id
        WHERE
            pb.deleted = 0
            <if test="reqVO.name != null and reqVO.name != ''">
                AND pb.name like CONCAT('%', #{reqVO.name}, '%')
            </if>
            <if test="reqVO.projectId != null and reqVO.projectId != ''">
                AND pb.project_id = #{reqVO.projectId}
            </if>
            <if test="reqVO.useStatus != null and reqVO.useStatus != ''">
                AND pb.use_status = #{reqVO.useStatus}
            </if>
        ORDER BY pb.create_time desc,pb.id
    </select>

    <select id="selectProductBasePage" resultType="com.yc.ship.module.product.controller.admin.productBase.vo.ProductBaseRespVO">
        SELECT
        DISTINCT
            pb.id,
            pb.`name`,
            pb.short_name shortName,
            pb.project_id projectId,
            pb.resource_category_id resourceCategoryId,
            pb.first_resource_id firstResourceId,
            pb.fixed_resource_id fixedResourceId,
            pb.can_repeat_buy canRepeatBuy,
            pb.check_type checkType,
            pb.check_times checkTimes,
            pb.is_refund isRefund,
            pb.is_auto_check isAutoCheck,
            pb.is_buy_check isBuyCheck,
            pb.start_point startPoint,
            pb.end_point endPoint,
            pb.route_id routeId,
            pb.stock,
            <if test="reqVO.productId != null and reqVO.productId != ''">
                pg.day_sort daySort,
            </if>
            pb.use_status useStatus,
            pb.create_time createTime,
            pb.is_only isOnly,
            pb.price_only priceOnly,
            pc.cate_name resourceName,
            pp.`name` projectName
        FROM
            product_base pb
                INNER JOIN product_project pp ON pp.id = pb.project_id
                INNER JOIN product_category pc ON pc.id = pb.resource_category_id
                <if test="reqVO.supplierName != null and reqVO.supplierName != ''">
                    INNER JOIN product_resource pr ON pr.category_id = pb.resource_category_id
                    INNER JOIN product_supplier ps ON ps.id = pr.supplier_id
                </if>
                <if test="reqVO.productId != null and reqVO.productId != ''">
                    INNER JOIN product_group pg ON pg.product_base_id = pb.id AND pg.deleted = 0
                </if>
                <if test="reqVO.specId != null and reqVO.specId != ''">
                    INNER JOIN product_base_price basePrice ON basePrice.product_base_id = pb.id
                </if>
        WHERE
            pb.deleted = 0
            <if test="reqVO.name != null and reqVO.name != ''">
                AND pb.name like CONCAT('%', #{reqVO.name}, '%')
            </if>
            <if test="reqVO.projectId != null and reqVO.projectId != ''">
                AND pb.project_id = #{reqVO.projectId}
            </if>
            <if test="reqVO.useStatus != null and reqVO.useStatus != ''">
                AND pb.use_status = #{reqVO.useStatus}
            </if>
            <if test="reqVO.productId != null and reqVO.productId != ''">
                AND pg.product_id = #{reqVO.productId}
            </if>
            <if test="reqVO.supplierName != null and reqVO.supplierName != ''">
                AND ps.`name` LIKE CONCAT('%', #{reqVO.supplierName}, '%')
            </if>
            <if test="reqVO.specId != null and reqVO.specId != ''">
                AND basePrice.specification_id = #{reqVO.specId} group by pb.id
            </if>
        ORDER BY pb.create_time desc
    </select>

    <select id="selectShipList" resultType="com.yc.ship.module.product.controller.admin.productBase.vo.ShipRespVO">
        SELECT id,code,name FROM resource_ship WHERE deleted = 0 and status =0
    </select>
</mapper>