<?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.zm.order.tool.server.routing.RoutingOrderPayDetailsDao">

  <resultMap id="BaseResultMap" type="com.zm.order.tool.server.model.RoutingOrderPayDetails">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="order_no" jdbcType="VARCHAR" property="orderNo" />
    <result column="user_id" jdbcType="BIGINT" property="userId" />
    <result column="seller_id" jdbcType="BIGINT" property="sellerId" />
    <result column="store_id" jdbcType="BIGINT" property="storeId" />
    <result column="pay_type" jdbcType="INTEGER" property="payType" />
    <result column="amount" jdbcType="INTEGER" property="amount" />
    <result column="pay_no" jdbcType="VARCHAR" property="payNo"/>
    <result column="third_pay_no" jdbcType="VARCHAR" property="thirdPayNo"/>
    <result column="created_at" jdbcType="TIMESTAMP" property="createdAt" />
    <result column="updated_at" jdbcType="TIMESTAMP" property="updatedAt" />
    <result column="is_deleted" jdbcType="INTEGER" property="isDeleted" />
    <result column="order_at" jdbcType="TIMESTAMP" property="orderAt" />
    <result column="pay_at" jdbcType="TIMESTAMP" property="payAt" />
    <result column="source" jdbcType="TIMESTAMP" property="source" />
    <result column="order_type" jdbcType="TIMESTAMP" property="orderType" />
    <result column="features" jdbcType="TIMESTAMP" property="features" />
    <result column="db_index" jdbcType="INTEGER" property="dbIndex"/>
    <result column="tb_index" jdbcType="INTEGER" property="tbIndex"/>
    <result column="sync_at" jdbcType="TIMESTAMP" property="syncAt"/>
  </resultMap>

  <sql id="tb">
    order_pay_details_0
  </sql>

  <sql id="cols_all">
    id,
    <include refid="cols_exclude_id"/>
  </sql>

  <sql id="cols_exclude_id">
    order_no, user_id, seller_id, store_id, pay_type, amount, pay_no, third_pay_no, created_at,
    updated_at, is_deleted, order_at, pay_at, source, order_type, features, db_index, tb_index, sync_at
  </sql>

  <sql id="values">
    #{id,jdbcType=BIGINT}, #{orderNo,jdbcType=VARCHAR}, #{userId,jdbcType=BIGINT},
    #{sellerId,jdbcType=BIGINT}, #{storeId,jdbcType=BIGINT}, #{payType,jdbcType=INTEGER},
    #{amount,jdbcType=INTEGER}, #{payNo}, #{thirdPayNo}, now(), now(), 0, #{orderAt}, #{payAt},
    #{source}, #{orderType}, #{features}
  </sql>

  <insert id="insert" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
    insert into
    <include refid="tb"/>
    (<include refid="cols_all"/>)
    values
    (<include refid="values"/>)
  </insert>

  <insert id="insertList" parameterType="list">
    insert into
    <include refid="tb"/>
    (<include refid="cols_all"/>)
    values
    <foreach collection="list" item="order" index="index" separator=",">
      (
      #{order.id}, #{order.orderNo}, #{order.userId},
      #{order.sellerId}, #{order.storeId}, #{order.payType},
      #{order.amount}, #{order.payNo}, #{order.thirdPayNo}, now(), now(), 0, #{order.orderAt}, #{order.payAt},
      #{order.source}, #{order.orderType}, #{order.features}
      )
    </foreach>
  </insert>

  <select id="selectByOrderNo" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all"/>
    from
    <include refid="tb"/>
    where
      order_no = #{orderNo}
    and
      user_id = #{userId}
    limit 30
  </select>

  <select id="selectList" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all"/>
    from
    <include refid="tb"/>
    <where>
      user_id = #{userId}
      <if test="orderNo != null and orderNo != ''">
        AND order_no = #{orderNo}
      </if>
      <if test="sellerId != null and sellerId != ''">
        AND seller_id = #{sellerId}
      </if>
      <if test="storeId != null and storeId != ''">
        AND store_id = #{storeId}
      </if>
      <if test="payType != null and payType != ''">
        AND pay_type = #{payType}
      </if>
      <if test="payNo != null and payNo != ''">
        AND pay_no = #{payNo}
      </if>
      <if test="thirdPayNo != null and thirdPayNo != ''">
        AND third_pay_no = #{thirdPayNo}
      </if>
    </where>
    order by id
    <if test="start != null">
      limit ${start}, ${pageSize}
    </if>
  </select>

  <update id="delete" parameterType="map">
    update
    <include refid="tb"/>
    <set>
      is_deleted = -1, updated_at = now()
    </set>
    where id = #{id} and user_id = #{userId}
  </update>

  <update id="deleteByOrderNo" parameterType="map">
    update
    <include refid="tb"/>
    <set>
      is_deleted = -1, updated_at = now()
    </set>
    where order_no = #{orderNo} and user_id = #{userId}
  </update>

  <update id="update" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
    update
    <include refid="tb"/>
    <set>
      <if test="payType != null">
        pay_type = #{payType,jdbcType=INTEGER},
      </if>
      <if test="amount != null">
        amount = #{amount,jdbcType=INTEGER},
      </if>
      updated_at = now()
    </set>
    where
      id = #{id}
    and
      user_id = #{userId}
  </update>

  <select id="selectByOrderNoList" parameterType="list" resultMap="BaseResultMap">
    select
    <include refid="cols_all"/>
    from
    <include refid="tb"/>
    where order_no in
    <foreach collection="list" item="orderNo" index="index" open="(" close=")" separator=",">
      #{orderNo}
    </foreach>
  </select>

</mapper>