<?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.bonus.detail.mapper.CuRegionAssessDetailMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuRegionAssessDetail" type="com.hzs.common.domain.member.detail.CuRegionAssessDetail">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="PK_REGION_ASSESS" property="pkRegionAssess"/>
        <result column="ASSESS_DATE" property="assessDate"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="TEAM_MONTH_PV" property="teamMonthPv"/>
        <result column="SMALL_AREA_PV" property="smallAreaPv"/>
        <result column="ASSESS_STATUS" property="assessStatus"/>
    </resultMap>

    <insert id="batchInsertCuRegionAssessDetail">
        insert ALL
        <foreach item="item" index="index" collection="cuRegionAssessDetailList" separator=" ">
            into cu_region_assess_detail (
            pk_id, pk_member, pk_region_assess, assess_date,
            pk_creator, pk_country)
            values (
            #{item.pkId}, #{item.pkMember}, #{item.pkRegionAssess}, #{item.assessDate},
            #{item.pkCreator}, #{item.pkCountry})
        </foreach>
        SELECT 1 FROM dual
    </insert>

    <update id="mergeCuRegionAssessDetailTeamPv">
        merge into cu_region_assess_detail a
            using (
                select cd.pk_id,
                       cr.pk_member,
                       cr.pk_awards,
                       (cr.month_consume_pv + cr.team_month_pv) team_month_pv
                from cu_region_assess_detail cd
                         inner join ${rangeTableName} cr
                                    on cd.pk_member = cr.pk_member
                where cd.del_flag = 0
                  and cd.assess_date = #{assessDate, jdbcType=DATE}
            ) b
            on (a.pk_id = b.pk_id)
            when matched then
                update set a.pk_awards = b.pk_awards, a.team_month_pv = b.team_month_pv
            WHERE a.pk_awards != b.pk_awards
            OR a.team_month_pv != b.team_month_pv
    </update>

    <update id="mergeCuRegionAssessDetailSmallAreaPv">
        merge into cu_region_assess_detail a
            using (
                select pk_id, max(big_area_pv) big_area_pv
                from (
                         select cd.pk_id, b.*
                         from cu_region_assess_detail cd
                                  inner join (
                             select pk_parent pk_member, (month_consume_pv + team_month_pv) big_area_pv
                             from ${rangeTableName}
                             union
                             select pk_member, month_consume_pv big_area_pv
                             from ${rangeTableName}
                         ) b
                                             on cd.pk_member = b.pk_member
                         where cd.del_flag = 0
                           and cd.assess_date = #{assessDate, jdbcType=DATE}
                     )
                group by pk_id
            ) b
            on (a.pk_id = b.pk_id)
            when matched then
                update set a.small_area_pv = a.team_month_pv - b.big_area_pv
    </update>

    <update id="updateCuRegionAssessDetailInit">
        update cu_region_assess_detail
        set assess_status = 2
        where del_flag = 0
          and assess_date = #{assessDate, jdbcType=DATE}
    </update>

    <update id="updateCuRegionAssessDetailByPv">
        update cu_region_assess_detail
        set assess_status = 0
        where del_flag = 0
          and team_month_pv >= 300000
          and small_area_pv >= 100000
          and assess_date = #{assessDate, jdbcType=DATE}
    </update>

    <update id="updateAssessRegionDetailByDate">
        update cu_region_assess_detail
        set del_flag      = 1,
            modified_time = sysdate
        where pk_region_assess in (
            select ca.pk_id
            from cu_region_assess ca
                     inner join (
                select pk_id
                from cu_member_retail_region
                where effective = 0
                  and del_flag = 0
                  and nvl(modified_time, creation_time) >= #{assessStartDate, jdbcType=DATE}
                  and nvl(modified_time, creation_time) &lt; #{assessEndDate, jdbcType=DATE}
            ) b
                                on ca.pk_region = b.pk_id
            where ca.del_flag = 0
              and ca.assess_start_date != #{assessEndDate,jdbcType=DATE})
    </update>

    <delete id="deleteAssessRegionDetailByDate">
        delete from cu_region_assess_detail
        where del_flag = 0 and
        pk_region_assess in (
        select pk_id from cu_region_assess
        where del_flag = 0
        and assess_start_date = #{assessStartDate, jdbcType=DATE}
        <choose>
            <when test="assessPeriod == 1">and assess_period = #{assessPeriod}</when>
            <otherwise>and assess_period >= #{assessPeriod}</otherwise>
        </choose>
        )
    </delete>

    <select id="batchQueryCuRegionAssessDetailSeq" resultType="java.lang.Long">
        select cu_region_assess_detail_seq.nextval
        from dual connect by rownum &lt;= #{rowNum}
    </select>

</mapper>
