<?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.swing.mapper.CuMemberSwingNetMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberSwingNetExt" type="com.hzs.common.domain.member.swing.ext.CuMemberSwingNetExt">
        <id column="PK_ID" property="pkId"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_SOURCE_MEMBER" property="pkSourceMember"/>
        <result column="PK_TARGET_MEMBER" property="pkTargetMember"/>
        <result column="source_member_code" property="sourceMemberCode"/>
        <result column="source_member_name" property="sourceMemberName"/>
        <result column="target_member_code" property="targetMemberCode"/>
        <result column="target_member_name" property="targetMemberName"/>
        <collection property="cuMemberSwingNetDetailExtList" ofType="com.hzs.common.domain.member.swing.ext.CuMemberSwingNetDetailExt">
            <result column="PK_DETAIL_ID" property="pkId"/>
            <result column="PK_SWING_NET" property="pkSwingNet"/>
            <result column="PK_MEMBER" property="pkMember"/>
            <result column="PLACE_DEPT" property="placeDept"/>
            <result column="FIRST_ACHIEVE_RATIO" property="firstAchieveRatio"/>
            <result column="FIRST_REACH_POINT" property="firstReachPoint"/>
            <result column="FIRST_CALCULATE" property="firstCalculate"/>
            <result column="REPURCHASE_ACHIEVE_RATIO" property="repurchaseAchieveRatio"/>
            <result column="REPURCHASE_REACH_POINT" property="repurchaseReachPoint"/>
            <result column="REPURCHASE_CALCULATE" property="repurchaseCalculate"/>
            <result column="member_code" property="memberCode"/>
            <result column="member_name" property="memberName"/>
        </collection>
    </resultMap>

    <select id="queryCuMemberSwingNetExt" resultMap="CuMemberSwingNetExt">
        select cn.pk_id, cn.pk_target_member, cn.pk_source_member,
        cd.pk_id pk_detail_id, cd.pk_member, cd.place_dept,
        cd.first_achieve_ratio, cd.repurchase_achieve_ratio,
        cd.first_calculate, cd.repurchase_calculate,
        cd.first_reach_point, cd.repurchase_reach_point,
        sou.member_code source_member_code, sou.member_name source_member_name,
        par.member_code target_member_code, par.member_name target_member_name,
        cm.member_code, cm.member_name,
        cn.creation_time
        from CU_MEMBER_SWING_NET cn
        inner join CU_MEMBER_SWING_NET_detail cd
        on cn.pk_id = cd.pk_swing_net
        left join cu_member sou
        on cn.pk_source_member = sou.pk_id
        left join cu_member par
        on cn.pk_target_member = par.pk_id
        left join cu_member cm
        on cd.pk_member = cm.pk_id
        where cn.del_flag = 0
        and cd.del_flag = 0
        <if test="pkCountry != null">
            and cn.pk_country = #{pkCountry}
        </if>
        <if test="pkId != null">
            and cn.pk_id = #{pkId}
        </if>
        <if test="sourceMemberCode != null and sourceMemberCode != ''">
            and sou.member_code like #{sourceMemberCode} || '%'
        </if>
        <if test="sourceMemberName != null and sourceMemberName != ''">
            and sou.member_name like #{sourceMemberName} || '%'
        </if>
        <if test="targetMemberCode != null and targetMemberCode != ''">
            and par.member_code like #{targetMemberCode} || '%'
        </if>
        <if test="targetMemberName != null and targetMemberName != ''">
            and par.member_name like #{targetMemberName} || '%'
        </if>
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name like #{memberName} || '%'
        </if>
        <if test="startCreationTime != null">
            and cn.creation_time >= #{startCreationTime}
        </if>
        <if test="endCreationTime != null">
            and #{endCreationTime} >= cn.creation_time
        </if>
        order by cn.creation_time desc
    </select>


<!--    甩网配置详情-->
    <select id="swingNetLogList" resultType="com.hzs.common.domain.member.swing.ext.CuMemberSwingNetLogExt">
        select
        cm.MEMBER_CODE memberCode,
        cm.MEMBER_NAME memberName,
        cm.PHONE,
        cmsn.PK_MEMBER pkMember,
        cmsn.PLACE_DEPT placeDept,
        cmsn.NET_FIRST_ACHIEVE netFirstAchieve,
        cmsn.NET_REPURCHASE_ACHIEVE netRepurchaseAchieve,
        cmsn.NET_REAL_ACHIEVE netRealAchieve,
        (cmsn.A_SUM_PV-cmsn.NET_FIRST_ACHIEVE) oldASumPv ,
        cmsn.A_SUM_PV aSumPv ,
        (cmsn.B_SUM_PV-cmsn.NET_FIRST_ACHIEVE) oldBSumPv ,
        cmsn.B_SUM_PV bSumPv,

        (cmsn.REP_A_SUM_PV-cmsn.NET_REPURCHASE_ACHIEVE) oldRepASumPv ,
        cmsn.REP_A_SUM_PV repASumPv ,
        (cmsn.REP_B_SUM_PV-cmsn.NET_REPURCHASE_ACHIEVE) oldRepBSumPv ,
        cmsn.REP_B_SUM_PV repBSumPv,

        (cmsn.A_SUM_REAL_PV-cmsn.NET_REAL_ACHIEVE) oldASumRealPv ,
        cmsn.A_SUM_REAL_PV aSumRealPv ,
        (cmsn.B_SUM_REAL_PV-cmsn.NET_REAL_ACHIEVE) oldBSumRealPv ,
        cmsn.B_SUM_REAL_PV bSumRealPv,
        cmsn.CREATION_TIME creationTime,
        cmsn.CREATION_TIME swingingDate,
        bv.vertex_name vertexName,
        ct.team_name teamName,
        ba.pk_transaction pkAwards
        from CU_MEMBER_SWING_NET_LOG cmsn
        INNER JOIN CU_MEMBER cm on cmsn.PK_MEMBER=cm.PK_ID
        left join cu_member_team ct on cm.pk_team_code=ct.pk_id
        left join bd_vertex bv  on cm.pk_vertex = bv.pk_id
        left join bd_awards ba on cm.pk_awards = ba.pk_id
        LEFT JOIN CU_MEMBER_SWING_NET_DETAIL cmsnd on cmsn.PK_MEMBER=cmsnd.PK_MEMBER
        where
        cmsn.DEL_FLAG=0
        and cm.DEL_FLAG=0
        and ba.DEL_FLAG=0
        <if test="pkId != null">
            and cmsnd.PK_SWING_NET=#{pkId}
        </if>
        <if test="pkCountry != null">
            and cmsn.pk_country = #{pkCountry}
        </if>
        <if test="pkCountry != null">
            and cm.PK_SETTLE_COUNTRY = #{pkCountry}
        </if>
        <if test="pkAwards != null">
            and ba.pk_id = #{pkAwards}
        </if>
        <if test="pkVertex != null">
            and bv.pk_id = #{pkVertex}
        </if>
        <if test="pkTeamCode != null">
            and ct.pk_id =#{pkTeamCode}
        </if>

        <if test="memberCode != null and memberCode != ''">
            and cm.member_code like #{memberCode} || '%'
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name like #{memberName} || '%'
        </if>
        <if test="startDate != null">
            and cmsn.creation_time >= #{startDate}
        </if>
        <if test="endDate != null">
            and #{endDate} >= cmsn.creation_time
        </if>
        order by cmsn.creation_time desc,cmsnd.pk_id desc
    </select>

</mapper>
