<?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.retail.member.mapper.CuMemberRetailRegionMapper">

    <!-- 查询会员收益区域 -->
    <select id="getRegionByMember" resultType="com.hzs.common.domain.retail.member.ext.CuMemberRetailRegionExt">
        select cm.member_code,
               cm.is_region,
               cmrr.*,
               bap.name province_val,
               bac.name city_val,
               bax.name county_val,
               cm.IS_ACTIVATE
        from cu_member cm
                 left join cu_member_retail_region cmrr
                           on cmrr.pk_member = cm.pk_id
                               and cmrr.del_flag = 0
                               and cmrr.effective = 0
                               and cmrr.system_type = #{systemType}
                 left join bd_grade bg
                           on bg.pk_id = cm.pk_settle_grade
                               and bg.del_flag = 0
                 left join bd_area bap
                           on bap.pk_id = cmrr.province
                               and bap.del_flag = 0
                 left join bd_area bac
                           on bac.pk_id = cmrr.city
                               and bac.del_flag = 0
                 left join bd_area bax
                           on bax.pk_id = cmrr.county
                               and bax.del_flag = 0
        where cm.del_flag = 0
          and cm.pk_id = #{pkMember}
          and bg.grade_value >= #{gradeValue}
    </select>

    <!-- 插入收益区域 -->
    <insert id="insertData">
        insert into cu_member_retail_region cmrr (
            pk_id,
            pk_member,
            province,
            city,
            county,
            source,
            creation_time,
            pk_creator,
            system_type,
            business_type
            )
        select cu_member_retail_region_seq.nextval,
               #{retailRegion.pkMember},
               #{retailRegion.province},
               #{retailRegion.city},
               #{retailRegion.county},
               #{retailRegion.source},
               sysdate,
               #{retailRegion.pkCreator},
               #{retailRegion.systemType},
               2
        from dual
        where not exists(select 1
                         from cu_member_retail_region cmrr
                         where cmrr.province = #{retailRegion.province}
                           and cmrr.city = #{retailRegion.city}
                           and cmrr.county = #{retailRegion.county}
                           and cmrr.del_flag = 0
                           and cmrr.effective = 0
                           and cmrr.system_type = #{retailRegion.systemType}
                           and cmrr.business_type = 2
                         )
    </insert>

    <!--    未绑定收益区域-->
    <select id="unboundAreaList" resultType="com.hzs.retail.member.vo.UnboundAreaVO">
        SELECT p.name  province,
               ct.name city,
               BA.PK_ID,
               BA.PARENT,
               BA.name county
        FROM BD_AREA BA
                 LEFT JOIN BD_AREA ct ON ct.pk_id = BA.parent
                 LEFT JOIN BD_AREA p ON p.pk_id = ct.parent
        WHERE BA.DEL_FLAG = 0
          AND BA.PK_COUNTRY = #{pkCountry}
          AND p.name IS NOT null
          AND BA.PK_ID NOT IN (SELECT CMRR.COUNTY
                               FROM CU_MEMBER_RETAIL_REGION CMRR
                               WHERE CMRR.DEL_FLAG = 0
                                 AND CMRR.PK_COUNTRY = #{pkCountry}
                                 AND CMRR.EFFECTIVE = 0
                                 and CMRR.SYSTEM_TYPE = #{systemType}
        )
        ORDER BY p.pk_id ASC, ct.pk_id ASC, ba.pk_id ASC
    </select>


    <!-- 查询会员收益区域-->
    <select id="memberretailRegList" resultType="com.hzs.common.domain.retail.member.ext.CuMemberRetailRegionExt">
        SELECT CRR.PK_ID pkId,
        crr.pk_member pkMember,
        crr.province,
        crr.city,
        crr.county,
        crr.effective,
        crr.creation_time creationTime,
        crr.modified_time modifiedTime,
        crr.EFFECTIVE_END_DATE effectiveEndDate,
        crr.source,
        cm.member_code memberCode,
        cm.member_name memberName,
        p.name provinceVal,
        c.name cityVal,
        pc.name countyVal,
        bg.GRADE_NAME gradeName
        FROM CU_MEMBER_RETAIL_REGION CRR
        INNER JOIN CU_MEMBER CM
        ON CRR.PK_MEMBER = CM.PK_ID
        INNER join BD_GRADE bg on bg.pk_id=cm.PK_SETTLE_GRADE
        LEFT JOIN BD_AREA P
        ON P.PK_ID = CRR.PROVINCE
        AND P.DEL_FLAG = 0
        LEFT JOIN BD_AREA C
        ON C.PK_ID = CRR.CITY
        AND C.DEL_FLAG = 0
        LEFT JOIN BD_AREA PC
        ON PC.PK_ID = CRR.COUNTY
        AND PC.DEL_FLAG = 0
        where CRR.DEL_FLAG = 0
        <if test="pkCountry != null">
            and CRR.PK_COUNTRY = #{pkCountry}
        </if>
        <if test="systemType != null">
            and CRR.SYSTEM_TYPE = #{systemType}
        </if>
        <if test="effective!=null">
            and CRR.EFFECTIVE=#{effective}
        </if>
        <if test="province!=null">
            and CRR.PROVINCE=#{province}
        </if>
        <if test="city!=null">
            and CRR.CITY=#{city}
        </if>
        <if test="county!=null">
            and CRR.COUNTY=#{county}
        </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="pkMember!=null">
            and CRR.PK_MEMBER=#{pkMember}
        </if>
        <if test="startDate != null and startDate != '' ">
            and CRR.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
        </if>
        <if test="endDate != null and endDate != '' ">
            and CRR.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="startDateModify != null and startDateModify != '' ">
            and CRR.MODIFIED_TIME >= to_date(#{startDateModify}, 'yyyy-mm-dd')
        </if>
        <if test="endDateModify != null and endDateModify != '' ">
            and CRR.MODIFIED_TIME &lt;= to_date(#{endDateModify} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
        </if>
        <if test="source!=null">
            and CRR.source=#{source}
        </if>
        <if test="businessType != null">
            and CRR.BUSINESS_TYPE = #{businessType}
        </if>
        order by CRR.CREATION_TIME desc
    </select>

    <select id="getRegionTreeList" resultType="com.hzs.common.domain.system.base.BdArea">
        SELECT
            pr.*,
            nvl( region.num, 0 )
        FROM
        (
            SELECT
                ba.PK_ID,
                ba.PARENT,
                ba.NAME,
                ba.BIND_NUM
            FROM
                bd_area ba
            WHERE
                ba.del_flag = 0
                AND ba.pk_country = 1
                AND ba.ENABLE_STATE = 0
        ) pr
        LEFT JOIN (
            SELECT
                <if test="queryType != null and queryType == 'city'">
                    city id,
                </if>
                <if test="queryType != null and queryType == 'county'">
                    county id,
                </if>
                count( 1 ) num
            FROM cu_member_retail_region
            WHERE
                del_flag = 0
            GROUP BY
                <if test="queryType != null and queryType == 'city'">
                    city
                </if>
                <if test="queryType != null and queryType == 'county'">
                    county
                </if>
        ) region ON pr.PK_ID = region.id
        <!--
        WHERE
        nvl( region.num, 0 ) <![CDATA[ < ]]> pr.BIND_NUM
        ORDER BY
        nvl( region.num, 0 ) DESC
        -->
    </select>
    <select id="getRegionByMemberList"
            resultType="com.hzs.common.domain.retail.member.ext.CuMemberRetailRegionExt">
        select cm.member_code,
               cm.is_region,
               cmrr.*,
               bap.name province_val,
               bac.name city_val,
               bax.name county_val,
               cm.IS_ACTIVATE
        from cu_member cm
                 left join cu_member_retail_region cmrr
                           on cmrr.pk_member = cm.pk_id
                               and cmrr.del_flag = 0
                               and cmrr.effective = 0
                               and cmrr.system_type = #{systemType}
                 left join bd_grade bg
                           on bg.pk_id = cm.pk_settle_grade
                               and bg.del_flag = 0
                 left join bd_area bap
                           on bap.pk_id = cmrr.province
                               and bap.del_flag = 0
                 left join bd_area bac
                           on bac.pk_id = cmrr.city
                               and bac.del_flag = 0
                 left join bd_area bax
                           on bax.pk_id = cmrr.county
                               and bax.del_flag = 0
        where cm.del_flag = 0
          and cm.pk_id = #{pkMember}
          and cmrr.effective = 0
          and bg.grade_value >= #{gradeValue}
    </select>

    <!-- 插入或更新区域选择 -->
    <update id="mergeMemberRetailRegion">
        merge into CU_MEMBER_RETAIL_REGION cmrr
            using (
                select
                #{retailRegion.pkMember} pk_member,
                #{retailRegion.province} province,
                #{retailRegion.city} city,
                #{retailRegion.county} county
                from dual
                ) tmp
            on (tmp.pk_member = cmrr.pk_member and cmrr.del_flag = 0)
            WHEN MATCHED THEN
                update
                    set cmrr.province      = tmp.province,
                        cmrr.city          = tmp.city,
                        cmrr.county        = tmp.county,
                        cmrr.pk_modified   = tmp.pk_member,
                        cmrr.modified_time = sysdate
            WHEN NOT MATCHED THEN
                insert
                    (pk_id, pk_member, province, city, county, pk_creator)
                    values
                    (cu_member_retail_region_seq.Nextval,
                     tmp.pk_member,
                     tmp.province,
                     tmp.city,
                     tmp.county,
                     tmp.pk_member)
    </update>

    <!-- 有效区域列表 -->
    <select id="listEffectiveRegion" resultType="com.hzs.common.domain.retail.member.CuMemberRetailRegion">
        select cmrr.*
        from CU_MEMBER_RETAIL_REGION cmrr
        where cmrr.del_flag = 0
        and cmrr.effective = 0
        and cmrr.business_type = 2
        and cmrr.effective_end_date >= #{queryDate}
    </select>

</mapper>
