<?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.payment.mapper.PayInfoMapper">
  <resultMap id="payInfoMap" type="com.tmerclub.cloud.payment.model.PayInfo">
    <id column="pay_id" property="payId" />
    <result column="create_time" property="createTime"/>
    <result column="update_time" property="updateTime"/>
    <result column="user_id" property="userId"/>
    <result column="biz_pay_no" property="bizPayNo"/>
    <result column="sys_type" property="sysType"/>
    <result column="pay_type" property="payType"/>
    <result column="pay_status" property="payStatus"/>
    <result column="pay_score" property="payScore"/>
    <result column="pay_amount" property="payAmount"/>
    <result column="version" property="version"/>
    <result column="callback_content" property="callbackContent"/>
    <result column="callback_time" property="callbackTime"/>
    <result column="confirm_time" property="confirmTime"/>
    <result column="pay_sys_type" property="paySysType"/>
    <result column="system_model" property="systemModel"/>
    <result column="biz_user_id" property="bizUserId"/>
  </resultMap>
  <sql id="Vo_Column_List">
    `pay_id`,`create_time`,`update_time`,`user_id`,`order_ids`,`refund_id`,`biz_pay_no`,`sys_type`,`pay_type`,`pay_entry`,`pay_status`,
    `pay_score`,`pay_amount`,`version`,`callback_content`,`callback_time`,`confirm_time`,`pay_sys_type`,`system_model`,`biz_user_id`
  </sql>
  <select id="getByPayId" resultMap="payInfoMap">
    select <include refid="Vo_Column_List"/> from pay_info where pay_id = #{payId}
  </select>

  <select id="listByPayIds" resultMap="payInfoMap">
    select <include refid="Vo_Column_List"/> from pay_info where
    <foreach collection="payIdList" item="payId" open="(" separator="OR" close=")">
      pay_id = #{payId}
    </foreach>
    order by pay_amount desc
  </select>

  <select id="getByOrderIdsAndEntry" resultMap="payInfoMap">
    select <include refid="Vo_Column_List"/> from pay_info
    where order_ids = #{orderIds} and pay_entry = #{payEntry} and pay_status = 1
  </select>

  <select id="getByOrderIdAndEntryAndToUserId" resultMap="payInfoMap">
    select <include refid="Vo_Column_List"/> from pay_info
    where order_ids = #{orderId} and pay_entry = #{payEntry}
    <if test="toUserId != null">
      and to_user_id = #{toUserId}
    </if>
  </select>

  <insert id="save">
    insert into pay_info (`pay_id`, `user_id`,`order_ids`,`refund_id`,`biz_pay_no`,`sys_type`,`pay_type`,`pay_entry`,`pay_status`,`pay_score`,`pay_amount`,`version`,`callback_content`,`callback_time`,`confirm_time`, `pay_sys_type`,`system_model`,`biz_user_id`)
    values (#{payInfo.payId},#{payInfo.userId},#{payInfo.orderIds},#{payInfo.refundId},#{payInfo.bizPayNo},#{payInfo.sysType},#{payInfo.payType},#{payInfo.payEntry},#{payInfo.payStatus},#{payInfo.payScore},#{payInfo.payAmount},#{payInfo.version},#{payInfo.callbackContent},#{payInfo.callbackTime},#{payInfo.confirmTime}, #{payInfo.paySysType}, #{payInfo.systemModel}, #{payInfo.bizUserId});
  </insert>
  <update id="update">
    update pay_info
    <set>
      <if test="payInfo.userId != null">
        `user_id` = #{payInfo.userId},
      </if>
      <if test="payInfo.bizPayNo != null">
        `biz_pay_no` = #{payInfo.bizPayNo},
      </if>
      <if test="payInfo.orderIds != null">
        `order_ids` = #{payInfo.orderIds},
      </if>
      <if test="payInfo.sysType != null">
        `sys_type` = #{payInfo.sysType},
      </if>
      <if test="payInfo.payType != null">
        `pay_type` = #{payInfo.payType},
      </if>
      <if test="payInfo.payStatus != null">
        `pay_status` = #{payInfo.payStatus},
      </if>
      <if test="payInfo.refundId != null">
        `refund_id` = #{payInfo.refundId},
      </if>
      <if test="payInfo.paySysType != null">
        `pay_sys_type` = #{payInfo.paySysType},
      </if>
      <if test="payInfo.payScore != null">
        `pay_score` = #{payInfo.payScore},
      </if>
      <if test="payInfo.payAmount != null">
        `pay_amount` = #{payInfo.payAmount},
      </if>
      <if test="payInfo.version != null">
        `version` = #{payInfo.version},
      </if>
      <if test="payInfo.callbackContent != null">
        `callback_content` = #{payInfo.callbackContent},
      </if>
      <if test="payInfo.callbackTime != null">
        `callback_time` = #{payInfo.callbackTime},
      </if>
      <if test="payInfo.confirmTime != null">
        `confirm_time` = #{payInfo.confirmTime},
      </if>
    </set>
    where pay_id = #{payInfo.payId}
  </update>
  <update id="updateToSuccess">
    update pay_info
    set `biz_pay_no` = #{payInfo.bizPayNo},`pay_status` = #{payInfo.payStatus},
    `callback_content` = #{payInfo.callbackContent},`callback_time` = #{payInfo.callbackTime}
    <if test="payInfo.paySysType != null">
      ,`pay_sys_type` = #{payInfo.paySysType}
    </if>
    where pay_id = #{payInfo.payId} and `pay_status` != 1
  </update>
  <select id="getPayStatusByOrderIds" resultType="java.lang.Integer">
    select pay_status from pay_info where order_ids = #{orderIds}
  </select>

  <select id="isPay" resultType="java.lang.Integer">
    select count(*) from pay_info where order_ids = #{orderIds} and pay_status = 1 and user_id = #{userId} and sys_type = #{sysType} and pay_entry = #{payEntry}
  </select>

  <select id="listByOrderIds" resultMap="payInfoMap">
    select * from pay_info where pay_status = 1 and
    <foreach collection="orderIds" item="orderId" separator="OR" open="(" close=")">
      order_ids = #{orderId}
    </foreach>
  </select>

  <select id="listPayedInfoByOrderIds" resultMap="payInfoMap">
    select * from pay_info
    where pay_status = #{payStatus} and user_id = #{userId} and
    <foreach collection="orderIds" item="orderId" separator="OR" open="(" close=")">
      order_ids = #{orderId}
    </foreach>
  </select>

  <select id="getPayInfoByUserIdAndOrderId" resultMap="payInfoMap">
    select * from pay_info where order_ids LIKE CONCAT('%',#{orderId},'%')
    <if test="userId != null">
      and user_id = #{userId}
    </if>
    and pay_status = 1 and pay_entry = 0 order by create_time desc
  </select>

  <select id="getPayInfoByPayId" resultMap="payInfoMap">
    select * from pay_info where pay_id = #{payId}
  </select>

  <update id="updatePayInfoBySplitGroup">
    update pay_info set order_ids = #{orderIds}
    where order_ids = #{oldOrderId}
  </update>

</mapper>
