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



    <select id="getCarNumById" resultType="java.lang.Integer">
        select ifnull(sum(ifnull(d.quantity_of_one,1)),0) num from trade_detail d
        where d.deleted = 0
        and EXISTS (
        select tdb.detail_id from trade_detail_base tdb
        inner join dispatch_car_dispatch_order_detail_base cdo on tdb.detail_id = cdo.detail_id
        and tdb.id = cdo.detail_base_id and tdb.order_id = cdo.order_id
        and cdo.deleted = 0 and tdb.use_date = cdo.use_date and cdo.car_dispatch_id = #{dispatchId}
        where tdb.deleted = 0 and tdb.order_id = d.order_id
        and tdb.use_status in (1,2,3,4,6)
        and d.id = tdb.detail_id
        )
        and NOT  EXISTS (
        SELECT t2.order_id
        FROM dispatch_external_resource_dispatch t1
        inner JOIN dispatch_external_resource_dispatch_order t2 ON t1.id=t2.dispatch_id and t2.deleted = 0
        where t1.deleted = 0 and t1.status = 1 and t1.is_bill = 1 and t1.car_dispatch_id = #{dispatchId} and d.order_id = t2.order_id
        )
    </select>

    <select id="getBatchCarNumById" resultType="java.util.Map">
        select p.car_dispatch_id dispatch_id,ifnull(sum(ifnull(p.quantity_of_one,1)),0) num from (
        select  cdo.car_dispatch_id,d.id,d.quantity_of_one from trade_detail d
        inner join trade_detail_base tdb on d.id = tdb.detail_id
        inner join dispatch_car_dispatch_order_detail_base cdo on tdb.detail_id = cdo.detail_id
        and tdb.id = cdo.detail_base_id and tdb.order_id = cdo.order_id
        and tdb.id = cdo.detail_base_id
        and cdo.deleted = 0 and tdb.use_date = cdo.use_date
        and  cdo.car_dispatch_id in
        <foreach collection="dispatchIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        where tdb.deleted = 0 and tdb.order_id = d.order_id
        and tdb.use_status in(1,2,3,4,6)
        and d.deleted = 0
        and NOT EXISTS (
        SELECT t2.order_id
        FROM dispatch_external_resource_dispatch t1
        inner JOIN dispatch_external_resource_dispatch_order t2 ON t1.id=t2.dispatch_id and t2.deleted = 0
        where t1.deleted = 0 and t1.status = 1 and t1.is_bill = 1
        and  cdo.car_dispatch_id in
        <foreach collection="dispatchIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        and d.order_id = t2.order_id
        )
        GROUP BY cdo.car_dispatch_id,d.id
        ) p GROUP BY p.car_dispatch_id
    </select>

    <select id="getBatchBoatNumById" resultType="java.util.Map">
        SELECT
        plan_id dispatch_id,ifnull( sum( ifnull( p.quantity_of_one, 1 ) ), 0 ) num from (SELECT
        cdo.plan_id,
        d.id,
        d.quantity_of_one
        FROM
        trade_detail d
        INNER JOIN trade_detail_base tdb
        on d.id = tdb.detail_id
        INNER JOIN dispatch_operate_plan_boat_order_detail_base cdo ON tdb.detail_id = cdo.detail_id
        AND tdb.id = cdo.detail_base_id
        AND tdb.order_id = cdo.order_id
        AND cdo.deleted = 0
        AND tdb.use_date = cdo.use_date
        and  cdo.plan_id in
        <foreach collection="dispatchIds" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        WHERE
        tdb.deleted = 0
        AND tdb.order_id = d.order_id
        AND tdb.use_status IN ( 1, 2, 3, 4, 6 )
        AND d.deleted = 0
        GROUP BY
        cdo.plan_id,
        d.id
        ) p GROUP BY plan_id
    </select>

    <select id="getBoatNumById" resultType="java.lang.Integer">
        select ifnull(sum(ifnull(d.quantity_of_one,1)),0) num from trade_detail d
        where d.deleted = 0 and EXISTS (
        select tdb.detail_id from trade_detail_base tdb
        inner join dispatch_operate_plan_boat_order_detail_base cdo on tdb.detail_id = cdo.detail_id
        and tdb.id = cdo.detail_base_id and tdb.order_id = cdo.order_id
        and cdo.deleted = 0 and tdb.use_date = cdo.use_date and cdo.plan_id = #{dispatchId}
        where tdb.deleted = 0 and tdb.order_id = d.order_id
        and tdb.use_status in(1,2,3,4,6) and d.id = tdb.detail_id
        )
    </select>



    <select id="pdaCheckTotalCount" resultType="java.lang.Integer">
        select count(distinct t4.id) num from trade_check t1
            inner join trade_check_detail t3 on t1.id = t3.check_id
            inner join trade_detail t2 on t1.detail_id = t2.id
            inner join trade_visitor t4 on t2.id = t4.detail_id
        where t3.dispatch_batch_id = #{dispatchId} and t2.voucher_status in (3,6,8,9)
    </select>


    <resultMap id="TradeCheckResultMap" type="com.yc.ship.module.trade.controller.admin.order.vo.check.CheckRespVO">
        <result property="id" column="id"/>
        <result property="operatorId" column="operator_id"/>
        <result property="type" column="type"/>
        <result property="orderId" column="order_id"/>
        <result property="detailId" column="detail_id"/>
        <result property="voucherCode" column="voucher_code"/>
        <result property="originalStatus" column="original_status"/>
        <association property="tradeDetailRespVO" resultMap="tradeDetailMap"/>
    </resultMap>


    <resultMap id="tradeDetailMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.TradeDetailRespVO">

        <result property="id" column="detail_id"/>
        <result property="orderId" column="order_id"/>
        <result property="orderNo" column="order_no"/>
        <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="productId" column="product_id"/>
        <result property="productName" column="product_name"/>
        <result property="specType" column="spec_type"/>
        <collection property="visitors" resultMap="visitorResultMap"/>
        <collection property="detailBaseList" resultMap="tradeDetailBaseResMap"/>
    </resultMap>

    <resultMap id="visitorResultMap" type="com.yc.ship.module.trade.dal.dataobject.order.TradeVisitorDO">
        <id property="id" column="visitorId"/>
        <result property="detailId" column="detail_id"/>
        <result property="name" column="name"/>
        <result property="gender" column="gender"/>
        <result property="credentialNo" column="credential_no"/>
        <result property="credentialType" column="credential_type"/>
        <result property="mobile" column="mobile"/>
        <result property="otaDetailId" column="ota_detail_id"/>
        <result property="isInsure" column="is_insure"/>
    </resultMap>

    <resultMap id="tradeDetailBaseResMap" type="com.yc.ship.module.trade.controller.admin.order.vo.order.DetailBaseRespVO">

        <result property="id" column="detail_base_id"/>
        <result property="detailId" column="detailId"/>
        <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="useStatus" column="use_status"/>
        <result property="checkStatus" column="base_check_status"/>
        <result property="verifyTime" column="base_verify_time"/>
        <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="specificationId" column="specification_id"/>
        <result property="specificationName" column="specification_name"/>
        <result property="specType" column="base_spec_type"/>
    </resultMap>



    <select id="querySimpleCheckPage" resultType="com.yc.ship.module.trade.controller.admin.order.vo.check.CheckRespVO">
        SELECT tc.*,tc.create_time checkTime, td.route_plan_id routePlanId, td.order_no orderNo, u.contact_name contactName, u.mobile
        from trade_check tc
        INNER JOIN trade_order td on td.id= tc.order_id
        INNER JOIN trade_order_user u on u.order_id = tc.order_id
        where 1= 1
        <if test="param.machineId != null and param.machineId != ''">
            AND tc.machine_id =#{param.machineId}
        </if>
        <if test="param.orderId != null and param.orderId != ''">
            AND tc.order_id =#{param.orderId}
        </if>
        <if test="param.detailId != null and param.detailId != ''">
            AND tc.detail_id =#{param.detailId}
        </if>
        <if test="param.voucherCode != null and param.voucherCode != ''">
            AND tc.voucher_code =#{param.voucherCode}
        </if>
        <if test="param.operatorId != null and param.operatorId != ''">
            AND tc.operator_id =#{param.operatorId}
        </if>
        <if test="param.beginTime != null">
            and tc.create_time <![CDATA[>= ]]> #{param.beginTime}
        </if>
        <if test="param.endTime != null">
            and tc.create_time <![CDATA[<= ]]> #{param.endTime}
        </if>
        <if test="param.type != null ">
            AND tc.type=#{param.type}
        </if>
        <if test="param.orderNo != null and param.orderNo != ''">
            AND td.order_no=#{param.orderNo}
        </if>
        <if test="param.operatorName != null and param.operatorName != ''">
            AND tc.operator_name like concat('%', #{param.operatorName} ,'%')
        </if>
        order by tc.create_time desc
    </select>

</mapper>