<?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.ota.dal.mysql.distributorproduct.DistributorAgencyPriceMapper">

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

    <select id="getDistributorAgencyPriceList"
            resultType="com.yc.ship.module.ota.dal.dataobject.distributorproduct.DistributorAgencyPriceDO">
        select t1.*
        from ota_distributor_agency_price t1
        where 1 = 1 and t1.deleted = 0
        <if test="params.distributorProductId != null and params.distributorProductId !=''">
            and t1.distributor_product_id = #{params.distributorProductId}
        </if>
        <if test="params.distributorId != null and params.distributorId !=''">
            and t1.distributor_id = #{params.distributorId}
        </if>
        <if test="params.productId != null and params.productId !=''">
            and t1.product_id = #{params.productId}
        </if>
        <if test="params.specType != null and params.specType !=''">
            and t1.spec_type = #{params.specType}
        </if>
    </select>

    <select id="getDistributorCalendarAgencyPriceList"
            resultType="com.yc.ship.module.ota.dal.dataobject.distributorproduct.DistributorAgencyPriceDO">
        select t1.id, t1.distributor_id,t1.distributor_product_id,t1.product_id,t1.product_sku_id,t1.spec_type,t1.cost_price,
               IFNULL(dp.price,t1.sale_price) sale_price,t1.stock,t1.is_use,t1.tenant_id
        from ota_distributor_agency_price t1
        <if test="useDate != null ">
            left join stock_day_price dp on dp.business_id = t1.distributor_product_id and dp.type = 4 and dp.spec_type = t1.spec_type
                 and #{useDate} between dp.start_date and dp.end_date and dp.deleted = 0
        </if>

        where 1 = 1 and t1.deleted = 0
        <if test="params.distributorProductId != null and params.distributorProductId !=''">
            and t1.distributor_product_id = #{params.distributorProductId}
        </if>
        <if test="params.distributorId != null and params.distributorId !=''">
            and t1.distributor_id = #{params.distributorId}
        </if>
        <if test="params.productId != null and params.productId !=''">
            and t1.product_id = #{params.productId}
        </if>
        <if test="params.specType != null and params.specType !=''">
            and t1.spec_type = #{params.specType}
        </if>
    </select>


    <select id="getDistributorCalendarRangeAgencyPriceList"
            resultType="com.yc.ship.module.ota.dal.dataobject.distributorproduct.DistributorAgencyPriceDO">
        select t1.id, t1.distributor_id,t1.distributor_product_id,t1.product_id,t1.product_sku_id,t1.spec_type,t1.cost_price,
        IFNULL(dp.price,t1.sale_price) sale_price,t1.stock,t1.is_use,t1.tenant_id,dp.start_date,dp.end_date
        from ota_distributor_agency_price t1
        left join stock_day_price dp on dp.business_id = t1.distributor_product_id and dp.type = 4 and dp.spec_type = t1.spec_type
        and ( #{startDate} between dp.start_date and dp.end_date or #{endDate} between dp.start_date and dp.end_date ) and dp.deleted = 0

        where 1 = 1 and t1.deleted = 0
        <if test="params.distributorProductId != null and params.distributorProductId !=''">
            and t1.distributor_product_id = #{params.distributorProductId}
        </if>
        <if test="params.distributorId != null and params.distributorId !=''">
            and t1.distributor_id = #{params.distributorId}
        </if>
        <if test="params.productId != null and params.productId !=''">
            and t1.product_id = #{params.productId}
        </if>
        <if test="params.specType != null and params.specType !=''">
            and t1.spec_type = #{params.specType}
        </if>
    </select>
</mapper>