<?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.KeyValueReferenceMapper">

    <delete id="deleteKeyValueReferenceByCellTemplate" parameterType="java.lang.Long">
        delete
        FROM
        key_value_reference a
        WHERE
        id IN (
        SELECT
        id
        FROM
        (
        SELECT
        b.id
        FROM
        key_value_reference b
        INNER JOIN
        cell_template c
        ON
        b.cell_template_id = c.id
        WHERE
        c.report_template_id = #{templateDbID,jdbcType=VARCHAR}
        )
        x
        )
    </delete>

    <resultMap id="financialStatementDetail" type="pwc.taxtech.atms.dpo.FinancialStatementDetail">
        <id column="Name" jdbcType="VARCHAR" property="name"/>
        <result column="INDUSTRY" jdbcType="VARCHAR" property="industry"/>
        <result column="ROW" jdbcType="INTEGER" property="row"/>
        <result column="COLUMN" jdbcType="INTEGER" property="column"/>
    </resultMap>

    <select id="getFinancialStatementDetails" parameterType="java.lang.String" resultMap="financialStatementDetail">
        SELECT DISTINCT
        t.name as Name,
        c.row_index as RowIndex,
        c.column_index as ColumnIndex,
        g.industry_ids as IndustryIds
        FROM
        key_value_reference s
        JOIN
        cell_template c
        ON
        s.cell_template_id = c.ID
        JOIN
        template t
        ON
        c.report_template_id = t.id
        JOIN
        template_group g
        ON
        t.template_group_id = g.id
        WHERE
        s.scope = 1
        AND s.key_value_config_id = #{configurationID,jdbcType=VARCHAR}
    </select>

    <resultMap id="taxReturnDetail" type="pwc.taxtech.atms.dpo.TaxReturnDetail">
        <id column="CellTemplateID" jdbcType="VARCHAR" property="cellTemplateId"/>
        <result column="TEMPLATE_ID" jdbcType="VARCHAR" property="templateId"/>
        <result column="TEMPLATE_GROUP_ID" jdbcType="VARCHAR" property="templateGroupId"/>
        <result column="CATEGORY" jdbcType="VARCHAR" property="category"/>
        <result column="PAY_TAX_TYPE" jdbcType="INTEGER" property="payTaxType"/>
        <result column="TEMPLATE_GROUP_NAME" jdbcType="VARCHAR" property="templateGroupName"/>
        <result column="NAME" jdbcType="VARCHAR" property="name"/>
        <result column="INDUSTRY" jdbcType="VARCHAR" property="industry"/>
        <result column="ROW" jdbcType="INTEGER" property="row"/>
        <result column="COLUMN" jdbcType="INTEGER" property="column"/>
    </resultMap>

    <select id="getTaxReturnDetails" parameterType="java.lang.String" resultMap="taxReturnDetail">
SELECT DISTINCT
        t.id as ID,
        "增值税纳税申报表" AS CATEGORY,
        g.pay_tax_type as PayTaxType,
        g.id AS TEMPLATE_GROUP_ID,
        g.name AS TEMPLATE_GROUP_NAME,
        t.name AS NAME,
        c.id AS CELL_TEMPLATE_ID,
        c.row_index AS ROW,
        c.column_index AS _COLUMN_,
        g.industry_ids AS INDUSTRY
        FROM key_value_reference s
        JOIN
        cell_template c
        ON
        s.cell_template_id = c.ID
        JOIN
        template t
        ON
        c.report_template_id = t.ID
        JOIN
        template_group g
        ON
        t.template_group_id = g.ID
        WHERE
        s.scope = 2
        AND s.key_value_config_id = #{configurationID,jdbcType=VARCHAR}
    </select>

    <resultMap id="analyticsModelDetail" type="pwc.taxtech.atms.dpo.AnalyticsModelDetail">
        <id column="ModelID" jdbcType="VARCHAR" property="modelId"/>
        <result column="CODE" jdbcType="VARCHAR" property="code"/>
        <result column="TYPE" jdbcType="VARCHAR" property="type"/>
        <result column="ENTITY_ID" jdbcType="VARCHAR" property="entityId"/>
        <result column="NAME" jdbcType="VARCHAR" property="name"/>
        <result column="INDUSTRY" jdbcType="VARCHAR" property="industry"/>
    </resultMap>

    <select id="getAnalyticsModelDetails" parameterType="java.lang.String" resultMap="analyticsModelDetail">
        SELECT DISTINCT
        m.id AS MODEL_ID,
        m.code AS CODE,
        m.type AS TYPE,
        s.organization_id AS ENTITY_ID,
        m.name AS NAME,
        s.industry_id AS INDUSTRY
        FROM
        key_value_reference s
        JOIN
        model m
        ON
        s.model_id = m.id
        WHERE
        s.scope = 3
        AND s.key_value_config_id = #{configurationID,jdbcType=VARCHAR}
    </select>

</mapper>