<?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.cool.store.mapper.wallet.WalletTradeMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.wallet.WalletTradeDO">
    <!--
      WARNING - @mbg.generated
    -->
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="module" jdbcType="VARCHAR" property="module" />
    <result column="pay_no" jdbcType="VARCHAR" property="payNo" />
    <result column="trade_id" jdbcType="VARCHAR" property="tradeId" />
    <result column="type" jdbcType="BIT" property="type" />
    <result column="pay_user_name" jdbcType="VARCHAR" property="payUserName" />
    <result column="pay_amount" jdbcType="DECIMAL" property="payAmount" />
    <result column="pay_status" jdbcType="BIT" property="payStatus" />
    <result column="pay_time" jdbcType="TIMESTAMP" property="payTime" />
    <result column="remark" jdbcType="VARCHAR" property="remark" />
    <result column="create_user_id" jdbcType="VARCHAR" property="createUserId" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="batch_code" jdbcType="VARCHAR" property="batchCode" />
  </resultMap>

  <!-- 批量新增 -->
  <insert id="insertBatch" parameterType="java.util.List">
    INSERT INTO zxjp_wallet_trade (module, pay_no, trade_id, type, pay_user_name, pay_amount, pay_status, pay_time, remark, create_user_id, batch_code)
    VALUES
    <foreach collection="list" item="item" separator=",">
      (#{item.module}, #{item.payNo}, #{item.tradeId}, #{item.type}, #{item.payUserName}, #{item.payAmount}, #{item.payStatus}, #{item.payTime}, #{item.remark}, #{item.createUserId}, #{item.batchCode})
    </foreach>
  </insert>

  <update id="updateByPayNo" parameterType="com.cool.store.entity.wallet.WalletTradeDO">
    UPDATE zxjp_wallet_trade
    <set>
      <if test="module != null">module = #{module},</if>
      <if test="tradeId != null">trade_id = #{tradeId},</if>
      <if test="type != null">type = #{type},</if>
      <if test="payUserName != null">pay_user_name = #{payUserName},</if>
      <if test="payAmount != null">pay_amount = #{payAmount},</if>
      <if test="payStatus != null">pay_status = #{payStatus},</if>
      <if test="payTime != null">pay_time = #{payTime},</if>
      <if test="remark != null">remark = #{remark},</if>
      <if test="createUserId != null">create_user_id = #{createUserId},</if>
      create_time = create_time
    </set>
    WHERE pay_no = #{payNo}
  </update>

  <update id="updateBatchByPayNo">
    <foreach collection="list" item="item" separator=";">
      UPDATE zxjp_wallet_trade
      <set>
        <if test="item.module != null">module = #{item.module},</if>
        <if test="item.tradeId != null">trade_id = #{item.tradeId},</if>
        <if test="item.type != null">type = #{item.type},</if>
        <if test="item.payUserName != null">pay_user_name = #{item.payUserName},</if>
        <if test="item.payAmount != null">pay_amount = #{item.payAmount},</if>
        <if test="item.payStatus != null">pay_status = #{item.payStatus},</if>
        <if test="item.payTime != null">pay_time = #{item.payTime},</if>
        <if test="item.remark != null">remark = #{item.remark},</if>
        <if test="item.createUserId != null">create_user_id = #{item.createUserId},</if>
        create_time = create_time
      </set>
      WHERE pay_no = #{item.payNo}
    </foreach>
  </update>

  <select id="getPayingOrderBatchCode" resultType="java.lang.String">
    SELECT batch_code, pay_status
    FROM zxjp_wallet_trade
    WHERE pay_status = 3 AND module = #{module} AND type = #{type} AND batch_code IS NOT NULL
    GROUP BY batch_code
  </select>

  <select id="getPayingOrder" resultMap="BaseResultMap">
    SELECT *
    FROM zxjp_wallet_trade
    WHERE pay_status = 3 AND module = #{module} AND type = #{type} AND batch_code IS  NULL
  </select>

  <select id="transferTradeList" resultMap="BaseResultMap">
    SELECT *
    FROM zxjp_wallet_trade
    WHERE pay_status = 3 AND module = #{module} AND type = #{type}
    order by id desc
  </select>
</mapper>