<?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.chinaweal.aiccs.aiccs.susnate.mapper.ENateOperRegDecMapper">

    <resultMap id="EntBaseWithNateOperRegDecVOMap" type="com.chinaweal.aiccs.aiccs.susnate.vo.EntBaseWithNateOperRegDecVO">
        <id property="pripid" column="pripid"/>
        <result property="uniscid" column="uniscid"/>
        <result property="regno" column="regno"/>
        <result property="entname" column="entname"/>
        <result property="enttype" column="enttype"/>
        <result property="enttypeCn" column="enttypeCn"/>
        <result property="industryphy" column="industryphy"/>
        <result property="industryco" column="industryco"/>
        <result property="estdate" column="estdate"/>
        <result property="regorg" column="regorg"/>
        <result property="regorgCn" column="regorgCn"/>
        <result property="opscope" column="opscope"/>
        <result property="opfrom" column="opfrom"/>
        <result property="opto" column="opto"/>
        <result property="regstate" column="regstate"/>
        <result property="regstateCn" column="regstateCn"/>
        <result property="dom" column="dom"/>
        <result property="regcap" column="regcap"/>
        <result property="regcapcur" column="regcapcur"/>
        <result property="regcapcurCn" column="regcapcurCn"/>
        <result property="name" column="name"/>
        <result property="apprdate" column="apprdate"/>
        <result property="suporg" column="suporg"/>
        <result property="suporgCn" column="suporgCn"/>
        <result property="regulatory" column="regulatory"/>
        <result property="regulatoryName" column="regulatoryName"/>
        <result property="hasNateOperRegDec" column="hasNateOperRegDec"/>
    </resultMap>

    <select id="listEntBaseWithNateOperRegDec" resultMap="EntBaseWithNateOperRegDecVOMap">
        SELECT
            eb.PRIPID AS pripid,
            eb.UNISCID AS uniscid,
            eb.REGNO AS regno,
            eb.ENTNAME AS entname,
            eb.ENTTYPE AS enttype,
            eb.ENTTYPE_CN AS enttypeCn,
            eb.INDUSTRYPHY AS industryphy,
            eb.INDUSTRYCO AS industryco,
            eb.ESTDATE AS estdate,
            eb.REGORG AS regorg,
            eb.REGORG_CN AS regorgCn,
            eb.OPSCOPE AS opscope,
            eb.OPFROM AS opfrom,
            eb.OPTO AS opto,
            eb.REGSTATE AS regstate,
            eb.REGSTATE_CN AS regstateCn,
            eb.DOM AS dom,
            eb.REGCAP AS regcap,
            eb.REGCAPCUR AS regcapcur,
            eb.REGCAPCUR_CN AS regcapcurCn,
            eb.NAME AS name,
            eb.APPRDATE AS apprdate,
            eb.suporg AS suporg,
            eb.suporg_cn AS suporgCn,
            eb.regulatory AS regulatory,
            eb.regulatory_name AS regulatoryName,
            CASE
                WHEN EXISTS (
                    SELECT 1 FROM e_nate_operregdec en
                    WHERE en.PRIPID = eb.PRIPID
                    AND en.DSTATE != '2'
                ) THEN '1'
                ELSE '0'
            END AS hasNateOperRegDec
        FROM e_baseinfo eb
        <where>
            <if test="query.hasNateOperRegDec != null and query.hasNateOperRegDec != ''">
                AND (
                    <choose>
                        <when test="query.hasNateOperRegDec == '1'">
                            EXISTS (
                                SELECT 1 FROM e_nate_operregdec en
                                WHERE en.PRIPID = eb.PRIPID
                                AND en.DSTATE != '2'
                            )
                        </when>
                        <when test="query.hasNateOperRegDec == '0'">
                            NOT EXISTS (
                                SELECT 1 FROM e_nate_operregdec en
                                WHERE en.PRIPID = eb.PRIPID
                                AND en.DSTATE != '2'
                            )
                        </when>
                    </choose>
                )
            </if>
            <if test="query.uniscid != null and query.uniscid != ''">
                AND eb.UNISCID = #{query.uniscid}
            </if>
            <if test="query.regno != null and query.regno != ''">
                AND eb.REGNO = #{query.regno}
            </if>
            <if test="query.entname != null and query.entname != ''">
                AND eb.ENTNAME LIKE CONCAT('%', #{query.entname}, '%')
            </if>
            <if test="query.searchUscc != null and query.searchUscc != ''">
                AND (eb.UNISCID = #{query.searchUscc} OR eb.REGNO = #{query.searchUscc})
            </if>
        </where>
        ORDER BY eb.PRIPID
    </select>

</mapper>