<?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.CuMemberAchieveLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.detail.CuMemberAchieveLog">
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="PERIOD" property="period"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="A_SUM_REAL_MODIFY_PV" property="aSumRealModifyPv"/>
        <result column="B_SUM_REAL_MODIFY_PV" property="bSumRealModifyPv"/>
        <result column="A_FIRST_MODIFY_PV" property="aFirstModifyPv"/>
        <result column="B_FIRST_MODIFY_PV" property="bFirstModifyPv"/>
        <result column="REP_A_FIRST_MODIFY_BALANCE" property="repAFirstModifyBalance"/>
        <result column="REP_B_FIRST_MODIFY_BALANCE" property="repBFirstModifyBalance"/>
        <result column="REP_A_MODIFY_SUM_PV" property="repAModifySumPv"/>
        <result column="REP_B_MODIFY_SUM_PV" property="repBModifySumPv"/>
        <result column="REP_A_MODIFY_BALANCE" property="repAModifyBalance"/>
        <result column="REP_B_MODIFY_BALANCE" property="repBModifyBalance"/>
        <result column="INFUSING_WATER_TYPE" property="infusingWaterType"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="REMARKS" property="remarks"/>
    </resultMap>

    <resultMap id="CuMemberAchieveLogVO" type="com.hzs.member.base.vo.CuMemberAchieveLogVO">
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="A_BALANCE" property="abalance"/>
        <result column="B_BALANCE" property="bbalance"/>
        <result column="A_SUM_REAL_PV" property="asumRealPv"/>
        <result column="B_SUM_REAL_PV" property="bsumRealPv"/>
        <result column="REP_A_BALANCE" property="repABalance"/>
        <result column="REP_B_BALANCE" property="repBBalance"/>
        <result column="REP_A_SUM_PV" property="repASumPv"/>
        <result column="REP_B_SUM_PV" property="repBSumPv"/>
        <result column="A_SUM_REAL_MODIFY_PV" property="asumRealModifyPv"/>
        <result column="B_SUM_REAL_MODIFY_PV" property="bsumRealModifyPv"/>
        <result column="A_SUM_PV" property="asumPv"/>
        <result column="A_FIRST_MODIFY_PV" property="afirstModifyPv"/>
        <result column="B_SUM_PV" property="bsumPv"/>
        <result column="B_FIRST_MODIFY_PV" property="bfirstModifyPv"/>
        <result column="REP_A_FIRST_MODIFY_BALANCE" property="repAFirstModifyBalance"/>
        <result column="REP_B_FIRST_MODIFY_BALANCE" property="repBFirstModifyBalance"/>
        <result column="REP_A_MODIFY_SUM_PV" property="repAModifySumPv"/>
        <result column="REP_B_MODIFY_SUM_PV" property="repBModifySumPv"/>
        <result column="REP_A_MODIFY_BALANCE" property="repAModifyBalance"/>
        <result column="REP_B_MODIFY_BALANCE" property="repBModifyBalance"/>
        <result column="INFUSING_WATER_TYPE" property="infusingWaterType"/>
        <result column="REMARKS" property="remarks"/>
        <result column="TEAM_NAME" property="teamName"/>
        <result column="VERTEX_NAME" property="vertexName"/>
        <result column="awards" property="awards"/>
        <result column="PHONE" property="phone"/>
        <result column="DUE_DATE" property="dueDate"/>
        <result column="SETTLE_DATE" property="settleDate"/>
    </resultMap>


    <!--特殊业务-注水业绩记录查询-->
    <select id="queryMemberAchieveLogList"
            resultMap="CuMemberAchieveLogVO">
        select
        cm.MEMBER_CODE,
        cm.MEMBER_NAME,
        cm.PHONE,
        a.PK_MEMBER,
        a.A_SUM_REAL_MODIFY_PV,
        a.B_SUM_REAL_MODIFY_PV,
        a.A_FIRST_MODIFY_PV,
        a.B_FIRST_MODIFY_PV,
        a.REP_A_FIRST_MODIFY_BALANCE,
        a.REP_B_FIRST_MODIFY_BALANCE,
        a.REP_A_MODIFY_SUM_PV,
        a.REP_B_MODIFY_SUM_PV,
        a.REP_A_MODIFY_BALANCE,
        a.REP_B_MODIFY_BALANCE,
        a.INFUSING_WATER_TYPE,
        a.REMARKS,
        a.APPROVE_STATUS,
        a.APPROVE_NUMBER,
        a.PROCESS_TYPE,
        a.EDIT_TYPE,
        a.COST,
        a.PERIOD,
        a.BUSINESS_NO,
        a.DUE_DATE,
        a.SETTLE_DATE,
        a.A_SUM_REAL_PV,
        a.B_SUM_REAL_PV,
        a.A_SUM_PV,
        a.B_SUM_PV,
        a.A_BALANCE,
        a.B_BALANCE,
        a.REP_A_SUM_PV,
        a.REP_B_SUM_PV,
        a.REP_A_BALANCE,
        a.REP_B_BALANCE,
        ct.TEAM_NAME,
        ver.VERTEX_NAME,
        awa.PK_TRANSACTION as awards
        from CU_MEMBER_ACHIEVE_LOG a
        INNER JOIN CU_MEMBER cm on a.PK_MEMBER=cm.PK_ID
        LEFT JOIN cu_member_team ct ON ct.pk_id = cm.pk_team_code
        left join bd_vertex ver on cm.pk_vertex = ver.pk_id
        left join bd_awards awa on awa.pk_id = cm.PK_AWARDS
        WHERE
        a.DEL_FLAG = 0
        AND cm.DEL_FLAG =0
        <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="infusingWaterType != null ">
            AND a.INFUSING_WATER_TYPE = #{infusingWaterType}
        </if>
        <if test="pkCountry != null ">
            AND a.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="pkAwards != null ">
            AND cm.PK_AWARDS = #{pkAwards}
        </if>
        <if test="pkTeamCode != null ">
            AND cm.pk_team_code = #{pkTeamCode}
        </if>
        <if test="pkVertex != null ">
            AND cm.pk_vertex = #{pkVertex}
        </if>
        <if test="startDate != null and endDate != null">
            and a.SETTLE_DATE between #{startDate, jdbcType=DATE} and #{endDate, jdbcType=DATE}
        </if>
        <if test="staDueDate != null and endDueDate != null">
            and a.DUE_DATE between #{staDueDate, jdbcType=DATE} and #{endDueDate, jdbcType=DATE}
        </if>

        <if test="waterStatus==0">
            and a.SETTLE_DATE &gt; sysdate
        </if>
        <if test="waterStatus==1">
            and a.SETTLE_DATE &lt;= sysdate
            and sysdate &lt;= a.DUE_DATE
        </if>
        <if test="waterStatus==2">
            and a.DUE_DATE &lt;sysdate
        </if>

        and (a.APPROVE_STATUS = 5
        or a.APPROVE_STATUS = 6 )
        order by a.PK_ID desc
    </select>

    <!--    根据期数查询历史业绩-->
    <select id="queryMemberAchieveHistoryList"
            resultMap="CuMemberAchieveLogVO">
        <foreach collection="calList" item="item" separator="UNION">
            select
            PK_MEMBER,
            PERIOD,
            A_SUM_REAL_PV,
            B_SUM_REAL_PV,
            A_SUM_PV,
            B_SUM_PV,
            A_BALANCE,
            B_BALANCE,
            REP_A_SUM_PV,
            REP_B_SUM_PV,
            REP_A_BALANCE,
            REP_B_BALANCE
            from ${item.tableName}
            <where>1=1
                <if test="item.period!= null">
                    and PERIOD =#{item.period}
                </if>
                <if test="item.pkMember!= null">
                    and PK_MEMBER =#{item.pkMember}
                </if>
            </where>
        </foreach>

    </select>

    <!--    特殊业务-注水业绩-审核列表-->
    <select id="queryTryAchieveLogList"
            resultMap="CuMemberAchieveLogVO">
        select a.PK_MEMBER,
               a.PK_COUNTRY,
               b.MEMBER_CODE,
               b.MEMBER_NAME,
               a.A_SUM_REAL_MODIFY_PV,
               a.B_SUM_REAL_MODIFY_PV,
               a.A_FIRST_MODIFY_PV,
               a.B_FIRST_MODIFY_PV,
               a.REP_A_FIRST_MODIFY_BALANCE,
               a.REP_B_FIRST_MODIFY_BALANCE,
               a.REP_A_MODIFY_SUM_PV,
               a.REP_B_MODIFY_SUM_PV,
               a.REP_A_MODIFY_BALANCE,
               a.REP_B_MODIFY_BALANCE,
               a.INFUSING_WATER_TYPE,
               a.REMARKS,
               a.APPROVE_STATUS,
               a.APPROVE_NUMBER,
               a.PROCESS_TYPE,
               a.EDIT_TYPE,
               a.COST,
               a.PERIOD,
               a.BUSINESS_NO,
               a.SETTLE_DATE,
               a.DUE_DATE
        from CU_MEMBER_ACHIEVE_LOG a
                 INNER JOIN CU_MEMBER b on a.PK_MEMBER = b.PK_ID
        WHERE a.DEL_FLAG = 0
          AND b.DEL_FLAG = 0
          and BUSINESS_NO = #{businessNo}
        order by a.PK_ID desc
    </select>

    <!--查询注水业绩最大审核次数-->
    <select id="selectByMaxApproveNumber" resultType="integer">
        select nvl(max(APPROVE_NUMBER), 0)
        from CU_MEMBER_ACHIEVE_LOG
        where PK_MEMBER = #{pkMember}
          and APPROVE_STATUS = #{approveStatus}
    </select>

    <!--    校验是否存在未审核数据-->
    <select id="checkAchieveList" resultMap="CuMemberAchieveLogVO"
            parameterType="com.hzs.member.base.vo.CuMemberAchieveLogVO">
        select b.MEMBER_CODE as memberCode,
        b.MEMBER_NAME as memberName
        from CU_MEMBER_ACHIEVE_LOG a
        LEFT JOIN CU_MEMBER b on a.PK_MEMBER=b.PK_ID
        where a.DEL_FLAG=0 and b.DEL_FLAG=0
        and (a.APPROVE_STATUS=1 or a.APPROVE_STATUS=2)
        <if test="pkMemberList != null and pkMemberList != ''">
            and a.PK_MEMBER in
            <foreach item="pkMember" collection="pkMemberList" open="(" separator="," close=")">
                #{pkMember}
            </foreach>
        </if>
        <if test="pkMember!= null ">
            and a.PK_MEMBER =#{pkMember}
        </if>
    </select>


</mapper>
