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

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

        select tdb.order_id,tdb.project_code ,count(distinct tdb.detail_id) quantity
        FROM trade_detail_base tdb
        where tdb.order_id=#{orderId} and tdb.project_code in
        <foreach collection="projectCodeList" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        group by tdb.order_id,tdb.project_code
    </select>

    <select id="selectDispatchFromOrder"
            resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDispatchDO">
        select t1.*,if(t2.id is null,'car','boat') as dispatchType from trade_order_dispatch t1
        left join dispatch_operate_plan_boat t2 on t1.dispatch_id = t2.id
        left join dispatch_car_dispatch t3 on t1.dispatch_id = t3.id where order_id=''
    </select>

    <select id="selectDispatchCount" resultType="java.util.Map">

        select count(*) totalCount, count(d.id) dispatchCount
        from trade_order_dispatch_product p
        left join trade_order_dispatch d on d.order_id = p.order_id and p.project_code = d.project_code  and d.deleted = 0
        where p.order_id=#{orderId}
    </select>

    <select id="getDispatchDetail" resultType="java.util.Map">
      select car_no name,start_date,start_time from dispatch_car_dispatch t
      inner join dispatch_car_dispatch_car c on t.id=c.car_dispatch_id and c.status=0
      inner join dispatch_driver_shift s on c.car_shift_id = s.id
      inner join dispatch_car o on s.car_infor = o.id
      where t.deleted = 0
      and t.start_time is not null
      and t.id=#{dispatchId}
      UNION ALL
      select boat_name,shift_date start_date,shift_time start_time from dispatch_operate_plan_boat_view
      where id=#{dispatchId}
    </select>

    <select id="selectDispatchDetailByRefund" resultType="com.yc.ship.module.trade.service.order.bo.TradeDispatchRefundBO">
        select  od.dispatch_id,od.order_id,td.trade_detail_id,tdb.product_base_id
        from trade_refund tr
                 INNER JOIN trade_refund_detail td on tr.id = td.refund_id
                 INNER JOIN trade_refund_detail_base trdb on tr.id = trdb.refund_id and td.id = trdb.refund_detail_id
                 INNER JOIN trade_detail_base tdb on trdb.trade_detail_base_id = tdb.id
                 INNER JOIN trade_order_dispatch od on tr.order_id = od.order_id and od.deleted = 0
        where tr.id = #{refundId}
        group by od.dispatch_id,od.order_id,td.trade_detail_id,tdb.product_base_id
    </select>

    <select id="selectDispatchListByOrderId" resultType="com.yc.ship.module.trade.dal.dataobject.order.TradeOrderDispatchDO">
        select distinct od.dispatch_id,od.project_code
        from trade_order_dispatch od
        where od.order_id = #{orderId} and od.deleted = 0
    </select>

    <!--由计调提供的查询sql，用于判断当前订单的计调状态 -->
    <select id="selectOrderDispatchStatus" resultType="java.util.Map">
        select a.id
             ,case when sum(a.num) = sum(ifnull(b.num,0)) and count(a.id) = count(b.id) then 2
                   when sum(ifnull(b.num,0)) = 0 and ifnull(count(b.id),0) = 0 then 0
                   else 1 end schedule_status
        from (
                 select o.id,tdb.project_code,pb.route_id,SUM(ifnull(td.quantity_of_one,1)) num
                 from trade_order o
                          inner join trade_detail td on o.id = td.order_id and td.deleted = 0
                          inner join trade_detail_base tdb on td.id=tdb.detail_id and tdb.deleted = 0 and tdb.project_code in('car', 'meet', 'send', 'boat')
                          inner join product_base pb on tdb.product_base_id = pb.id
                 where 1=1
                   and o.deleted = 0 And o.id in
                    <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
                        #{item}
                    </foreach>
                 group by o.id,tdb.project_code,pb.route_id
             ) a
                 left join (
            select o.id,tdb.project_code,pb.route_id,SUM(ifnull(td.quantity_of_one,1)) num
            from trade_order o
                     inner join trade_detail td on o.id = td.order_id and td.deleted = 0
                     inner join trade_detail_base tdb on td.id=tdb.detail_id and tdb.deleted = 0
                and tdb.project_code in('car', 'meet', 'send') and tdb.use_status in(1,2,3)
                     inner join product_base pb on tdb.product_base_id = pb.id
                     inner join dispatch_car_dispatch_order_detail_base cdodb on tdb.id = cdodb.detail_base_id and td.id = cdodb.detail_id
                and td.order_id = cdodb.order_id and tdb.use_date = cdodb.use_date and cdodb.deleted = 0
            where 1=1
              and o.deleted = 0 And o.id in
                <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
                    #{item}
                </foreach>
            group by o.id,tdb.project_code,pb.route_id
            union
            select o.id,tdb.project_code,pb.route_id,SUM(ifnull(td.quantity_of_one,1)) num
            from trade_order o
                     inner join trade_detail td on o.id = td.order_id and td.deleted = 0
                     inner join trade_detail_base tdb on td.id=tdb.detail_id and tdb.deleted = 0
                and tdb.project_code = 'boat' and tdb.use_status in(1,2,3)
                     inner join product_base pb on tdb.product_base_id = pb.id
                     inner join dispatch_operate_plan_boat_order_detail_base cdodb on tdb.id = cdodb.detail_base_id and td.id = cdodb.detail_id
                and td.order_id = cdodb.order_id and tdb.use_date = cdodb.use_date and cdodb.deleted = 0
            where 1=1 And o.id in
                <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
                    #{item}
                </foreach>
              and o.deleted = 0
            group by o.id,tdb.project_code,pb.route_id
        ) b on a.id = b.id and a.project_code = b.project_code and a.route_id = b.route_id
        group by a.id
        union
        select td.id
             ,case when dgd.order_number is not null then 2 else 0 end schedule_status
        FROM trade_order td
                 INNER JOIN trade_detail tdl ON td.id = tdl.order_id and tdl.deleted = 0
                 INNER JOIN product_spu ps ON ps.id = tdl.product_id
                 INNER JOIN product_group pg ON pg.product_id = ps.id and pg.deleted = 0
                 INNER JOIN product_project pp ON pp.id = pg.project_id and pp.code = 'guide'
                 left join dispatch_guide_dispatch dgd on td.id=dgd.order_id and dgd.deleted = 0
        WHERE 1=1
          and td.id in
                <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
                    #{item}
                </foreach>
          and td.deleted = 0
          and td.order_status in(6,9)
          and td.id in(
            select tdl.order_id
            FROM trade_detail tdl
                     INNER JOIN product_spu ps ON ps.id = tdl.product_id
                     INNER JOIN product_group pg ON pg.product_id = ps.id and pg.deleted = 0
                     INNER JOIN product_project pp ON pp.id = pg.project_id
            where tdl.deleted = 0 AND tdl.order_id in
            <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
                #{item}
            </foreach>
            group by tdl.order_id having count(pp.code) = 1
        )
        union
        select td.id
        ,case when erdo.order_id is not null then 2 else 0 end schedule_status
        FROM trade_order td
        left join dispatch_external_resource_dispatch_order erdo on td.id = erdo.order_id and erdo.deleted = 0
        left join dispatch_external_resource_dispatch erd on erdo.dispatch_id = erd.id and erd.status = 1 and erd.deleted = 0
        WHERE 1=1
        and td.id in
        <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
            #{item}
        </foreach>
        and td.deleted = 0
        and td.id in(
            select tdb.order_id
            FROM trade_detail_base tdb
            where tdb.deleted = 0 AND tdb.order_id in
            <foreach collection="orderIdList" item="item" separator=","  open="(" close=")">
              #{item}
            </foreach>
            group by tdb.order_id having count(DISTINCT tdb.product_base_id ) = 1
        )
    </select>

    <select id="selectOrderIdListByDispatchId" resultType="java.lang.Long">
        select distinct od.order_id orderId
        from trade_order_dispatch od
        where od.dispatch_id = #{dispatchId}
        <if test="projectCode!=null and projectCode!=''">
            and od.project_code = #{projectCode}
        </if>
    </select>

    <select id="selectDispatchProductBaseIdList" resultType="java.lang.Long">
        select distinct pd.product_base_id
        from dispatch_operate_plan_boat_route dcdr
                 inner join product_route_plan_detail pd on pd.id = dcdr.route_id and pd.deleted = 0
        where dcdr.plan_id = #{dispatchId} and dcdr.deleted = 0
        union all
        select distinct pd.product_base_id
        from dispatch_car_dispatch_route dcdr
                 inner join product_route_plan_detail pd on pd.id = dcdr.route_id and pd.deleted = 0
        where dcdr.car_dispatch_id = #{dispatchId} and dcdr.deleted = 0
    </select>

    <delete id="delOrderDispatch">
        delete from trade_order_dispatch tod
        where tod.dispatch_id = #{dispatchId}
        <if test="projectCode!=null and projectCode!=''">
        and tod.project_code = #{projectCode}
        </if>
    </delete>

</mapper>