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


    <select id="detail" resultType="com.bonus.canteen.core.supply.vo.DrpMenuMaterialSupplierDetailVO">
        select dmms.material_id,
               dmms.supplier_id,
               ds.supplier_name,
               ac.canteen_id,
               ac.canteen_name
        from drp_menu_material_supplier dmms
                 left join alloc_canteen ac on dmms.canteen_id = ac.canteen_id
                 left join drp_supplier ds on dmms.supplier_id = ds.supplier_id
        where (dmms.material_id = #{materialId} or dmms.category_id = #{materialId})
    </select>
    <select id="getSupplierByMaterialIdAndCanteenId"
            resultType="com.bonus.canteen.core.supply.vo.DrpMenuMaterialSupplierCanteenVO">
        select
        <if test="content.type != null">
            <if test="content.type == 1">
                dmms.material_id as materialCategoryId,
            </if>
            <if test="content.type == 2">
                dmms.category_id as materialCategoryId,
            </if>
            dmms.supplier_id,
            ds.supplier_name
        </if>
        from drp_menu_material_supplier dmms
        left join drp_supplier ds on dmms.supplier_id = ds.supplier_id
        <where>
            <if test="content.canteenId != null">
                dmms.canteen_id = #{content.canteenId}
            </if>
            <if test="content.materialCategoryIdList != null and content.materialCategoryIdList.size() > 0">
                <if test="content.type != null and content.type == 1">
                    and dmms.material_id in
                </if>
                <if test="content.type != null and content.type == 2">
                    and dmms.category_id in
                </if>
                <foreach close=")" collection="content.materialCategoryIdList" item="materialCategoryId" open="("
                         separator=",">
                    #{materialCategoryId}
                </foreach>
            </if>
            <if test="content.type != null and content.type == 1">
                group by dmms.material_id, dmms.supplier_id, ds.supplier_name
            </if>
            <if test="content.type != null and content.type == 2">
                group by dmms.category_id, dmms.supplier_id, ds.supplier_name
            </if>
        </where>


    </select>

    <select id="getMaterialSupplierByMaterialId" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialSupplierVO">
        SELECT
        dmms.material_id,
        dmms.supplier_id,
        ds.supplier_name,
        dmms.alternative_supplier
        FROM
        drp_menu_material_supplier dmms
        LEFT JOIN drp_supplier ds on dmms.supplier_id = ds.supplier_id

        WHERE dmms.material_id in
        <foreach collection="materialIdList" item="materialId" open="(" close=")" separator=",">
            #{materialId}
        </foreach>
    </select>

    <select id="getMaterialSupplierCert" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierSimpleVo">
        SELECT
        DISTINCT ds.supplier_id,
        ds.supplier_name,
        CASE WHEN dsq.id IS NOT NULL THEN 1 ELSE 2 END if_have_cert
        FROM drp_supplier ds
        LEFT JOIN drp_supplier_qualification dsq ON ds.supplier_id = dsq.supplier_id  AND dsq.del_flag =2 AND dsq.cert_id = #{certId}
        WHERE ds.supplier_id IN
        <foreach collection="supplierIdList" item="supplierId" open="(" close=")" separator=",">
            #{supplierId}
        </foreach>
    </select>


    <select id="checkSupplierMaterialCert" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierMaterialCertVO"
            parameterType="com.bonus.canteen.core.supply.dto.DrpSupplierMaterialCertDTO">
        select
            mm.material_id,
            CASE WHEN dsq.cert_id IS NOT NULL THEN 1 ELSE 2 END if_have_cert
        from menu_material mm
        left join drp_supplier_qualification dsq on mm.cert_id = dsq.cert_id and supplier_id = #{content.supplierId}
        where mm.material_id in
        <foreach collection="content.materialIdList" item="materialId" open="(" close=")" separator=",">
            #{materialId}
        </foreach>
    </select>

</mapper>
