Files
korea-research/admin/src/main/resources/mappers/researcher/ResearcherMapper.xml
2025-07-14 17:46:15 +09:00

401 lines
16 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="app.module.admin.usecase.researcher.adpaters.persistence.ResearcherMapper">
<select id="list" parameterType="app.module.admin.usecase.researcher.adpaters.web.model.ResearchersListReqVO">
select
a.seq,
a.id,
COUNT(rhh.id) AS household_count,
a.name,
a.phone,
max(u.code_text) AS region_main_code_name,
case a.account_disabled when false then '사용' when true then '미사용' end account_disabled_flag
from admin a
join admin_has_role ahr ON a.seq = ahr.admin_seq and ahr.is_deleted = false
left join uicode u ON u.parent_id = 'code.region.main' AND u.id = a.region_main_code
left join researcher_has_household rhh ON rhh.admin_seq = a.seq and rhh.is_deleted = false
left join household h ON h.seq = rhh.household_seq AND h.is_panel IS true and h.is_deleted = false
where
ahr.role_id = 'ROLE_RESEARCHER'
<if test="searchField == '' and searchValue != null and searchValue != ''">
and ( a.name LIKE CONCAT('%', #{searchValue}, '%') or a.phone LIKE CONCAT('%', #{searchValue}, '%') )
</if>
<if test="searchField == 'name' and searchValue != null and searchValue != ''">
and a.name LIKE CONCAT('%', #{searchValue}, '%')
</if>
<if test="searchField == 'phone' and searchValue != null and searchValue != ''">
and a.phone LIKE CONCAT('%', #{searchValue}, '%')
</if>
<if test="searchArea != null and searchArea != ''">
and a.region_main_code = #{searchArea}
</if>
<if test="searchAccountDisabled != null and searchAccountDisabled != ''">
and a.account_disabled = #{searchAccountDisabled}::boolean
</if>
group by a.seq
<if test="isPaging">
LIMIT #{size} OFFSET #{page}*#{size}
</if>
</select>
<select id="countAll" parameterType="app.module.admin.usecase.researcher.adpaters.web.model.ResearchersListReqVO" resultType="long">
select COUNT(DISTINCT a.seq)
from admin a
join admin_has_role ahr ON a.seq = ahr.admin_seq AND ahr.is_deleted = false
left join researcher_has_household rhh ON rhh.admin_seq = a.seq AND rhh.is_deleted = false
left join household h ON h.seq = rhh.household_seq AND h.is_panel IS true AND h.is_deleted = false
where ahr.role_id = 'ROLE_RESEARCHER'
<if test="searchField == '' and searchValue != null and searchValue != ''">
and (a.name LIKE CONCAT('%', #{searchValue}, '%') OR a.phone LIKE CONCAT('%', #{searchValue}, '%'))
</if>
<if test="searchField == 'name' and searchValue != null and searchValue != ''">
and a.name LIKE CONCAT('%', #{searchValue}, '%')
</if>
<if test="searchField == 'phone' and searchValue != null and searchValue != ''">
and a.phone LIKE CONCAT('%', #{searchValue}, '%')
</if>
<if test="searchArea != null and searchArea != ''">
and a.region_main_code = #{searchArea}
</if>
<if test="searchAccountDisabled != null and searchAccountDisabled != ''">
and a.account_disabled = #{searchAccountDisabled}::boolean
</if>
</select>
<select id="detail">
select
a.seq,
a.id,
a.name,
a.phone,
a.region_main_code,
u.code_text AS region_main_code_name,
case a.account_disabled when false then '사용' when true then '미사용' end account_disabled_flag
from admin a
join admin_has_role ahr ON a.seq = ahr.admin_seq
left join uicode u ON u.parent_id = 'code.region.main' AND u.id = a.region_main_code
where ahr.role_id = 'ROLE_RESEARCHER' and ahr.is_deleted = false
<if test="adminSeq > 0">
and a.seq = #{adminSeq}
</if>
</select>
<select id="mappingHouseholdList">
select
h.seq,
h.household_num,
h.owner_name,
h.phone,
rm.code_text AS region_main_code_name,
rs.code_text AS region_sub_code_name,
rd.code_text AS region_detail_code_name,
il.code_text AS industry_large_code_name,
im.code_text AS industry_middle_code_name,
case h.is_panel when true then '유지' when false then '탈락' end panel_flag,
h.owner_age,
h.address,
h.industry_sub_middle,
h.industry_detail,
h.usage_type,
a.name AS researcherName,
a.phone AS researcherPhone
from household h
join researcher_has_household rhh ON h.seq = rhh.household_seq and rhh.is_deleted = false
join admin a ON a.seq = rhh.admin_seq and a.is_deleted = false
left join uicode rm ON rm.id = h.region_main_code
left join uicode rs ON rs.id = h.region_sub_code
left join uicode rd ON rd.id = h.region_detail_code
left join uicode il ON il.id = h.industry_large_code
left join uicode im ON im.id = h.industry_middle_code
where 1=1
<if test="seq > 0">
and rhh.admin_seq = #{seq}
</if>
<if test="isPaging">
LIMIT #{size} OFFSET #{page}*#{size}
</if>
</select>
<select id="mappingHouseholdListCountAll">
select
count(*)
from household h
join researcher_has_household rhh ON h.seq = rhh.household_seq and rhh.is_deleted = false
where 1=1
<if test="seq > 0">
and rhh.admin_seq = #{seq}
</if>
</select>
<select id="visitHouseholdList">
select
rhv.id,
rhh.admin_seq,
rhv.household_seq,
h.household_num,
h.owner_name,
rhv.visit_date,
rhv.content,
rhv.management_type,
case rhv.is_gift_sent when true then '전달완료' when false then '-' end is_gift_sent_flag,
TO_CHAR(rhv.updated_at, 'YYYY-MM-DD HH24:MI') AS updated_at,
rhv.updated_by
from household_visit rhv
join household h on rhv.household_seq = h.seq
join researcher_has_household rhh on rhh.household_seq = h.seq and rhh.is_deleted = false
where rhv.is_deleted = false
<if test="seq > 0">
and rhh.admin_seq = #{seq}
</if>
<if test="searchMonth != null and searchMonth != ''">
and TO_CHAR(rhv.visit_date ,'YYYYMM')= #{searchMonth}
</if>
order by rhv.visit_date
</select>
<select id="selectVisitHouseholdPercentage">
with visit_ranking AS (
select
hv.household_seq,
hv.visit_date,
hv.is_gift_sent,
ROW_NUMBER() OVER (PARTITION BY hv.household_seq ORDER BY hv.visit_date ASC) AS visit_rank
from household_visit hv
where TO_CHAR(hv.visit_date, 'YYYYMM') = #{searchMonth}
and hv.is_deleted = FALSE
),
visit_counts AS (
select
COUNT(DISTINCT CASE WHEN vr.visit_rank = 1 THEN vr.household_seq END) AS first_visit_count,
COUNT(DISTINCT CASE WHEN vr.visit_rank = 2 THEN vr.household_seq END) AS second_visit_count,
COUNT(DISTINCT CASE WHEN vr.is_gift_sent = true THEN vr.household_seq END) AS gift_sent_count
from researcher_has_household rhh
left join visit_ranking vr ON rhh.household_seq = vr.household_seq
where rhh.is_deleted = false
and rhh.admin_seq = #{seq}
),
total_households AS (
select COUNT(DISTINCT rhh.household_seq) AS total_household_count
from researcher_has_household rhh
where rhh.is_deleted = false
AND rhh.admin_seq = #{seq}
)
SELECT
th.total_household_count ,
vc.first_visit_count,
vc.second_visit_count,
vc.gift_sent_count,
ROUND(
CASE
WHEN th.total_household_count > 0
THEN (vc.first_visit_count::NUMERIC / th.total_household_count) * 100
ELSE 0
END,
2
) AS first_visit_percentage,
ROUND(
CASE
WHEN th.total_household_count > 0
THEN (vc.second_visit_count::NUMERIC / th.total_household_count) * 100
ELSE 0
END,
2
) AS second_visit_percentage,
ROUND(
CASE
WHEN th.total_household_count > 0
THEN (vc.gift_sent_count::NUMERIC / th.total_household_count) * 100
ELSE 0
END,
2
) AS gift_sent_percentage
FROM total_households th
CROSS JOIN visit_counts vc
</select>
<select id="visitHouseholdListCountAll">
select count(*)
from household_visit rhv
join researcher_has_household rhh on rhh.household_seq = rhv.household_seq
join admin a on a.seq = rhh.admin_seq
where rhv.is_deleted = false
<if test="seq > 0">
and rhh.admin_seq = #{seq}
</if>
<if test="searchMonth != null and searchMonth != ''">
and TO_CHAR(rhv.visit_date ,'YYYYMM')= #{searchMonth}
</if>
</select>
<select id="visitHouseholdDetail">
select
rhv.id,
rhv.household_seq,
rhv.visit_date,
rhv.content,
rhv.management_type,
rhv.is_gift_sent as is_gift_sent_flag,
rhv.created_at
from household_visit rhv
where rhv.is_deleted = false and rhv.id = #{id}
</select>
<update id="updateGiftSentToFalse">
/* updateGiftSentToFalse : 동일 household_seq 의 다른 방문 중 전달여부 true → false */
UPDATE household_visit
SET is_gift_sent = false
WHERE household_seq = #{householdSeq}
AND TO_CHAR(visit_date, 'YYYYMM') = TO_CHAR(#{visitDate}, 'YYYYMM')
AND is_gift_sent = true
AND (#{id} IS NULL OR id != #{id})
AND is_deleted = false
</update>
<update id="updateVisitHousehold">
update household_visit
set visit_date = #{visitDate},
content = #{content},
management_type = #{managementType},
<choose>
<when test="isGiftSent">
is_gift_sent = true,
</when>
<when test="!isGiftSent">
is_gift_sent = false,
</when>
</choose>
<include refid="common.fields.UpdateBaseSet" />
where id = #{id}
</update>
<update id="visitHouseholdDelete">
update household_visit
set is_deleted = true,
deleted_at = NOW(),
deleted_by = #{deletedBy}
where id in (
<foreach collection="selectIds" item="item" separator=",">
#{item}
</foreach>)
</update>
<update id="deleteHouseholdResearcherMapping">
update researcher_has_household
set is_deleted = true,
deleted_at = NOW(),
deleted_by = #{deletedBy}
where household_seq = #{householdSeq}
</update>
<update id="insertHouseholdResearcherMapping">
insert into researcher_has_household(
id,
admin_seq,
household_seq,
created_at,
created_by,
updated_at,
updated_by
)values(
nextval('household_has_role_seq'),
#{researcherSeq},
#{householdSeq},
NOW(),
#{createdBy},
NOW(),
#{createdBy}
)
</update>
<select id="getPopupHouseholds"
resultType="app.module.admin.usecase.researcher.adpaters.persistence.model.GetPopupHouseholds">
SELECT
hvr.*,
COUNT(*) OVER () AS total
FROM (
SELECT
h.seq AS householdSeq,
h.household_num AS householdNum,
h.owner_name AS householdName,
h.phone AS householdPhone,
h.region_main_code AS regionMainCode,
h.region_main_name AS regionMainName,
h.region_sub_code AS regionSubCode,
h.region_sub_name AS regionSubName,
h.region_detail_code AS regionDetailCode,
h.region_detail_name AS regionDetailName,
h.industry_large_code as industryLargeCode,
h.industry_large_name AS industryLargeName,
h.industry_middle_code as industryMiddleCode,
h.industry_middle_name AS industryMiddleName,
h.is_panel AS isPanel,
r.researcher_name AS researcherName
FROM v_household_with_code h
LEFT JOIN v_household_visit v ON h.seq = v.household_seq
LEFT JOIN v_household_researcher r ON h.seq = r.household_seq
) AS hvr
<where>
1=1
<if test="cmd.ownerField != null and cmd.ownerField != ''">
/* 경영주 검색 */
<choose>
<when test="cmd.ownerField eq 'name'">
AND hvr.householdName ILIKE '%' || #{cmd.ownerValue} || '%'
</when>
<when test="cmd.ownerField eq 'phone'">
AND hvr.householdPhone ILIKE '%' || #{cmd.ownerValue} || '%'
</when>
</choose>
</if>
<if test="cmd.regionMainCode != null and cmd.regionMainCode != ''">
/* 실사 검색 */
AND hvr.regionMainCode = #{cmd.regionMainCode}
</if>
<if test="cmd.regionSubCode != null and cmd.regionSubCode != ''">
/* 지역 검색 */
AND hvr.regionSubCode = #{cmd.regionSubCode}
</if>
<if test="cmd.regionDetailCode != null and cmd.regionDetailCode != ''">
/* 세부지역 검색 */
AND hvr.regionDetailCode = #{cmd.regionDetailCode}
</if>
<if test="cmd.isPanel != null">
/* 패널 유지 여부 */
<choose>
<when test="cmd.isPanel">
AND hvr.isPanel = true
</when>
<when test="!cmd.isPanel">
AND hvr.isPanel = false
</when>
</choose>
</if>
<if test="cmd.isMapping != null">
/* 맵핑 여부 */
<choose>
<when test="cmd.isMapping">
AND r.researcher_name is not null
</when>
<when test="!cmd.isMapping">
AND r.researcher_name is null
</when>
</choose>
</if>
</where>
ORDER BY hvr.householdNum ASC
OFFSET #{pageable.offset} ROWS FETCH NEXT #{pageable.pageSize} ROWS ONLY
</select>
<select id="selectResearcherId">
select seq
from admin a
join admin_has_role ahr on ahr.admin_seq = a.seq and ahr.role_id = 'ROLE_RESEARCHER' and ahr.is_deleted = false
where a.id = #{id} and a.is_deleted = false
</select>
</mapper>