<?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.purchase.mapper.DrpPurchaseOrderGoodsMapper">

    <select id="getDrpOrderGoodsLastPriceList" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpMaterialLatestPriceVO">
        SELECT
        sub.material_id,
        sub.single_price as latestPrice
        FROM (
        SELECT
        a.material_id,
        a.single_price,
        ROW_NUMBER() OVER (PARTITION BY a.material_id ORDER BY a.create_time DESC) AS rn
        FROM
        drp_order_goods_detail a
        left join drp_order_goods b on a.order_goods_id = b.order_goods_id

        WHERE
        b.order_status = 2 and b.approve_status = 3 and b.supplier_confirm_status = 2 and b.del_flag = 2
        AND a.material_id IN
        <foreach collection="materialIdList" item="materialId" separator="," open="(" close=")">
            #{materialId}
        </foreach>
        <if test="canteenId != null">
            AND b.canteen_id = #{canteenId}
        </if>
        ) sub
        WHERE sub.rn = 1
    </select>

    <select id="getDrpOrderGoodsList" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpOrderGoodsPageVO">
        SELECT
        t1.order_goods_id,
        t1.supplier_id,
        t3.supplier_name,
        t1.order_status,
        t1.order_amount,
        t1.request_arrival_time,
        t1.remark,
        t1.if_dismantle,
        t1.create_by,
        t1.create_time,
        t1.warehouse_id,
        dw.warehouse_name,
        t1.supply_address,
        t1.order_title,
        t1.approve_status,
        t1.process_instance_id,
        t1.supplier_confirm_status,
        t1.total_num,
        t1.total_into_num,
        t1.approve_by,
        t1.approve_time,
        t1.canteen_id,
        t1.stall_id,
        ac.canteen_name,
        ifnull(ac.area_id, t1.area_id) as area_id,
        aa.area_name,
        t1.confirm_time,
        t1.confirm_remark
        FROM
        drp_order_goods t1
        LEFT JOIN drp_supplier t3 ON t1.supplier_id = t3.supplier_id
        LEFT JOIN drp_warehouse dw ON dw.warehouse_id = t1.warehouse_id
        left join alloc_canteen ac on t1.canteen_id = ac.canteen_id
        left join alloc_area aa on aa.area_id = ac.area_id
        WHERE t1.del_flag = #{delFlag}
        <if test="content.orderGoodsId != null and content.orderGoodsId != ''">
            AND (
            t1.order_goods_id like CONCAT('%', #{content.orderGoodsId}, '%')
            or
            t1.order_title like CONCAT('%', #{content.orderGoodsId}, '%')
            )
        </if>
        <if test="content.supplierId != null">
            AND t1.supplier_id = #{content.supplierId}
        </if>
        <if test="content.orderStatus != null">
            AND t1.order_status = #{content.orderStatus}
        </if>
        <if test="content.startTime != null">
            AND t1.request_arrival_time <![CDATA[ >= ]]> #{content.startTime}
        </if>
        <if test="content.endTime != null">
            AND t1.request_arrival_time <![CDATA[ <= ]]> #{content.endTime}
        </if>
        <if test="content.supplierStatus != null">
            AND t3.`status` = #{content.supplierStatus}
        </if>
        <if test="content.warehouseId != null and content.warehouseId != '' ">
            AND t1.warehouse_id = #{content.warehouseId}
        </if>
        <if test="content.approveStatus != null">
            AND t1.approve_status = #{content.approveStatus}
        </if>
        <if test="content.supplierConfirmStatus != null">
            AND t1.supplier_confirm_status = #{content.supplierConfirmStatus}
        </if>
        <if test="content.instanceIds != null and content.instanceIds.size() > 0">
            AND t1.process_instance_id IN
            <foreach collection="content.instanceIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="content.createBy != null and content.createBy != ''">
            AND t1.create_by = #{content.createBy}
        </if>
        <if test="content.crStartTime != null">
            AND t1.create_time <![CDATA[ >= ]]> #{content.crStartTime}
        </if>
        <if test="content.crEndTime != null">
            AND t1.create_time <![CDATA[ <= ]]> #{content.crEndTime}
        </if>
        <if test="content.ifDismantle != null">
            AND t1.if_dismantle = #{content.ifDismantle}
        </if>
        <if test="content.ifAllInto == 2">
            AND (
            t1.total_into_num <![CDATA[ < ]]> t1.total_num
            or total_into_num is NULL
            )
        </if>
        <if test="content.canteenIdList != null and content.canteenIdList.size() > 0">
            and t1.canteen_id  IN
            <foreach collection="content.canteenIdList" item="canteenId" separator="," open="(" close=")">
                #{canteenId}
            </foreach>
        </if>
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            and ac.area_id  IN
            <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="content.orderGoodsIdList != null and content.orderGoodsIdList.size() > 0">
            and t1.order_goods_id  IN
            <foreach collection="content.orderGoodsIdList" item="orderGoodsId" separator="," open="(" close=")">
                #{orderGoodsId}
            </foreach>
        </if>
        <if test="content.stallIdList != null and content.stallIdList.size() > 0">
            and t1.order_goods_id  IN (
            select order_goods_id from drp_order_goods_detail dogd  where dogd.order_goods_id = t1.order_goods_id and dogd.stall_id in
            <foreach collection="content.stallIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
            )
        </if>
        <if test="content.canteenId != null">
            AND t1.canteen_id = #{content.canteenId}
        </if>
        ORDER BY t1.create_time DESC,t1.order_goods_id DESC
    </select>

    <select id="getDrpOrderGoodsInfo" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpOrderGoodsVO">
        SELECT t1.order_goods_id,
               t1.supplier_id,
               t2.supplier_name,
               t1.order_status,
               t1.order_amount,
               t1.request_arrival_time,
               t1.remark,
               t1.if_dismantle,
               t1.create_by,
               t1.create_time,
               t1.warehouse_id,
               dw.warehouse_name,
               t1.supply_address,
               t1.order_title,
               t1.approve_status,
               t1.process_instance_id,
               t1.supplier_confirm_status,
               t1.total_num,
               t1.canteen_id,
               t1.stall_id,
               as2.stall_name,
               ac.canteen_name,
               ifnull(t1.total_into_num, 0) as total_into_num,
               ifnull(ac.area_id, t1.area_id) as area_id,
               aa.area_name,
               t1.confirm_time,
               t1.confirm_remark
        FROM drp_order_goods t1
                 LEFT JOIN drp_supplier t2 ON t1.supplier_id = t2.supplier_id
                 LEFT JOIN drp_warehouse dw ON dw.warehouse_id = t1.warehouse_id
                 left join alloc_canteen ac on t1.canteen_id = ac.canteen_id
                 left join alloc_area aa on aa.area_id = ac.area_id
                 left join alloc_stall as2 on t1.stall_id = as2.stall_id
        WHERE t1.order_goods_id = #{orderGoodsId}
    </select>
</mapper>
