<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.zioinfo.mall.payment.mapper.PaymentMapper">
  <select id="findById" resultType="com.zioinfo.mall.payment.MallPayment">SELECT * FROM mall_payment WHERE id = #{id}</select>
  <select id="findByOrderId" resultType="com.zioinfo.mall.payment.MallPayment">SELECT * FROM mall_payment WHERE order_id = #{orderId} ORDER BY id DESC LIMIT 1</select>
  <select id="findAll" resultType="com.zioinfo.mall.payment.MallPayment">
    SELECT * FROM mall_payment
    <where><if test="status != null and status != ''">status = #{status}</if></where>
    ORDER BY id DESC LIMIT #{limit}
  </select>
  <insert id="insert" parameterType="com.zioinfo.mall.payment.MallPayment" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO mall_payment (order_id, method, pg_tid, amount, status, card_no_enc, account_no_enc, vbank_no, vbank_bank, paid_at)
    VALUES (#{orderId}, #{method}, #{pgTid}, #{amount}, COALESCE(#{status},'READY'),
            #{cardNoEnc}, #{accountNoEnc}, #{vbankNo}, #{vbankBank}, #{paidAt})
  </insert>
  <update id="updateStatus">UPDATE mall_payment SET status = #{status}, paid_at = CASE WHEN #{status}='PAID' THEN NOW() ELSE paid_at END WHERE id = #{id}</update>
</mapper>
