<?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.settle.ReconciliationMapper">

    <resultMap id="ReconMap" type="com.ga.core.vo.settle.ReconciliationVO"/>

    <select id="selectByMonth" resultMap="ReconMap">
        SELECT * FROM reconciliation WHERE settle_month = #{settleMonth}
        ORDER BY company_code
    </select>

    <insert id="upsert" parameterType="com.ga.core.vo.settle.ReconciliationVO">
        INSERT INTO reconciliation (company_code, settle_month, company_total, system_total,
                                    diff_amount, diff_count, status, created_at)
        VALUES (#{companyCode}, #{settleMonth}, #{companyTotal}, #{systemTotal},
                #{diffAmount}, #{diffCount}, COALESCE(#{status},'PENDING'), NOW())
    </insert>

    <update id="resolve">
        UPDATE reconciliation SET status = 'MATCHED', resolve_note = #{note},
                                  resolved_by = #{userId}, resolved_at = NOW()
        WHERE recon_id = #{reconId}
    </update>

</mapper>
