<?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.dao.OrderDiscountDao">

    <resultMap id="OrderDiscountMap" type="com.zm.order.tool.server.model.OrderDiscount">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="user_id" jdbcType="BIGINT" property="userId"/>
        <result column="order_no" jdbcType="VARCHAR" property="orderNo"/>
        <result column="biz_type" jdbcType="INTEGER" property="bizType"/>
        <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">
        user_id, order_no, biz_type, 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>

    <sql id="values">
      #{id}, #{userId}, #{orderNo}, #{bizType}, #{itemId}, #{discountLevel}, #{discountId},
      #{discountType}, #{discountName}, #{discountSummary}, #{discountAmount}, #{features},
       now(), now(), 0, #{sellerId}, #{discountText}
    </sql>

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

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

    <select id="selectByOrderNo" parameterType="map" resultMap="OrderDiscountMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        where order_no = #{orderNo}
        and user_id = #{userId}
        and is_deleted = 0
    </select>

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

    <select id="selectList" parameterType="map" resultMap="OrderDiscountMap">
        select
        <include refid="cols_all"/>
        from
        <include refid="tb"/>
        <where>
            user_id = #{userId}
            <if test="orderNo != null and orderNo != ''">
                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="discountIdList != null and discountIdList.size > 0">
                AND discount_id in
                <foreach collection="discountIdList" item="discountId" index="index" open="(" close=")" separator=",">
                    #{discountId}
                </foreach>
            </if>
            <if test="discountLevel != null">
                AND discount_level = #{discountLevel}
            </if>
            <if test="discountType != null">
                AND discount_type = #{discountType}
            </if>
            <if test="discountTypeList != null and discountTypeList.size > 0">
                AND discount_type in
                <foreach collection="discountTypeList" item="discountType" index="index" open="(" close=")"
                         separator=",">
                    #{discountType}
                </foreach>
            </if>
            <if test="discountId != null">
                AND discount_id = #{discountId}
            </if>
            <if test="discountIdList != null and discountIdList.size > 0">
                AND discount_id in
                <foreach collection="discountIdList" item="discountId" index="index" open="(" close=")" separator=",">
                    #{discountId}
                </foreach>
            </if>
            AND is_deleted = 0
        </where>

    </select>


    <select id="selectListByUserIds" parameterType="map" resultMap="OrderDiscountMap">
        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>
            AND is_deleted = 0
        </where>

    </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}
        and user_id =#{userId}
    </update>

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

    <update id="updateTwo" parameterType="com.zm.order.tool.server.model.OrderDiscount">
        update
        <include refid="tb"/>
        <set>
            <if test="bizType != null">
                biz_type = #{bizType},
            </if>
            <if test="itemId != null">
                item_id = #{itemId},
            </if>
            <if test="discountLevel != null">
                discount_level = #{discountLevel},
            </if>
            <if test="discountId != null">
                discount_id = #{discountId},
            </if>
            <if test="discountType != null">
                discount_type = #{discountType},
            </if>
            <if test="discountName != null">
                discount_name = #{discountName},
            </if>
            <if test="discountSummary != null">
                discount_summary = #{discountSummary},
            </if>
            <if test="discountText != null">
                discount_text = #{discountText},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            <if test="isDeleted != null">
                is_deleted = #{isDeleted},
            </if>
            <if test="features != null and features != ''">
                features = #{features},
            </if>
            updated_at = now()
        </set>
        where id = #{id}
        and user_id = #{userId}
    </update>


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


    <update id="deletes" parameterType="map">
        update
        <include refid="tb"/>
        set is_deleted = #{deleteType},
        updated_at = now()
        where user_id = #{userId}
        and id in
        <foreach collection="idList" item="id" index="index" open="(" close=")" separator=",">
            #{id}
        </foreach>
    </update>

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

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

    <update id="updateId" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            id = #{newId},
            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="updateDiscountAmount" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            updated_at= now()
        </set>
        where
        id =#{id}
        and user_id =#{userId}
    </update>

    <update id="updateDiscountAmountTwo" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            <if test="discountAmount != null">
                discount_amount = #{discountAmount},
            </if>
            updated_at= now()
        </set>
        where
        id =#{id}
        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="updateSellerUser" parameterType="map">
        update
        <include refid="tb"/>
        <set>
            seller_id = #{sellerId},
            user_id = #{newUserId},
            updated_at= now()
        </set>
        where
        user_id = #{oldUserId}
        and order_no = #{orderNo}
    </update>

</mapper>