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

    <resultMap id="VOMap" type="com.ga.core.vo.settle.PaymentVO">
        <id property="paymentId" column="payment_id"/>
        <result property="settleId" column="settle_id"/>
        <result property="agentId" column="agent_id"/>
        <result property="payAmount" column="pay_amount"/>
        <result property="bankCode" column="bank_code"/>
        <result property="accountNo" column="account_no" typeHandler="com.ga.common.mybatis.EncryptTypeHandler"/>
        <result property="payDate" column="pay_date"/>
        <result property="payStatus" column="pay_status"/>
        <result property="transferFileId"   column="transfer_file_id"/>
        <result property="failReason"       column="fail_reason"/>
        <result property="incomeTaxAmount"  column="income_tax_amount"/>
        <result property="localTaxAmount"   column="local_tax_amount"/>
        <result property="vatAmount"        column="vat_amount"/>
        <result property="netAmount"        column="net_amount"/>
        <result property="deductionAmount"  column="deduction_amount"/>
        <result property="createdAt"        column="created_at"/>
        <result property="updatedAt"        column="updated_at"/>
    </resultMap>

    <resultMap id="RespMap" type="com.ga.core.vo.settle.PaymentResp"/>

    <select id="selectList" resultMap="RespMap">
        SELECT p.payment_id, p.settle_id, p.agent_id, a.agent_name,
               p.bank_code,
               (SELECT cc.code_name FROM common_code cc WHERE cc.group_code='BANK_CODE' AND cc.code=p.bank_code) AS bank_name,
               p.pay_amount, p.pay_date,
               p.pay_status,
               (SELECT cc.code_name FROM common_code cc WHERE cc.group_code='PAYMENT_STATUS' AND cc.code=p.pay_status) AS pay_status_name,
               p.fail_reason, s.settle_month
        FROM payment p
        JOIN settle_master s ON s.settle_id = p.settle_id
        JOIN agent a         ON a.agent_id = p.agent_id
        <where>
            <if test="settleMonth != null and settleMonth != ''">AND s.settle_month = #{settleMonth}</if>
            <if test="agentId != null">AND p.agent_id = #{agentId}</if>
            <if test="status != null and status != ''">AND p.pay_status = #{status}</if>
        </where>
        ORDER BY p.payment_id DESC
    </select>

    <select id="selectById" resultMap="VOMap">
        SELECT * FROM payment WHERE payment_id = #{paymentId}
    </select>

    <insert id="insert" parameterType="com.ga.core.vo.settle.PaymentVO" useGeneratedKeys="true" keyProperty="paymentId">
        INSERT INTO payment (settle_id, agent_id, pay_amount, bank_code, account_no,
                             pay_date, pay_status, created_at)
        VALUES (#{settleId}, #{agentId}, #{payAmount}, #{bankCode},
                #{accountNo,typeHandler=com.ga.common.mybatis.EncryptTypeHandler},
                #{payDate}, COALESCE(#{payStatus},'PENDING'), NOW())
    </insert>

    <insert id="insertBatch">
        INSERT INTO payment (settle_id, agent_id, pay_amount, bank_code, account_no,
                             pay_date, pay_status, created_at)
        VALUES
        <foreach collection="list" item="p" separator=",">
            (#{p.settleId}, #{p.agentId}, #{p.payAmount}, #{p.bankCode},
             #{p.accountNo,typeHandler=com.ga.common.mybatis.EncryptTypeHandler},
             #{p.payDate}, COALESCE(#{p.payStatus},'PENDING'), NOW())
        </foreach>
    </insert>

    <update id="updateStatus">
        UPDATE payment SET pay_status = #{status},
                           fail_reason = #{failReason},
                           updated_at = NOW()
        WHERE payment_id = #{paymentId}
    </update>

    <select id="selectPendingByMonthAndBank" resultMap="VOMap">
        SELECT p.*
        FROM payment p
        JOIN settle_master s ON s.settle_id = p.settle_id
        WHERE p.pay_status = 'PENDING'
          AND s.settle_month = #{settleMonth}
          <if test="bankCode != null and bankCode != '' and bankCode != 'GENERIC_CSV'">
              AND p.bank_code = #{bankCode}
          </if>
        ORDER BY p.payment_id
    </select>

    <update id="updateTransferFile">
        UPDATE payment SET transfer_file_id = #{transferFileId}, pay_status = 'SENT', updated_at = NOW()
        WHERE payment_id IN
        <foreach collection="paymentIds" item="id" open="(" separator="," close=")">#{id}</foreach>
    </update>

    <select id="selectBySettleMonth" resultMap="VOMap">
        SELECT p.*
        FROM payment p
        JOIN settle_master s ON s.settle_id = p.settle_id
        WHERE s.settle_month = #{settleMonth}
        ORDER BY p.payment_id
    </select>

    <select id="selectByAgentAndMonth" resultMap="VOMap">
        SELECT p.*
        FROM payment p
        JOIN settle_master s ON s.settle_id = p.settle_id
        WHERE p.agent_id = #{agentId}
          AND s.settle_month = #{settleMonth}
        ORDER BY p.payment_id
    </select>

    <update id="updateTaxes">
        UPDATE payment
           SET income_tax_amount = #{incomeTaxAmount},
               local_tax_amount  = #{localTaxAmount},
               vat_amount        = #{vatAmount},
               net_amount        = #{netAmount},
               updated_at        = NOW()
         WHERE payment_id = #{paymentId}
    </update>

    <update id="updateTaxesBatch">
        <foreach collection="list" item="p" separator=";">
            UPDATE payment
               SET income_tax_amount = #{p.incomeTaxAmount},
                   local_tax_amount  = #{p.localTaxAmount},
                   vat_amount        = #{p.vatAmount},
                   net_amount        = #{p.netAmount},
                   updated_at        = NOW()
             WHERE payment_id = #{p.paymentId}
        </foreach>
    </update>

    <update id="updateDeduction">
        UPDATE payment
           SET deduction_amount = #{deductionAmount},
               net_amount       = #{netAmount},
               updated_at       = NOW()
         WHERE payment_id = #{paymentId}
    </update>

</mapper>
