<?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.CellTemplateConfigMapper">
    <select id="getCellTemplateConfigByTemplateId" parameterType="java.lang.Long" resultMap="BaseResultMap">
        SELECT
        B.*
        FROM
        cell_template A
        JOIN
        cell_template_config B ON A.id = B.cell_template_id
        WHERE
        A.report_template_id = #{templateId,jdbcType=VARCHAR}
    </select>

    <delete id="deleteCellTemplateConfigByCellTemplate" parameterType="java.lang.Long">
        DELETE FROM cell_template_config
        WHERE
        id IN (SELECT
        X.id
        FROM
        (SELECT
        B.id
        FROM
        cell_template_config B
        INNER JOIN cell_template C ON B.cell_template_id = C.id

        WHERE
        C.report_template_id = #{templateId,jdbcType=VARCHAR}) X)
    </delete>

    <insert id="batchInsert" parameterType="pwc.taxtech.atms.entity.CellTemplateConfig">
        INSERT INTO cell_template_config
        (id,cell_template_id,report_template_id,data_source_type,formula,formula_description,account_codes,invoice_type,tax_rate,invoice_amount_type,model_ids,create_by,create_time,update_by,update_time,invoice_category,formula_data_source,validation,validation_description,voucher_keyword)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <choose>
                    <when test="item.id != null">
                        #{item.id,jdbcType=BIGINT},
                    </when>
                </choose>
                <choose>
                    <when test="item.cellTemplateId != null">
                        #{item.cellTemplateId,jdbcType=BIGINT},
                    </when>
                    <otherwise>
                        0,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.reportTemplateId != null">
                        #{item.reportTemplateId,jdbcType=BIGINT},
                    </when>
                    <otherwise>
                        0,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.dataSourceType != null">
                        #{item.dataSourceType,jdbcType=INTEGER},
                    </when>
                    <otherwise>
                        0,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.formula != null">
                        #{item.formula,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.formulaDescription != null">
                        #{item.formulaDescription,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.accountCodes != null">
                        #{item.accountCodes,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.invoiceType != null">
                        #{item.invoiceType,jdbcType=INTEGER},
                    </when>
                    <otherwise>
                        0,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.taxRate != null">
                        #{item.taxRate,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.invoiceAmountType != null">
                        #{item.invoiceAmountType,jdbcType=INTEGER},
                    </when>
                    <otherwise>
                        0,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.modelIds != null">
                        #{item.modelIds,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.createBy != null">
                        #{item.createBy,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.createTime != null">
                        #{item.createTime,jdbcType=TIMESTAMP},
                    </when>
                    <otherwise>
                        CURRENT_TIMESTAMP,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.updateBy != null">
                        #{item.updateBy,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.updateTime != null">
                        #{item.updateTime,jdbcType=TIMESTAMP},
                    </when>
                    <otherwise>
                        CURRENT_TIMESTAMP,
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.invoiceCategory != null">
                        #{item.invoiceCategory,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.formulaDataSource != null">
                        #{item.formulaDataSource,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.validation != null">
                        #{item.validation,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.validationDescription != null">
                        #{item.validationDescription,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
                <choose>
                    <when test="item.voucherKeyword != null">
                        #{item.voucherKeyword,jdbcType=VARCHAR},
                    </when>
                    <otherwise>
                        "",
                    </otherwise>
                </choose>
            </trim>
        </foreach>;
        SELECT 1 FROM DUAL;
    </insert>

    <insert id="batchInsert2" parameterType="pwc.taxtech.atms.entity.CellTemplateConfig">
        INSERT INTO cell_template_config
        (id,cell_template_id,report_template_id,data_source_type,formula,formula_description,account_codes,invoice_type,tax_rate,invoice_amount_type,model_ids,create_by,create_time,update_by,update_time,invoice_category,formula_data_source,validation,validation_description,voucher_keyword)
        VALUES
        <foreach collection="list" item="item"  index="index" separator=",">
            (#{item.id,jdbcType=INTEGER}, #{item.cellTemplateId,jdbcType=INTEGER},
            #{item.reportTemplateId,jdbcType=INTEGER},
            #{item.dataSourceType,jdbcType=INTEGER}, #{item.formula,jdbcType=VARCHAR},
            #{item.formulaDescription,jdbcType=VARCHAR},
            #{item.accountCodes,jdbcType=VARCHAR}, #{item.invoiceType,jdbcType=INTEGER},
            #{item.taxRate,jdbcType=VARCHAR},
            #{item.invoiceAmountType,jdbcType=INTEGER}, #{item.modelIds,jdbcType=VARCHAR},
            #{item.createBy,jdbcType=VARCHAR},
            #{item.createTime,jdbcType=TIMESTAMP}, #{item.updateBy,jdbcType=VARCHAR},
            #{item.updateTime,jdbcType=TIMESTAMP},
            #{item.invoiceCategory,jdbcType=VARCHAR}, #{item.formulaDataSource,jdbcType=VARCHAR},
            #{item.validation,jdbcType=VARCHAR},
            #{item.validationDescription,jdbcType=VARCHAR}, #{item.voucherKeyword,jdbcType=VARCHAR})
        </foreach>;
        SELECT 1 FROM DUAL;
    </insert>
</mapper>