<?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 cm.IS_ACTIVATE=0
          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,
            REGION_VERTEX_PK_ID
            )
        select cu_member_retail_region_seq.nextval,
               #{retailRegion.pkMember},
               #{retailRegion.province},
               #{retailRegion.city},
               #{retailRegion.county},
               #{retailRegion.source},
               sysdate,
               #{retailRegion.pkCreator},
               #{retailRegion.systemType},
               #{retailRegion.regionVertexPkId}
        from dual
        where not exists(select 1
                         from cu_member_retail_region cmrr
                                  INNER JOIN cu_member cm ON cmrr.pk_member = cm.pk_id
                                  LEFT JOIN BD_VERTEX bv ON cm.PK_VERTEX = bv.PK_ID
                                  LEFT JOIN BD_REGION_VERTEX brv ON bv.REGION_VERTEX_PK_ID = brv.PK_ID
                         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 cm.pk_id = #{retailRegion.pkMember}
            )
    </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,
        brv.pk_id as regionVertexPkId,
        brv.name as regionVertexName
        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
        left join BD_REGION_VERTEX brv
        on brv.pk_id = crr.REGION_VERTEX_PK_ID
        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="regionVertexPkId != null">
            and brv.pk_id = #{regionVertexPkId}
        </if>
        order by CRR.CREATION_TIME desc
    </select>

    <!-- 会员购买订单数量 -->
    <select id="getOrderCount" resultType="int">
        select count(1)
        from sa_order so
        left join sa_order_items soi
        on soi.pk_order = so.pk_id
        and soi.del_flag = 0
        and soi.is_gift = 1
        left join bd_wares bw
        on bw.pk_id = soi.pk_wares
        and bw.del_flag = 0
        and bw.system_type = so.system_type
        where so.del_flag = 0
        and so.order_status = 1
        and so.pk_member = #{pkMember}
        and so.order_type in (41, 42, 43)
        and so.system_type = #{systemType}
        and bw.wares_code in
        <foreach collection="waresCodeList" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="validateBindCountA" resultType="java.lang.Integer">
        SELECT
        COUNT(DISTINCT cmrr.pk_member)
        FROM
        CU_MEMBER_RETAIL_REGION cmrr
        where 1=1
        <if test="mrrExt.province != null">
            and cmrr.PROVINCE = #{mrrExt.province}
        </if>
        <if test="mrrExt.city != null">
            and cmrr.CITY = #{mrrExt.city}
        </if>
        <if test="mrrExt.county != null">
            and cmrr.COUNTY = #{mrrExt.county}
        </if>
        <if test="yesOrNoValue != null">
            and cmrr.EFFECTIVE = #{yesOrNoValue}
        </if>
        <if test="systemType != null">
            and cmrr.SYSTEM_TYPE = #{systemType}
        </if>
        and cmrr.del_flag = 0
        AND cmrr.PK_MEMBER IN (select pk_id from CU_MEMBER where PK_VERTEX in (select pk_id from BD_VERTEX where REGION_VERTEX_PK_ID in (select
        REGION_VERTEX_PK_ID from BD_VERTEX where pk_id in (select PK_VERTEX FROM cu_member WHERE PK_ID = #{pkId} )) and DEL_FLAG = 0))
        AND cmrr.PK_MEMBER != #{pkId}
    </select>
    <select id="validateBindCountB" resultType="java.lang.Integer">
        SELECT
        COUNT(DISTINCT cmrr.pk_member)
        FROM
        CU_MEMBER_RETAIL_REGION cmrr
        where 1=1
        <if test="mrrExt.province != null">
            and cmrr.PROVINCE = #{mrrExt.province}
        </if>
        <if test="mrrExt.city != null">
            and cmrr.CITY = #{mrrExt.city}
        </if>
        <if test="mrrExt.county != null">
            and cmrr.COUNTY = #{mrrExt.county}
        </if>
        <if test="yesOrNoValue != null">
            and cmrr.EFFECTIVE = #{yesOrNoValue}
        </if>
        <if test="systemType != null">
            and cmrr.SYSTEM_TYPE = #{systemType}
        </if>
        and cmrr.del_flag = 0
        AND cmrr.PK_MEMBER IN (select pk_id from CU_MEMBER where PK_VERTEX in (select pk_id from BD_VERTEX where REGION_VERTEX_PK_ID in (select
        REGION_VERTEX_PK_ID from BD_VERTEX where pk_id in (select PK_VERTEX FROM cu_member WHERE PK_ID = #{pkId} )) and DEL_FLAG = 0))
        AND cmrr.PK_MEMBER != #{pkId}
        <!--
        GROUP BY
        NVL(TO_CHAR(brv.PK_ID), 'NULL_GROUP')
         -->
    </select>

    <select id="getRegionVertexPkIdByPkMember" resultType="java.lang.Long" parameterType="java.lang.Long">
        SELECT REGION_VERTEX_PK_ID
        FROM BD_VERTEX
        WHERE pk_id IN (select PK_VERTEX FROM cu_member cm WHERE cm.pk_id = #{pkMember})
    </select>


</mapper>
