<?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.ningdatech.carapi.industry.mapper.DataScreenMapper">
    <select id="tainCertificate" resultType="com.ningdatech.carapi.industry.model.po.CeriticatePO">
        SELECT
            <choose>
                <when test="param.regionLevel != null and param.regionLevel == 1">
                    ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) region_id,
                    ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) region_name,
                    ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END ) region_code,
                </when>
                <otherwise>
                    r.id region_id,r.name region_name,r.region_code,
                </otherwise>
            </choose>
            count(cf.id) count
        FROM `nd_certificate` cf
            left join nd_region r on cf.region_id = r.id
            left join nd_region r2 on r.parent_id = r2.id
            left join nd_company_info c on cf.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and cf.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) ,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) ,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END )
            </when>
            <otherwise>
                r.id,r.name,r.region_code
            </otherwise>
        </choose>
        order by region_code asc
    </select>

    <select id="laboratoryQualificationCertificate" resultType="com.ningdatech.carapi.industry.model.po.CeriticatePO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) region_id,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) region_name,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END ) region_code,
            </when>
            <otherwise>
                r.id region_id,r.name region_name,r.region_code,
            </otherwise>
        </choose>
        count(wc.id) count
        FROM nd_lab_worker_certificate wc
        left join nd_region r on wc.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on wc.company_id = c.id
        where wc.certificate_type = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and wc.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) ,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) ,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END )
            </when>
            <otherwise>
                r.id,r.name,r.region_code
            </otherwise>
        </choose>
        order by region_code asc
    </select>

    <select id="laboratoryAcceptanceCertificate" resultType="com.ningdatech.carapi.industry.model.po.CeriticatePO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) region_id,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) region_name,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END ) region_code,
            </when>
            <otherwise>
                r.id region_id,r.name region_name,r.region_code,
            </otherwise>
        </choose>
        count(ac.id) count
        FROM nd_lab_acceptance_certificate ac
        left join nd_region r on ac.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on ac.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and ac.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) ,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) ,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END )
            </when>
            <otherwise>
                r.id,r.name,r.region_code
            </otherwise>
        </choose>
        order by region_code asc
    </select>

    <select id="recordCertificateMortarEnterprise" resultType="com.ningdatech.carapi.industry.model.po.CeriticatePO">
        SELECT
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) region_id,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) region_name,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END ) region_code,
            </when>
            <otherwise>
                r.id region_id,r.name region_name,r.region_code,
            </otherwise>
        </choose>
        count(fc.id) count
        FROM nd_mortar_enterprise_filing_certificate fc
        left join nd_region r on fc.region_id = r.id
        left join nd_region r2 on r.parent_id = r2.id
        left join nd_company_info c on fc.company_id = c.id
        where 1 = 1
        <if test="param.regionIds != null and param.regionIds.size>0">
            and fc.region_id in
            <foreach collection="param.regionIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        ${param.dataScopeSql}
        group by
        <choose>
            <when test="param.regionLevel != null and param.regionLevel == 1">
                ( CASE WHEN r.level = 2 then r.id ELSE r2.id END ) ,
                ( CASE WHEN r.level = 2 then r.name ELSE r2.NAME END ) ,
                ( CASE WHEN r.level = 2 then r.region_code ELSE r2.region_code END )
            </when>
            <otherwise>
                r.id,r.name,r.region_code
            </otherwise>
        </choose>
        order by region_code asc
    </select>
</mapper>
