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

    <select id="selectByInquirySupplierId" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpQuoteDetailPageVO">
        SELECT
            b.quote_id,
            a.material_id,
            c.material_code,
            c.image_url,
            c.material_name,
            c.category_id,
            d.category_name,
            a.unit_id,
            e.unit_name,
            a.size,
            a.pur_num,
            b.single_price,
            b.total_price,
            a.notes
        FROM
            drp_inquiry_detail a
            LEFT JOIN drp_quote b ON a.inquiry_detail_id = b.inquiry_detail_id
            LEFT JOIN menu_material c ON a.material_id = c.material_id
            LEFT JOIN menu_material_category d ON d.category_id = c.category_id
            LEFT JOIN drp_unit e ON e.unit_id = a.unit_id
        where
        b.inquiry_supplier_id = #{inquirySupplierId}
    </select>

    <select id="selectByDrpFinalBidsDTO" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpFinalBidsQuotePageVO">
        select
        a.inquiry_detail_id,
        a.material_id,
        b.material_code,
        b.material_name,
        d.category_name,
        a.pur_num,
        a.unit_id,
        c.unit_name,
        b.image_url,
        dd.guide_price,dd.create_time
        from drp_inquiry_detail a left join menu_material b on b.material_id = a.material_id
        left join drp_unit c on c.unit_id = a.unit_id
        left join menu_material_category d on d.category_id = b.category_id
        LEFT JOIN (
        SELECT
        dmgp.material_id,
        dmgp.guide_price,
        dmgp.create_time
        FROM
        drp_material_guide_price dmgp
        JOIN (
        SELECT
        material_id,
        MAX(create_time) AS max_crtime
        FROM
        drp_material_guide_price
        GROUP BY material_id
        ) max_times ON dmgp.material_id = max_times.material_id AND dmgp.create_time = max_times.max_crtime
        ) as dd ON a.material_id = dd.material_id
        WHERE
        a.inquiry_id = #{content.inquiryId}
        <if test="content.materialName != null and content.materialName != ''">
            and b.material_name like #{content.materialName}
        </if>
    </select>


    <select id="selectByInquiryId" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpInquiryDetailPageVO">
        SELECT
            a.inquiry_detail_id,
            a.inquiry_id,
            a.material_id,
            b.material_code,
            b.material_name,
            d.category_name,
            a.unit_id,
            c.unit_name,
            a.notes,
            a.size,
            a.pur_num,
            t1.quote_num,
            t1.single_price,
            t1.total_price,
            b.image_url,
            a.remark,
            di.bid_time
        FROM
            drp_inquiry_detail a
            left join drp_inquiry di on a.inquiry_id = di.inquiry_id
            LEFT JOIN menu_material b ON a.material_id = b.material_id
            LEFT JOIN drp_unit c ON a.unit_id = c.unit_id
            LEFT JOIN menu_material_category d ON d.category_id = b.category_id
            LEFT JOIN (
            SELECT
                a.inquiry_detail_id,
                a.quote_num,
                a.single_price,
                a.total_price
            FROM
                drp_quote a
                LEFT JOIN drp_inquiry_supplier b ON b.inquiry_supplier_id = a.inquiry_supplier_id
            WHERE
                supplier_id = #{bidSupplierId}
            ) t1 ON t1.inquiry_detail_id = a.inquiry_detail_id
        WHERE 1 = 1
        <if test="inquiryId != null and inquiryId != '' ">
           and a.inquiry_id = #{inquiryId};
        </if>

    </select>


    <select id="listExcelDetail" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpExcelInquiryDetailVO">
        select
        C.material_code,
        C.material_name ,
        D.category_name ,
        B.unit_name ,
        A.pur_num ,
        A.size ,

        <if test="bidSupplierId != null">
            t1.quote_num,
            t1.single_price neSinglePrice,
            t1.total_price neTotalPrice,
        </if>
        A.notes
        from
        drp_inquiry_detail A
        left join drp_unit B on A.unit_id = B.unit_id
        left join menu_material C on A.material_id = C.material_id
        left join menu_material_category D on C.category_id = D.category_id
        <if test="bidSupplierId != null">
            LEFT JOIN (
            SELECT
            a.inquiry_detail_id,
            a.quote_num,
            a.single_price,
            a.total_price
            FROM
            drp_quote a
            LEFT JOIN drp_inquiry_supplier b ON b.inquiry_supplier_id = a.inquiry_supplier_id
            WHERE
            supplier_id = #{bidSupplierId}
            ) t1 ON t1.inquiry_detail_id = a.inquiry_detail_id
        </if>

        where
        A.inquiry_id = #{inquiryId}
    </select>

    <select id="listExcelFinalBidsDetail" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpExcelFinalBidsDetailVO">
        select
            c.material_name ,
            d.category_name ,
            a.notes ,
            b.unit_name ,
            a.size ,
            a.pur_num ,
            e.quote_num ,
            e. single_price as neSinglePrice,
            e.total_price as neTotalPrice
        from
            drp_inquiry_detail a
                LEFT JOIN drp_unit b on a.unit_id = b.unit_id
                LEFT JOIN menu_material c on a.material_id = c.material_id
                LEFT JOIN menu_material_category d on c.category_id = d.category_id
                LEFT JOIN drp_quote e on a.inquiry_detail_id = e.inquiry_detail_id
        where
            a.inquiry_id = #{inquiryId} and e.inquiry_supplier_id = #{inquirySupplierId}
    </select>
    <select id="selectByInquiryIdPage" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpInquiryDetailPageVO">
        SELECT
        a.inquiry_detail_id,
        a.inquiry_id,
        a.material_id,
        b.material_code,
        b.material_name,
        d.category_name,
        a.unit_id,
        c.unit_name,
        a.notes,
        a.size,
        a.pur_num,
        t1.quote_num,
        t1.single_price,
        t1.total_price,
        b.image_url,
        a.remark,
        di.bid_time
        FROM
        drp_inquiry_detail a
        left join drp_inquiry di on a.inquiry_id = di.inquiry_id
        LEFT JOIN menu_material b ON a.material_id = b.material_id
        LEFT JOIN drp_unit c ON a.unit_id = c.unit_id
        LEFT JOIN menu_material_category d ON d.category_id = b.category_id
        LEFT JOIN (
        SELECT
        a.inquiry_detail_id,
        a.quote_num,
        a.single_price,
        a.total_price
        FROM
        drp_quote a
        LEFT JOIN drp_inquiry_supplier b ON b.inquiry_supplier_id = a.inquiry_supplier_id
        WHERE 1 = 1
        <if test="bidSupplierIdList != null and bidSupplierIdList.size() > 0">
            and supplier_id IN
            <foreach collection="bidSupplierIdList" item="bidSupplierId" separator="," open="(" close=")">
                #{bidSupplierId}
            </foreach>
        </if>
        ) t1 ON t1.inquiry_detail_id = a.inquiry_detail_id

    </select>

    <select id="selectDetailListByInquiryId" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpInquiryDetailPageVO">
        SELECT
        a.inquiry_detail_id,
        a.inquiry_id,
        a.material_id,
        b.material_code,
        b.material_name,
        d.category_name,
        a.unit_id,
        c.unit_name,
        a.notes,
        a.size,
        a.pur_num,
        b.image_url,
        a.remark,
        di.bid_time
        FROM
        drp_inquiry_detail a
        left join drp_inquiry di on a.inquiry_id = di.inquiry_id
        LEFT JOIN menu_material b ON a.material_id = b.material_id
        LEFT JOIN drp_unit c ON a.unit_id = c.unit_id
        LEFT JOIN menu_material_category d ON d.category_id = b.category_id
        WHERE 1 = 1
        <if test="inquiryId != null and inquiryId != '' ">
            and a.inquiry_id = #{inquiryId};
        </if>
    </select>
</mapper>



