<?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.hintnew.NewHintOrderPayDetailsDao">

    <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="TIMESTAMP" property="orderType" />
        <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" />
    </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, income_amount, commission_amount, order_sub_type
    </sql>

    <update id="update" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            order_no = #{orderNo},
            user_id = #{userId},
            seller_id = #{sellerId},
            store_id = #{storeId},
            pay_type = #{payType},
            amount = #{amount},
            pay_no = #{payNo},
            third_pay_no = #{thirdPayNo},
            created_at = #{createdAt},
            is_deleted = #{isDeleted},
            order_at = #{orderAt},
            pay_at = #{payAt},
            source = #{source},
            order_type = #{orderType},
            features = #{features},
            updated_at = #{updatedAt}
        </set>
        where id = #{id}
    </update>

    <select id="count" parameterType="map" resultType="int">
        select
        count(1)
        from
        <include refid="tb"/>
    </select>

    <update id="updateBizType" parameterType="com.zm.order.tool.server.model.OrderPayDetails">
        update
        <include refid="tb"/>
        <set>
            biz_type = #{bizType}, updated_at = now()
        </set>
        where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

    <select id="scan" parameterType="map" resultMap="BaseResultMap">
        select
        id, user_id
        from
        <include refid="tb"/>
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
        </where>
        order by id
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

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

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

    <select id="selectIdList" parameterType="map" resultMap="BaseResultMap">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <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="userIdList != null and userIdList.size > 0">
                AND user_id in
                <foreach collection="userIdList" item="userId" index="index" open="(" close=")" separator=",">
                    #{userId}
                </foreach>
            </if>
        </where>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <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}, #{payDetail.status},
            #{payDetail.bizType}, #{payDetail.performanceType}, #{payDetail.masterDataSource},
            #{payDetail.incomeAmount}, #{payDetail.commissionAmount}, #{payDetail.orderSubType}
            )
        </foreach>
    </insert>

    <delete id="delete" parameterType="list">
        delete from order_pay_details where id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </delete>
</mapper>