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


    <select id="listMaterialUnit" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialBidRecordsPageVO">
        SELECT distinct sub.material_id,
                        sub.material_name,
                        sub.material_code,
                        sub.category_id,
                        d.category_name,
                        sub.unit_id,
                        du.unit_name,
                        sub.image_url,
                        dms.supplier_id,
                        ds.supplier_name,
                        sub.area_id,
                        aa.area_name
        FROM menu_material sub
                 LEFT JOIN drp_unit du ON du.unit_id = sub.unit_id
                 LEFT JOIN menu_material_category d ON d.category_id = sub.category_id
                 left join drp_menu_material_supplier dms on sub.material_id = dms.material_id
                 left join drp_supplier ds on dms.supplier_id = ds.supplier_id
                 left join alloc_area aa on sub.area_id = aa.area_id
                 left join drp_material_latest_price dmlp
                           on sub.material_id = dmlp.material_id and dmlp.unit_id = sub.unit_id and dmlp.price_type = 1
        WHERE  1=1
<!--        sub.del_flag = 2-->

        <if test="content.areaId != null">
            and sub.area_id = #{content.areaId}
        </if>
        <if test="content.areaIdList != null and content.areaIdList.size() != 0">
            and sub.area_id in
            <foreach collection="content.areaIdList" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        <!--区域权限-->
        <if test="content.areaAuth != null and content.areaAuth.size() > 0">
            and sub.area_id in
            <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="content.materialName != null and content.materialName != ''">
            and (sub.material_name like concat(concat('%', #{content.materialName}), '%')
                or sub.material_code like concat(concat('%', #{content.materialName}), '%')
                )
        </if>
        <if test="content.categoryIdList != null and content.categoryIdList.size() > 0">
            and sub.category_id in
            <foreach close=")" collection="content.categoryIdList" item="categoryId" open="(" separator=",">
                #{categoryId}
            </foreach>
        </if>
        <if test="content.materialType != null">
            and sub.material_type = #{content.materialType}
        </if>
        <if test="content.winSupplierId != null">
            and dmlp.supplier_id = #{content.winSupplierId}
        </if>
        <if test="content.bidTimeStart != null">
            and dmlp.start_time >= #{content.bidTimeStart}
        </if>
        <if test="content.bidTimeEnd != null">
            and dmlp.end_time <![CDATA[ <= ]]> #{content.bidTimeEnd}
        </if>
        <if test="content.priceState != null ">
            <if test="content.priceState == 1">
                and now() between dmlp.start_time and dmlp.end_time
            </if>
            <if test="content.priceState == 2">
                and (NOT now() between dmlp.start_time and dmlp.end_time)
            </if>
        </if>
        <if test="content.startTime != null">
            and dmlp.create_time >= #{content.startTime}
        </if>
        <if test="content.endTime != null">
            and dmlp.create_time  <![CDATA[ <= ]]> #{content.endTime}
        </if>

    </select>
    <select id="listSupplierWinRecords" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialSupplierWinRecordsVO">
        SELECT
        a.material_id,
        b.unit_id,
        a.win_price
        FROM
        drp_material_win_bid_records a
        LEFT JOIN menu_material b ON a.material_id = b.material_id
        WHERE
        a.win_supplier_id = #{supplierId}
        AND a.material_id IN
        <foreach collection="materialIdList" item="materialId" separator="," open="(" close=")">
            #{materialId}
        </foreach>
        <if test="materialInquiryId != null">
         and a.material_inquiry_id=  #{materialInquiryId}
        </if>
        order by a.create_time desc
    </select>
    <select id="listSupplierPriceByMaterialId"
            resultType="com.bonus.canteen.core.supply.vo.DrpMaterialBidRecordsSupplierVO">
        SELECT a.win_supplier_id,
               b.supplier_name winSupplierName,
               a.win_date,
               a.win_price,
               a.farmers_market_price,
               a.bid_validity_start_time,
               a.bid_validity_end_time,
               a.material_inquiry_id,
               mm.material_name,
               mm.material_code,
               mmc.category_name,
               du.unit_id,
               du.unit_name
        FROM drp_material_win_bid_records a
                 LEFT JOIN drp_supplier b ON b.supplier_id = a.win_supplier_id
                 LEFT JOIN menu_material mm on a.material_id = mm.material_id
                 LEFT JOIN menu_material_category mmc on mm.category_id = mmc.category_id
                 LEFT JOIN drp_unit du on mm.unit_id = du.unit_id
        WHERE a.material_id = #{materialId}
    </select>
    <select id="listSupplierWinRecordsBySupplier"
            resultType="com.bonus.canteen.core.supply.vo.DrpMaterialSupplierWinRecordsVO">
        SELECT
        sub.material_id,
        sub.unit_id,
        sub.win_price
        FROM (
        SELECT
        a.material_id,
        b.unit_id,
        a.win_price,
<!--        ROW_NUMBER() OVER (PARTITION BY a.material_id ORDER BY a.crtime DESC) AS rn-->
        @row_num := IF(@prev_material = a.material_id, @row_num + 1, 1)  AS rn
        FROM
        drp_material_win_bid_records a
        LEFT JOIN menu_material b ON a.material_id = b.material_id
        WHERE
        a.win_supplier_id =  #{supplierId} and now() between a.bid_validity_start_time and a.bid_validity_end_time
        AND a.material_id IN
        <foreach collection="materialIdList" item="materialId" separator="," open="(" close=")">
            #{materialId}
        </foreach>
        ) sub
        WHERE sub.rn = 1
    </select>
    <select id="listSupplierPriceByMaterialIdLast"
            resultType="com.bonus.canteen.core.supply.vo.DrpMaterialBidRecordsSupplierVO">
        SELECT a.win_supplier_id,
        b.supplier_name winSupplierName,
        a.win_date,
        a.win_price,
        a.farmers_market_price,
        a.bid_validity_start_time,
        a.bid_validity_end_time,
        a.material_inquiry_id
        FROM drp_material_win_bid_records a
        LEFT JOIN drp_supplier b ON b.supplier_id = a.win_supplier_id
        WHERE a.material_id = #{materialId}
        order by a.create_time desc
        limit 1
    </select>
    <select id="listSupplierPriceByMaterialIdList" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialBidRecordsSupplierVO">
        SELECT
        sub.material_id,
        sub.win_supplier_id,
        sub.winSupplierName,
        sub.win_date,
        sub.win_price,
        sub.bid_validity_start_time,
        sub.bid_validity_end_time,
        sub.material_inquiry_id
        FROM
        (
        SELECT
        a.material_id,
        a.win_supplier_id,
        b.supplier_name winSupplierName,
        a.win_date,
        a.win_price,
        a.bid_validity_start_time,
        a.bid_validity_end_time,
        a.material_inquiry_id,
        @row_num := IF(@prev_material = material_id, @row_num + 1, 1)  AS row_num
<!--        ROW_NUMBER() OVER ( PARTITION BY a.material_id ORDER BY a.crtime DESC ) AS row_num-->
        FROM
        drp_material_win_bid_records a
        LEFT JOIN drp_supplier b ON b.supplier_id = a.win_supplier_id
        ) sub
        WHERE
        sub.row_num = 1
        AND sub.material_id IN
        <foreach collection="materialIdList" item="materialId" separator="," open="(" close=")">
            #{materialId}
        </foreach>
    </select>
    <select id="getLastOrderGoodsPrice"
            resultType="com.bonus.canteen.core.supply.vo.DrpLastOrderGoodsVO">
        select dogd.single_price as lastOrderGoodsPrice, dog.create_time as lastOrderGoodsDate,
        dog.supplier_id as lastOrderGoodsSupplierId, ds.supplier_name as lastOrderGoodsSupplierName
        from drp_order_goods_detail dogd
        left join drp_order_goods dog on dogd.order_goods_id = dog.order_goods_id
        left join drp_supplier ds on dog.supplier_id = ds.supplier_id
        where dogd.material_id = #{materialId} and dogd.unit_id = #{unitId}
        and dog.order_status = 2
        and dog.del_flag = 2
        and dog.approve_status = 3
        order by dog.create_time desc
        limit 1
    </select>

    <select id="getMarketPrice" resultType="com.bonus.canteen.core.supply.vo.DrpMarketPriceVO">
        select dmpd.unit_price, dmp.harvest_time, dmp.harvest_source
        from drp_market_price_detail dmpd
        left join drp_market_price dmp on dmpd.harvest_id = dmp.harvest_id
        where dmpd.material_id = #{materialId} and dmpd.unit_id = #{unitId}
        and dmp.del_flag = 2
        and dmp.commit_status = 2
        order by dmp.create_time desc
        limit 3
    </select>

    <select id="getMaterialSupplier" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialSupplierVO">
        select mm.material_id,
        mm.material_name,
        mm.material_code,
        mm.category_id,
        mmc.category_name,
        mm.unit_id,
        du.unit_name,
        dms.supplier_id,
        ds.supplier_name,
        dms.alternative_supplier,
        mm.area_id,
        aa.area_name,
        dms.if_supply
        from menu_material mm
        left join drp_menu_material_supplier dms on mm.material_id = dms.material_id and dms.category_id is null
        <if test="content.canteenId != null">
            and dms.canteen_id = #{content.canteenId}
        </if>
        left join drp_supplier ds on dms.supplier_id = ds.supplier_id
        left join menu_material_category mmc on mm.category_id = mmc.category_id
        left join drp_unit du on mm.unit_id = du.unit_id
        left join alloc_area aa on mm.area_id = aa.area_id
        <where>
            1=1
<!--            and mm.del_flag = 2-->
            -- 根据所选食堂的所属区域过滤原料
            and (
            mm.area_id in (select area_id from alloc_canteen where canteen_id = #{content.canteenId})
            or mm.area_id is null
            )
            <if test="content.key != null and content.key != ''">
                and (
                mm.material_name like concat('%', #{content.key}, '%')
                or mm.material_code like concat('%', #{content.key}, '%')
                or mm.bar_code like concat('%', #{content.key}, '%')
                )
            </if>
            <if test="content.categoryIdList != null and content.categoryIdList.size() > 0">
                and mm.category_id in
                <foreach collection="content.categoryIdList" item="categoryId" separator="," open="(" close=")">
                    #{categoryId}
                </foreach>
            </if>
            <if test="content.areaIdList != null and content.areaIdList.size() > 0">
                and ( mm.area_id in
                <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
                    or mm.area_id is null
                )
            </if>
        </where>
    </select>

    <select id="getLastOrderGoodsPriceList" resultType="com.bonus.canteen.core.supply.vo.DrpLastOrderGoodsVO">
        select * from (
        select dogd.single_price                                                             as lastOrderGoodsPrice,
               dog.create_time                                                                    as lastOrderGoodsDate,
               dog.supplier_id                                                               as lastOrderGoodsSupplierId,
               ds.supplier_name                                                              as lastOrderGoodsSupplierName,
               dogd.order_goods_id                                                           as orderGoodsId,
                dogd.material_id                                                              as materialId,
                dogd.unit_id                                                                  as unitId,
<!--               ROW_NUMBER() OVER ( PARTITION BY dogd.material_id ORDER BY dogd.crtime DESC ) AS rn-->
                   @row_num := IF(@prev_material = dogd.material_id, @row_num + 1, 1)  AS rn
        from drp_order_goods_detail dogd
                 left join drp_order_goods dog on dogd.order_goods_id = dog.order_goods_id
                 left join drp_supplier ds on dog.supplier_id = ds.supplier_id
                left join alloc_canteen ac on dog.canteen_id = ac.canteen_id
        where dog.order_status = 2
          and dog.del_flag = 2
          and dog.approve_status = 3
        <if test="materialAndUnitDTOS != null and materialAndUnitDTOS.size() > 0">
            and CONCAT(dogd.material_id, dogd.unit_id) IN
            <foreach collection="materialAndUnitDTOS" item="item" open="(" separator="," close=")">
                CONCAT(#{item.materialId}, #{item.unitId})
            </foreach>
        </if>
        <if test="areaId != null ">
            and ac.area_id = #{areaId}
        </if>
        ) sub
        where sub.rn = 1
    </select>

    <select id="getMarketPriceList" resultType="com.bonus.canteen.core.supply.vo.DrpMarketPriceVO">
        select * from (
        select dmpd.unit_price,
               dmp.harvest_time,
               dmp.harvest_source,
               dmpd.material_id                                                             as materialId,
               dmpd.unit_id                                                                 as unitId,
<!--               ROW_NUMBER() OVER ( PARTITION BY dmpd.material_id ORDER BY dmp.crtime DESC ) AS rn-->
        @row_num := IF(@prev_material = dmpd.material_id, @row_num + 1, 1)  AS rn
        from drp_market_price_detail dmpd
                 left join drp_market_price dmp on dmpd.harvest_id = dmp.harvest_id
                 left join alloc_area ac on dmp.area_id = ac.area_id
        where dmp.del_flag = 2
          and dmp.commit_status = 2
        <if test="materialAndUnitDTOS != null and materialAndUnitDTOS.size() > 0">
            and CONCAT(dmpd.material_id, dmpd.unit_id) IN
            <foreach collection="materialAndUnitDTOS" item="item" open="(" separator="," close=")">
                CONCAT(#{item.materialId}, #{item.unitId})
            </foreach>
        </if>
        <if test="areaId != null">
            and ac.area_id = #{areaId}
        </if>
        ) sub
        where sub.rn in (1, 2, 3)
    </select>
</mapper>
