<?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.yonge.cooleshow.biz.dal.dao.UserFirstTimeDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.UserFirstTime">
            <result column="id_" property="id" />
	        <result column="user_id_" property="userId" />
	        <result column="user_type_" property="userType" />
	        <result column="time_type_" property="timeType" />
	        <result column="time_" property="time" />
	        <result column="create_time_" property="createTime" />
		</resultMap>  
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.user_id_ as userId
        , t.user_type_ as userType
        , t.time_type_ as timeType
        , t.time_ as `time`
        , t.create_time_ as createTime
        </sql>

    <select id="selectAllList" resultType="com.yonge.cooleshow.biz.dal.entity.UserFirstTime">
        SELECT
            <include refid="baseColumns"/>
        FROM user_first_time t
        <where>
            <if test="param.userId != null">
                and t.user_id_ = #{param.userId}
            </if>
            <if test="param.userIds != null">
                AND t.user_id_ IN (<foreach collection="param.userIds" separator="," item="item">#{item}</foreach>)
            </if>
            <if test="param.userType != null and param.userType != ''">
                and find_in_set(t.user_type_ , #{param.userType})
            </if>
            <if test="param.timeType != null and param.timeType != ''">
                and find_in_set(t.time_type_ , #{param.timeType})
            </if>
            <if test="param.startTime != null">
                and t.time_ &gt;= #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and t.time_ &lt;= #{param.endTime}
            </if>
        </where>
        order by t.time_
    </select>

    <!--达标活动参与用户-->
    <select id="selectActivityParticipateUserPage"
            resultType="com.yonge.cooleshow.biz.dal.entity.UserFirstTime">
        SELECT t.user_id_ AS userId FROM (
            SELECT t1.user_id_, t1.user_type_, <choose><when test="record.calculationMethod.code == 'AND'">MAX(t1.time_)</when><otherwise>MIN(t1.time_)</otherwise></choose>  AS time_ FROM user_first_time t1
            <where>
                <if test="record.userType != null and record.userType != ''">
                    and find_in_set(t1.user_type_ , #{record.userType})
                </if>
                <if test="record.timeType != null and record.timeType != ''">
                    and find_in_set(t1.time_type_ , #{record.timeType})
                </if>
                <if test="record.startTime != null">
                    and t1.time_ &gt;= #{record.startTime}
                </if>
                <if test="record.endTime != null">
                    and t1.time_ &lt;= #{record.endTime}
                </if>
            </where>
            GROUP BY t1.user_id_ ORDER BY t1.time_ ASC
        ) t
        <where>
            <if test="record.activityId != null">
                AND t.user_id_ NOT IN (SELECT t2.user_id_ FROM activity_user_reward t2 WHERE t2.activity_id_ = #{record.activityId})
            </if>
        </where>
        ORDER BY t.time_ ASC
    </select>
    <!--达标活动参与用户-->

</mapper>