<?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.TradeDetailBaseMapper">

    <select id="selectNeedSupplierOrderList"
            resultType="com.yc.ship.module.trade.service.order.bo.TradeOrderDetailBaseBO">

        SELECT tdb.*,td.product_id,td.quantity_of_one,td.use_real_name,count(distinct td.id) quantity,sum(tdb.price) amount
        FROM trade_detail td
        INNER JOIN trade_detail_base tdb on td.id = tdb.detail_id
        WHERE td.order_id=#{orderId} and tdb.product_base_type = 1
        GROUP BY tdb.supplier_product_id,td.product_id
        UNION ALL
        SELECT tdb.*,td.product_id,td.quantity_of_one,td.use_real_name,count(distinct td.id) quantity,sum(tdb.price) amount
        FROM trade_detail td
        INNER JOIN trade_detail_base tdb on td.id = tdb.detail_id
        WHERE td.order_id=#{orderId} and tdb.is_system_direct = 1 and tdb.product_base_type = 0
        GROUP BY tdb.product_resource_id, td.product_id,tdb.product_base_id
    </select>

    <select id="selectBaseListFromOrder"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO">

        SELECT tdb.*
        FROM trade_detail td
        INNER JOIN trade_detail_base tdb on td.id = tdb.detail_id
        <where>
            td.order_id=#{orderId}
            and td.product_id=#{baseBo.productId}
            <if test="baseBo.productBaseType == 1">
                and tdb.supplier_product_id=#{baseBo.supplierProductId}
            </if>
            <if test="baseBo.productBaseType != 1">
                and tdb.product_resource_id=#{baseBo.productResourceId}
                and tdb.product_base_id=#{baseBo.productBaseId}
            </if>
        </where>
    </select>

    <select id="selectDistinctBaseListBySupplierOrderId"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO">

        SELECT tdb.*
        FROM trade_detail_base tdb
        <where>
            tdb.plat_supplier_order_id=#{platSupplierOrderId}
        </where>
        group by tdb.product_base_id,tdb.spec_type
    </select>

    <select id="selectBaseListBySupplierOrderNo"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO">

        SELECT tdb.*
        FROM trade_detail_base tdb
        <where>
            tdb.supplier_voucher_code=#{supplierVoucherCode}
        </where>
    </select>

    <select id="getProjectCodeOrderId" resultType="java.util.Map">
        SELECT distinct projectCode
        FROM trade_detail_base
        where 1= 1
        <if test="orderId != null and orderId != ''">
            AND td.order_id = #{orderId}
        </if>
    </select>

    <select id="selectDetailBaseBySupplierVoucherCode"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO">
        SELECT * FROM trade_detail_base where supplier_voucher_code=#{supplierVoucherCode} and use_date =  CURDATE() limit 1
    </select>

    <select id="getDistinctDetailListBySupplierOrderId" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailDO">
        SELECT  t1.* from trade_detail t1 inner join  trade_detail_base t2 on t1.id = t2.detail_id
        <where>
            t2.plat_supplier_order_id =#{platSupplierOrderId}
        </where>
    </select>

    <select id="selectCountBySpecType" resultType="java.lang.Integer">

        select count(*) totalCount
        from
        (
            SELECT tdb.id
            FROM trade_detail_base tdb
            <where>
                tdb.plat_supplier_order_id=#{platSupplierOrderId} and
                tdb.spec_type = #{specType}
            </where>
            group by tdb.detail_id
        )t
    </select>

    <update id="updateStatusByOrderId">
        update trade_detail_base tdb
        set tdb.use_status = #{status}
        where tdb.order_id = #{orderId}
    </update>

    <update id="updateStatusByDetailId">
        update trade_detail_base set use_status = 3 where detail_id = #{detailId} and use_status = 1
    </update>

    <select id="selectDetailIdByPlatSupplierId" resultType="java.lang.Long">

        SELECT tdb.detail_id
        FROM trade_detail_base tdb
        <where>
            tdb.plat_supplier_order_id=#{platSupplierOrderId}
        </where>
        group by tdb.detail_id
    </select>

    <update id="updateRefundingBatch">
        <foreach collection="tradeDetailBaseList" item="item" separator=";"  close=";">
            UPDATE trade_detail_base SET use_status = #{refundStatus}
            WHERE id = #{item.id} AND use_status = #{item.useStatus}
        </foreach>
    </update>

    <select id="selectProductBaseByOrder" resultType="com.yc.ship.module.trade.service.order.bo.TradeOrderDetailBaseBO">
        select td.product_id,tdb.product_base_id,tdb.product_resource_id,tdb.product_base_type,tdb.use_date,count(*) quantity
        from trade_detail td
        inner join trade_detail_base tdb on td.id = tdb.detail_id
        where td.order_id =#{orderId}
        group by td.product_id,tdb.product_base_id
    </select>

    <select id="getProductBaseBySupplierVoucherCode" resultType="com.yc.ship.module.trade.service.order.bo.TradeOrderDetailBaseBO">
        select * from trade_detail_base where supplier_voucher_code = #{supplierVoucherCode}
    </select>

    <select id="selectProductBaseList" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeDetailBaseDO">
        SELECT
            db.*
        FROM
            trade_detail_base db
        <if test="channelId != null">
            INNER JOIN product_check_channel cc ON cc.product_id = db.product_base_id
        </if>
        WHERE db.detail_id = #{detailId} and db.use_status = 1
        <if test="channelId != null">
            and cc.deleted = 0 and cc.channel_id = #{channelId}
        </if>
    </select>

    <select id="selectVisitorOrderProductBaseCount" resultType="java.util.Map">
        select tv.credential_no,count(tdb.id) total
        from trade_detail_base tdb
        INNER JOIN trade_visitor tv on tdb.detail_id = tv.detail_id
        where tdb.product_base_id =#{productBaseId} and tdb.use_status in (1,2) and tdb.use_date = #{useDate}
        <if test="excludeOrderId!=null">
            and tdb.order_id != #{excludeOrderId}
        </if>
        and tv.credential_no in
        <foreach collection="credentialNoList" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        group by tv.credential_no
        HAVING total>0
    </select>
    <update id="updateDeleted">
        UPDATE trade_detail_base SET deleted = #{deleted},update_time = now() WHERE order_id = #{orderId}
    </update>

</mapper>