<?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.old.routing.OldHintOrderDiscountDao">

    <resultMap id="OrderDiscountMap" type="com.zm.order.tool.server.model.OrderDiscount">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="item_id" jdbcType="VARCHAR" property="itemId"/>
        <result column="discount_level" jdbcType="INTEGER" property="discountLevel"/>
        <result column="discount_id" jdbcType="VARCHAR" property="discountId"/>
        <result column="discount_type" jdbcType="INTEGER" property="discountType"/>
        <result column="discount_name" jdbcType="VARCHAR" property="discountName"/>
        <result column="discount_summary" jdbcType="VARCHAR" property="discountSummary"/>
        <result column="discount_amount" jdbcType="INTEGER" property="discountAmount"/>
        <result column="features" jdbcType="VARCHAR" property="features"/>
        <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="seller_id" jdbcType="INTEGER" property="sellerId"/>
        <result column="discount_text" jdbcType="VARCHAR" property="discountText"/>
    </resultMap>

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

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

    <sql id="cols_exclude_id">
        biz_type, user_id, order_no, item_id, discount_level, discount_id, discount_type,
        discount_name, discount_summary, discount_amount, features,
        created_at, updated_at, is_deleted, seller_id, discount_text
    </sql>

    <select id="list" parameterType="map" resultMap="OrderDiscountMap">
        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>

    <select id="listById" parameterType="map" resultMap="OrderDiscountMap">
        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>

    <update id="update" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="updateUserId != null">
                user_id = #{updateUserId},
            </if>
            updated_at= now()
        </set>
        where order_no =#{orderNo}
    </update>

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

    <update id="updateDeleted" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            is_deleted = 100
        </set>
        where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </update>

    <update id="updateSellerId" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            seller_id = case id
            <foreach collection="list" index="index" item="discount" separator="" open="" close="">
                when #{discount.id} then #{discount.sellerId}
            </foreach>
        </set>
        end
        where id in
        <foreach collection="list" index="index" item="discount" separator="," open="(" close=")">
            #{discount.id}
        </foreach>
    </update>

    <update id="updateBatch" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            biz_type = 4, updated_at = now(),
            <trim prefix="discount_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.discountType}
                </foreach>
            </trim>
            <trim prefix="discount_name = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.discountName}
                </foreach>
            </trim>
            <trim prefix="features = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.features}
                </foreach>
            </trim>
            <trim prefix="seller_id = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.sellerId}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>

    <update id="updateBizType" parameterType="map">
        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="batchUpdateDiscountType" parameterType="list">
        update
        <include refid="tb"/>
        <trim prefix="set" suffixOverrides=",">
            updated_at = now(),
            <trim prefix="discount_type = case id" suffix="end,">
                <foreach collection="list" index="index" item="order">
                    when #{order.id} then #{order.discountType}
                </foreach>
            </trim>
        </trim>
        where id in
        <foreach collection="list" index="index" item="order" separator="," open="(" close=")">
            #{order.id}
        </foreach>
    </update>

    <select id="scan" 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="selectList" parameterType="map" resultMap="OrderDiscountMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="maxId != null">
                <![CDATA[ AND id < #{maxId} ]]>
            </if>
            <if test="excludeSellerId != null">
                and seller_id != #{excludeSellerId}
            </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="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="bizType != null">
                and biz_type = #{bizType}
            </if>
            <if test="endId != null">
                <![CDATA[ AND id <= #{endId} ]]>
            </if>
            <if test="orderAtStart != null">
                <![CDATA[ AND created_at >= #{orderAtStart} ]]>
            </if>
            <if test="orderAtEnd != null">
                <![CDATA[ AND updated_atcreated_at <= #{orderAtEnd} ]]>
            </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>


    <select id="selectListById" parameterType="map" resultMap="OrderDiscountMap">
        select
        id, order_no, seller_id, biz_type, user_id
        from
        <include refid="tb"/>
        <where>
             id > #{lastId}
        </where>
        order by id asc
        limit 1000
    </select>

    <delete id="physicalDeleted" parameterType="map">
        delete from order_discounts where order_no in
        <foreach collection="orderNoList" item="orderNo" index="index" open="(" close=")" separator=",">
            #{orderNo}
        </foreach>
    </delete>
</mapper>