<?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.testor.module.partner.dao.TPartnerOrgDao">
    <!-- 开启二级缓存 -->
    <!-- <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/> -->
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.testor.module.partner.model.domain.TPartnerOrg">
        <id column="id" property="id"/>
        <id column="name" property="name"/>
        <id column="credit_code" property="creditCode"/>
        <id column="legal_person" property="legalPerson"/>

        <id column="is_blacklist" property="isBlacklist"/>
        <id column="blacklist_reason" property="blacklistReason"/>
        <id column="tenant_id" property="tenantId"/>
    </resultMap>
    <select id="getResid" resultType="com.testor.module.partner.model.dto.TPartnerOrgDTO">
        select a.name , b.id as id,b.partner_id from t_partner_org a
            left join t_partner_org_res b on a.id = b.partner_id
        where  a.status = 0 and a.is_blacklist = '0'
        <if test="query.name!='' and query.name!=null">
            and a.name like concat('%', #{query.name},'%')
        </if>
        <if test="query.id!='' and query.id!=null">
            and b.org_id = #{query.id}
        </if>
    </select>
    <sql id="select_base_column">
        po.id,po.org_id orgId, po.type_id typeId, po.name, po.credit_code as creditCode, po.legal_person legalPerson, po.responsible_person responsiblePerson,
        po.phone, po.contract_start_date contractStartDate, po.contract_end_date contractEndDate,
        po.is_blacklist isBlacklist, po.blacklist_reason blacklistReason,
        po.tenant_id as tenantId, po.status, po.remarks, po.create_by createBy, po.create_date createDate,
        po.update_by as updateBy, po.update_date updateDate
    </sql>

    <sql id="common_where_if">
        <if test="query.id!=null and query.id!=''">
            and po.id=#{query.id}
        </if>
        <if test="query.orgId!=null and query.orgId!=''">
            and po.org_id=#{query.orgId}
        </if>
        <if test="query.typeId!='' and query.typeId!=null">
            and po.type_id=#{query.typeId}
        </if>
        <if test="query.name!='' and query.name!=null">
            and po.name like concat('%',#{query.name},'%')
        </if>
        <if test="query.creditCode!='' and query.creditCode!=null">
            and po.credit_code=#{query.creditCode}
        </if>
        <if test="query.legalPerson!='' and query.legalPerson!=null">
            and po.legal_person=#{query.legalPerson}
        </if>
        <if test="query.responsiblePerson!='' and query.responsiblePerson!=null">
            and po.responsible_person=#{query.responsiblePerson}
        </if>
        <if test="query.phone!='' and query.phone!=null">
            and po.phone=#{query.phone}
        </if>
        <if test="query.contractStartDate!='' and query.contractStartDate!=null">
            and po.contract_start_date > #{query.contractStartDate}
        </if>
        <if test="query.contractEndDate!='' and query.contractEndDate!=null">
            and #{query.contractEndDate} > po.contract_end_date
        </if>
        <if test="query.isBlacklist!='' and query.isBlacklist!=null">
            and po.is_blacklist=#{query.isBlacklist}
        </if>
    </sql>

    <!-- (@i:=@i+1) AS 'serialNumber',-->
    <!-- (SELECT @i:=0) AS itable,-->
    <!--<select id="findPartnerExportData" resultType="com.testor.module.partner.model.excel.PartnerReport"
            parameterType="com.testor.module.partner.model.dto.TPartnerOrgParam">
        select
        po.id,po.name, po.credit_code as creditCode, po.legal_person legalPerson, po.responsible_person
        responsiblePerson,
        po.phone, po.contract_start_date contractStartDate, po.contract_end_date contractEndDate,
        pc.name certificateName, pc.valid_end_date certificateValidEndDate,
        py.name as typeName
        from t_partner_org po
        left join t_partner_type py on po.type_id = py.id
        left join t_partner_certificate pc on po.id = pc.biz_id and pc.type=1 and pc.status='0'
        <where>
            po.status = '0'
            <include refid="common_where_if"></include>
            <if test="query.condOrgIdList!=null">
                and po.org_id in
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>-->

    <select id="findPartnerExportData" resultType="com.testor.module.partner.model.excel.PartnerReport"
            parameterType="arraylist">
        SELECT tpo.name AS partner_name,
        tpo.credit_code,
        tpt.name AS type_name,
        tpo.legal_person,
        tpor.responsible_person,
        tpor.phone,
        tpor.contract_start_date,
        tpor.contract_end_date,
        tpc.type AS certificate_name,
        tpc.valid_end_date AS certificate_valid_end_date,
        tpor.id
        FROM
        t_partner_org_res AS tpor
        LEFT JOIN t_partner_org AS tpo ON tpor.partner_id = tpo.id
        LEFT JOIN t_partner_certificate AS tpc ON tpo.id=tpc.biz_id
        LEFT JOIN t_partner_type AS tpt ON tpor.type_id = tpt.id
        <where>
            <if test="query!=null">
                and tpor.org_id in
                <foreach collection="query" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="list" resultType="com.testor.module.partner.model.vo.PartnerOrgVO"
            parameterType="com.testor.module.partner.model.dto.TPartnerOrgParam">
        select * from (
        select
        org_name,
        partner_name,
        type_name,
        legal_person,
        responsible_person,
        phone,
        is_blacklist,
        partner_id,
        res_id,
        case  when a != 0 then  0 else 1 end as certificate_status
        from (
        SELECT
        tso.org_name,
        tpo.name AS partner_name,
        tpt.name AS type_name,
        tpo.legal_person,
        tpor.responsible_person,
        tpor.phone,
        tpor.certificate_status,
        tpo.is_blacklist,
        tpo.id AS partner_id,
        tpor.id AS res_id,
        sum(CASE
        WHEN tpc.valid_end_date!=null and tpc.valid_end_date &lt; NOW() THEN 1 ELSE 0 END) as a
        FROM
        t_partner_org_res AS tpor
        LEFT JOIN t_partner_org AS tpo ON tpor.partner_id = tpo.id
        LEFT JOIN t_partner_type AS tpt ON tpor.type_id = tpt.id
        LEFT JOIN t_sys_org AS tso ON tpor.org_id=tso.org_id
        left join t_partner_certificate tpc on tpo.id = tpc.biz_id
        <where>

            <if test="query.name!=null and query.name!=''">
                AND tpo.name like concat('%',#{query.name},'%')
            </if>
            <if test="query.condOrgIdList!=null">
                AND tpor.org_id IN
                <foreach collection="query.condOrgIdList" index="index" item="item" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>

            and tpor.org_id in  (select org.org_id from t_sys_org org where  org.org_id = '${query.orgId}')
        </where>
        group by tpor.id,tso.org_name,tpo.name,tpt.name,tpo.legal_person,tpor.responsible_person,tpor.phone,tpo.is_blacklist,tpo.id,tpor.id
        ORDER BY tpor.create_date DESC) a ) b
        <where>
            <if test="query.certificateStatus!=null ">
                AND certificate_status = #{query.certificateStatus}
            </if>
        </where>
    </select>

    <select id="detail" resultType="com.testor.module.partner.model.vo.PartnerOrgDetailVO"
            parameterType="string">
        SELECT tpo.name AS partner_name,
                tpor.id AS res_id,
	            tpo.id AS partner_id,
	            tpo.credit_code,
	            tpo.legal_person,
	            tpt.name AS type_name,
	            tpor.responsible_person,
	            tpor.phone,
                tpor.contract_start_date,
                tpor.contract_end_date,
	            tpor.certificate_status,
	            tpo.is_blacklist,
	            tpo.blacklist_reason
        FROM
            t_partner_org_res AS tpor
            LEFT JOIN t_partner_org AS tpo
        ON tpor.partner_id = tpo.id
            LEFT JOIN t_partner_type AS tpt ON tpor.type_id = tpt.id
        WHERE tpor.id=#{resId}
    </select>


    <select id="blacklist" resultType="com.testor.module.partner.model.vo.PartnerOrgBlacklistVO"
            parameterType="com.testor.module.partner.model.dto.TPartnerOrgParam">
        -- 相关方黑名单列表
        SELECT
        tpo.name AS partner_name,
        tpo.credit_code,
        tpo.legal_person,
        tpor.certificate_status,
        tpor.id,
        tso.org_name,
        tpo.blacklist_reason,
        tpo.id AS partner_id
        FROM
        t_partner_org_res AS tpor
        LEFT JOIN t_partner_org AS tpo ON tpor.partner_id = tpo.id
        LEFT JOIN t_sys_org AS tso ON tpo.is_blacklist_by = tso.org_id
        WHERE tpo.is_blacklist=#{query.isBlacklist}
        <if test="query.certificateStatus!=null">
            AND tpor.certificate_status = #{query.certificateStatus}
        </if>
        <if test="query.name!=null and query.name!=''">
            AND tpo.name = #{query.name}
        </if>
--         group by tpo.id
        ORDER BY tpor.create_date DESC
    </select>
    <select id="getOrgId" resultType="com.testor.module.partner.model.dto.TPartnerOrgDTO">
        select b.name , b.id as id,a.partner_id from t_partner_org_res a left join t_partner_org b on a.partner_id = b.id
        where  a.status = 0 and b.is_blacklist = '0'
        and a.org_id = #{query.id}
        <if test="query.name!='' and query.name!=null">
            and a.name like concat('%', #{query.name},'%')
        </if>
    </select>

</mapper>