<?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.tmerclub.cloud.distribution.mapper.EnterprisePayMapper">
  <resultMap id="enterprisePayMap" type="com.tmerclub.cloud.distribution.model.EnterprisePay">
    <id column="ent_pay_id" property="entPayId" />
    <result column="status" property="status"/>
    <result column="user_id" property="userId"/>
    <result column="open_id" property="openId"/>
    <result column="ent_pay_order_no" property="entPayOrderNo"/>
    <result column="amount" property="amount"/>
    <result column="type" property="type"/>
    <result column="biz_id" property="bizId"/>
    <result column="version" property="version"/>
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="out_batch_no" property="outBatchNo"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `ent_pay_id`,`status`,`user_id`,`open_id`,`ent_pay_order_no`,`amount`,`type`,`biz_id`,`version`,`create_time`,`update_time`,`out_batch_no`
  </sql>
  <select id="list" resultMap="enterprisePayMap">
    select <include refid="Vo_Column_List"/> from enterprise_pay order by ent_pay_id desc
  </select>
  <select id="getByEntPayId" resultMap="enterprisePayMap">
    select <include refid="Vo_Column_List"/> from enterprise_pay where ent_pay_id = #{entPayId}
  </select>
  <insert id="save">
    insert into enterprise_pay (`status`,`user_id`,`open_id`,`ent_pay_order_no`,`amount`,`type`,`biz_id`,`version`,`out_batch_no`)
    values (#{enterprisePay.status},#{enterprisePay.userId},#{enterprisePay.openId},#{enterprisePay.entPayOrderNo},#{enterprisePay.amount},#{enterprisePay.type},#{enterprisePay.bizId},#{enterprisePay.version},#{enterprisePay.outBatchNo});
  </insert>
  <update id="update">
    update enterprise_pay
    <set>
      <if test="enterprisePay.status != null">
        `status` = #{enterprisePay.status},
      </if>
      <if test="enterprisePay.userId != null">
        `user_id` = #{enterprisePay.userId},
      </if>
      <if test="enterprisePay.openId != null">
        `open_id` = #{enterprisePay.openId},
      </if>
      <if test="enterprisePay.entPayOrderNo != null">
        `ent_pay_order_no` = #{enterprisePay.entPayOrderNo},
      </if>
      <if test="enterprisePay.amount != null">
        `amount` = #{enterprisePay.amount},
      </if>
      <if test="enterprisePay.type != null">
        `type` = #{enterprisePay.type},
      </if>
      <if test="enterprisePay.bizId != null">
        `biz_id` = #{enterprisePay.bizId},
      </if>
      <if test="enterprisePay.version != null">
        `version` = #{enterprisePay.version},
      </if>
      <if test="enterprisePay.outBatchNo != null">
        `out_batch_no` = #{enterprisePay.outBatchNo},
      </if>
    </set>
    where ent_pay_id = #{enterprisePay.entPayId}
  </update>
  <delete id="deleteById">
    delete from enterprise_pay where ent_pay_id = #{entPayId}
  </delete>

  <select id="listApplyEnterprisePay" resultMap="enterprisePayMap">
    SELECT ep.* FROM enterprise_pay ep WHERE ep.status = 1 and out_batch_no is null limit 1000
  </select>

  <select id="listEnterprisePay" resultMap="enterprisePayMap">
    SELECT ep.* FROM enterprise_pay ep WHERE ep.status = 1 and out_batch_no is not null
  </select>

  <update id="paySuccess">
    UPDATE enterprise_pay ep SET ep.status = 2,ep.update_time = now(),ep.version = ep.version + 1 WHERE ep.ent_pay_order_no =#{entPayOrderNo} AND ep.version=#{version}
  </update>

  <update id="payFailed">
    UPDATE enterprise_pay ep SET ep.status = -1,ep.update_time = now(),ep.version = ep.version + 1 WHERE ep.ent_pay_order_no =#{entPayOrderNo} AND ep.version=#{version}
  </update>

  <update id="updateBatch">
    <foreach collection="enterprisePayList" item="enterprisePay" separator=";">
      UPDATE enterprise_pay SET `out_batch_no` = #{enterprisePay.outBatchNo} WHERE ent_pay_id = #{enterprisePay.entPayId}
    </foreach>
  </update>

</mapper>
