<?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.NewHintOrderDiscountDao">

    <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="count" parameterType="map" resultType="int">
        select
        count(1)
        from
        <include refid="tb"/>
    </select>

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

    <insert id="replaceList" parameterType="list">
        replace into
        <include refid="tb"/>
        (<include refid="cols_all"/>)
        values
        <foreach collection="discountList" item="discount" index="index" separator=",">
            (
            #{discount.id}, #{discount.bizType}, #{discount.userId}, #{discount.orderNo}, #{discount.itemId},
            #{discount.discountLevel},
            #{discount.discountId}, #{discount.discountType}, #{discount.discountName},
            #{discount.discountSummary}, #{discount.discountAmount}, #{discount.features},
            #{discount.createdAt}, #{discount.updatedAt}, #{discount.isDeleted}, #{discount.sellerId},
            #{discount.discountText}
            )
        </foreach>
    </insert>

    <update id="update" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            user_id = #{userId},
            order_no = #{orderNo},
            biz_type = #{bizType},
            item_id = #{itemId},
            discount_level = #{discountLevel},
            discount_id = #{discountId},
            discount_type = #{discountType},
            discount_name = #{discountName},
            discount_summary = #{discountSummary},
            discount_amount = #{discountAmount},
            features = #{features},
            created_at = #{createdAt},
            is_deleted = #{isDeleted},
            seller_id = #{sellerId},
            updated_at = #{updatedAt}
        </set>
        where id = #{id}
    </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="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="selectList" parameterType="map" resultMap="OrderDiscountMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            <if test="lastId != null">
                and id > #{lastId}
            </if>
            <if test="sellerId != null">
                and seller_id = #{sellerId}
            </if>
            <if test="bizType != null">
                and biz_type = #{bizType}
            </if>
            <if test="startId != null">
                <![CDATA[ AND id >= #{startId} ]]>
            </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>
        order by id asc
        limit ${size}
    </select>


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