<?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.trade.dal.mysql.billorder.BillOrderMapper">

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

    <select id="getProductSpuPlat"
            resultType="com.yc.ship.module.product.api.dto.ProductSpuRespDTO">
        SELECT
            ps.*,
            odp.id productBaseId,
            od.id distributorId
        FROM
            product_spu ps
                INNER JOIN ota_distributor_product odp ON ps.id = odp.product_id
                INNER JOIN ota_distributor od ON odp.distributor_id = od.id
        WHERE  ps.id = #{productId} AND od.account_tenant_id = #{tenantId}
            LIMIT 1
    </select>

    <select id="getProductSpu" resultType="com.yc.ship.module.product.api.dto.ProductSpuRespDTO">
        SELECT ps.* FROM product_spu ps WHERE ps.id = #{productId}
    </select>

    <!-- 产品相关信息 -->

    <select id="getProductSupInfo"
            resultType="com.yc.ship.module.product.api.dto.ProductGroupRespDTO">
        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,
        p.product_name productName,
        pb.can_repeat_buy canRepeatBuy,
        pb.`name` productBaseName,
        pp.`name` projectName,
        pp1.`name` specificationName
        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
        INNER JOIN product_project pp1 ON pp1.id = pg.specification_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="getPlatProductSupInfo"
            resultType="com.yc.ship.module.product.api.dto.PlatProductRespDTO">
        SELECT
        pg.id,
        pg.product_id productId,
        dp.product_id platProductId,
        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,
        pg.max_num maxNum,
        p.product_name productName
        FROM
        product_group pg
        INNER JOIN ota_distributor_product dp ON dp.id = pg.product_base_id
        INNER JOIN product_spu p ON p.id = dp.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="getRefundAmount" resultType="java.math.BigDecimal">
        SELECT if(SUM(r.refund_amount) is NULL,0,SUM(r.refund_amount)) refundAmount
        FROM (
                 select r.refund_amount
                 from trade_order_pay p
                          INNER JOIN trade_refund_record r on p.id = r.order_pay_id
                 WHERE p.order_id = #{orderId}  and p.payment_type = 6
                   AND r.refund_status = 6
                 union all
                 select r.refund_amount
                 from trade_order_pay p
                          INNER JOIN trade_order_bind tob ON tob.order_id = p.order_id AND tob.type = 2
                          INNER JOIN trade_refund_record r on p.id = r.order_pay_id
                 WHERE tob.bind_id = #{orderId}  and p.payment_type = 6
                   AND r.refund_status = 6
             ) r
    </select>

    <select id="getBillOrderId" resultType="com.yc.ship.module.trade.dal.dataobject.billorder.BillOrderDO">
        SELECT obo.* FROM ota_bill_order obo WHERE obo.order_id = #{orderId}  LIMIT 1
    </select>
</mapper>