<?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.kxs.user.biz.mapper.KxsUserAmountLogMapper">

    <resultMap type="com.kxs.user.api.model.KxsUserAmountLog" id="KxsUserAmountLogMap">
        <result column="id" property="id" />
        <result column="create_time" property="createTime" />
        <result column="update_time" property="updateTime" />
        <result column="del_flag" property="delFlag" />
        <result column="version" property="version" />
        <result column="user_id" property="userId" />
        <result column="before_amount" property="beforeAmount" />
        <result column="before_amount" property="beforeAmount" />
        <result column="before_total_amount" property="beforeTotalAmount" />
        <result column="after_amount" property="afterAmount" />
        <result column="after_total_amount" property="afterTotalAmount" />
        <result column="amount" property="amount" />
        <result column="variation_type" property="variationType" />
        <result column="variation_cause" property="variationCause" />
        <result column="kind" property="kind" />
        <result column="remark" property="remark" />
        <result column="brand_id" property="brandId" />

    </resultMap>
    <update id="automaticTableCreationTasks" parameterType="String">
        CREATE  TABLE IF NOT EXISTS `${tableName}` LIKE kxs_user_amount_log;
    </update>
    <select id="getByPage" resultType="com.kxs.user.api.vo.kxsapp.userAccount.UserAmountVariationListVO">
        SELECT * FROM ${tableName}
        <where>
            AND del_flag = 0
            AND user_id = #{query.userId}
            <if test="query.kind != null and query.kind != ''">
                AND kind = #{query.kind}
            </if>
            <if test="query.brandIds != null">
                AND ${tableName}.brand_id IN
                <foreach item="brandId" index="index" collection="query.brandIds" open="(" separator="," close=")">
                    #{brandId}
                </foreach>
            </if>
            <if test="query.changeType != null">
                AND variation_type IN (
                     SELECT
                        kctk.id
                    FROM
                        kxs_change_type_kind kctk
                        LEFT JOIN
                            kxs_change_type kct
                        ON
                        kctk.change_type_id = kct.id
                    WHERE
                 kct.id In
                <foreach item="ct" index="index" collection="query.changeType" open="(" separator="," close=")">
                    #{ct}
                </foreach>
                 )
            </if>
        </where>
    </select>
    <select id="variationTypeList" resultType="com.kxs.user.api.vo.kxsapp.userAccount.VariationTypeListVO">
        SELECT * FROM kxs_change_type WHERE del_flag = 0
        <if test="kind != null and kind != '' and kind != 3">
            AND kind = #{kind}
        </if>
    </select>
    <select id="variationList" resultType="com.kxs.user.api.vo.kxsapp.userAccount.VariationListVO">
        SELECT * FROM kxs_change_type_kind WHERE del_flag = 0
        <if test="changeTypeId != null and changeTypeId != '' and changeTypeId != 0">
            AND change_type_id = #{changeTypeId}
        </if>
    </select>

</mapper>
