java-base-app/bd-business/bd-business-member/src/main/resources/mapper/member/base/CuMemberRegionMapper.xml

353 lines
14 KiB
XML

<?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.CuMemberRegionMapper">
<!-- 查询会员收益区域 -->
<select id="getRegionByMember" resultType="com.hzs.common.domain.member.ext.CuMemberRegionExt">
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
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
<if test="type != null and type == 1">
and cmrr.PROVINCE != 0
and cmrr.CITY = 0
and cmrr.COUNTY = 0
</if>
<if test="type != null and type == 2">
and cmrr.PROVINCE != 0
and cmrr.CITY != 0
and cmrr.COUNTY = 0
</if>
<if test="type != null and type == 3">
and cmrr.PROVINCE != 0
and cmrr.CITY != 0
and cmrr.COUNTY != 0
</if>
</select>
<!-- 可选择的收益区域 -->
<select id="listChooseRegion" resultType="com.hzs.common.domain.system.base.BdArea">
select ba.PK_ID, ba.PARENT, ba.NAME
from bd_area ba
where ba.del_flag = 0
and ba.pk_country = 1
and ba.ENABLE_STATE = 0
and ba.pk_id not in (
SELECT
COUNTY
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 1=1
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 = #{userId} )) and DEL_FLAG = 0))
)
</select>
<!-- 插入收益区域 -->
<insert id="insertData">
insert into cu_member_retail_region cmrr (
pk_id,
pk_member,
province,
city,
county,
creation_time,
pk_creator,
REGION_VERTEX_PK_ID,
source
)
select cu_member_retail_region_seq.nextval,
#{retailRegion.pkMember},
#{retailRegion.province},
#{retailRegion.city},
#{retailRegion.county},
sysdate,
#{retailRegion.pkCreator},
#{retailRegion.regionVertexPkId},
#{retailRegion.source}
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 cm.pk_id = #{retailRegion.pkMember}
)
</insert>
<!-- 未绑定收益区域-->
<select id="unboundAreaList" resultType="com.hzs.member.base.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
)
ORDER BY p.pk_id ASC, ct.pk_id ASC, ba.pk_id ASC
</select>
<!-- 查询会员收益区域-->
<select id="memberretailRegList" resultType="com.hzs.common.domain.member.ext.CuMemberRegionExt">
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,
ba.AWARDS_NAME as awardsName
FROM CU_MEMBER_RETAIL_REGION CRR
INNER JOIN CU_MEMBER CM
ON CRR.PK_MEMBER = CM.PK_ID
INNER JOIN BD_AWARDS ba ON ba.pk_id = cm.PK_AWARDS
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="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>
<!-- 批量修改已到期的会员收益区域-->
<update id="batchUpdateRetailRegion" parameterType="java.lang.Long">
UPDATE CU_MEMBER_RETAIL_REGION
SET EFFECTIVE=1,
MODIFIED_TIME=sysdate
WHERE EFFECTIVE = 0
AND DEL_FLAG = 0
AND EFFECTIVE_END_DATE &lt; = sysdate
</update>
<!-- 重新判断收益区域数据 -->
<select id="listCheckRegionMember" resultType="com.hzs.common.domain.member.ext.CuMemberRegionExt">
select cmrr.pk_member, cmrr.source, bg.grade_value, cmrr.creation_time, cmrr.system_type
from cu_member_retail_region cmrr
inner join cu_member cm
on cm.pk_id = cmrr.pk_member
left join bd_grade bg
on bg.pk_id = cm.pk_settle_grade
where cmrr.del_flag = 0
and cmrr.effective = 0
</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="listRepeatData" resultType="com.hzs.common.domain.member.region.CuMemberRegion">
select cmrr.province, cmrr.city, cmrr.county
from cu_member_retail_region cmrr
where cmrr.del_flag = 0
and cmrr.effective = 0
and cmrr.system_type = #{systemType}
group by cmrr.province, cmrr.city, cmrr.county
having count(1) > 1
</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>
<!-- 更新非最新区域其它数据 -->
<update id="updateRepeatDate">
update cu_member_retail_region cmrr
set cmrr.effective = 1,
cmrr.modified_time = sysdate
where cmrr.del_flag = 0
and cmrr.effective = 0
and cmrr.province = #{province}
and cmrr.city = #{city}
and cmrr.county = #{county}
and cmrr.system_type = #{systemType}
and cmrr.pk_id not in (select pk_id
from (select cmrr.pk_id
from cu_member_retail_region cmrr
where cmrr.del_flag = 0
and cmrr.effective = 0
and cmrr.province = #{province}
and cmrr.city = #{city}
and cmrr.county = #{county}
and cmrr.system_type = #{systemType}
order by cmrr.creation_time)
where rownum = 1)
</update>
</mapper>