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


    <select id="selectAllList" resultType="com.bonus.canteen.core.supply.vo.DrpInquiryPageVO">
        select
        a.inquiry_id,
        a.title,
        a.start_time,
        a.end_time,
        a.status,
        a.request_arrival_time,
        a.inquiry_notes,
        a.phone,
        a.link_man,
        a.address,
        a.bid_total_price,
        a.supplier_id bidSupplierId,
        c.supplier_name bidSupplierName,
        a.crtime,
        a.crby,
        a.bid_time,
        a.area,
        a.area_id,
        b.area_name
        from
        drp_inquiry a
        left join drp_supplier c on c.supplier_id = a.supplier_id
        left join alloc_area b on a.area_id = b.area_id
        where
        a.del_flag = 2
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            and a.area in
            <foreach collection="content.areaIdList" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        <!--区域权限-->
        <if test="content.areaAuth != null and content.areaAuth.size() > 0">
            and a.area_id in
            <foreach collection="content.areaAuth" item="areaId" separator="," open="(" close=")">
                #{areaId}
            </foreach>
        </if>
        <if test="content.orderStatus != null">
            <if test="content.orderStatus == 1">
                and a.status= 1
            </if>
            <if test="content.orderStatus == 2">
                and a.status <![CDATA[ <> ]]> 1
            </if>
        </if>
        <if test="content.approveStatus != null">
            and a.approve_status = #{content.approveStatus}
        </if>
        <if test="content.inquiryId != null and content.inquiryId != ''">
            and a.inquiry_id = #{content.inquiryId}
            or a.title like Concat(Concat('%',#{content.inquiryId}),'%')
        </if>
        <if test="content.status != null">
            <if test="content.status == 1">
                and a.status = 2

            </if>
            <if test="content.status == 2">
                and a.status not in (1,2)
            </if>
        </if>
        <if test="content.bidSupplierId != null">
            and a.supplier_id = #{content.bidSupplierId}
        </if>
        <if test="content.crby != null and content.crby != ''">
            and a.crby like #{content.crby}
        </if>
        <if test="content.startTime != null  ">
            and a.crtime >= #{content.startTime}
        </if>
        <if test="content.endTime != null  ">
            and a.crtime <![CDATA[ <= ]]> #{content.endTime}
        </if>
        <if test="content.inquiryStartTime != null  ">
            and a.status <![CDATA[ <> ]]> 1
            and a.start_time >= #{content.inquiryStartTime}
        </if>
        <if test="content.inquiryEndTime != null  ">
            and a.status <![CDATA[ <> ]]> 1
            and a.end_time <![CDATA[ <= ]]> #{content.inquiryEndTime}
        </if>
        <if test="content.inquiryState != null">
            <if test="content.inquiryState == 1">
                and a.status not in (1,5)
                and a.start_time > now()
            </if>
            <if test="content.inquiryState == 2">
                and a.status not in (1,5)
                and now() between a.start_time and a.end_time
            </if>
            <if test="content.inquiryState == 3">
                and a.status not in (1,5)
                and now() > a.end_time
            </if>
            <if test="content.inquiryState == 4">
                and a.status = 5
            </if>
        </if>

        order by a.crtime desc
    </select>

    <select id="selectByQuoteDTO" resultType="com.bonus.canteen.core.supply.vo.DrpQuotePageVO">
        SELECT
        b.inquiry_supplier_id,
        a.inquiry_id,
        a.title,
        a.bid_time,
        a.request_arrival_time,
        b.arrival_time,
        b.bid_status,
        a.start_time,
        a.end_time,
        b.quote_time,
        b.quote_amount,
        a.inquiry_notes,
        a.link_man,
        a.phone,
        a.address
        FROM
        drp_inquiry a
        LEFT JOIN drp_inquiry_supplier b ON a.inquiry_id = b.inquiry_id
        WHERE
        a.del_flag = 2
        and a.status not in
        <foreach collection="notCommitStatus" item="status" open="(" close=")" separator=",">
            #{status}
        </foreach>
        <if test="content.inquiryId != null and content.inquiryId != ''">
            and a.inquiry_id = #{content.inquiryId}
        </if>
        <if test="content.bidStatus != null and content.bidStatus != 0">
            and b.bid_status = #{content.bidStatus}
        </if>
        <if test="supplierAuthority != null">
            and b.supplier_id = #{supplierAuthority}
        </if>
        <if test="content.keyword != null and content.keyword != ''">
            and (
            a.inquiry_id like concat('%',#{content.keyword},'%')
            or a.title like concat('%',#{content.keyword},'%')
            )
        </if>
        order by a.crtime desc
    </select>

    <select id="selectExcelInquiry" resultType="com.bonus.canteen.core.supply.vo.DrpExcelInquiryVO">
        select a.inquiry_id,
               a.title,
               a.start_time,
               a.end_time,
               a.request_arrival_time,
               a.inquiry_notes,
               a.phone,
               a.link_man,
               a.address,
               a.supplier_id,
               c.supplier_name,
               a.bid_time,
               a.crtime
        from drp_inquiry a
                 left join drp_supplier c on c.supplier_id = a.supplier_id
        where a.inquiry_id = #{inquiryId}
    </select>

    <select id="selectExcelQuote" resultType="com.bonus.canteen.core.supply.vo.DrpExcelQuoteVO">
        select a.inquiry_id,
               a.title,
               a.start_time,
               a.end_time,
               c.quote_time,
               a.request_arrival_time,
               a.inquiry_notes,
               a.phone,
               a.link_man,
               a.address
        from drp_inquiry a
                 left join drp_inquiry_supplier c on c.inquiry_id = a.inquiry_id
        where c.inquiry_supplier_id = #{inquirySupplierId}
    </select>

    <select id="selectExcelFinalBids" resultType="com.bonus.canteen.core.supply.vo.DrpExcelFinalBidsVO">
        select a.inquiry_id,
               a.title,
               b.warehouse_name,
               a.bid_time,
               a.request_arrival_time,
               a.link_man,
               a.phone,
               a.status as       neBidStatus,
               a.address,
               a.inquiry_notes,
               d.supplier_name,
               a.bid_total_price neBidTotalPrice
        from drp_inquiry a
                 left join drp_warehouse b on a.warehouse_id = b.warehouse_id
                 left join drp_inquiry_supplier c on c.inquiry_id = a.inquiry_id
                 left join drp_supplier d on d.supplier_id = a.supplier_id
        where c.inquiry_id = #{inquiryId}
          and c.supplier_id = #{supplierId}
    </select>
    <select id="selectBidTimesAndAmount" resultType="com.bonus.canteen.core.supply.po.SupplierBidTimesAndAmountModel">
        SELECT
        count( id ) bidNumbers,
        sum( bid_total_price ) cumulativeBidAmount
        FROM
        drp_inquiry
        WHERE
        supplier_id = #{supplierId}
        <if test="content.startTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ >= ]]> #{content.startTime}
        </if>
        <if test="content.endTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ <= ]]> #{content.endTime}
        </if>
    </select>
    <select id="selectBidTimesAndAmount" resultType="com.bonus.canteen.core.supply.po.SupplierBidTimesAndAmountModel"
            databaseId="kb">
        SELECT
        count( id ) bidNumbers,
        sum( bid_total_price ) cumulativeBidAmount
        FROM
        drp_inquiry
        WHERE
        supplier_id = #{supplierId}
        <if test="content.startTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ >= ]]> to_date(#{content.startTime},'YYYY-MM-DD')
        </if>
        <if test="content.endTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ <= ]]> to_date(#{content.endTime},'YYYY-MM-DD')
        </if>
    </select>
    <select id="selectLastBidInfo" resultType="com.bonus.canteen.core.supply.po.SupplierLastBidInfoModel">
        SELECT
        bid_time lastBidDate,
        bid_total_price lastBidAmount
        FROM
        drp_inquiry
        WHERE
        supplier_id = #{supplierId}
        <if test="content.startTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ >= ]]> #{content.startTime}
        </if>
        <if test="content.endTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ <= ]]> #{content.endTime}
        </if>
        AND `status` = #{bidStatus}
        ORDER BY
        bid_time DESC
        LIMIT 1
    </select>
    <select id="selectLastBidInfo" resultType=" com.bonus.canteen.core.supply.po.SupplierLastBidInfoModel" databaseId="kb">
        SELECT
        bid_time lastBidDate,
        bid_total_price lastBidAmount
        FROM
        drp_inquiry
        WHERE
        supplier_id = #{supplierId}
        <if test="content.startTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ >= ]]> to_date( #{content.startTime}, 'YYYY-MM-DD' )
        </if>
        <if test="content.endTime != null">
            and DATE_FORMAT( bid_time, '%Y-%m-%d' ) <![CDATA[ <= ]]> to_date( #{content.endTime}, 'YYYY-MM-DD' )
        </if>
        AND `status` = #{bidStatus}
        ORDER BY
        bid_time DESC
        LIMIT 1
    </select>

</mapper>



