<?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.bonus.canteen.core.supply.mapper.DrpSupplierComeGoMapper">

    <select id="pageSupplierMaterialIntoAndOut"
            resultType="com.bonus.canteen.core.supply.vo.DrpSupplierMaterialIntoAndOutPageVO">
        SELECT
        *
        FROM
        (
        SELECT
        a.into_id drpOrderId,
        1 drpOrderType,
        a.warehouse_id,
        b.warehouse_name,
        a.total_amount,
        a.total_num,
        a.into_date drpOrderTime,
        a.crby,
        a.crtime,
        b.area_id,
        aa.area_name
        FROM
        drp_into_inventory a
        LEFT JOIN drp_warehouse b ON b.warehouse_id = a.warehouse_id
        left join alloc_area aa on aa.area_id = b.area_id
        WHERE
        a.`status` = #{status}
        UNION ALL
        SELECT
        a.out_id drpOrderId,
        2 drpOrderType,
        a.warehouse_id,
        b.warehouse_name,
        a.total_amount,
        a.total_num,
        a.out_date drpOrderTime,
        a.crby,
        a.crtime,
        b.area_id,
        aa.area_name
        FROM
        drp_out_inventory a
        LEFT JOIN drp_warehouse b ON b.warehouse_id = a.warehouse_id
        left join alloc_area aa on aa.area_id = b.area_id
        WHERE
        a.`status` = #{status}
        ) t
        <where>
            <if test="content.drpOrderId != null and content.drpOrderId != ''">
                AND drpOrderId like #{content.drpOrderId}
            </if>
            <if test="content.startTime != null">
                AND drpOrderTime <![CDATA[ >= ]]> #{content.startTime}
            </if>
            <if test="content.endTime != null">
                AND drpOrderTime<![CDATA[ <= ]]> #{content.endTime}
            </if>
            <if test="content.drpOrderType != null and content.drpOrderType != ''">
                AND drpOrderType = #{content.drpOrderType}
            </if>
            <if test="content.crby != null and content.crby != ''">
                and crby like #{content.crby}
            </if>
            <if test="content.crtimeStart != null">
                and crtime <![CDATA[ >= ]]> #{content.crtimeStart}
            </if>
            <if test="content.crtimeEnd != null">
                and crtime <![CDATA[ <= ]]> #{content.crtimeEnd}
            </if>
            <if test="warehouseAuthority != null and warehouseAuthority.size() > 0">
                and warehouse_id in
                <foreach collection="warehouseAuthority" item="warehouseId" separator="," open="(" close=")">
                    #{warehouseId}
                </foreach>
            </if>
            <if test="areaAuth != null and areaAuth.size() > 0">
                and area_id in
                <foreach collection="areaAuth" item="areaId" separator="," open="(" close=")">
                    #{areaId}
                </foreach>
            </if>
            <if test="content.areaIdList != null and content.areaIdList.size() > 0">
                AND area_id in
                <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="content.warehouseIdList != null and content.warehouseIdList.size() > 0">
                AND warehouse_id in
                <foreach collection="content.warehouseIdList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY
        crtime DESC
    </select>
    <select id="pageSupplierComeAndGo" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierComeAndGoPageVO">
        SELECT a.order_goods_id,
               a.order_title,
               a.total_into_num,
               a.order_amount,
               a.total_num orderTotalNum,
               a.supplier_id,
               b.supplier_name,
               a.request_arrival_time,
               a.crby,
               a.crtime,
               a.warehouse_id,
               c.warehouse_name,
               a.supply_address,
               c.area_id,
               aa.area_name,
               a.canteen_id,
               ac.canteen_name,
               a.stall_id
        FROM drp_order_goods a
                 left join alloc_canteen ac on ac.canteen_id = a.canteen_id
                 left join drp_supplier b on b.supplier_id = a.supplier_id
                 left join drp_warehouse c on c.warehouse_id = a.warehouse_id
                 left join alloc_area aa on aa.area_id = c.area_id
        <where>
            a.order_status = #{orderStatus}
              and a.approve_status = 3
              and a.supplier_confirm_status = 2
            <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
                AND a.order_goods_id like #{content.orderGoodsId}
            </if>
            <if test="content.orderTitle != null and content.orderTitle != ''">
                AND a.order_title like #{content.orderTitle}
            </if>
            <if test="content.intoStatus != null">
                <if test="content.intoStatus == 1">
                    AND a.total_into_num = a.total_num
                </if>
                <if test="content.intoStatus == 2">
                    AND (a.total_into_num is null or a.total_into_num = 0)
                </if>
                <if test="content.intoStatus == 3">
                    AND (a.total_into_num <![CDATA[ > ]]> 0 AND a.total_into_num <![CDATA[ < ]]> a.total_num)
                </if>
            </if>
            <if test="content.supplierId != null">
                AND a.supplier_id = #{content.supplierId}
            </if>
            <if test="content.crby != null and content.crby != ''">
                and a.crby like #{content.crby}
            </if>
            <if test="content.crtimeStart != null">
                and a.crtime <![CDATA[ >= ]]> #{content.crtimeStart}
            </if>
            <if test="content.crtimeEnd != null">
                and a.crtime <![CDATA[ <= ]]> #{content.crtimeEnd}
            </if>
            <if test="content.areaIdList != null and content.areaIdList.size() > 0">
                AND aa.area_id in
                <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="content.areaAuth != null and content.areaAuth.size() > 0">
                and aa.area_id in
                <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">
                    #{areaId}
                </foreach>
            </if>
        </where>
        order by a.crtime desc,a.order_goods_id desc
    </select>
    <select id="listPayAndRefundRecords" resultType="com.bonus.canteen.core.supply.vo.DrpPayAndRefundRecordsListVO">
        SELECT
            *
        FROM
            (
            SELECT
                pay_id id,
                pay_amount amount,
                pay_bank bank,
                pay_account_name account_name,
                pay_account account,
                pay_time time,
                pay_remark remark,
                1 AS payRefundType
            FROM
                drp_order_goods_pay
            WHERE
                type = 2 and order_goods_id = #{content.orderGoodsId}
                UNION ALL
            SELECT
                refund_money_id id,
                refund_amount amount,
                collect_bank bank,
                collect_account_name account_name,
                collect_account account,
                refund_time time,
                refund_remark remark,
                2 AS payRefundType
            FROM
                drp_order_goods_refund
            WHERE
                type = 2 and order_goods_id = #{content.orderGoodsId}
            ) t
        ORDER BY
            time DESC
    </select>
    <select id="listPayAndRefundAmount" resultType="com.bonus.canteen.core.supply.po.OrderGoodsPayRefundAmountModel">
        SELECT
            pay_id,
            order_goods_id,
            pay_amount,
            type payStatus,
            1 payRefundType
        FROM
            drp_order_goods_pay
        where
            order_goods_id IN
            <foreach collection="orderGoodsIdList" item="orderGoodsId" separator="," open="(" close=")">
                #{orderGoodsId}
            </foreach>
        UNION ALL
        SELECT
            refund_money_id pay_id,
            order_goods_id,
            refund_amount pay_amount,
            type payStatus,
            2 payRefundType
        FROM
            drp_order_goods_refund
        where
            order_goods_id IN
            <foreach collection="orderGoodsIdList" item="orderGoodsId" separator="," open="(" close=")">
                #{orderGoodsId}
            </foreach>
    </select>
    <select id="pageWaitPayAndRefundRecords"
            resultType="com.bonus.canteen.core.supply.vo.DrpOrderPayAndRefundRecordsPageVO">
        select * from (
            SELECT
              a.order_goods_id,a.order_title,a.supplier_id,b.supplier_name,t1.waitAmount,t2.amount,1 as payRefundType,a.crtime
            FROM
              drp_order_goods a LEFT JOIN drp_supplier b on b.supplier_id = a.supplier_id
                                LEFT JOIN (select order_goods_id,sum(pay_amount) waitAmount from drp_order_goods_pay where type = 1 GROUP BY order_goods_id) t1 on t1.order_goods_id = a.order_goods_id
                                LEFT JOIN (select order_goods_id,sum(pay_amount) amount from drp_order_goods_pay where type = 2 GROUP BY order_goods_id) t2 on t2.order_goods_id = a.order_goods_id
            WHERE
              a.order_status = #{orderStatus}
            AND a.approve_status = #{approveStatus}
            AND a.supplier_confirm_status = #{supplierConfirmStatus}
            and t1.waitAmount is not null
            UNION all
            SELECT
              a.order_goods_id,a.order_title,a.supplier_id,b.supplier_name,t1.waitAmount,t2.amount,2 as payRefundType,a.crtime
            FROM
              drp_order_goods a LEFT JOIN drp_supplier b on b.supplier_id = a.supplier_id
                                LEFT JOIN (select order_goods_id,sum(refund_amount) waitAmount from drp_order_goods_refund where type = 1 GROUP BY order_goods_id) t1 on t1.order_goods_id = a.order_goods_id
                                LEFT JOIN (select order_goods_id,sum(refund_amount) amount from drp_order_goods_refund where type = 2 GROUP BY order_goods_id) t2 on t2.order_goods_id = a.order_goods_id
            WHERE
              a.order_status = #{orderStatus}
            AND a.approve_status = #{approveStatus}
            AND a.supplier_confirm_status = #{supplierConfirmStatus}
            and t1.waitAmount is not null) tt1
        <where>
            <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
                AND tt1.order_goods_id like #{content.orderGoodsId}
            </if>
            <if test="content.orderTitle != null and content.orderTitle != ''">
                AND tt1.order_title like #{content.orderTitle}
            </if>
            <if test="content.supplierId != null">
                AND tt1.supplier_id = #{content.supplierId}
            </if>
            <if test="content.crtimeStart != null">
                and tt1.crtime <![CDATA[ >= ]]> #{content.crtimeStart}
            </if>
            <if test="content.crtimeEnd != null">
                and tt1.crtime <![CDATA[ <= ]]> #{content.crtimeEnd}
            </if>
        </where>
        ORDER BY tt1.crtime desc,tt1.order_goods_id
    </select>
    <select id="payAndRefundTotal" resultType="com.bonus.canteen.core.supply.vo.DrpPayAndRefundTotalVO">
        select
            (select sum(pay_amount) from drp_order_goods_pay where type = 1 and (crtime BETWEEN #{startTime} and #{endTime})) as todayApplyPayAmount,
            (select sum(pay_amount) from drp_order_goods_pay where type = 2 and (crtime BETWEEN #{startTime} and #{endTime})) as todayPayAmount,
            (select sum(refund_amount) from drp_order_goods_refund where type = 1 and (crtime BETWEEN #{startTime} and #{endTime})) as todayApplyRefundAmount,
            (select sum(refund_amount) from drp_order_goods_refund where type = 2 and (crtime BETWEEN #{startTime} and #{endTime})) as todayRefundAmount,
            (select sum(pay_amount) from drp_order_goods_pay where type = 1 ) as totalApplyPayAmount,
            (select sum(pay_amount) from drp_order_goods_pay where type = 2 ) as totalPayAmount,
            (select sum(refund_amount) from drp_order_goods_refund where type = 1 ) as totalApplyRefundAmount,
            (select sum(refund_amount) from drp_order_goods_refund where type = 2 ) as totalRefundAmount
    </select>
    <select id="getPaySumByOrderId" resultType="com.bonus.canteen.core.supply.vo.DrpPayAndRefundSumGetByOrderIdVO">
        SELECT
            a.order_goods_id,
            a.order_amount,
            IFNULL( t1.amount, 0 ) applyAmount,
            IFNULL( t2.amount, 0 ) isAmount,
            a.order_amount - IFNULL( t2.amount, 0 ) waitAmount
        FROM
            drp_order_goods a
                LEFT JOIN ( SELECT order_goods_id, sum( pay_amount ) amount FROM drp_order_goods_pay WHERE type = 1 GROUP BY order_goods_id ) t1 ON t1.order_goods_id = a.order_goods_id
                LEFT JOIN ( SELECT order_goods_id, sum( pay_amount ) amount FROM drp_order_goods_pay WHERE type = 2 GROUP BY order_goods_id ) t2 ON t2.order_goods_id = a.order_goods_id
        WHERE
            a.order_goods_id = #{content.orderGoodsId}
    </select>
    <select id="getRefundSumByOrderId" resultType="com.bonus.canteen.core.supply.vo.DrpPayAndRefundSumGetByOrderIdVO">
        SELECT
            a.order_goods_id,
            a.order_amount,
            IFNULL( t1.amount, 0 ) applyAmount,
            IFNULL( t2.amount, 0 ) isAmount,
            a.order_amount - IFNULL( t2.amount, 0 ) waitAmount
        FROM
            drp_order_goods a
                LEFT JOIN ( SELECT order_goods_id, sum( refund_amount ) amount FROM drp_order_goods_refund WHERE type = 1 GROUP BY order_goods_id ) t1 ON t1.order_goods_id = a.order_goods_id
                LEFT JOIN ( SELECT order_goods_id, sum( refund_amount ) amount FROM drp_order_goods_refund WHERE type = 2 GROUP BY order_goods_id ) t2 ON t2.order_goods_id = a.order_goods_id
        WHERE
            a.order_goods_id = #{content.orderGoodsId}
    </select>
</mapper>
