<?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.stat.biz.mapper.KxsUserActTradeMapper">

    <resultMap type="com.kxs.stat.api.model.KxsUserActTrade" id="KxsUserActTradeMap">
        <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="trade_date" property="tradeDate"/>
        <result column="brand_id" property="brandId"/>
        <result column="act_num" property="actNum"/>
        <result column="trade_month" property="tradeMonth"/>
        <result column="total_type" property="totalType"/>

    </resultMap>
    <select id="getUserTradeMonthList" resultType="com.kxs.stat.api.model.KxsUserActTrade">
        SELECT SUM(act_num) as act_num, user_id
        FROM kxs_user_act_trade
        <where>
            and brand_id = 14
            and total_type = 1
        </where>
        GROUP BY user_id
    </select>
    <select id="getUserTradeTotal" resultType="com.kxs.stat.api.vo.statUser.PercentVO">
        SELECT
        SUM(act_num) AS total,
        brand_id as name
        FROM
        kxs_user_act_trade
        WHERE del_flag != 1
        AND user_id = #{userId}
        AND total_type = #{totalType}
        <if test="type == 1">
            and trade_date in
            <foreach collection="times" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
        </if>
        <if test="type == 2">
            and trade_month in
            <foreach collection="times" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
        </if>
        GROUP BY
        brand_id
    </select>
    <select id="getUserActListByDayOrMonth" resultType="com.kxs.stat.api.vo.statUser.TrendTimesVO">
        SELECT
        SUM(act_num) AS total
        <if test="type == 1">
            ,trade_date as time
        </if>
        <if test="type == 2">
            ,trade_month as time
        </if>
        <if test="brandId != null">
            ,brand_id
        </if>
        FROM
        kxs_user_act_trade
        WHERE del_flag != 1
        AND user_id = #{userId}
        AND total_type = #{totalType}
        <if test="type == 1">
            AND trade_date IN
            <foreach collection="times" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
        </if>
        <if test="type == 2">
            AND trade_month IN
            <foreach collection="times" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
        </if>
        GROUP BY
        <if test="type == 1">
            trade_date
        </if>
        <if test="type == 2">
            trade_month
        </if>
        <if test="brandId != null">
            ,brand_id
        </if>
    </select>
    <select id="getActivityStatByGdOrPos" resultType="java.lang.Integer">
        select
            sum(act_num)
        from
            kxs_user_act_trade
        where
            del_flag != 1
            and total_type = 0
            and user_id = #{userId}
            and trade_date in
            <foreach collection="times" item="time" open="(" separator="," close=")">
                #{time}
            </foreach>
            <if test="type == 1">
                and brand_id != 14
            </if>
            <if test="type == 2">
                and brand_id = 14
            </if>
    </select>
    <select id="getActivityStatByLkb" resultType="java.lang.Integer">
        select
        sum(act_num)
        from
        kxs_lkb_act_trade
        where
        del_flag != 1
        and total_type = 0
        and user_id = #{userId}
        and trade_date in
        <foreach collection="times" item="time" open="(" separator="," close=")">
            #{time}
        </foreach>
    </select>
    <select id="getUserActListByAll" resultType="java.lang.Integer">
        SELECT
        SUM(act_num) AS total
        FROM
        kxs_user_act_trade
        WHERE del_flag != 1
        AND user_id = #{userId}
        AND total_type = #{totalType}
        <if test="type == 1">
            and brand_id != 14
        </if>
        <if test="type == 2">
            and brand_id = 14
        </if>
        <if test="toMonth != 3">
            <if test="toMonth == 1">
                and trade_month != #{month}
            </if>
            <if test="toMonth == 2">
                and trade_month = #{month}
            </if>
        </if>
    </select>
    <select id="getActivityStatByLkbList" resultType="com.kxs.stat.api.vo.statUser.TrendTimesVO">
        select
        sum(act_num) as total,  trade_date as `time`
        from
        kxs_lkb_act_trade
        where
        del_flag != 1
        and total_type = #{totalType}
        and user_id = #{userId}
        and trade_date in
        <foreach collection="times" item="time" open="(" separator="," close=")">
            #{time}
        </foreach>
        group by
        `time`
    </select>


</mapper>
