<?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.hint.HintOrderPayDetailsDao">

    <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="INTEGER" property="source" />
        <result column="order_type" jdbcType="INTEGER" property="orderType" />
        <result column="order_sub_type" jdbcType="INTEGER" property="orderSubType"/>
        <result column="features" jdbcType="VARCHAR" property="features" />
        <result column="income_amount" jdbcType="INTEGER" property="incomeAmount" />
        <result column="commission_amount" jdbcType="INTEGER" property="commissionAmount" />
    </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, income_amount, commission_amount, order_sub_type
    </sql>

    <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="scanList" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </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="excludeSellerId != null">
                and seller_id != #{excludeSellerId}
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="orderAtStart != null">
                <![CDATA[ AND order_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND order_at <= #{orderAtEnd} ]]>
            </if>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="storeId != null">
                and store_id = #{storeId}
            </if>
        </where>
        <choose>
            <when test="sortFlag == 1">
                order by updated_at, id
            </when>
            <otherwise>
                order by id
            </otherwise>
        </choose>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <select id="scanId" parameterType="map" resultType="long">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="orderNo != null">
                and order_no = #{orderNo}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <if test="userId != null">
                and user_id = #{userId}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
        </where>
        order by id
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <select id="listById" 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>
    </select>

    <select id="listByNo" parameterType="map" resultMap="BaseResultMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no in
        <foreach collection="list" item="param" index="index" open="(" close=")" separator=",">
            #{param}
        </foreach>
    </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>

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

    <select id="selectIdList" parameterType="map" resultMap="BaseResultMap">
        select
        id
        from
        <include refid="tb"/>
        <where>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="updatedAtStart != null">
                <![CDATA[ AND updated_at >= #{updatedAtStart} ]]>
            </if>
            <if test="updatedAtEnd != null">
                <![CDATA[ AND updated_at < #{updatedAtEnd} ]]>
            </if>
            <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>
        <choose>
            <when test="sortFlag == 1">
                order by updated_at, id
            </when>
            <otherwise>
                order by id
            </otherwise>
        </choose>
        <if test="start != null">
            limit ${start}, ${pageSize}
        </if>
        <if test="size != null">
            limit ${size}
        </if>
    </select>

    <update id="batchUpdateDate" parameterType="list">
        update
        <include refid="tb"/>
        <set>
            updated_at = now()
        </set>
        where
        id in
        <foreach collection="list" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

    <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>


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

    <update id="updateBatchOrderBaking" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 4, updated_at = now(),
            <trim prefix="features = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.features}
                </foreach>
            </trim>
            <trim prefix="order_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.orderType}
                </foreach>
            </trim>
            <trim prefix="source = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.source}
                </foreach>
            </trim>
            <trim prefix="pay_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.payType}
                </foreach>
            </trim>
            <trim prefix="performance_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.performanceType}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>

    <select id="checkPay" parameterType="map" resultMap="BaseResultMap">
        select
        income_amount, commission_amount
        from
        <include refid="tb"/>
        limit 1
    </select>

</mapper>