<?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.ga.core.mapper.tax.WithholdingTaxAdjustmentMapper">

    <!--
        멱등 upsert. AggregateStep에서 gross<0 설계사·정산월마다 1건.
        ON CONFLICT(agent_id, settle_month, reason) DO UPDATE.
    -->
    <insert id="upsert">
        INSERT INTO withholding_tax_adjustment
            (agent_id, settle_month, reason, base_amount, income_tax_adj, local_tax_adj)
        VALUES
            (#{agentId}, #{settleMonth}, #{reason}, #{baseAmount}, #{incomeTaxAdj}, #{localTaxAdj})
        ON CONFLICT (agent_id, settle_month, reason) DO UPDATE SET
            base_amount    = EXCLUDED.base_amount,
            income_tax_adj = EXCLUDED.income_tax_adj,
            local_tax_adj  = EXCLUDED.local_tax_adj
    </insert>

    <!-- 정산월 역분개 전량 삭제 (재실행 멱등) -->
    <delete id="deleteBySettleMonth">
        DELETE FROM withholding_tax_adjustment WHERE settle_month = #{settleMonth}
    </delete>

</mapper>
