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



    <select id="selectAllList" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierPageVO">
        SELECT DISTINCT
        a.supplier_id,
        a.supplier_num,
        a.supplier_name,
        a.area_id,
        aa.area_name,
        a.address,
        a.linkman,
        a.linkman_number,
        a.email,
        a.wechat,
        a.opening_bank,
        a.bank_account,
        a.supplier_simple_name,
        a.telephone,
        a.company_web,
        a.region_province,
        a.region_city,
        a.region_district,
        a.tax_rate,
        a.payment_days,
        a.standby_linkman,
        a.standby_phone,
        a.standby_email,
        a.standby_wechat,
        a.regist_fund,
        a.establish_date,
        a.workers_num,
        a.client_num,
        a.last_year_operating_income,
        a.before_year_operating_income,
        a.business_scope,
        a.top_five_client,
        a.top_five_supplier,
        a.company_introduction,
        a.main_advantage,
        a.if_receive_notice,
        a.notice_style,
        a.supplier_user_id,
        a.`status`,
        a.supplier_score,
        a.create_time,
        a.create_by,
        a.update_time,
        a.update_by,
        a.id_card_img_z,
        a.id_card_img_f,
        a.attachment
        FROM
        drp_supplier a
        LEFT JOIN drp_supplier_category b on a.supplier_id = b.supplier_id
        LEFT JOIN alloc_area aa on a.area_id = aa.area_id
        where
        a.del_flag = #{delFlag}
        <if test="content.supplierId != null">
            and a.supplier_id = #{content.supplierId}
        </if>
        <if test="content.supplierNum != null and content.supplierNum != ''">
            and a.supplier_num = #{content.supplierNum}
        </if>
        <if test="content.supplierName != null and content.supplierName != ''">
            and a.supplier_name like #{content.supplierName}
        </if>
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            AND a.area_id in
            <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="content.linkman != null and content.linkman != ''">
            and a.linkman like #{content.linkman}
        </if>
        <if test="content.status != null">
            and a.status = #{content.status}
        </if>
        <if test="content.ifBindSupplierUser != null and content.ifBindSupplierUser == 1">
            and a.supplier_user_id is not null
        </if>
        <if test="content.ifBindSupplierUser != null and content.ifBindSupplierUser == 2">
            and a.supplier_user_id is null
        </if>
        <if test="content.supplierScore != null">
            and a.supplier_score <![CDATA[ >= ]]> #{content.supplierScore}
        </if>
        <if test="content.categoryIdList != null and content.categoryIdList.size() > 0">
            and b.category_id in
            <foreach collection="content.categoryIdList" item="categoryId" separator="," open="(" close=")">
                #{categoryId}
            </foreach>
        </if>
        order by a.create_time desc
    </select>

    <select id="listSupplier" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierVO">
        SELECT
        ds.supplier_id,
        ds.supplier_name,
        ds.supplier_score,
        ds.area_id,
        aa.area_name
        FROM
        drp_supplier ds
        LEFT JOIN alloc_area aa ON aa.area_id = ds.area_id
        where ds.status = #{status}
        and ds.del_flag = #{delFlag}
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            and ds.area_id in
            <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="content.supplierName != null and content.supplierName != ''">
            and ds.supplier_name like concat('%',#{content.supplierName},'%')
        </if>
    </select>

    <select id="getCategorySupplier" resultType="com.bonus.canteen.core.supply.vo.DrpMaterialSupplierVO"
            parameterType="com.bonus.canteen.core.supply.dto.DrpMaterialInfoDTO">
        select
        mmc.category_id,
        mmc.category_name,
        dms.supplier_id,
        ds.supplier_name,
        dms.alternative_supplier,
        dms.canteen_id
        from menu_material_category mmc
        left join drp_menu_material_supplier dms on mmc.category_id  = dms.category_id and dms.material_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
        <where>
          <!--  and mmc.del_flag = 2-->
            and (
            mmc.area_id in (select area_id from alloc_canteen where canteen_id = #{content.canteenId})
            or mmc.area_id is null
            )
            <if test="content.categoryIdList != null and content.categoryIdList.size() > 0">
                and mmc.category_id in
                <foreach collection="content.categoryIdList" item="categoryId" separator="," open="(" close=")">
                    #{categoryId}
                </foreach>
            </if>
        </where>
    </select>

    <select id="getSupplierMainInfoById" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierMainInfoVO">
        SELECT
        ds.supplier_id,
        ds.supplier_num,
        ds.supplier_name,
        ds.address,
        ds.linkman,
        ds.linkman_number,
        ds.email,
        ds.wechat,
        ds.opening_bank,
        ds. bank_account,
        ds. supplier_simple_name,
        ds.telephone,
        ds.company_web,
        ds.region_province,
        ds.region_city,
        ds.region_district,
        ds.tax_rate,
        ds.payment_days,
        ds.standby_linkman,
        ds.standby_phone,
        ds.standby_email,
        ds.standby_wechat,
        ds.regist_fund,
        ds.establish_date,
        ds.workers_num,
        ds.client_num,
        ds.last_year_operating_income,
        ds.before_year_operating_income,
        ds.business_scope,
        ds.top_five_client,
        ds.top_five_supplier,
        ds.company_introduction,
        ds.main_advantage,
        ds. if_receive_notice,
        ds.notice_style,
        ds.supplier_user_id,
        ds.`status`,
        ds.supplier_score,
        ds.area_id,
        aa.area_name
        FROM
        drp_supplier ds
        LEFT JOIN alloc_area aa on ds.area_id = aa.area_id
        where
        ds.supplier_id = #{supplierId}
    </select>

    <select id="pageSupplier" resultType="com.bonus.canteen.core.supply.vo.DrpSupplierScorePageVO">
        SELECT
        a.supplier_id,
        a.supplier_name,
        a.area_id,
        aa.area_name
        FROM
        drp_supplier a
        LEFT JOIN alloc_area aa on a.area_id = aa.area_id
        where
        a.del_flag = #{delFlag}
        <if test="content.supplierId != null">
            and a.supplier_id = #{content.supplierId}
        </if>
        <if test="content.areaIdList != null and content.areaIdList.size() > 0">
            and a.area_id in
            <foreach collection="content.areaIdList" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>

</mapper>







