<?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="pwc.taxtech.atms.dao.OrganizationMapper">
    <resultMap extends="BaseResultMap" id="ResultMapWithAssociation" type="pwc.taxtech.atms.entity.Organization">
        <association column="AREA_ID" property="area" select="pwc.taxtech.atms.dao.AreaMapper.selectByPrimaryKey"/>
        <association column="BUSINESS_UNIT_ID" property="businessUnit" select="pwc.taxtech.atms.dao.BusinessUnitMapper.selectByPrimaryKey"/>
    </resultMap>
    <select id="selectByExampleWithAssociation" parameterType="pwc.taxtech.atms.entity.OrganizationExample"
            resultMap="ResultMapWithAssociation">
        SELECT
        <if test="distinct">
            DISTINCT
        </if>
        <include refid="Base_Column_List"/>
        FROM organization
        <if test="_parameter != null">
            <include refid="Example_Where_Clause"/>
        </if>
        <if test="orderByClause != null">
            ORDER BY ${orderByClause}
        </if>
    </select>

    <resultMap id="OrgBasicDto" type="pwc.taxtech.atms.dpo.OrgBasicDto">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="client_code" jdbcType="VARCHAR" property="clientCode" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="code" jdbcType="VARCHAR" property="code" />
        <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
        <result column="tax_payer_number" jdbcType="VARCHAR" property="taxPayerNumber" />
        <result column="region_id" jdbcType="VARCHAR" property="regionId" />
        <result column="structure_id" jdbcType="VARCHAR" property="structureId" />
        <result column="industry_id" jdbcType="VARCHAR" property="industryId" />
        <result column="business_unit_id" jdbcType="VARCHAR" property="businessUnitId" />
        <result column="is_active" jdbcType="BIT" property="isActive" />
        <result column="p_level" jdbcType="INTEGER" property="pLevel" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="area_id" jdbcType="VARCHAR" property="areaId" />
        <result column="english_name" jdbcType="VARCHAR" property="englishName" />
        <result column="abbreviation" jdbcType="VARCHAR" property="abbreviation" />
        <result column="invoice_type" jdbcType="VARCHAR" property="invoiceType" />
        <result column="legal_person_name" jdbcType="VARCHAR" property="legalPersonName" />
        <result column="manufacture_address" jdbcType="VARCHAR" property="manufactureAddress" />
        <result column="register_address" jdbcType="VARCHAR" property="registerAddress" />
        <result column="bank_account_name" jdbcType="VARCHAR" property="bankAccountName" />
        <result column="bank_account_number" jdbcType="VARCHAR" property="bankAccountNumber" />
        <result column="phone_number" jdbcType="VARCHAR" property="phoneNumber" />
        <result column="registration_type" jdbcType="VARCHAR" property="registrationType" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="vehicle_routing_location" jdbcType="VARCHAR" property="vehicleRoutingLocation" />
        <result column="ratepayer" jdbcType="VARCHAR" property="ratepayer" />
        <result column="address" jdbcType="VARCHAR" property="address" />
        <result column="foundation_date" jdbcType="TIMESTAMP" property="foundationDate" />
        <result column="registration_date" jdbcType="TIMESTAMP" property="registrationDate" />
        <result column="registration_location" jdbcType="VARCHAR" property="registrationLocation" />
        <result column="registration_capital" jdbcType="VARCHAR" property="registrationCapital" />
        <result column="business_allotted_time_from" jdbcType="TIMESTAMP" property="businessAllottedTimeFrom" />
        <result column="business_allotted_time_to" jdbcType="TIMESTAMP" property="businessAllottedTimeTo" />
        <result column="legal_code" jdbcType="VARCHAR" property="legalCode" />
        <result column="vehicleroutinglocation" jdbcType="VARCHAR" property="vehicleroutinglocation" />
        <result column="business_scope" jdbcType="VARCHAR" property="businessScope" />
        <result column="architecture_type" jdbcType="VARCHAR" property="architectureType" />
        <result column="num_of_branches" jdbcType="TINYINT" property="numOfBranches" />
        <result column="api_update_flag" jdbcType="BIT" property="apiUpdateFlag" />
        <result column="effec_time_of_general_taxpayers" jdbcType="TIMESTAMP" property="effecTimeOfGeneralTaxpayers" />
        <result column="registration_location_en" jdbcType="VARCHAR" property="registrationLocationEn" />
        <result column="paid_in_capital" jdbcType="VARCHAR" property="paidInCapital" />
        <result column="general_tax_payer_effective_time" jdbcType="TIMESTAMP" property="generalTaxPayerEffectiveTime" />
        <result column="oversea" jdbcType="BIT" property="oversea" />
        <result column="reg_status" jdbcType="VARCHAR" property="regStatus" />
        <result column="national_economic_industry" jdbcType="VARCHAR" property="nationalEconomicIndustry" />
        <result column="engage_national_prohibit_industry" jdbcType="BIT" property="engageNationalProhibitIndustry" />
        <result column="logout_time" jdbcType="TIMESTAMP" property="logoutTime" />
        <result column="BUSINESS_UNIT_ID" jdbcType="VARCHAR" property="businessUnitId"/>
        <result column="BUSINESS_UNIT_NAME" jdbcType="VARCHAR" property="businessUnitName"/>
        <result column="AREA_ID" jdbcType="VARCHAR" property="areaId"/>
        <result column="AREA_NAME" jdbcType="VARCHAR" property="areaName"/>
        <result column="INDUSTRY_ID" jdbcType="VARCHAR" property="industryId"/>
        <result column="INDUSTRY_NAME" jdbcType="VARCHAR" property="industryName"/>
        <result column="STRUCTURE_ID" jdbcType="VARCHAR" property="structureId"/>
        <result column="STRUCTURE_NAME" jdbcType="VARCHAR" property="structureName"/>
        <result column="REGION_NAME" jdbcType="VARCHAR" property="regionName"/>
    </resultMap>

    <select id="selectIndBusiunitAreaOrgstrctReg" parameterType="map" resultMap="OrgBasicDto">
        SELECT
        org.*,
        bu.id AS BUSINESS_UNIT_ID,
        bu.name AS BUSINESS_UNIT_NAME,
        area.id AS AREA_ID,
        area.name AS AREA_NAME,
        ind.id AS INDUSTRY_ID,
        ind.name AS INDUSTRY_NAME,
        os.id AS STRUCTURE_ID,
        os.name AS STRUCTURE_NAME,
        reg.merger_name AS REGION_NAME
        FROM
        organization  org
        LEFT JOIN
        industry  ind
        on
        org.industry_id = ind.id
        LEFT JOIN
        business_unit  bu
        on
        org.business_unit_id = bu.id
        LEFT JOIN
        area  area
        on
        org.area_id = area.id
        left JOIN
        organization_structure  os
        on
        org.structure_id = os.id
        left JOIN
        region  reg
        on
        org.region_id = reg.id
        <if test="orgIsActive != null and orgIsActive == true">
            WHERE
            org.is_active = 1
        </if>
        ORDER BY
        name
    </select>

    <resultMap id="OrganizationDto" type="pwc.taxtech.atms.dpo.OrganizationDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>

        <!-- 注意! OrganizationName 对应 name-->
        <result column="ORGANIZATION_NAME" jdbcType="VARCHAR" property="name"/>
        <result column="PARENT_ID" jdbcType="VARCHAR" property="parentId"/>
        <result column="INDUSTRY_NAME" jdbcType="VARCHAR" property="industryName"/>
        <result column="BUSINESS_UNIT_NAME" jdbcType="VARCHAR" property="businessUnitName"/>
        <result column="AREA_NAME" jdbcType="VARCHAR" property="areaName"/>
        <result column="BUSINESS_UNIT_ID" jdbcType="VARCHAR" property="businessUnitId"/>
        <result column="AREA_ID" jdbcType="VARCHAR" property="areaId"/>
        <result column="REGION_ID" jdbcType="VARCHAR" property="regionId"/>
        <result column="INDUSTRY_ID" jdbcType="VARCHAR" property="industryId"/>

        <!-- 注意! OrgStructureID 对应 structureID -->
        <result column="ORG_STRUCTURE_ID" jdbcType="VARCHAR" property="structureId"/>

        <!-- 注意! OrgStructureName 对应 structureName -->
        <result column="ORG_STRUCTURE_NAME" jdbcType="VARCHAR" property="structureName"/>
        <result column="P_LEVEL" jdbcType="INTEGER" property="pLevel"/>
        <result column="CODE" jdbcType="VARCHAR" property="code"/>
        <result column="TAX_PAYER_NUMBER" jdbcType="VARCHAR" property="taxPayerNumber"/>
        <result column="IS_ACTIVE" jdbcType="SMALLINT" property="isActive"/>
    </resultMap>

    <select id="selectIndBusiunitAreaOrgstrct" parameterType="map" resultMap="OrganizationDto">
        SELECT
        org.id AS ID,
        org.name AS ORGANIZATION_NAME,
        org.parent_id AS PARENT_ID,
        ind.name AS INDUSTRY_NAME,
        ind.id AS INDUSTRY_ID,
        bu.name AS BUSINESS_UNIT_NAME,
        bu.id AS BUSINESS_UNIT_ID,
        area.name AS AREA_NAME,
        area.id AS AREA_ID,
        org.region_id AS REGION_ID,
        os.id AS ORG_STRUCTURE_ID,
        os.name AS ORG_STRUCTURE_NAME,
        org.p_level AS P_LEVEL,
        org.code AS CODE,
        org.tax_payer_number AS TAX_PAYER_NUMBER,
        org.is_active AS IS_ACTIVE
        FROM
        organization  org
        left JOIN
        industry  ind
        on
        org.industry_id = ind.id
        left JOIN
        business_unit  bu
        on
        org.business_unit_id = bu.id
        left JOIN
        area  area
        on
        org.area_id = area.id
        left JOIN
        organization_structure  os
        on
        org.structure_id = os.id
        <if test="orgIsActive != null and orgIsActive == true ">
            where
            org.is_active = 1
        </if>
    </select>

    <select id="selectIndBusUnitAreaOrgStrctReg" parameterType="map" resultMap="OrganizationDtoForAreaRegIndBusList">
        SELECT
        P.name AS NAME,
        p.id AS ID,
        unit.id AS BUSINESS_UNIT_ID,
        unit.name AS BUSINESS_UNIT_NAME,
        p.region_id AS REGION_ID,
        p.industry_id AS INDUSTRY_ID,
        industry.name AS INDUSTRY_NAME,
        p.parent_id AS PARENT_ID,
        p.structure_id AS STRUCTURE_ID,
        structure.name AS STRUCTURE_NAME,
        p.area_id AS AREA_ID,
        area.name AS AREA_NAME,
        city.name AS REGION_NAME,
        city.parent_id AS PARENT_NAME
        FROM
        organization p
        LEFT JOIN
        business_unit unit
        on
        p.business_unit_id = unit.id --tempq
        left JOIN
        industry industry
        on
        p.industry_id = industry.id --industryReal
        left JOIN
        organization_structure structure
        on
        p.structure_id = structure.id --structureReal
        left JOIN
        area area
        on
        p.area_id = area.id --areaReal
        left JOIN
        region city
        on
        p.region_id = city.id --cityReal
        where
        p.area_id = #{areaId,jdbcType=VARCHAR} --input areaId
        AND CITY.level_type = #{regionLevelType,jdbcType=INTEGER} --City Level Type
        <if test="orgIsActive != null and orgIsActive == true ">
            AND P.is_active = 1
        </if>
        <if test="orgIsActive != null and orgIsActive == false ">
            AND P.is_active = 0
        </if>
    </select>

    <resultMap id="OrganizationDtoForAreaRegIndBusList" type="pwc.taxtech.atms.dpo.OrganizationDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="NAME" jdbcType="VARCHAR" property="name"/>
        <result column="BUSINESS_UNIT_ID" jdbcType="VARCHAR" property="businessUnitId"/>
        <result column="BUSINESS_UNIT_NAME" jdbcType="VARCHAR" property="businessUnitName"/>
        <result column="REGION_ID" jdbcType="VARCHAR" property="regionId"/>
        <result column="INDUSTRY_ID" jdbcType="VARCHAR" property="industryId"/>
        <result column="INDUSTRY_NAME" jdbcType="VARCHAR" property="industryName"/>
        <result column="PARENT_ID" jdbcType="VARCHAR" property="parentId"/>
        <result column="STRUCTURE_ID" jdbcType="VARCHAR" property="structureId"/>
        <result column="STRUCTURE_NAME" jdbcType="VARCHAR" property="structureName"/>
        <result column="AREA_ID" jdbcType="VARCHAR" property="areaId"/>
        <result column="AREA_NAME" jdbcType="VARCHAR" property="areaName"/>
        <result column="REGION_NAME" jdbcType="VARCHAR" property="regionName"/>
        <result column="PARENT_NAME" jdbcType="VARCHAR" property="parentName"/>
    </resultMap>

    <resultMap id="OrganizationDtoForCDOrgList" type="pwc.taxtech.atms.dpo.OrganizationDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="NAME" jdbcType="VARCHAR" property="name"/>
        <result column="ATTRIBUTE_ID" jdbcType="VARCHAR" property="attributeId"/>
        <result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName"/>
        <result column="DIMENSION_ID" jdbcType="VARCHAR" property="dimensionId"/>
        <result column="DIMENSION_NAME" jdbcType="VARCHAR" property="dimensionName"/>
        <result column="DIMENSION_VALUE_ID" jdbcType="VARCHAR" property="dimensionValueId"/>
        <result column="DIMENSION_VALUE_NAME" jdbcType="VARCHAR" property="dimensionValueName"/>
    </resultMap>

    <!-- * 由于C#中OrganizationService.cs line 2356~2357以下代码被注释,故JAVA中未join * statDimension表做联合查询。 join sd in stat Dimension on d.ID equals sd.DimensionID * from sdTp in sdTemp.DefaultIfEmpty() -->
    <select id="getCustomDimensionOrgList" resultMap="OrganizationDtoForCDOrgList">
        SELECT DISTINCT
        ORG.id AS ID,
        ORG.name AS NAME,
        D.attribute_id AS ATTRIBUTE_ID,
        S.name AS ATTRIBUTE_NAME,
        D.id AS DIMENSION_ID,
        D.name AS DIMENSION_NAME,
        DV.id AS DIMENSION_VALUE_ID,
        DV.name AS DIMENSION_VALUE_NAME
        FROM
        organization  ORG
        JOIN
        dimension_value_org  DVG
        ON
        ORG.id = DVG.organization_id
        JOIN
        dimension_value  DV
        ON
        DVG.dimension_value_id = DV.id
        JOIN
        dimension  D
        ON
        DV.dimension_id = D.id
        JOIN
        statistic_attribute  S
        ON
        D.attribute_id = S.id
        WHERE
        ORG.is_active = 1
        AND S.is_active = 1
        AND D.is_active = 1
        AND DV.is_active = 1
    </select>

    <resultMap id="OrgDtoForGetDimValueOrgList" type="pwc.taxtech.atms.dpo.DimensionValueOrgDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="DIMENSION_VALUE_ID" jdbcType="VARCHAR" property="dimensionValueId"/>
        <result column="ORGANIZATION_ID" jdbcType="VARCHAR" property="organizationId"/>
        <result column="ORGANIZATION_NAME" jdbcType="VARCHAR" property="organizationName"/>
        <result column="DIMENSION_ID" jdbcType="VARCHAR" property="dimensionId"/>
        <result column="DIMENSION_NAME" jdbcType="VARCHAR" property="dimensionName"/>
        <result column="DIMENSION_VALUE" jdbcType="VARCHAR" property="dimensionValue"/>
    </resultMap>

    <select id="getDimensionValueOrgDtoList" resultMap="OrgDtoForGetDimValueOrgList">
        SELECT
        DVQ.dimension_value_id AS DIMENSION_VALUE_ID,
        DVGQ.organization_id AS ORGANIZATION_ID,
        DVGQ.name AS ORGANIZATION_NAME,
        DVQ.dimension_id AS DIMENSION_ID,
        d.name AS DIMENSION_NAME,
        DVQ.dimension_value_name AS DIMENSION_VALUE,
        DVGQ.organization_id AS ID
        FROM
        (
        SELECT
        org.id AS DIMENSION_VALUE_ID,
        org.name AS DIMENSION_VALUE_NAME,
        org.is_active AS IS_ACTIVE,
        "c61a5bd6-a996-4952-9869-d053966537e8" DIMENSION_ID,
        org.create_time AS CREATE_TIME,
        org.update_time AS UPDATE_TIME
        FROM
        organization  org
        where
        org.is_active = 1

        union

        SELECT
        bu.id AS DIMENSION_VALUE_ID,
        bu.name AS DIMENSION_VALUE_NAME,
        bu.is_active AS IS_ACTIVE,
        "c61a5bd6-a996-4952-9869-d053995237e5" DIMENSION_ID,
        bu.create_time AS CREATE_TIME,
        bu.update_time AS UPDATE_TIME
        FROM
        business_unit  bu
        where
        bu.is_active = 1

        union

        SELECT
        AREA.id AS DIMENSION_VALUE_ID,
        AREA.name AS DIMENSION_VALUE_NAME,
        AREA.is_active AS IS_ACTIVE,
        "c61a5bd6-a996-4952-9869-d053995237e6" DIMENSION_ID,
        SYSDATE() AS CREATE_TIME,
        SYSDATE() AS UPDATE_TIME
        FROM
        area AREA
        where
        AREA.is_active = 1

        union

        SELECT
        dv.id AS DIMENSION_VALUE_ID,
        dv.name AS DIMENSION_VALUE_NAME,
        dv.is_active AS IS_ACTIVE,
        dv.dimension_id AS DIMENSION_ID,
        dv.create_time AS CREATE_TIME,
        dv.update_time AS UPDATE_TIME
        FROM
        dimension_value dv
        )
        DVQ
        JOIN
        (
        SELECT
        id as ID,
        Name
        FROM
        dimension
        WHERE
        is_active = 1
        )
        d
        on
        DVQ.dimension_id = d.id
        JOIN
        (
        SELECT
        "c61a5bd6_-a996_-4952_-9869_-d053966537e8_" ID,
        org.structure_id AS DIMENSION_VALUE_ID,
        org.id AS ORGANIZATION_ID,
        org.name AS NAME
        FROM
        organization org
        WHERE
        org.is_active = 1

        UNION

        SELECT
        "c61a5bd6_-a996_-4952_-9869_-d053995237e5_" ID,
        org.business_unit_id AS DIMENSION_VALUE_ID,
        org.id AS ORGANIZATION_ID,
        org.name AS NAME
        FROM
        organization  org
        WHERE
        org.is_active = 1

        UNION

        SELECT
        "c61a5bd6_-a996_-4952_-9869_-d053995237e6_" ID,
        org.area_id AS DIMENSION_VALUE_ID,
        org.id AS ORGANIZATION_ID,
        org.name AS NAME
        FROM
        organization  org
        WHERE
        org.is_active = 1

        UNION

        SELECT
        dv.id AS ID,
        dv.dimension_value_id AS DIMENSION_VALUE_ID,
        dv.organization_id AS ORGANIZATION_ID,
        org.name AS NAME
        FROM
        dimension_value_org  dv
        JOIN
        organization  org
        on
        dv.organization_id = org.ID
        WHERE
        org.is_active = 1
        )
        DVGQ
        on
        DVQ.DIMENSION_VALUE_ID = DVGQ.DIMENSION_VALUE_ID
    </select>

    <resultMap id="OrgDtoForGetAllCusDimOrgList" type="pwc.taxtech.atms.dpo.OrganizationDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="NAME" jdbcType="VARCHAR" property="name"/>
        <result column="ATTRIBUTE_ID" jdbcType="VARCHAR" property="attributeId"/>
        <result column="ATTRIBUTE_NAME" jdbcType="VARCHAR" property="attributeName"/>
        <result column="DIMENSION_ID" jdbcType="VARCHAR" property="dimensionId"/>
        <result column="DIMENSION_NAME" jdbcType="VARCHAR" property="dimensionName"/>
        <result column="DIMENSION_VALUE_ID" jdbcType="VARCHAR" property="dimensionValueId"/>
        <result column="DIMENSION_VALUE_NAME" jdbcType="VARCHAR" property="dimensionValueName"/>
    </resultMap>

    <select id="getAllCustomDimensionOrgList" resultMap="OrgDtoForGetAllCusDimOrgList">
        SELECT
        Q.id AS ID,
        Q.name AS NAME,
        D.attribute_id AS ATTRIBUTE_ID,
        S.name AS ATTRIBUTE_NAME,
        D.id AS DIMENSION_ID,
        D.name AS DIMENSION_NAME,
        DV.id AS DIMENSION_VALUE_ID,
        DV.name AS DIMENSION_VALUE_NAME
        FROM
        (
        SELECT
        id as ID,
        name as NAME
        FROM
        organization  P
        WHERE
        P.is_active = 1
        )
        Q
        JOIN
        dimension_value_org  DVG
        ON
        Q.id = DVG.organization_id
        JOIN
        dimension_value  DV
        ON
        DVG.dimension_value_id = DV.id
        JOIN
        dimension  D
        ON
        DV.dimension_id = D.id
        JOIN
        statistic_attribute  S
        ON
        D.attribute_id = S.id
        WHERE
        S.is_active = 1
    </select>

    <resultMap id="OrgDtoForGetSingleOrgByOrgID" type="pwc.taxtech.atms.dpo.OrganizationDto">
        <id column="id" jdbcType="VARCHAR" property="id" />
        <result column="client_code" jdbcType="VARCHAR" property="clientCode" />
        <result column="name" jdbcType="VARCHAR" property="name" />
        <result column="code" jdbcType="VARCHAR" property="code" />
        <result column="parent_id" jdbcType="VARCHAR" property="parentId" />
        <result column="tax_payer_number" jdbcType="VARCHAR" property="taxPayerNumber" />
        <result column="region_id" jdbcType="VARCHAR" property="regionId" />
        <result column="structure_id" jdbcType="VARCHAR" property="structureId" />
        <result column="industry_id" jdbcType="VARCHAR" property="industryId" />
        <result column="business_unit_id" jdbcType="VARCHAR" property="businessUnitId" />
        <result column="is_active" jdbcType="BIT" property="isActive" />
        <result column="p_level" jdbcType="INTEGER" property="pLevel" />
        <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
        <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
        <result column="area_id" jdbcType="VARCHAR" property="areaId" />
        <result column="english_name" jdbcType="VARCHAR" property="englishName" />
        <result column="abbreviation" jdbcType="VARCHAR" property="abbreviation" />
        <result column="invoice_type" jdbcType="VARCHAR" property="invoiceType" />
        <result column="legal_person_name" jdbcType="VARCHAR" property="legalPersonName" />
        <result column="manufacture_address" jdbcType="VARCHAR" property="manufactureAddress" />
        <result column="register_address" jdbcType="VARCHAR" property="registerAddress" />
        <result column="bank_account_name" jdbcType="VARCHAR" property="bankAccountName" />
        <result column="bank_account_number" jdbcType="VARCHAR" property="bankAccountNumber" />
        <result column="phone_number" jdbcType="VARCHAR" property="phoneNumber" />
        <result column="registration_type" jdbcType="VARCHAR" property="registrationType" />
        <result column="remark" jdbcType="VARCHAR" property="remark" />
        <result column="vehicle_routing_location" jdbcType="VARCHAR" property="vehicleRoutingLocation" />
        <result column="ratepayer" jdbcType="VARCHAR" property="ratepayer" />
        <result column="address" jdbcType="VARCHAR" property="address" />
        <result column="foundation_date" jdbcType="TIMESTAMP" property="foundationDate" />
        <result column="registration_date" jdbcType="TIMESTAMP" property="registrationDate" />
        <result column="registration_location" jdbcType="VARCHAR" property="registrationLocation" />
        <result column="registration_capital" jdbcType="VARCHAR" property="registrationCapital" />
        <result column="business_allotted_time_from" jdbcType="TIMESTAMP" property="businessAllottedTimeFrom" />
        <result column="business_allotted_time_to" jdbcType="TIMESTAMP" property="businessAllottedTimeTo" />
        <result column="legal_code" jdbcType="VARCHAR" property="legalCode" />
        <result column="vehicleroutinglocation" jdbcType="VARCHAR" property="vehicleroutinglocation" />
        <result column="business_scope" jdbcType="VARCHAR" property="businessScope" />
        <result column="architecture_type" jdbcType="VARCHAR" property="architectureType" />
        <result column="num_of_branches" jdbcType="TINYINT" property="numOfBranches" />
        <result column="api_update_flag" jdbcType="BIT" property="apiUpdateFlag" />
        <result column="effec_time_of_general_taxpayers" jdbcType="TIMESTAMP" property="effecTimeOfGeneralTaxpayers" />
        <result column="registration_location_en" jdbcType="VARCHAR" property="registrationLocationEn" />
        <result column="paid_in_capital" jdbcType="VARCHAR" property="paidInCapital" />
        <result column="general_tax_payer_effective_time" jdbcType="TIMESTAMP" property="generalTaxPayerEffectiveTime" />
        <result column="oversea" jdbcType="BIT" property="oversea" />
        <result column="reg_status" jdbcType="VARCHAR" property="regStatus" />
        <result column="national_economic_industry" jdbcType="VARCHAR" property="nationalEconomicIndustry" />
        <result column="engage_national_prohibit_industry" jdbcType="BIT" property="engageNationalProhibitIndustry" />
        <result column="logout_time" jdbcType="TIMESTAMP" property="logoutTime" />
        <result column="INDUSTRY_NAME" jdbcType="VARCHAR" property="industryName"/>
        <result column="REGION_NAME" jdbcType="VARCHAR" property="regionName"/>
        <result column="STRUCTURE_NAME" jdbcType="VARCHAR" property="structureName"/>
        <result column="PARENT_REGION_ID" jdbcType="VARCHAR" property="parentRegionId"/>
        <result column="AREA_NAME" jdbcType="VARCHAR" property="areaName"/>
        <result column="BUSINESS_UNIT_NAME" jdbcType="VARCHAR" property="businessUnitName"/>
    </resultMap>

    <select id="getSingleOrgByOrgIdToOrgDto" parameterType="map" resultMap="OrgDtoForGetSingleOrgByOrgID">
        SELECT
        M.*,
        N.name AS INDUSTRY_NAME,
        CONCAT(PREGION.name,T.name) AS REGION_NAME,
        S.name AS STRUCTURE_NAME,
        T.parent_id AS PARENT_REGION_ID,
        AR.name AS AREA_NAME,
        BU.name AS BUSINESS_UNIT_NAME
        FROM
        organization  M
        LEFT JOIN
        industry  N
        ON
        M.industry_id = N.id
        LEFT JOIN
        region  T
        ON
        M.region_id = T.id
        LEFT JOIN
        region  PREGION
        ON
        T.parent_id = PREGION.id
        LEFT JOIN
        area  AR
        ON
        M.area_id = AR.id
        LEFT JOIN
        organization_structure  S
        ON
        M.structure_id = S.id
        LEFT JOIN
        business_unit  BU
        ON
        M.business_unit_id = BU.id
        WHERE
        M.id = #{orgId}
    </select>

    <select id="countForCodeUniqueValidate" parameterType="map" resultType="java.lang.Integer">
        select
        count(*)
        from
        organization
        where
        (
        "" = #{innerId}
        or id != #{innerId}
        )
        and code = #{innerCode}
    </select>

    <resultMap id="OrgGeneralInfoMiddleDto" type="pwc.taxtech.atms.dto.organization.OrgGeneralInfoMiddleDto">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <result column="ORG_NAME" jdbcType="VARCHAR" property="orgName"/>
        <result column="TAX_PAYER_NUMBER" jdbcType="VARCHAR" property="taxPayerNumber"/>
        <result column="ACCOUNT_SET_NAME" jdbcType="VARCHAR" property="accountSetName"/>
        <result column="SERVICE_NAME" jdbcType="VARCHAR" property="serviceName"/>
        <result column="BUSINESS_UNIT_NAME" jdbcType="VARCHAR" property="businessUnitName"/>
        <result column="INDUSTRY_NAME" jdbcType="VARCHAR" property="industryName"/>
        <result column="AREA_NAME" jdbcType="VARCHAR" property="areaName"/>
        <result column="AREA_ID" jdbcType="VARCHAR" property="areaId"/>
        <result column="BUSINESS_UNIT_ID" jdbcType="VARCHAR" property="businessUnitId"/>
    </resultMap>

    <select id="selectJoinToOrgGeneralInfo" parameterType="map" resultMap="OrgGeneralInfoMiddleDto">
        SELECT
        M.id AS ID,
        M.name AS ORG_NAME,
        M.tax_payer_number AS TAX_PAYER_NUMBER,
        ACC_SET.name AS ACCOUNT_SET_NAME,
        ST.name AS SERVICE_NAME,
        T.name AS BUSINESS_UNIT_NAME,
        N.name AS INDUSTRY_NAME,
        AR.name AS AREA_NAME,
        M.area_id AS AREA_ID,
        M.business_unit_id AS BUSINESS_UNIT_ID
        FROM
        organization  M
        LEFT JOIN
        industry  N
        ON
        M.industry_id = N.id
        LEFT JOIN
        organization_service_template_group  STG
        ON
        M.id = STG.organization_id
        LEFT JOIN
        service_type  ST
        ON
        STG.organization_id = ST.id
        LEFT JOIN
        business_unit  T
        ON
        T.is_active = 1
        AND M.business_unit_id = T.id
        LEFT JOIN
        area  AR
        ON
        AR.is_active = 1
        AND M.area_id = AR.id
        LEFT JOIN
        enterprise_account_set_org  SET_ORG
        ON
        M.id = SET_ORG.organization_id
        LEFT JOIN
        enterprise_account_set  ACC_SET
        ON
        ACC_SET.is_active = 1
        AND SET_ORG.enterprise_account_set_id = ACC_SET.id
    </select>
</mapper>