<?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.newdatasource.NewDataSourceOrderPayDetailsDao">

  <resultMap id="BaseResultMap" type="com.zm.order.tool.server.model.OrderPayDetails">
    <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="INTEGER" property="orderType" />
    <result column="order_sub_type" jdbcType="INTEGER" property="orderSubType" />
    <result column="features" jdbcType="TIMESTAMP" property="features" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="biz_type" jdbcType="INTEGER" property="bizType" />
    <result column="performance_type" jdbcType="INTEGER" property="performanceType" />
    <result column="master_data_source" jdbcType="INTEGER" property="masterDataSource" />
    <result column="income_amount" jdbcType="INTEGER" property="incomeAmount"/>
    <result column="commission_amount" jdbcType="INTEGER" property="commissionAmount"/>
    <result column="order_sub_type" jdbcType="INTEGER" property="orderSubType"/>
  </resultMap>

  <sql id="tb">
    order_pay_details
  </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, status, biz_type, performance_type, master_data_source, order_sub_type,
    income_amount, commission_amount
  </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}, #{status}, #{bizType}, #{performanceType},#{masterDataSource},
    #{orderSubType}, #{incomeAmount}, #{commissionAmount}
  </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},#{order.createdAt}, #{order.updatedAt}, #{order.isDeleted},
      #{order.orderAt}, #{order.payAt},
      #{order.source}, #{order.orderType}, #{order.features}, #{order.status},
      #{order.bizType}, #{order.performanceType},#{order.masterDataSource},
      #{order.orderSubType},#{order.incomeAmount}, #{order.commissionAmount}
      )
    </foreach>
  </insert>

  <insert id="replaceList" parameterType="list">
    replace 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},#{order.createdAt}, #{order.updatedAt}, #{order.isDeleted},
      #{order.orderAt}, #{order.payAt},
      #{order.source}, #{order.orderType}, #{order.features}, #{order.status},
      #{order.bizType}, #{order.performanceType},#{order.masterDataSource},
      #{order.orderSubType},#{order.incomeAmount}, #{order.commissionAmount}
      )
    </foreach>
  </insert>

  <insert id="replace" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
    replace into
    <include refid="tb"/>
    (<include refid="cols_all"/>)
    value
      (
      #{id}, #{orderNo}, #{userId},
      #{sellerId}, #{storeId}, #{payType},
      #{amount}, #{payNo}, #{thirdPayNo},#{createdAt}, #{updatedAt}, #{isDeleted},
      #{orderAt}, #{payAt},
      #{source}, #{orderType}, #{features}, #{status}, #{bizType}, #{performanceType},
      #{masterDataSource},#{orderSubType},#{incomeAmount}, #{commissionAmount}
      )
  </insert>

  <insert id="inserts" 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},#{order.createdAt}, #{order.updatedAt}, #{order.isDeleted},
      #{order.orderAt}, #{order.payAt},
      #{order.source}, #{order.orderType}, #{order.features}, #{order.status},
      #{order.bizType}, #{order.performanceType},#{order.masterDataSource},#{order.orderSubType},
      #{order.incomeAmount}, #{order.commissionAmount}
      )
    </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}
    and is_deleted = 0
    limit 30
  </select>

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

  <select id="selectOne" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all"/>
    from
    <include refid="tb"/>
    where id = #{id} and user_id = #{userId}
  </select>

  <select id="selectList" parameterType="map" resultMap="BaseResultMap">
    select
    <include refid="cols_all"/>
    from
    <include refid="tb"/>
    <where>
      <choose>
        <when test="userIdList != null and userIdList.size > 0">
          AND user_id in
          <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
            #{userId}
          </foreach>
        </when>
        <otherwise>
          AND user_id = #{userId}
        </otherwise>
      </choose>
      <if test="orderNoList != null and orderNoList.size > 0">
        AND order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
          #{orderNo}
        </foreach>
      </if>
      <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="updateBizType8" parameterType="map">
    update
    <include refid="tb"/>
    <set>
      biz_type = 8, updated_at = now()
    </set>
    where order_no = #{orderNo} and user_id = #{userId}
  </update>

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

  <update id="deleteByOrderNos" parameterType="map">
    delete
    from
    <include refid="tb"/>
    where
    user_id in
    <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
      #{userId}
    </foreach>
    AND order_no in
    <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
      #{orderNo}
    </foreach>
  </update>

  <update id="update" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
    update
    <include refid="tb"/>
    <set>
      <if test="isDeleted != null">
        is_deleted = #{isDeleted,jdbcType=INTEGER},
      </if>
      <if test="payType != null">
        pay_type = #{payType,jdbcType=INTEGER},
      </if>
      <if test="amount != null">
        amount = #{amount,jdbcType=INTEGER},
      </if>
      <if test="payNo != null">
        pay_no = #{payNo,jdbcType=VARCHAR},
      </if>
      <if test="thirdPayNo != null">
        third_pay_no = #{thirdPayNo,jdbcType=VARCHAR},
      </if>
      <if test="features != null">
        features = #{features, jdbcType=VARCHAR},
      </if>
      <if test="storeId != null">
        store_id = #{storeId},
      </if>
      <if test="status != null">
        status = #{status},
      </if>
      <if test="source != null">
        source = #{source},
      </if>
      <if test="orderType != null">
        order_type = #{orderType},
      </if>
      updated_at = now()
    </set>
    where
    id = #{id}
    and
    user_id = #{userId}
  </update>

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


  <update id="updateExtra" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
    update
    <include refid="tb"/>
    <set>
      features = #{features, jdbcType=VARCHAR},
      updated_at = now()
    </set>
    where
    id = #{id}
    and
    user_id = #{userId}
  </update>

  <update id="updateAmount" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
    update
    <include refid="tb"/>
    <set>
      amount = #{amount},
      updated_at = now()
    </set>
    where
    id = #{id}
    and
    user_id = #{userId}
  </update>

  <update id="realDeleteByOrderNo" parameterType="map">
    delete from
    <include refid="tb"/>
    where order_no = #{orderNo} and user_id = #{userId}
  </update>
</mapper>