Subversion Repositories SmartDukaan

Rev

Rev 8899 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?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="in.shop2020.payment.persistence.PaymentMapper">

  <resultMap type="payment" id="paymentResult">
    <id property="id" column="id"/>
    <collection property="attributes" javaType="ArrayList" column="id" ofType="paymentAttribute" select="getAttributesForPayment"/>
  </resultMap>

  <insert id="insertPayment" parameterType="payment" useGeneratedKeys="true" keyProperty="id">
    INSERT INTO payment
      (gatewayId, userId, merchantTxnId, amount, status, initTimestamp, isDigital) 
    VALUES
      (#{gatewayId}, #{userId}, #{merchantTxnId}, #{amount}, #{status},  now(), #{isDigital})
  </insert>

  <insert id="insertPaymentAttribute" parameterType="map">
    INSERT INTO paymentattribute
      (payment_id, name, value)
    VALUES(#{paymentId}, #{name}, #{value})
    ON DUPLICATE KEY UPDATE value=values(value);
  </insert>

  <update id="updatePayment" parameterType="payment">
    UPDATE payment
    SET
    <if test="gatewayPaymentId != null">
      gatewayPaymentId = #{gatewayPaymentId},
    </if>
    <if test="sessionId != null">
      sessionId = #{sessionId},
    </if>
    <if test="gatewayTxnStatus != null">
      gatewayTxnStatus = #{gatewayTxnStatus},
    </if>
    <if test="description != null">
      description = #{description},
    </if>
    <if test="gatewayTxnId != null">
      gatewayTxnId = #{gatewayTxnId},
    </if>
    <if test="authCode != null">
      authCode = #{authCode},
    </if>
    <if test="referenceCode != null">
      referenceCode = #{referenceCode},
    </if> 
    <if test="gatewayTxnDate != null">
      gatewayTxnDate = #{gatewayTxnDate},
    </if>
    <if test="status != null">
      status = #{status},
    </if>
    <if test="refundAmount != null">
      refundAmount = #{refundAmount},
    </if>
    errorCode = #{errorCode},
    successTimestamp = #{successTimestamp},
    errorTimestamp = #{errorTimestamp},
    provisionalCaptureTimestamp = #{provisionalCaptureTimestamp}
    WHERE id=#{id}
  </update>

  <sql id="selectAllPaymentFields">
    SELECT * FROM payment
  </sql>

  <select id="getPayment" parameterType="int" resultMap="paymentResult">
    <include refid="selectAllPaymentFields"/>
    WHERE id=#{id}
  </select>
  
  <select id="getPaymentForTxn" parameterType="int" resultMap="paymentResult">
    SELECT * FROM payment
    WHERE merchantTxnId=#{merchantTxnId}
    AND isDigital = 0 
    ORDER BY id DESC
  </select>
  
  <select id="getPaymentForRechargeTxn" parameterType="int" resultMap="paymentResult">
        SELECT * FROM payment
    WHERE merchantTxnId=#{merchantTxnId}
    AND isDigital = 1
    ORDER BY id DESC
  </select>
  
  <sql id="getPaymentsWhereClause">
    <if test="fromTime != ''">
      initTimestamp &gt;= #{fromTime}
    </if>
    <if test="toTime != ''">
      AND initTimestamp &lt;= #{toTime}
    </if>
    
<!--
    <if test="toTime != '' &amp;&amp; status == 0">
      AND initTimestamp &lt;= #{toTime}
    </if>
        <if test="toTime != '' &amp;&amp; status != 0">
      AND (errorTimestamp &lt;= #{toTime} OR successTimestamp &lt;= #{toTime})
    </if>
 -->
    <if test="status != -1">
      AND status=#{status}
    </if>
    <if test="gatewayId != 0">
      AND gatewayId = #{gatewayId}
    </if>
  </sql>
  
  <select id="getPayments" parameterType="map" resultMap="paymentResult">
    SELECT * FROM payment
    <where>
      <include refid="getPaymentsWhereClause"/>
    </where>
    ORDER BY id
  </select>

  <select id="getPaymentsByCapturedDate" parameterType="map" resultMap="paymentResult">
    SELECT * FROM payment
    <where>
      status = #{status}
          <if test="fromTime != ''">
            AND successTimestamp &gt;= #{fromTime}
          </if>
          <if test="toTime != ''">
            AND successTimestamp &lt;= #{toTime}
          </if>
          <if test="gatewayId != 0">
        AND gatewayId = #{gatewayId}
      </if>
    </where>
    ORDER BY id
  </select>

  <select id="getPaymentsForUser" parameterType="map" resultMap="paymentResult">
    SELECT * FROM payment
    <where>
      <include refid="getPaymentsWhereClause"/>
      AND userId=#{userId}
    </where>
    ORDER BY id
  </select>

  <select id="getMinMaxPaymentAmount" resultType="hashmap">
    SELECT MIN(AMOUNT) MIN, MAX(AMOUNT) MAX from payment WHERE status = 2
  </select>
  
  <select id="getAttributesForPayment" parameterType="int" resultType="paymentAttribute">
    SELECT * FROM paymentattribute where payment_id = #{id}
  </select>
</mapper>