<?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="cn.ezhizao.project.business.supplier.mapper.BizSupplierMapper">

    <resultMap type="cn.ezhizao.project.business.supplier.domain.BizSupplier" id="BizSupplierResult">
        <id column="id" property="id"/>
    </resultMap>

    <select id="getList" resultMap="BizSupplierResult" parameterType="cn.ezhizao.project.business.supplier.domain.BizSupplier">
        SELECT t1.*,
        ( SELECT biz_supplier_contacts.name
        FROM biz_supplier_contacts
        WHERE t1.id = biz_supplier_contacts.supplier_id
        AND biz_supplier_contacts.is_default = 1
        AND biz_supplier_contacts.deleted = 0
        LIMIT 1
        ) AS contact_name,
        ( SELECT biz_supplier_contacts.phone_number
        FROM biz_supplier_contacts
        WHERE t1.id = biz_supplier_contacts.supplier_id
        AND biz_supplier_contacts.is_default = 1
        AND biz_supplier_contacts.deleted = 0
        LIMIT 1
        ) AS contact_phone_number
        FROM biz_supplier t1
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0
            <if test="tenantId != null and tenantId != 0"> AND t1.tenant_id = #{tenantId}</if>
            <if test="isInnerOutsource != null "> AND t1.is_inner_outsource = #{isInnerOutsource}</if>
            <if test="name != null and name != ''"> AND t1.name LIKE CONCAT('%', #{name}, '%')</if>
            <if test="mnemonicCode != null and mnemonicCode != ''"> AND t1.mnemonic_code LIKE CONCAT('%', #{mnemonicCode}, '%')</if>
            <if test="code != null and code != ''"> AND t1.code LIKE CONCAT('%', #{code}, '%')</if>

        </trim>

    </select>
    <select id="getListByDept" resultMap="BizSupplierResult">
        select * from biz_supplier
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            id not in (select supplier_id from sys_dept where del_flag = 0 and dept_id != #{deptId} ) and
             deleted = 0 and is_inner_outsource = 1
            <if test="tenantId != null and tenantId != 0"> AND tenant_id = #{tenantId}</if>
            <if test="isInnerOutsource != null "> AND is_inner_outsource = #{isInnerOutsource}</if>
            <if test="name != null and name != ''"> AND name LIKE CONCAT('%', #{name}, '%')</if>
            <if test="mnemonicCode != null and mnemonicCode != ''"> AND mnemonic_code LIKE CONCAT('%', #{mnemonicCode}, '%')</if>
            <if test="code != null and code != ''"> AND code LIKE CONCAT('%', #{code}, '%')</if>

        </trim>

    </select>
    <select id="getOutsourceDept" resultMap="BizSupplierResult">
        select t2.*,t1.dept_id as outsourceDeptId,t1.dept_name as outsourceDeptName from sys_dept t1 left join biz_supplier t2 on t1.supplier_id = t2.id where
        t1.is_outsourcing = 1 and t1.del_flag = 0 and t2.deleted = 0
    </select>

    <delete id="physicalDelete">
        DELETE FROM biz_supplier
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            <if test="id != null">
                id = #{id} AND
            </if>
       <!-- 删除条件为其他外键可以在这里加 -->
        </trim>
    </delete>

    <select id="checkAllReturn" resultType="java.lang.Boolean">
        select  case when count(1) > 0 then false else true end from biz_outsourced_order_detail t left join biz_return_receipt_detail r on r.outsource_detail_id = t.id and r.deleted = 0 and r.is_audit = 1 where t.deleted = 0 and t.supplier_id = #{id} and r.id is null
    </select>
</mapper>
