<?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.productGroup.ProductGroupMapper">

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

    <select id="selectSpuGroupList" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupRespVO">
        SELECT
        pg.id,
        pg.product_id productId,
        pg.product_base_id productBaseId,
        pg.product_base_type productBaseType,
        pg.sku_id skuId,
        pg.project_id projectId,
        pg.specification_id specificationId,
        pg.first_resource_id firstResourceId,
        pg.cost_price costPrice,
        pg.sale_price salePrice,
        pg.spec_type specType,
        pg.need_choose needChoose,
        pg.max_num maxNum,
        pg.day_sort daySort,
        pg.specification_show specificationShow,
        p.product_name productName,
        pb.`name` productBaseName,
        pb.can_repeat_buy canRepeatBuy,
        pp.`name` projectName,
        pp.code projectCode,
        pp.`name` specificationName,
        pb.start_point startPoint,
        pb.end_point endPoint,
        pb.use_status useStatus
        FROM
        product_group pg
        INNER JOIN product_spu p ON p.id = pg.product_id
        INNER JOIN product_base pb ON pb.id = pg.product_base_id
        INNER JOIN product_project pp ON pp.id = pg.project_id
        WHERE
        pg.deleted = 0
        and pg.product_base_type = 0
        <if test="productId != null and productId != ''">
            AND pg.product_id = #{productId}
        </if>
        <if test="planId != null">
            and  exists (
                select rt.product_base_id from product_route_plan_detail rt where rt.plan_id = #{planId} and rt.deleted=0 and rt.is_use = 1 and pb.id = rt.product_base_id
            )
        </if>
        ORDER BY pg.id,pg.update_time
    </select>

    <select id="selectSpuGroupListV2" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupRespVO">
        SELECT
        pg.id,
        pg.product_id productId,
        pg.product_base_id productBaseId,
        pg.product_base_type productBaseType,
        pg.sku_id skuId,
        pg.project_id projectId,
        pg.specification_id specificationId,
        pg.first_resource_id firstResourceId,
        pg.cost_price costPrice,
        pg.sale_price salePrice,
        pg.spec_type specType,
        pg.need_choose needChoose,
        pg.max_num maxNum,
        pg.day_sort daySort,
        pg.specification_show specificationShow,
        p.product_name productName,
        pb.`name` productBaseName,
        pb.can_repeat_buy canRepeatBuy,
        pp.`name` projectName,
        pp.code projectCode,
        pp.`name` specificationName,
        pb.start_point startPoint,
        pb.end_point endPoint,
        pb.use_status useStatus
        FROM
        product_group pg
        INNER JOIN product_spu p ON p.id = pg.product_id
        INNER JOIN product_base pb ON pb.id = pg.product_base_id
        INNER JOIN product_project pp ON pp.id = pg.project_id
        WHERE
        pg.deleted = 0
        and pg.product_base_type = 0
        <if test="productId != null and productId != ''">
            AND pg.product_id = #{productId}
        </if>
        ORDER BY pg.id,pg.update_time
    </select>

    <select id="selectSpuGroupByPlanIdList" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.ProductGroupRespVO">
        SELECT
        pg.id,
        pg.product_id productId,
        pg.product_base_id productBaseId,
        pg.product_base_type productBaseType,
        pg.sku_id skuId,
        pg.project_id projectId,
        pg.specification_id specificationId,
        pg.first_resource_id firstResourceId,
        pg.cost_price costPrice,
        pg.sale_price salePrice,
        pg.spec_type specType,
        pg.need_choose needChoose,
        pg.max_num maxNum,
        pg.day_sort daySort,
        pg.specification_show specificationShow,
        p.product_name productName,
        pb.`name` productBaseName,
        pb.can_repeat_buy canRepeatBuy,
        pp.`name` projectName,
        pp.code projectCode,
        pp.`name` specificationName,
        pb.start_point startPoint,
        pb.end_point endPoint,
        pb.use_status useStatus
        FROM
        product_group pg
        INNER JOIN product_spu p ON p.id = pg.product_id
        INNER JOIN product_base pb ON pb.id = pg.product_base_id
        INNER JOIN product_project pp ON pp.id = pg.project_id
        WHERE
        pg.deleted = 0
        and pg.product_base_type = 0
        <if test="productId != null and productId != ''">
            AND pg.product_id = #{productId}
        </if>
        <if test="planId != null">
            and pb.id in (
                select product_base_id from product_route_plan_detail where plan_id = #{planId} and deleted=0 and is_use = 1
            )
        </if>
        ORDER BY pg.id,pg.update_time
    </select>

    <select id="selectPlatProductList" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.PlatProductRespVO">
        SELECT
        pg.id,
        pg.product_id productId,
        pg.product_base_id productBaseId,
        pg.product_base_type productBaseType,
        pg.sku_id skuId,
        pg.cost_price costPrice,
        pg.sale_price salePrice,
        pg.need_choose needChoose,
        p.room_type roomType,
        p.bed_type bedType,
        p.has_breakfast hasBreakfast,
        p.product_name productName
        FROM
        product_group pg
        INNER JOIN product_spu p ON p.id = pg.product_id
        WHERE
            pg.deleted = 0
          and pg.product_base_type = 1
        <if test="productId != null and productId != ''">
            AND pg.product_id = #{productId}
        </if>
        ORDER BY pg.id,pg.update_time
    </select>

    <select id="selectDistributorProductId" resultType="java.util.Map">
        SELECT
            dp.id
        FROM
            ota_distributor_product dp
                INNER JOIN ota_distributor d ON d.id = dp.distributor_id
        WHERE
            d.is_use = 1
          AND d.deleted = 0
          AND dp.is_use_otc = 1
          AND dp.deleted = 0
          AND d.account_tenant_id = #{tenantId}
          AND dp.product_id = #{productId}
    </select>

    <select id="queryAgencyId" resultType="java.util.Map">
        SELECT DISTINCT p.id distributorId
        FROM (
        SELECT t1.* FROM ota_distributor t1
            INNER JOIN otc_agency_store_user t2 ON t1.account_tenant_id = t2.tenant_id
        WHERE t2.user_id = #{userId}
        UNION ALL
        SELECT * FROM ota_distributor WHERE type = 2 and account_tenant_id = #{tenantId}
        ) p limit 1
    </select>

    <select id="selectPlatProductPriceList" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.PlatProductPriceRespVO">
        SELECT p.id , p.distributor_id distributorId, p.distributor_product_id distributorProductId, p.product_id productId,
               p.product_sku_id productSkuId, p.spec_type specType, p.cost_price costPrice, p.sale_price salePrice
            from ota_distributor_agency_price p
        WHERE deleted = 0
          AND product_id = #{productId}
          AND distributor_id = #{agencyId}
    </select>

    <select id="selectPlatProductPriceList2" resultType="com.yc.ship.module.product.controller.admin.productSpu.groupVO.PlatProductPriceRespVO">
        SELECT p.id , p.distributor_id distributorId, p.distributor_product_id distributorProductId, p.product_id productId,
               p.product_sku_id productSkuId, p.spec_type specType, p.cost_price costPrice, p.sale_price salePrice
            from ota_distributor_agency_price p
        WHERE deleted = 0
            AND distributor_id = #{agencyId}
            AND product_id in
        <foreach collection="productIds" item="productId" open="(" separator="," close=")">
            #{productId}
        </foreach>
    </select>

    <update id="updateDistributorProductIsUse">
        UPDATE ota_distributor_product SET is_use = #{isUse} WHERE product_id = #{productId};
    </update>
    <!-- 下架分销产品及旅行社含有该产品的组合产品 -->
    <update id="updateDistributorProduct">
        UPDATE ota_distributor_product SET sale_price = #{salePrice}, is_use = 0 WHERE product_id = #{productId};
        UPDATE product_spu SET is_use = 0 WHERE id in (
            SELECT g.product_id FROM product_group g
                 INNER JOIN ota_distributor_product odp ON odp.id = g.product_base_id
            WHERE odp.product_id = #{productId} AND odp.deleted = 0 AND g.deleted = 0);
    </update>

    <update id="updateDistributorPrice">
        UPDATE ota_distributor_agency_price SET product_sku_id = #{skuIdNew}, sale_price = #{salePrice} WHERE product_sku_id = #{skuId}
    </update>

    <update id="updateDistributorProductId">
        UPDATE ota_distributor_product SET product_sku_id = #{skuIdNew} WHERE product_sku_id = #{skuId}
    </update>

    <update id="deletePlatProduct">
        UPDATE product_group SET deleted = 1 WHERE product_id = #{productId} and product_base_type = 1 and deleted = 0
    </update>

</mapper>