<?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.base.mapper.CuMemberRetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.base.CuMemberRetail">
    <result column="PK_COUNTRY" property="pkCountry" />
        <result column="PK_MEMBER" property="pkMember" />
        <result column="PK_PARENT" property="pkParent" />
        <result column="MEMBER_CODE" property="memberCode" />
        <result column="MEMBER_NAME" property="memberName" />
        <result column="CUSTOMER_TYPE" property="customerType" />
        <result column="PHONE" property="phone" />
        <result column="PK_GRADE" property="pkGrade" />
        <result column="PK_AWARDS" property="pkAwards" />
        <result column="PK_SHARE_AWARDS" property="pkShareAwards" />
        <result column="ENABLE_STATUS" property="enableStatus" />
        <result column="ACCOUNT_STATUS" property="accountStatus" />
        <result column="PAY_STATUS" property="payStatus" />
        <result column="PAY_TIME" property="payTime" />
        <result column="CATEGORY" property="category" />
        <result column="PURCHASE_STATUS" property="purchaseStatus" />
        <result column="INCOME_STATUS" property="incomeStatus" />
        <result column="PK_TEAM_CODE" property="pkTeamCode" />
        <result column="PK_CENTER_CODE" property="pkCenterCode" />
        <result column="PK_VERTEX" property="pkVertex" />
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry" />
        <result column="IS_REAL_NAME" property="isRealName" />
        <result column="SYSTEM_TYPE" property="systemType" />
        <result column="PK_RATE" property="pkRate" />
        <result column="NEW_CONSUME_PV" property="newConsumePv" />
        <result column="CONSUME_PV" property="consumePv" />
        <result column="MONTH_CONSUME_PV" property="monthConsumePv" />
        <result column="NEW_CONSUME_AMOUNT" property="newConsumeAmount" />
        <result column="CONSUME_AMOUNT" property="consumeAmount" />
        <result column="MONTH_CONSUME_AMOUNT" property="monthConsumeAmount" />
        <result column="TEAM_NEW_PV" property="teamNewPv" />
        <result column="TEAM_CONSUME_PV" property="teamConsumePv" />
        <result column="TEAM_MONTH_PV" property="teamMonthPv" />
        <result column="TEAM_NEW_AMOUNT" property="teamNewAmount" />
        <result column="TEAM_CONSUME_AMOUNT" property="teamConsumeAmount" />
        <result column="TEAM_MONTH_AMOUNT" property="teamMonthAmount" />
        <result column="RECOMMEND_NUM" property="recommendNum" />
        <result column="TEAM_NUM" property="teamNum" />
        <result column="BIG_TEAM_PV" property="bigTeamPv" />
        <result column="SMALL_TEAM_PV" property="smallTeamPv" />
        <result column="REG_CONSUME_NEW_AMOUNT" property="regConsumeNewAmount" />
        <result column="REG_CONSUME_NEW_PV" property="regConsumeNewPv" />
        <result column="REG_TEAM_NEW_AMOUNT" property="regTeamNewAmount" />
        <result column="REG_TEAM_NEW_PV" property="regTeamNewPv" />
        <result column="REP_CONSUME_NEW_AMOUNT" property="repConsumeNewAmount" />
        <result column="REP_CONSUME_NEW_PV" property="repConsumeNewPv" />
        <result column="REP_TEAM_NEW_AMOUNT" property="repTeamNewAmount" />
        <result column="REP_TEAM_NEW_PV" property="repTeamNewPv" />
        <result column="POINT_COUNT" property="pointCount" />
        <result column="WAIT_POINT_COUNT" property="waitPointCount" />
        <result column="REP_PV_BALANCE" property="repPvBalance" />
        <result column="GIFT_ONE" property="giftOne" />
        <result column="GIFT_TWO" property="giftTwo" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        PK_COUNTRY,
        PK_MEMBER, PK_PARENT, MEMBER_CODE, MEMBER_NAME, CUSTOMER_TYPE, PHONE, PK_GRADE, PK_AWARDS, PK_SHARE_AWARDS, ENABLE_STATUS, ACCOUNT_STATUS, PAY_STATUS, PAY_TIME, CATEGORY, PURCHASE_STATUS, INCOME_STATUS, PK_TEAM_CODE, PK_CENTER_CODE, PK_VERTEX, PK_SETTLE_COUNTRY, IS_REAL_NAME, SYSTEM_TYPE, PK_RATE, NEW_CONSUME_PV, CONSUME_PV, MONTH_CONSUME_PV, NEW_CONSUME_AMOUNT, CONSUME_AMOUNT, MONTH_CONSUME_AMOUNT, TEAM_NEW_PV, TEAM_CONSUME_PV, TEAM_MONTH_PV, TEAM_NEW_AMOUNT, TEAM_CONSUME_AMOUNT, TEAM_MONTH_AMOUNT, RECOMMEND_NUM, TEAM_NUM, BIG_TEAM_PV, SMALL_TEAM_PV, REG_CONSUME_NEW_AMOUNT, REG_CONSUME_NEW_PV, REG_TEAM_NEW_AMOUNT, REG_TEAM_NEW_PV, REP_CONSUME_NEW_AMOUNT, REP_CONSUME_NEW_PV, REP_TEAM_NEW_AMOUNT, REP_TEAM_NEW_PV, POINT_COUNT, WAIT_POINT_COUNT, REP_PV_BALANCE, GIFT_ONE, GIFT_TWO
    </sql>
    <select id="getFindCumemberRetailDetail" resultType="java.lang.Integer">
        select 1
        from user_tables
        where table_name = upper(#{settleTableName})
    </select>
    <select id="getMemberPointByMemberCode" resultType="com.hzs.member.base.vo.MemberPointVO">
        SELECT
        cmr.MEMBER_CODE,
        cmr.MEMBER_NAME,
        cmr.POINT_COUNT,
        cmr.WAIT_POINT_COUNT,
        cmr.REP_PV_BALANCE,
        COUNT(CASE WHEN curd.STAGE = 1 AND curd.STAGE_STATUS = 1 THEN 1 END) AS firstOrderQuantity,
        COUNT(CASE WHEN curd.STAGE = 2 AND curd.STAGE_STATUS = 1 THEN 1 END) AS secondOrderQuantity,
        COUNT(CASE WHEN curd.STAGE = 3 AND curd.STAGE_STATUS = 1 THEN 1 END) AS threeOrderQuantity
        FROM
        ${settleTableName} cmr
        LEFT JOIN ${settleDetailTableName} curd ON cmr.PK_MEMBER = curd.PK_MEMBER
        WHERE 1=1
        <if test="memberCode != null and memberCode != ''">
            AND cmr.MEMBER_CODE = #{memberCode}
        </if>
        GROUP BY
        cmr.MEMBER_CODE,
        cmr.MEMBER_NAME,
        cmr.POINT_COUNT,
        cmr.WAIT_POINT_COUNT,
        cmr.REP_PV_BALANCE
        <if test="sort != null and sort != '' and sortColumn != null and sortColumn != ''">
            ORDER BY cmr.${sortColumn} ${sort}
        </if>
    </select>
    <select id="getMemberPointByUserId" resultType="com.hzs.member.base.vo.MemberPointVO">
        SELECT
            cmr.MEMBER_CODE,
            cmr.MEMBER_NAME,
            cmr.POINT_COUNT,
            cmr.WAIT_POINT_COUNT,
            cmr.REP_PV_BALANCE,
            COUNT(CASE WHEN curd.STAGE = 1 AND curd.STAGE_STATUS = 1 THEN 1 END) AS firstOrderQuantity,
            COUNT(CASE WHEN curd.STAGE = 2 AND curd.STAGE_STATUS = 1 THEN 1 END) AS secondOrderQuantity,
            COUNT(CASE WHEN curd.STAGE = 3 AND curd.STAGE_STATUS = 1 THEN 1 END) AS threeOrderQuantity
        FROM
            ${settleTableName} cmr
                LEFT JOIN ${settleDetailTableName} curd ON cmr.PK_MEMBER = curd.PK_MEMBER
        WHERE
            cmr.PK_MEMBER = #{userId}
        GROUP BY
            cmr.MEMBER_CODE,
            cmr.MEMBER_NAME,
            cmr.POINT_COUNT,
            cmr.WAIT_POINT_COUNT,
            cmr.REP_PV_BALANCE
    </select>

</mapper>
