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

    <!-- 通用查询映射结果 -->
    <resultMap id="CuRegionAssessExt" type="com.hzs.common.domain.member.detail.ext.CuRegionAssessExt">
        <id column="PK_ID" property="pkId" />
        <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_MEMBER" property="pkMember" />
        <result column="PK_REGION" property="pkRegion" />
        <result column="ASSESS_PERIOD" property="assessPeriod" />
        <result column="ASSESS_START_DATE" property="assessStartDate" />
        <result column="ASSESS_END_DATE" property="assessEndDate" />
        <result column="REACH_NUM" property="reachNum" />
        <result column="SOURCE" property="source" />
        <result column="MEMBER_CODE" property="memberCode" />
        <result column="MEMBER_NAME" property="memberName" />
        <result column="PROVINCE_NAME" property="provinceName" />
        <result column="CITY_NAME" property="cityName" />
        <result column="COUNTY_NAME" property="countyName" />
        <result column="ASSESS_ADDRESS" property="assessAddress" />
        <collection property="cuRegionAssessDetailList" ofType="com.hzs.common.domain.member.detail.CuRegionAssessDetail">
            <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" />
        </collection>
    </resultMap>
    <insert id="batchInsertCuRegionAssess">
        insert ALL
        <foreach item="item" index="index" collection="cuRegionAssessList" separator=" " >
            into cu_region_assess(pk_id,pk_member,assess_period,pk_region,assess_start_date,assess_end_date,
            source,pk_creator,pk_country) values
            (
            #{item.pkId},#{item.pkMember},#{item.assessPeriod},#{item.pkRegion},#{item.assessStartDate},#{item.assessEndDate},
            #{item.source},#{item.pkCreator},#{item.pkCountry}
            )
        </foreach>
        SELECT 1 FROM dual
    </insert>
    <update id="mergeCuRegionAssess">
        merge into CU_REGION_ASSESS a
            using(
                select cd.pk_region_assess,sum(1) REACH_NUM from cu_region_assess_detail cd
                inner join cu_region_assess ca
                on cd.pk_region_assess=ca.pk_id
                where cd.ASSESS_STATUS=0 and cd.del_flag=0 and ca.del_flag=0
                and ca.assess_start_date &lt;= #{assessDate,jdbcType=DATE}
                and ca.assess_end_date >= #{assessDate,jdbcType=DATE}
                group by cd.pk_region_assess
            ) b
            on (a.pk_id=b.pk_region_assess)
            when matched then
                update set a.REACH_NUM=b.REACH_NUM
    </update>
    <update id="updateAssessRegionByDate">
        update cu_region_assess set del_flag=1,modified_time=sysdate where pk_id 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="deleteAssessRegionByDate">
        delete 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="queryCuMemberRetailRegion" resultType="com.hzs.common.domain.retail.member.CuMemberRetailRegion">
        select * from cu_member_retail_region where effective=0 and del_flag=0
        and nvl(modified_time,creation_time)>= #{startDate, jdbcType=DATE}
        and nvl(modified_time,creation_time) &lt; #{endDate, jdbcType=DATE}
    </select>
    <select id="batchQueryCuRegionAssessSeq" resultType="java.lang.Long">
        select cu_region_assess_seq.nextval from dual connect by rownum &lt;= #{rowNum}
    </select>
    <select id="queryRegionAssessByCondition" resultMap="CuRegionAssessExt">
        select cm.member_code,cm.member_name,ca.reach_num,ca.assess_period,
               ca.assess_start_date,ca.assess_end_date,ca.reach_num,
               (province.name||city.name||county.name) assess_address,
               cd.assess_date,cd.assess_status,cd.team_month_pv,cd.small_area_pv,
               cd.assess_status /* province.name,city.name,county.name,*/
         from cu_region_assess ca
        inner join cu_region_assess_detail cd
        on ca.pk_id=cd.pk_region_assess
        inner join cu_member cm
        on cm.pk_id=ca.pk_member
        inner join cu_member_retail_region cr
        on cr.pk_id=ca.pk_region
        left join bd_area province on province.PK_ID=cr.province
        left join bd_area city on city.PK_ID=cr.city
        left join bd_area county on county.PK_ID=cr.county
        where ca.del_flag=0 and cd.del_flag=0  and cm.del_flag=0 and cr.del_flag=0
        <if test="regionAssessParam.assessDate != null">
            and cd.assess_date &lt;= #{regionAssessParam.assessDate,jdbcType=DATE}
        </if>
        <if test="regionAssessParam.pkMember != null">
            and ca.pk_member = #{regionAssessParam.pkMember}
        </if>
        <if test="regionAssessParam.pkCountry != null">
            and ca.pk_country = #{regionAssessParam.pkCountry}
        </if>
        <if test="regionAssessParam.memberCode != null and regionAssessParam.memberCode != ''">
            and cm.member_code = #{regionAssessParam.memberCode}
        </if>
        <if test="regionAssessParam.memberName != null and regionAssessParam.memberName != ''">
            and cm.member_name = #{regionAssessParam.memberName}
        </if>
    </select>
    <select id="queryDueCuMemberRetailRegion" resultType="com.hzs.common.domain.retail.member.CuMemberRetailRegion">
        select * from cu_member_retail_region where effective=0 and del_flag=0
        and effective_end_date>= #{startDate, jdbcType=DATE}
        and effective_end_date &lt; #{endDate, jdbcType=DATE}
    </select>
    <select id="queryRegionAssessByDate"
            resultType="com.hzs.common.domain.member.detail.ext.CuRegionAssessExt">
        select cd.pk_member,count(cd.pk_id) pk_id from cu_region_assess ca
          inner join cu_region_assess_detail cd
                     on ca.pk_id=cd.pk_region_assess
        where ca.del_flag=0 and cd.del_flag=0
          and cd.assess_date>=#{startDate, jdbcType=DATE} and cd.assess_date &lt;=#{endDate, jdbcType=DATE}
          and assess_status=0 and ca.pk_member in
        <foreach collection="memberRetailRegionList" item="item" open="(" close=")" separator=",">
            #{item.pkMember}
        </foreach>
          group by cd.pk_member
    </select>

</mapper>
