<?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.order.TradeDetailBizMapper">
    <resultMap id="tradeDetailMap" type="com.yc.ship.module.trade.service.order.bo.TradeDetailGuideRefundBO">
        <result property="id" column="id"/>
        <result property="orderId" column="order_id"/>
        <result property="voucherCode" column="voucher_code"/>
        <result property="voucherStatus" column="voucher_status"/>
        <result property="useDate" column="use_date"/>
        <result property="validityStartDate" column="validity_start_date"/>
        <result property="validityEndDate" column="validity_end_date"/>
        <result property="usageDays" column="usage_days"/>
        <result property="checkStatus" column="check_status"/>
        <result property="takeStatus" column="take_status"/>
        <result property="refundStatus" column="refund_status"/>
        <result property="verifyTime" column="verify_time"/>
        <result property="specType" column="spec_type"/>
        <result property="price" column="price"/>
        <result property="actualPrice" column="actual_price"/>
        <result property="changeSpecType" column="change_type"/>
        <result property="refundAmount" column="refund_money"/>

        <collection property="tradeDetailBaseList" ofType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO" javaType="java.util.List">
            <result property="id" column="detail_base_id"/>
            <result property="detailId" column="detail_id"/>
            <result property="productBaseId" column="product_base_id"/>
            <result property="productBaseName" column="product_base_name"/>
            <result property="productBaseType" column="product_base_type"/>
            <result property="productResourceId" column="product_resource_id"/>
            <result property="productResourceName" column="product_resource_name"/>
            <result property="supplierProductId" column="supplier_product_id"/>
            <result property="supplierId" column="supplier_id"/>
            <result property="supplierName" column="supplier_name"/>
            <result property="isSystemDirect" column="is_system_direct"/>
            <result property="useStatus" column="use_status"/>
            <result property="checkStatus" column="base_check_status"/>
            <result property="verifyTime" column="base_verify_time"/>
            <result property="price" column="base_price"/>
            <result property="usageCount" column="usage_count"/>
            <result property="productResourcePrice" column="product_resource_price"/>
            <result property="projectId" column="project_id"/>
            <result property="projectName" column="project_name"/>
            <result property="projectCode" column="project_code"/>
            <result property="specificationId" column="specification_id"/>
            <result property="specificationName" column="specification_name"/>
            <result property="specType" column="base_spec_type"/>
            <result property="supplierVoucherCode" column="supplier_voucher_code"/>
            <result property="platSupplierOrderId" column="plat_supplier_order_id"/>
        </collection>
    </resultMap>


    <select id="selectInnerTradeDetailWithBaseByGuideRefund" resultMap="tradeDetailMap">
        select tdi.*,tdb.id detail_base_id,tdb.detail_id,tdb.product_base_id,tdb.product_base_name,tdb.product_base_type,tdb.product_resource_id,tdb.product_resource_name,tdb.supplier_product_id,tdb.supplier_id,
        tdb.supplier_name,tdb.is_system_direct,tdb.use_status,tdb.check_status base_check_status,tdb.verify_time base_verify_time,tdb.price base_price,tdb.usage_count,tdb.product_resource_price,tdb.project_id,tdb.project_name,
        tdb.specification_id,tdb.specification_name,tdb.spec_type base_spec_type,tdb.supplier_voucher_code,tdb.plat_supplier_order_id,tdb.project_code,
        grd.change_type
        FROM dispatch_guide_refund_detail grd
        INNER JOIN trade_detail td on td.id = grd.order_detail_id
        INNER JOIN trade_detail_base tdb on tdb.supplier_voucher_code = td.voucher_code
        INNER JOIN trade_detail tdi on tdi.id = tdb.detail_id
        where grd.refund_apply_id = #{refundId}
    </select>

    <select id="selectTradeDetailWithBaseByGuideRefund" resultMap="tradeDetailMap">
        select td.*,tdb.id detail_base_id,tdb.detail_id,tdb.product_base_id,tdb.product_base_name,tdb.product_base_type,tdb.product_resource_id,tdb.product_resource_name,tdb.supplier_product_id,tdb.supplier_id,
               tdb.supplier_name,tdb.is_system_direct,tdb.use_status,tdb.check_status base_check_status,tdb.verify_time base_verify_time,tdb.price base_price,tdb.usage_count,tdb.product_resource_price,tdb.project_id,tdb.project_name,
               tdb.specification_id,tdb.specification_name,tdb.spec_type base_spec_type,tdb.supplier_voucher_code,tdb.plat_supplier_order_id,tdb.project_code,
               grd.change_type,grd.refund_money
        FROM dispatch_guide_refund_detail grd
        INNER JOIN trade_detail td on td.id = grd.order_detail_id
        INNER JOIN trade_detail_base tdb on tdb.detail_id = td.id
        where grd.refund_apply_id = #{refundId}
    </select>

    <select id="selectTradeOrderByGuideRefund" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDO">
        SELECT tod.*
        FROM dispatch_guide_refund_apply gra
        INNER JOIN trade_order tod on gra.order_id = tod.id
        where gra.id = #{refundId}
    </select>


    <select id="selectInnerGuideRefundCount" resultType="java.lang.Integer">
        SELECT count(gra.id) totalCount
        FROM trade_order tod
        INNER JOIN dispatch_guide_refund_apply gra on gra.order_id = tod.id
        INNER JOIN dispatch_guide_refund_detail grd on gra.id = grd.refund_apply_id
        INNER JOIN trade_detail_base tdb on tdb.supplier_voucher_code = grd.voucher_code
        where tod.external_origin_order_no = #{orderNO}
          <if test="detailIdList!=null and detailIdList.size() >0">
              and tdb.detail_id in
              <foreach collection="detailIdList" item="item" separator=","  open="(" close=")">
                  #{item}
              </foreach>
          </if>
          and gra.`status` in (1,3)
    </select>

    <select id="selectGuideRefundCount" resultType="java.lang.Integer">
        SELECT count(gra.id) totalCount
        FROM dispatch_guide_refund_apply gra
        INNER JOIN dispatch_guide_refund_detail grd on gra.id = grd.refund_apply_id
        where gra.order_id = #{orderId}
        <if test="detailIdList!=null and detailIdList.size() >0">
            and grd.order_detail_id in
            <foreach collection="detailIdList" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
        </if>
        and gra.`status` in (1,3)
    </select>

    <select id="selectSelfDetailTotalAmount" resultType="java.util.Map">
        SELECT ifnull(sum(tdb.price),0) totalAmount,ifnull(sum(tdb.original_price),0) totalOriginAmount
        FROM trade_detail_base tdb
        WHERE tdb.detail_id = #{detailId} and tdb.product_base_type = 0
    </select>
</mapper>
