<?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.qs.mp.channel.mapper.ChannelUserRelMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qs.mp.channel.domain.ChannelUserRel">
        <id column="id" property="id" />
        <result column="channel_id" property="channelId" />
        <result column="user_id" property="userId" />
        <result column="created_time" property="createdTime" />
        <result column="updated_time" property="updatedTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id, channel_id, user_id, created_time, updated_time
    </sql>
    
    <!-- 查询渠道及其子渠道经销商用户数总和 -->
    <select id="getChannelTotalUserCnt" resultType="integer">
        select count(t1.user_id) as userCnt 
		from mp_channel_user_rel t1
		left join mp_channel t2 on t1.channel_id = t2.channel_id
		where (t2.channel_no like concat(#{channelNo}, '.%') or t2.channel_no = #{channelNo})
    </select>
    
    <!-- 根据渠道No查询渠道指定时间内新增的用户数量 -->
    <select id="getChannelNewUserCnt" resultType="integer">
        select count(t1.user_id) as userCnt 
		from mp_channel_user_rel t1
		left join mp_channel t2 on t1.channel_id = t2.channel_id
		where (t2.channel_no like concat(#{channelNo}, '.%') or t2.channel_no = #{channelNo})
		<if test="days != null">
			<!-- AND date_format(t1.created_time,'%Y-%m-%d') between date_format(DATE_ADD(CURDATE(),INTERVAL #{days} DAY),'%Y-%m-%d') and date_format(CURDATE(),'%Y-%m-%d') -->
			AND t1.created_time > DATE_SUB(CURDATE(),INTERVAL #{days} DAY)
		 </if>
    </select>

</mapper>
