<?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.hzs.member.achieve.mapper.CuActiveMemberCensusMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.achieve.CuActiveMemberCensus">
        <id column="PK_ID" property="pkId" />
    <result column="CREATION_TIME" property="creationTime" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="REGISTER" property="register" />
        <result column="UPGRADE" property="upgrade" />
        <result column="REPURCHASE" property="repurchase" />
        <result column="NEWLY_VILLAGE" property="newlyVillage" />
    </resultMap>

    <resultMap id="CuActiveMemberCensusVO" type="com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO">
        <id column="PK_ID" property="pkId" />
        <result column="CREATION_TIME" property="creationTime" />
        <result column="compareTime" property="compareTime" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="REGISTER" property="register" />
        <result column="UPGRADE" property="upgrade" />
        <result column="REPURCHASE" property="repurchase" />
        <result column="NEWLY_VILLAGE" property="newlyVillage" />
        <result column="TOTALCOUNT" property="totalCount" />
        <result column="PLACE_DEPT" property="placeDept" />

    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        CREATION_TIME,
        PK_ID, PK_MEMBER, REGISTER, UPGRADE, REPURCHASE, NEWLY_VILLAGE
    </sql>

    <!--查询会员当天是否活跃-->
    <select id="checkActiveMember" parameterType="com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO"
            resultType="com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO">
        select cam.*
        from CU_ACTIVE_MEMBER_CENSUS cam
        where 1=1
        <if test="payTime == null">
           and to_char(cam.CREATION_TIME, 'yyyy-mm-dd') = to_char(sysdate, 'yyyy-mm-dd')
        </if>
        <if test="payTime != null and payTime!='' ">
            and to_char(cam.CREATION_TIME, 'yyyy-mm-dd') = #{payTime}
        </if>
        <if test="pkMember != null">
            and cam.pk_member=#{pkMember}
        </if>
    </select>
    <!--查询伞下活跃用户-->
    <select id="queryActiveMember" parameterType="com.hzs.member.achieve.controller.vo.CuActiveMemberCensusVO"
            resultMap="CuActiveMemberCensusVO">
        select * from (
        select t.CREATION_TIME as CREATION_TIME,
        to_char(t.CREATION_TIME,'yyyy-mm-dd') as compareTime,
        sum(t.TOTALCOUNT) as TOTALCOUNT,t.PLACE_DEPT from
        (select cam.CREATION_TIME,
        cam.REGISTER + cam.UPGRADE + cam.REPURCHASE + cam.NEWLY_VILLAGE as TOTALCOUNT,
        cm.PLACE_DEPT
        from CU_ACTIVE_MEMBER_CENSUS cam
        inner join (select t.pk_id, t.PLACE_DEPT,t.del_Flag
        from cu_member t
        where t.del_flag = 0 start
        with t.pk_place_parent =#{pkMember}
        connect by t.pk_place_parent = prior t.pk_id) cm
        on cm.pk_id = cam.pk_member
        where cm.del_Flag = 0
        <if test="startDate != null and startDate != ''">
            and cam.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != ''">
            and cam.CREATION_TIME &lt;= to_date(#{endDate}, 'yyyy-mm-dd')
        </if>
        ) t
        group by t.CREATION_TIME,t.PLACE_DEPT
        ) a
        order by a.CREATION_TIME
    </select>

</mapper>
