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

    <select id="page" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpContractPageVO">
        SELECT dpc.*,
               ds.supplier_name,
               ac.area_id,
               aa.area_name,
               ac.canteen_id,
               ac.canteen_name
        FROM drp_procurement_contract dpc
                 LEFT JOIN alloc_canteen ac ON ac.canteen_id = dpc.canteen_id
                 left join drp_supplier ds on ds.supplier_id = dpc.supplier_id
                 left join alloc_area aa on aa.area_id = ac.area_id
        WHERE dpc.del_flag = #{delFlag}
        <if test="content.contractInfo != null and content.contractInfo != ''">
            and (dpc.contract_id like #{content.contractInfo}
                or
                 dpc.contract_title like #{content.contractInfo})
        </if>
        <if test="content.commitStatus != null">
            and dpc.commit_status = #{content.commitStatus}
        </if>
        <if test="content.contractStatus != null">
            and dpc.contract_status = #{content.contractStatus}
        </if>
        <if test="content.approveStatus != null">
            and dpc.approve_status = #{content.approveStatus}
        </if>
        <if test="content.contractStartTime != null">
            and dpc.contract_start_time <![CDATA[ <= ]]> #{content.contractEndTime}
        </if>
        <if test="content.contractEndTime != null">
            and dpc.contract_end_time <![CDATA[ >= ]]> #{content.contractStartTime}
        </if>
        <if test="content.canteenId != null">
            and dpc.canteen_id = #{content.canteenId}
        </if>
        <if test="content.supplierId != null">
            and dpc.supplier_id = #{content.supplierId}
        </if>
        <if test="content.crtimeStart != null">
            and dpc.create_time <![CDATA[ >= ]]> #{content.crtimeStart}
        </if>
        <if test="content.crtimeEnd != null">
            and dpc.create_time <![CDATA[ <= ]]> #{content.crtimeEnd}
        </if>
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            and ac.area_id in
            <foreach collection="content.areaIdList" item="areaId" open="(" close=")" separator=",">
                #{areaId}
            </foreach>
        </if>
        order by dpc.create_time desc
    </select>

    <select id="getDetailListByContractId" resultType="com.bonus.canteen.core.supply.purchase.domain.vo.DrpContractDetailVO">
        select dpcd.*,
               mm.material_name,
               mm.material_code,
               mmc.category_name,
               du.unit_name,
                mm.image_url
        from drp_procurement_contract_detail dpcd
                 left join menu_material mm on mm.material_id = dpcd.material_id
                 left join menu_material_category mmc on mmc.category_id = mm.category_id
                 left join drp_unit du on du.unit_id = mm.unit_id
        where dpcd.contract_id = #{contractId}
    </select>

    <select id="getBySupplierId" resultType="com.bonus.canteen.core.supply.purchase.domain.bo.DrpProcurementContract">
        select dpc.*
        from drp_procurement_contract dpc
        where dpc.supplier_id = #{supplierId}
        and dpc.del_flag = 2
        and dpc.commit_status = 2
        and dpc.contract_status = 2
        and dpc.approve_status = 3
        and now() between dpc.contract_start_time and dpc.contract_end_time
    </select>
</mapper>
